imgs_list.append(np.array(Image.open(image).resize((384, 384)))) masks_list.append(np.array(Image.open(mask).resize((384, 384)))) imgs_np = np.asarray(imgs_list) masks_np = np.asarray(masks_list) # %% print(imgs_np.shape, masks_np.shape) # %% [markdown] # ## Plot images + masks + overlay (mask over original) # %% from keras_unet.utils import plot_imgs plot_imgs(org_imgs=imgs_np, mask_imgs=masks_np, nm_img_to_plot=10, figsize=6) # %% [markdown] # ## Get data into correct shape, dtype and range (0.0-1.0) # %% print(imgs_np.max(), masks_np.max()) # %% x = np.asarray(imgs_np, dtype=np.float32) / 255 y = np.asarray(masks_np, dtype=np.float32) / 255 # %% print(x.max(), y.max()) # %%
model.save_weights(model_filename_final) # Plot training history if val_ratio != 0: plot_segm_history(history) else: plot_segm_history(history, metrics=["iou"], losses=["loss"]) #################### # PLOT TEST RESULT # #################### # Plot original + pred + overlay (pred on top of original) images_test, file_names = load_test_data(test_directory) # Predictions from best model according to val_loss if val_ratio != 0: model.load_weights(model_filename_best) labels_pred_best = model.predict(images_test) plot_imgs(org_imgs=images_test, mask_imgs=labels_pred_best, nm_img_to_plot=10) # Predictions from final model model.load_weights(model_filename_final) labels_pred_final = model.predict(images_test) plot_imgs(org_imgs=images_test, mask_imgs=labels_pred_final, nm_img_to_plot=10) # Save final predictions save_test_predictions(labels_pred_final, predictions_directory, file_names)
data_gen_args=dict(rotation_range=15., width_shift_range=0.05, height_shift_range=0.05, shear_range=50, zoom_range=0.2, horizontal_flip=True, vertical_flip=True, fill_mode='constant')) # Train model results = model.fit_generator( train_gen, validation_data=([img_val_data, annot_val_data]), steps_per_epoch=100, epochs=epochs, callbacks=[early_stopper, checkpointer, reduce_learning_rate]) if evaluate: # Load testing data x_test = np.load(directory + r'\test_imgs_v256_3npy.npy').astype('float32') y_test = np.load(directory + r'\test_annot_v5.npy').astype('float32') # Getting the predictions for the testing set y_pred = model.predict(x_test) plot_imgs(org_imgs=x_test[500:, :, :, 1], mask_imgs=y_test[500:, :, :, 1], pred_imgs=y_pred[500:, :, :, 1], nm_img_to_plot=5)
#%% Test predict from keras_unet.utils import plot_imgs model.load_weights(model_filename) x_val, y_val = next(train_gen_patch) print("train shape:", x_val.shape) print("mask shape:", y_val.shape) pred = model.predict(x_val) print("pred shape:", pred.shape) x_val = x_val[:,:,:,:3] plot_imgs( org_imgs=x_val, # required - original images mask_imgs=y_val, # required - ground truth masks pred_imgs=pred, # optional - predicted masks nm_img_to_plot=x_val.shape[0], # optional - number of images to plot color="red", fontsize=25 ) #%% from imgaug import augmenters as iaa import skimage.io as io import matplotlib.pyplot as plt seq = iaa.Sequential([ iaa.Crop(px=(0, 16)), # crop images from each side by 0 to 16px (randomly chosen) iaa.Fliplr(0.5), # horizontally flip 50% of the images iaa.GaussianBlur(sigma=(0, 3.0)) # blur images with a sigma of 0 to 3.0