2017-12-01 10 views
1

ケラスでは、私は深く監視された畳み込みネットワークを設計しました。正確には、9つの出力レイヤーを持っています。Keras:model.fit_generatorを使用したマルチ出力モデル用ジェネレータの使用

  1. Keras: How to use fit_generator with multiple outputs of different type
  2. https://keras.io/getting-started/functional-api-guide/

    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を与えている:私は利回りは、単純な発電機を開発しました。

しかし、私はこのエラーを取得しておいてください。

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

seg_8はコンパイルされていますが、ジェネレータではseg_8ではYがありません。彼らは一致してはいけませんか? – Ajjo

+0

はい、私はいくつかのテストを実行していたので、コードからその部分を削除しました。私はコードを更新しました –

答えて

0

あなたのコードが検証ではなく、トレーニング中に障害が発生している。ここで

はコードです。 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) 
+0

それをキャッチしてくれてありがとう! –

関連する問題