簡體   English   中英

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

[英]Keras: Using a generator for multi-output model with model.fit_generator

在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:如何使用fit_generator與不同類型的多個輸出
  2. https://keras.io/getting-started/functional-api-guide/

但是我一直收到這個錯誤:

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)

您的代碼在驗證期間失敗,而不是培訓。 看起來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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM