Keras:使用带有model.fit_generator的多输出模型的生成器

时间:2017-12-01 02:18:34

标签: python python-3.x deep-learning keras keras-layer

在keras中,我设计了一个深受监督的卷积网络。确切地说,它有9个输出层。我开发了一个简单的生成器,产生:

yield(X, {'conv10': y, 'seg_1': y, 'seg_2': y, 'seg_3': y, 'seg_4': y, 'seg_5': y, 'seg_6': y, 'seg_7': y, 'seg_8': y}) 

我按照以下建议给出了这个sintax:

  1. Keras: How to use fit_generator with multiple outputs of different type
  2. https://keras.io/getting-started/functional-api-guide/
  3. 但是我一直收到这个错误:

    Traceback (most recent call last):
      File "modeltrain.py", line 180, in <module>
        model.fit_generator(next_batch(X_train_r, y_train_r, batch_size), steps_per_epoch=(X_train_r.shape[0]/batch_size), validation_data=(X_val_r, y_val_r), epochs=100, callbacks=[csv_logger, model_check])
      File "/home/m/.local/lib/python3.6/site-packages/keras/legacy/interfaces.py", line 87, in wrapper
        return func(*args, **kwargs)
      File "/home/m/.local/lib/python3.6/site-packages/keras/engine/training.py", line 1978, in fit_generator
        val_x, val_y, val_sample_weight)
      File "/home/m/.local/lib/python3.6/site-packages/keras/engine/training.py", line 1382, in _standardize_user_data
        exception_prefix='target')
      File "/home/m/.local/lib/python3.6/site-packages/keras/engine/training.py", line 111, in _standardize_input_data
        'Found: array with shape ' + str(data.shape))
    ValueError: The model expects 9 target arrays, but only received one array. Found: array with shape (70, 512, 512, 1)
    

    我不知道还能做什么!

    以下是代码:

    # Importing the pre processed data in the text file. 
    
    X_train= np.loadtxt("X_train.txt")
    X_test= np.loadtxt("X_test.txt")
    X_val= np.loadtxt("X_val.txt")
    y_train= np.loadtxt("y_train.txt")
    y_test= np.loadtxt("y_test.txt")
    y_val= np.loadtxt("y_val.txt")enter 
    
    # Resize the input matrix so that it satisfies (batch, x, y, z)
    
    new_size=512
    X_train_r=X_train.reshape(X_train.shape[0],new_size,new_size)
    X_train_r=np.expand_dims(X_train_r, axis=3)
    y_train_r=y_train.reshape(y_train.shape[0],new_size,new_size)
    y_train_r=np.expand_dims(y_train_r, axis=3)
    X_val_r=X_val.reshape(X_val.shape[0],new_size,new_size)
    X_val_r=np.expand_dims(X_val_r, axis=3)
    y_val_r=y_val.reshape(y_val.shape[0],new_size,new_size)
    y_val_r=np.expand_dims(y_val_r, axis=3)
    X_test_r=X_test.reshape(X_test.shape[0],new_size,new_size)
    X_test_r=np.expand_dims(X_test_r, axis=3)
    y_test_r=y_test.reshape(y_test.shape[0],new_size,new_size)
    y_test_r=np.expand_dims(y_test_r, axis=3)
    
    def next_batch(Xs, ys, size): 
        while true: 
            perm=np.random.permutation(Xs.shape[0]) 
            for i in np.arange(0, Xs.shape[0], size):
                X=Xs[perm[i:i+size]]
                y=ys[perm[i:i+size]]
                yield(X, {'conv10': y, 'seg_1': y, 'seg_2': y, 'seg_3': y, 'seg_4': y, 'seg_5': y, 'seg_6': y, 'seg_7': y,'seg_8': y })
    
    # Model Training 
    model= get_unet()
    batch_size=1
    
    #Compile the model
    adam=optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08)
    model.compile(loss={'conv10': dice_coef_loss, 'seg_8': loss_seg, 'seg_7': loss_seg , 'seg_6': loss_seg, 'seg_5': loss_seg , 'seg_4': loss_seg , 'seg_3': loss_seg, 'seg_2': loss_seg, 'seg_1': loss_seg}, optimizer=adam, metrics=['accuracy'])
    
        #Fit the model
        model.fit_generator(next_batch(X_train_r, y_train_r, batch_size), steps_per_epoch=(X_train_r.shape[0]/batch_size), validation_data=(X_val_r, y_val_r), epochs=100)
    

1 个答案:

答案 0 :(得分:2)

您的代码在验证期间失败,而不是培训。看起来validation_data参数在应该传入生成器时传入数组。以下是使用相同生成器进行验证和培训的简单示例:

a = Input(shape=(10,))
o1 = Dense(5, name='output1')(a)
o2 = Dense(7, name='output2')(a)
model = Model(inputs=a, outputs=[o1,o2])
model.compile(optimizer='sgd', loss='mse')

def generator():
    batch_size = 8
    x = np.zeros((batch_size, 10))
    y1 = np.zeros((batch_size, 5))
    y2 = np.zeros((batch_size, 7))
    while True:
        yield x, {'output1': y1, 'output2': y2}

model.fit_generator(generator(), 1, 1, validation_data=generator(), validation_steps=1)