简体   繁体   中英

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

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

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), ...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM