Hi, my U-Net shall predict CT images from MRIs. I have already trained and validated the U-Net with concatenated 3D MRIs/CTs as follows:
def train_valid_model(X, Y, x_val, y_val):
s = X.shape
model = UNet_model_2D(s[1], s[2], 1) # s[1]=256, s[2]=256
callbacks = [
tf.keras.callbacks.TensorBoard(
log_dir='logs',
histogram_freq=1,
write_graph=True,
write_images=True,
)
]
# return a History object whose attribute '.history ' is a record of
# training loss, metrics, validation loss, and validation metrics values
results = model.fit(
x=X, # concatenated 3D MRIs
y=Y, # concatenated 3D reference CTs
batch_size=16,
epochs=200,
verbose=1,
callbacks=callbacks,
validation_data = (x_val, y_val), # concatenated 3D MRIs/CTs
)
tmp = list(results.history.values())
train_loss=tmp[0][:] # train loss
val_loss=tmp[1][:] # val loss
# write/append csv file
f = open('log_train_loss_TF_CT.csv', 'a')
writer = csv.writer(f)
writer.writerow(train_loss)
f.close()
f = open('log_val_loss_TF_CT.csv', 'a')
writer = csv.writer(f)
writer.writerow(val_loss)
f.close()
model.save('pCT_2D_deep_large_batch16', save_format='tf')
Looking at the loss function graph in TensorBoard, I found that after 60 epochs, there is a good compromise between further convergence and overfitting. Therefore, I now want to predict the CTs from the concatenated test MRIs with the model parameters/weights as they were after 60 epochs. How do I do this?
I have the following approach so far:
# load trained & validated model
model_name = 'pCT_2D_deep_large_batch16'
model = tf.keras.models.load_model(model_name, compile=False)
# load concatenated test MRIs
X_test = nib.load('test_MRIs.nii.gz').get_fdata()
# predict sCTs
predicted_data = model.predict(X_test, verbose=1)
# save predicted sCTs as concatenated NIfTI file
image = nib.Nifti1Image(predicted_data, affine=None)
nib.save(image, 'predicted_sCTs.nii.gz')
In the Spyder console, this is what comes up:
200/200 [==============================] - 433s 2s/step
Is there a way to stop at 60/200? Can anyone help me please?