Keras custom generator TypeError: 'NoneType' object is not callable










0















I've written a custom generator to load my dataset and feed it into the fit_generator method. But I'm getting an error. question_train,img_lis_train and answers_train are lists of strings. I want mygen to return batches of 32 samples of the format:



[images,questions] , answers


Here is the code:



def mygen(questions_train,img_lis_train,answers_train):
start = 0
data_size = len(questions_train)
batch_size = 32
while True:
if( start+batch_size <= data_size ):
batch_ques = questions_train[ start : start+batch_size ]
batch_ans = answers_train[ start : start+batch_size ]
batch_img_names = img_lis_train[ start : start+batch_size ]
elif(start < data_size):
batch_ques = questions_train[ start : ]
batch_ans = answers_train[ start : ]
batch_img_names = img_lis_train[ start : start+batch_size ]
else:
start = 0
continue

batch_img =
for img_name in batch_img_names:
img = load_img('./dataset/images/' + str(img_name) + '.png' , target_size = (224,224))
img = img_to_array(img)
batch_img.append( preprocess_input(img) )

start += 32
print('start = ' + str(start))
yield [batch_img, batch_ques] ,batch_ans

fc_model.fit_generator(mygen, steps_per_epoch=3376, epochs = 10)


Here is the error I get:



 File "mycode.py", line 210, in <module>
fc_model.fit_generator(mygen, steps_per_epoch=3376, epochs = 10)
File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
return func(*args, **kwargs)
File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/models.py", line 1223, in fit_generator
initial_epoch=initial_epoch)
File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
return func(*args, **kwargs)
File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/engine/training.py", line 2083, in fit_generator
generator_output = next(output_generator)
StopIteration
Exception ignored in: <bound method BaseSession.__del__ of <tensorflow.python.client.session.Session object at 0x7f936527fcc0>>
Traceback (most recent call last):
File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 712, in __del__
File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/tensorflow/python/framework/c_api_util.py", line 31, in __init__
TypeError: 'NoneType' object is not callable









share|improve this question




























    0















    I've written a custom generator to load my dataset and feed it into the fit_generator method. But I'm getting an error. question_train,img_lis_train and answers_train are lists of strings. I want mygen to return batches of 32 samples of the format:



    [images,questions] , answers


    Here is the code:



    def mygen(questions_train,img_lis_train,answers_train):
    start = 0
    data_size = len(questions_train)
    batch_size = 32
    while True:
    if( start+batch_size <= data_size ):
    batch_ques = questions_train[ start : start+batch_size ]
    batch_ans = answers_train[ start : start+batch_size ]
    batch_img_names = img_lis_train[ start : start+batch_size ]
    elif(start < data_size):
    batch_ques = questions_train[ start : ]
    batch_ans = answers_train[ start : ]
    batch_img_names = img_lis_train[ start : start+batch_size ]
    else:
    start = 0
    continue

    batch_img =
    for img_name in batch_img_names:
    img = load_img('./dataset/images/' + str(img_name) + '.png' , target_size = (224,224))
    img = img_to_array(img)
    batch_img.append( preprocess_input(img) )

    start += 32
    print('start = ' + str(start))
    yield [batch_img, batch_ques] ,batch_ans

    fc_model.fit_generator(mygen, steps_per_epoch=3376, epochs = 10)


    Here is the error I get:



     File "mycode.py", line 210, in <module>
    fc_model.fit_generator(mygen, steps_per_epoch=3376, epochs = 10)
    File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
    return func(*args, **kwargs)
    File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/models.py", line 1223, in fit_generator
    initial_epoch=initial_epoch)
    File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
    return func(*args, **kwargs)
    File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/engine/training.py", line 2083, in fit_generator
    generator_output = next(output_generator)
    StopIteration
    Exception ignored in: <bound method BaseSession.__del__ of <tensorflow.python.client.session.Session object at 0x7f936527fcc0>>
    Traceback (most recent call last):
    File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 712, in __del__
    File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/tensorflow/python/framework/c_api_util.py", line 31, in __init__
    TypeError: 'NoneType' object is not callable









    share|improve this question


























      0












      0








      0








      I've written a custom generator to load my dataset and feed it into the fit_generator method. But I'm getting an error. question_train,img_lis_train and answers_train are lists of strings. I want mygen to return batches of 32 samples of the format:



      [images,questions] , answers


      Here is the code:



      def mygen(questions_train,img_lis_train,answers_train):
      start = 0
      data_size = len(questions_train)
      batch_size = 32
      while True:
      if( start+batch_size <= data_size ):
      batch_ques = questions_train[ start : start+batch_size ]
      batch_ans = answers_train[ start : start+batch_size ]
      batch_img_names = img_lis_train[ start : start+batch_size ]
      elif(start < data_size):
      batch_ques = questions_train[ start : ]
      batch_ans = answers_train[ start : ]
      batch_img_names = img_lis_train[ start : start+batch_size ]
      else:
      start = 0
      continue

      batch_img =
      for img_name in batch_img_names:
      img = load_img('./dataset/images/' + str(img_name) + '.png' , target_size = (224,224))
      img = img_to_array(img)
      batch_img.append( preprocess_input(img) )

      start += 32
      print('start = ' + str(start))
      yield [batch_img, batch_ques] ,batch_ans

      fc_model.fit_generator(mygen, steps_per_epoch=3376, epochs = 10)


      Here is the error I get:



       File "mycode.py", line 210, in <module>
      fc_model.fit_generator(mygen, steps_per_epoch=3376, epochs = 10)
      File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
      return func(*args, **kwargs)
      File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/models.py", line 1223, in fit_generator
      initial_epoch=initial_epoch)
      File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
      return func(*args, **kwargs)
      File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/engine/training.py", line 2083, in fit_generator
      generator_output = next(output_generator)
      StopIteration
      Exception ignored in: <bound method BaseSession.__del__ of <tensorflow.python.client.session.Session object at 0x7f936527fcc0>>
      Traceback (most recent call last):
      File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 712, in __del__
      File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/tensorflow/python/framework/c_api_util.py", line 31, in __init__
      TypeError: 'NoneType' object is not callable









      share|improve this question
















      I've written a custom generator to load my dataset and feed it into the fit_generator method. But I'm getting an error. question_train,img_lis_train and answers_train are lists of strings. I want mygen to return batches of 32 samples of the format:



      [images,questions] , answers


      Here is the code:



      def mygen(questions_train,img_lis_train,answers_train):
      start = 0
      data_size = len(questions_train)
      batch_size = 32
      while True:
      if( start+batch_size <= data_size ):
      batch_ques = questions_train[ start : start+batch_size ]
      batch_ans = answers_train[ start : start+batch_size ]
      batch_img_names = img_lis_train[ start : start+batch_size ]
      elif(start < data_size):
      batch_ques = questions_train[ start : ]
      batch_ans = answers_train[ start : ]
      batch_img_names = img_lis_train[ start : start+batch_size ]
      else:
      start = 0
      continue

      batch_img =
      for img_name in batch_img_names:
      img = load_img('./dataset/images/' + str(img_name) + '.png' , target_size = (224,224))
      img = img_to_array(img)
      batch_img.append( preprocess_input(img) )

      start += 32
      print('start = ' + str(start))
      yield [batch_img, batch_ques] ,batch_ans

      fc_model.fit_generator(mygen, steps_per_epoch=3376, epochs = 10)


      Here is the error I get:



       File "mycode.py", line 210, in <module>
      fc_model.fit_generator(mygen, steps_per_epoch=3376, epochs = 10)
      File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
      return func(*args, **kwargs)
      File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/models.py", line 1223, in fit_generator
      initial_epoch=initial_epoch)
      File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
      return func(*args, **kwargs)
      File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/keras/engine/training.py", line 2083, in fit_generator
      generator_output = next(output_generator)
      StopIteration
      Exception ignored in: <bound method BaseSession.__del__ of <tensorflow.python.client.session.Session object at 0x7f936527fcc0>>
      Traceback (most recent call last):
      File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 712, in __del__
      File "/opt/apps/Python-3.5.1/lib/python3.5/site-packages/tensorflow/python/framework/c_api_util.py", line 31, in __init__
      TypeError: 'NoneType' object is not callable






      python tensorflow machine-learning keras generator






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 11:08









      today

      10.5k21536




      10.5k21536










      asked Nov 13 '18 at 9:12









      Nirvan AnjirbagNirvan Anjirbag

      357417




      357417






















          1 Answer
          1






          active

          oldest

          votes


















          0














          You need to construct the generator by calling it and passing your data to it, otherwise how is it supposed to generate batches? Here is the correct way of doing it:



          fc_model.fit_generator(mygen(q_train, i_train, a_train), ...)





          share|improve this answer
























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277465%2fkeras-custom-generator-typeerror-nonetype-object-is-not-callable%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            You need to construct the generator by calling it and passing your data to it, otherwise how is it supposed to generate batches? Here is the correct way of doing it:



            fc_model.fit_generator(mygen(q_train, i_train, a_train), ...)





            share|improve this answer





























              0














              You need to construct the generator by calling it and passing your data to it, otherwise how is it supposed to generate batches? Here is the correct way of doing it:



              fc_model.fit_generator(mygen(q_train, i_train, a_train), ...)





              share|improve this answer



























                0












                0








                0







                You need to construct the generator by calling it and passing your data to it, otherwise how is it supposed to generate batches? Here is the correct way of doing it:



                fc_model.fit_generator(mygen(q_train, i_train, a_train), ...)





                share|improve this answer















                You need to construct the generator by calling it and passing your data to it, otherwise how is it supposed to generate batches? Here is the correct way of doing it:



                fc_model.fit_generator(mygen(q_train, i_train, a_train), ...)






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 15 '18 at 9:10

























                answered Nov 13 '18 at 11:06









                todaytoday

                10.5k21536




                10.5k21536



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277465%2fkeras-custom-generator-typeerror-nonetype-object-is-not-callable%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    How to read a connectionString WITH PROVIDER in .NET Core?

                    Node.js Script on GitHub Pages or Amazon S3

                    Museum of Modern and Contemporary Art of Trento and Rovereto