I need to train a 3D_Unet model with (128x128x128) patches of 42 CT scans.
My input data is 128x128x128 for the CT scans and also for masks. I extended the shape of arrays to (128, 128, 128, 1). Where 1 is the channel.
The problem is how to feed the model with my list of 40 4D-arrays?
How can I use the model.fit() or model.train_on_batch with the correct input shape specified in my Model?
project_name = '3D-Unet Segmentation of Lungs'
img_rows = 128
img_cols = 128
img_depth = 128
# smooth = 1
K.set_image_data_format('channels_last')
#corresponds to inputs with shape:
#(batch, spatial_dim1, spatial_dim2, spatial_dim3, channels)
def get_unet():
inputs = Input(shape=(img_depth, img_rows, img_cols, 1))
conv1 = Conv3D(32, (3, 3, 3), activation='relu', padding='same')(inputs)
conv1 = Conv3D(32, (3, 3, 3), activation='relu', padding='same')(conv1)
pool1 = MaxPooling3D(pool_size=(2, 2, 2))(conv1)
conv2 = Conv3D(64, (3, 3, 3), activation='relu', padding='same')(pool1)
conv2 = Conv3D(64, (3, 3, 3), activation='relu', padding='same')(conv2)
pool2 = MaxPooling3D(pool_size=(2, 2, 2))(conv2)
....
model = Model(inputs=[inputs], outputs=[conv10])
model.summary()
#plot_model(model, to_file='model.png')
model.compile(optimizer=Adam(lr=1e-5, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.000000199),
loss='binary_crossentropy', metrics=['accuracy'])
return model
What should I specify in either .train_on_batch() or .fit()?
This is the error I get when using the .train_on_batch option:
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 42 arrays
model.train_on_batch(train_arrays_list, mask_arrays_list)
This is the error I get when using the .model.fit option after having increased the shape of arrays with axis=0.
UnboundLocalError: local variable 'batch_index' referenced before assignment
model.fit(train_arrays_list[0], mask_arrays_list[0],
batch_size=1,
epochs=50,
verbose=1,
shuffle=True,
validation_split=0.10,
callbacks=[model_checkpoint, csv_logger])
You have to transform your list of numpy arrays of shape (128, 128, 128, 1) into a stacked 5 dimensional numpy array of shape (42, 128, 128, 128, 1). You can do this with: model.fit(np.array(train_arrays_list), np.array(mask_arrays_list), batch_size=1, ...)