def net_predict(): model = Unet(backbone_name=backbone, encoder_weights=None, input_shape=(256, 256, 1)) model.load_weights(checkpoint) preds_train = model.predict(X_train, verbose=1) preds_val = model.predict(X_valid, verbose=1) preds_train_t = (preds_train > 0.5).astype(np.uint8) preds_val_t = (preds_val > 0.5).astype(np.uint8) plot_sample(X_valid, y_valid, preds_val, preds_val_t, ix=None)
def workflow(self): # define model model = Unet(backbone_name='resnet50', encoder_weights='imagenet') #model.compile('Adam', 'binary_crossentropy', ['binary_accuracy']) model.load_weights( os.path.join(self.cfgs["SAVE_DIR"], "epoch" + str(self.epoch) + ".h5")) print("RETORE SUCCESSFULLY!") test_images, test_ulabels, test_elabels, test_rlabels, filelist = self.dl.get_test_data( ) # TEST: print('start') start = time.clock() results = model.predict(test_images, batch_size=5, verbose=1) stop = time.clock() print('程序运行时间:', str(stop - start), ' 秒') pmlabels = results[0] print(len(results)) mkdirs(self.cfgs["SAVE_DIR"], ['images', 'labels_e', 'labels_r', 'preds', 'preds_threshold']) for ii in range(results[0].shape[0]): cv2.imwrite( os.path.join(self.cfgs["SAVE_DIR"], 'images/{}'.format(filelist[ii][0])), test_images[ii, :] * 255) cv2.imwrite( os.path.join(self.cfgs["SAVE_DIR"], 'labels_e/{}'.format(filelist[ii][1])), test_elabels[ii, :] * 255) cv2.imwrite( os.path.join(self.cfgs["SAVE_DIR"], 'labels_r/{}'.format(filelist[ii][1])), test_rlabels[ii, :] * 255) cv2.imwrite( os.path.join(self.cfgs["SAVE_DIR"], 'preds/{}'.format(filelist[ii][1])), results[-1][ii, :]) pred_threshold = threshold(results[-1][ii, :]) cv2.imwrite( os.path.join(self.cfgs["SAVE_DIR"], 'preds_threshold/{}'.format(filelist[ii][1])), pred_threshold * 255)
plt.title('Model loss') plt.ylabel('Loss') plt.xlabel('Epoch') plt.legend(['Train', 'Test'], loc='upper left') plt.show() else: print("testing") model.load_weights('best_model.h5') if False: DistanceValues = np.zeros(0) for i in range(len(test)): print(i, len(test) - 1) print(x_test[i]) image, gt_mask = test[i] image = np.expand_dims(image, axis=0) pr_mask = model.predict(image) distanceBase = np.ones(gt_mask[:, :, 1].shape, dtype=np.uint8) - np.uint8(gt_mask[:, :, 1]) distanceMap = cv2.distanceTransform(distanceBase, cv2.DIST_L2, 3) values = distanceMap[np.where(pr_mask.squeeze()[:, :, 1] > 0.9)] DistanceValues = np.append(DistanceValues, values) #print(np.amin(distanceMap),np.amax(distanceMap)) #print(np.count_nonzero(pr_mask.squeeze()[np.where(pr_mask.squeeze()[:,:,1]>0.9)]==gt_mask[np.where(gt_mask[:,:,1]>0.9)])/np.count_nonzero(pr_mask.squeeze()[np.where(pr_mask.squeeze()[:,:,1]>0.9)]))# #fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4, figsize=(15,5))#, ax5 #ax1.imshow(distanceMap, cmap='gray') #ax2.imshow(gt_mask[:,:,1], cmap='gray') #ax3.imshow(pr_mask.squeeze()[:,:,1], cmap='gray') #ax4.boxplot(values)#imshow(gt_mask[:,:,1]==pr_mask.squeeze()[:,:,1], cmap='gray')# #plt.show() fig, (ax1, ax2) = plt.subplots(1, 2) ax1.boxplot(DistanceValues)
def main(): ######################################### # input parser ######################################### parser = argparse.ArgumentParser( description='Road Segmentation Challenge- EPFL.') group_data = parser.add_argument_group('model arguments') group_data.add_argument( '--model', type=str, default="unet", choices=["unet", "manunet4", "manunet5", "manunet6"], help='select the Neural Network model you desired to use.') args = parser.parse_args() for arg in vars(args): print(arg, getattr(args, arg)) modelname = args.model ######################################### # generate data ######################################### # 1: Devide the data data_division.make_folders() # 2 : Load entire images # Generators training_generator = generator.DataGenerator( constants.train_image_path, constants.train_mask_path, augmentation=helpers.aug_with_crop, batch_size=1, ) validation_generator = generator.DataGenerator(constants.val_image_path, constants.val_mask_path) ######################################### # Model and training ######################################### if (modelname == "manunet4"): model = unetManual() model.summary() elif (modelname == "manunet5"): model = unetManualFiveDeep() model.summary() elif (modelname == "manunet6"): model = unetManualSixDeep() model.summary() else: model = Unet(backbone_name='efficientnetb7', encoder_weights='imagenet', encoder_freeze=False) model.compile(optimizer='Adam', loss=bce_jaccard_loss, metrics=[sm.metrics.FScore(threshold=0.5)]) history = model.fit_generator(training_generator, shuffle=True, epochs=30, workers=4, use_multiprocessing=True, validation_data=validation_generator, verbose=1, callbacks=[callbacks.lr_reducer]) # plotting history #helpers.plot_training_history(history) # Save model model.save(constants.PATH + "saved_" + modelname + ".h5") print("Trained model was successfully saved on disk.") #model = load_model(constants.PATH + "saved_"+modelname+".h5") ######################################### # Testing and make predictions ######################################### test = helpers.listdir_fullpath(constants.IMAGE_PATH) os.makedirs(constants.MASK_TEST_PATH) for pth in test: name = os.listdir(pth)[0] path = pth + "/" + name print(path) image = mpimg.imread(path) / 255 if (modelname == "manunet4" or modelname == "manunet5" or modelname == "manunet6"): image = cv2.resize( image, dsize=(384, 384), interpolation=cv2.INTER_CUBIC ) # resize test images to (384,384) to feed to manual Unet prediction = cv2.resize(model.predict(np.expand_dims( image, axis=0)).reshape(384, 384), dsize=(608, 608), interpolation=cv2.INTER_CUBIC ) # resize the predictions to (608,608) else: prediction = model.predict(np.expand_dims(image, axis=0)).reshape( 608, 608) mpimg.imsave(constants.MASK_TEST_PATH + name, prediction) print("Image " + name + " saved") submission_filename = constants.PATH + "test_final_" + modelname + ".csv" image_filenames = helpers.listdir_fullpath(constants.MASK_TEST_PATH) make_submission.masks_to_submission(submission_filename, *image_filenames)
mPrecision_ = 0 mRecall_ = 0 mIU_ = 0 mF1_ = 0 for i in tqdm(range(dlina)): x = get_image(images[i]) if vis and i % numvis == 0: x_vis = x.copy() x = np.expand_dims(x, axis=0) x = preprocessing_fn(x) y_pred = model.predict(x) y_pred = np.argmax(y_pred, axis=-1) y_pred = np.squeeze(y_pred) if vis and i % numvis == 0: y_pred_vis = y_pred.astype(np.uint8) y_pred_vis *= 255 // 2 overlay_pred = cv2.addWeighted( x_vis, 1, cv2.applyColorMap(y_pred_vis, cv2.COLORMAP_OCEAN), 1, 0) y_true = get_image(labels[i]) y_true = y_true.astype('int64') if vis and i % numvis == 0: y_true_vis = y_true.astype(np.uint8)
activation=config.activation) elif config.model == "Nestnet": model = Nestnet(backbone_name=config.backbone, encoder_weights=config.weights, decoder_block_type=config.decoder_block_type, classes=config.nb_class, activation=config.activation) elif config.model == "Xnet": model = Xnet(backbone_name=config.backbone, encoder_weights=config.weights, decoder_block_type=config.decoder_block_type, classes=config.nb_class, activation=config.activation) else: raise model.load_weights(os.path.join(model_path, config.exp_name + ".h5")) model.compile(optimizer="Adam", loss=dice_coef_loss, metrics=["binary_crossentropy", mean_iou, dice_coef]) p_test = model.predict(x_test, batch_size=config.batch_size, verbose=config.verbose) eva = model.evaluate(x_test, y_test, batch_size=config.batch_size, verbose=config.verbose) IoU = compute_iou(y_test, p_test) print("\nSetup: {}".format(config.exp_name)) print(">> Testing dataset mIoU = {:.2f}%".format(np.mean(IoU))) print(">> Testing dataset mDice = {:.2f}%".format(eva[3] * 100.0))
backbone_name = 'efficientnetb3' weight = '20201122-170215_Unet_efficientnetb3_model.h5' model = Unet(backbone_name, classes=1, activation='sigmoid') model_path ='../user_data/model/' + weight model.load_weights(model_path) # model summary print(model.summary(line_length=120)) TEST_MASK_PATH = '../prediction_result/images/' predicted_test = model.predict(X_test) # Save test mask print('Get img name and path') each_test_name = [] each_test_path = [] for i in tqdm(list({i.split('.')[0] for i in os.listdir(TEST_PATH)})): i = i.split('.')[0] each_test_path.append(TEST_PATH + '{}.jpg'.format(i)) each_test_name.append(i) each_test_path.remove(each_test_path[0])
elif status == 'Test': path = os.path.join('', '') model.load_weights(os.path.join(path, '', '')) import scipy.io as sio testData = sio.loadmat('Training.mat') images = testData['images'] masks = testData['masks'] N = images.shape[0] for i in range(N): oriimage = images[i, :, :].astype(np.float64) / 255 s = oriimage.shape image = cv2.resize(oriimage, (256, 256)) image = image[np.newaxis, :, :, np.newaxis] pred = model.predict(image) pred = pred[0, :, :, 0] finalPred = cv2.resize(pred, (s[1], s[0])) predMask = (finalPred > 0.05).astype(np.int) import matplotlib.pyplot as plt r = oriimage.copy() g = oriimage.copy() b = oriimage.copy() r[masks[i, :, :] == 1] = 1 g[masks[i, :, :] == 1] = 0 b[masks[i, :, :] == 1] = 0 r = r[:, :, np.newaxis] g = g[:, :, np.newaxis] b = b[:, :, np.newaxis] plt.figure()
insulator = False return list def rescale(box, factor): return (box[0] * factor, box[1] * factor, box[2] * factor, box[3] * factor) display = [] skipped = 0 failed_to_individually_crop = 0 for batch in range(0, batches): print("Starting batch " + str(batch)) img, label = image_generator.next() crop_img, crop_label = crop_generator.next() out = model.predict(img) for i in range(0, len(label)): if label[i] == 1: tag = "Healthy" else: tag = "Broken" if random() > 0.2: train_or_validate = "train" else: train_or_validate = "validation" save_file_mask = save_folder + "/" + train_or_validate + "/masks/" + tag + "/" + "image" + str( i) + ".jpg" save_file_img = save_folder + "/" + train_or_validate + "/frames/" + tag + "/" + "image" + str( i) + ".jpg"
def workflow(self): # define model model = Unet(backbone_name='resnet50', encoder_weights='imagenet') adam = keras.optimizers.Adam(lr=self.cfgs["LEARNING_RATE"]) model.summary() # model.compile('Adam', sigmoid_cross_entropy_balanced) model.compile( 'Adam', # cross_entropy_balanced loss=self.define_loss(), # 'binary_crossentropy' ) test_images, test_ulabels, test_elabels, test_rlabels, filelist = self.dl.get_test_data( ) if self.cfgs["RESTORE"]: model.load_weights( os.path.join(self.cfgs["SAVE_DIR"], "weights", "epoch150.h5")) print("RETORE SUCCESSFULLY!") callback = TensorBoard('./graph') callback.set_model(model) train_names = [ 'loss', 'u_outputs_sig_loss', 'e_fuse_sig_loss', 'r_fuse_sig_loss', 'fuse_dir_loss' ] current_learning_rate = self.cfgs["LEARNING_RATE"] K.set_value(model.optimizer.lr, current_learning_rate) for i in range(self.cfgs["EPOCH"]): print("[I] EPOCH {}".format(i)) # TRAIN for j in tqdm(range(self.cfgs["STEP"])): images_batch, ulabels_batch, elabels_batch, rlabels_batch, d_labels_batch = self.dl.next_batch( "train") Logs = model.train_on_batch( images_batch, self.define_train_y(ulabels_batch, elabels_batch, rlabels_batch, d_labels_batch), ) write_log(callback, train_names, Logs, i) if i % self.cfgs["INTERVAL"] == 0 and i >= 0: # TEST: results = model.predict(test_images, batch_size=10, verbose=0) logits = results[-1] r_logits = results[-2] # result analyse and show rlt_worker = ResultManager(i, logits, test_ulabels) # r_analyst.compute_roc(savename='roc_vegas_{}.csv'.format(i)) # rlt_worker_r = ResultManager(i, r_logits, test_rlabels) rlt_worker.run() # rlt_worker_r.run() for ii in range(results[0].shape[0]): # cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/images/{}'.format(filelist[ii][0])), # test_images[ii, :] * 255) # cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/labels/{}'.format(filelist[ii][1])), # test_ulabels[ii, :] * 255) # cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/labels_e/{}'.format(filelist[ii][1])), # test_elabels[ii, :] * 255) # cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/labels_r/{}'.format(filelist[ii][1])), # test_rlabels[ii, :] * 255) #cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/preds/{}'.format(filelist[ii][1])), # results[-1][ii, :]) pred_threshold = threshold(results[-1][ii, :]) cv2.imwrite( os.path.join( self.cfgs["SAVE_DIR"], 'main_outputs/preds_threshold/{}'.format( filelist[ii][1])), pred_threshold * 255) # cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_e1/{}'.format(filelist[ii][1])), # threshold(results[1][ii, :]) * 255) # cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_e2/{}'.format(filelist[ii][1])), # threshold(results[2][ii, :]) * 255) # cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_e3/{}'.format(filelist[ii][1])), # threshold(results[3][ii, :]) * 255) # cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_e4/{}'.format(filelist[ii][1])), # threshold(results[4][ii, :]) * 255) # cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_e5/{}'.format(filelist[ii][1])), # threshold(results[5][ii, :]) * 255) # cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_r1/{}'.format(filelist[ii][1])), # threshold(results[6][ii, :]) * 255) # cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_r2/{}'.format(filelist[ii][1])), # threshold(results[7][ii, :]) * 255) # cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_r3/{}'.format(filelist[ii][1])), # threshold(results[8][ii, :]) * 255) # cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_r4/{}'.format(filelist[ii][1])), # threshold(results[9][ii, :]) * 255) # cv2.imwrite(os.path.join(self.cfgs["SAVE_DIR"], 'main_outputs/out_r5/{}'.format(filelist[ii][1])), # threshold(results[10][ii, :]) * 255) # SAVE WEIGHTS current_learning_rate = current_learning_rate * self.cfgs[ "LEARNING_RATE_DECAY"] K.set_value(model.optimizer.lr, current_learning_rate) print('[I] Current Learning Rate: ', current_learning_rate) model_json = model.to_json() with open("model.json", "w") as json_file: json_file.write(model_json) model.save_weights( os.path.join(self.cfgs["SAVE_DIR"], "epoch{}.h5".format(i)))
def run(hyperparams): print('Running model...') #### Begin model input ########################################################################################## def get_model(model_json_fname, modelwtsfname): # This is only for prediction if os.path.isfile(model_json_fname): # Model reconstruction from JSON file with open(model_json_fname, 'r') as f: model = model_from_json(f.read()) else: model = get_unet() #model.summary() # Load weights into the new model model.load_weights(modelwtsfname) return model def focal_loss(gamma=2., alpha=.25): def focal_loss_fixed(y_true, y_pred): pt_1 = tf.where(tf.equal(y_true, 1), y_pred, tf.ones_like(y_pred)) pt_0 = tf.where(tf.equal(y_true, 0), y_pred, tf.zeros_like(y_pred)) return -K.sum( alpha * K.pow(1. - pt_1, gamma) * K.log(pt_1)) - K.sum( (1 - alpha) * K.pow(pt_0, gamma) * K.log(1. - pt_0)) return focal_loss_fixed def jaccard_coef(y_true, y_pred): smooth = 1.0 intersection = K.sum(y_true * y_pred, axis=[-0, -1, 2]) sum_ = K.sum(y_true + y_pred, axis=[-0, -1, 2]) jac = (intersection + smooth) / (sum_ - intersection + smooth) return K.mean(jac) def jaccard_coef_int(y_true, y_pred): smooth = 1.0 y_pred_pos = K.round(K.clip(y_pred, 0, 1)) intersection = K.sum(y_true * y_pred_pos, axis=[-0, -1, 2]) sum_ = K.sum(y_true + y_pred_pos, axis=[-0, -1, 2]) jac = (intersection + smooth) / (sum_ - intersection + smooth) return K.mean(jac) def jaccard_coef_loss(y_true, y_pred): return -K.log(jaccard_coef(y_true, y_pred)) + binary_crossentropy( y_pred, y_true) def dice_coef_batch(y_true, y_pred): smooth = 1.0 intersection = K.sum(y_true * y_pred, axis=[-0, -1, 2]) sum_ = K.sum(y_true + y_pred, axis=[-0, -1, 2]) dice = ((2.0 * intersection) + smooth) / (sum_ + intersection + smooth) return K.mean(dice) def dice_coef(y_true, y_pred): smooth = 1.0 y_true_f = K.flatten(y_true) y_pred_f = K.flatten(y_pred) intersection = K.sum(y_true_f * y_pred_f) dice_smooth = ((2. * intersection) + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth) return (dice_smooth) def dice_coef_loss(y_true, y_pred): return -dice_coef(y_true, y_pred) def dice_coef_batch_loss(y_true, y_pred): return -dice_coef_batch(y_true, y_pred) #Define the neural network def get_unet(): droprate = 0.25 filt_size = 32 inputs = Input((None, None, 1)) conv1 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(inputs) conv1 = Dropout(droprate)(conv1) conv1 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(conv1) pool1 = MaxPooling2D(pool_size=(2, 2))(conv1) filt_size = filt_size * 2 conv2 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(pool1) conv2 = Dropout(droprate)(conv2) conv2 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(conv2) pool2 = MaxPooling2D(pool_size=(2, 2))(conv2) filt_size = filt_size * 2 conv3 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(pool2) conv3 = Dropout(droprate)(conv3) conv3 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(conv3) pool3 = MaxPooling2D(pool_size=(2, 2))(conv3) filt_size = filt_size * 2 conv4 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(pool3) conv4 = Dropout(droprate)(conv4) conv4 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(conv4) pool4 = MaxPooling2D(pool_size=(2, 2))(conv4) filt_size = filt_size * 2 conv5 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(pool4) conv5 = Dropout(droprate)(conv5) conv5 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(conv5) filt_size = filt_size / 2 up6 = concatenate([ Conv2DTranspose(filt_size, (2, 2), strides=(2, 2), padding='same')(conv5), conv4 ], axis=3) conv6 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(up6) conv6 = Dropout(droprate)(conv6) conv6 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(conv6) filt_size = filt_size / 2 up7 = concatenate([ Conv2DTranspose(filt_size, (2, 2), strides=(2, 2), padding='same')(conv6), conv3 ], axis=3) conv7 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(up7) conv7 = Dropout(droprate)(conv7) conv7 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(conv7) filt_size = filt_size / 2 up8 = concatenate([ Conv2DTranspose(filt_size, (2, 2), strides=(2, 2), padding='same')(conv7), conv2 ], axis=3) conv8 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(up8) conv8 = Dropout(droprate)(conv8) conv8 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(conv8) filt_size = filt_size / 2 up9 = concatenate([ Conv2DTranspose(filt_size, (2, 2), strides=(2, 2), padding='same')(conv8), conv1 ], axis=3) conv9 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(up9) conv9 = Dropout(droprate)(conv9) conv9 = Conv2D(filt_size, (3, 3), activation='relu', padding='same')(conv9) conv10 = Conv2D(1, (1, 1), activation='sigmoid')(conv9) model = Model(inputs=[inputs], outputs=[conv10]) #model.compile(optimizer=Adam(lr=1e-5), loss=dice_coef_loss, metrics=[dice_coef]) #model.compile(optimizer=Nadam(lr=1e-3), loss=dice_coef_loss, metrics=[dice_coef]) #model.compile(optimizer=Adadelta(), loss=dice_coef_loss, metrics=[dice_coef]) return model def save_model_to_json(model, model_json_fname): #model = unet.UResNet152(input_shape=(None, None, 3), classes=1,encoder_weights="imagenet11k") #model = get_unet() #model.summary() # serialize model to JSON model_json = model.to_json() with open(model_json_fname, "w") as json_file: json_file.write(model_json) def preprocess_data(do_prediction, inputnpyfname, targetnpyfname, expandChannel, backbone): # Preprocess the data (beyond what I already did before) print('-' * 30) print('Loading and preprocessing data...') print('-' * 30) # Load, normalize, and cast the data imgs_input = (np.load(inputnpyfname).astype('float32') / (2**16 - 1) * (2**8 - 1)).astype('uint8') print('Input images information:') print(imgs_input.shape) print(imgs_input.dtype) hist, bins = np.histogram(imgs_input) print(hist) print(bins) if not do_prediction: imgs_mask_train = np.load(targetnpyfname).astype('uint8') print('Input masks information:') print(imgs_mask_train.shape) print(imgs_mask_train.dtype) hist, bins = np.histogram(imgs_mask_train) print(hist) print(bins) # Make the grayscale images RGB since that's what the model expects apparently if expandChannel: imgs_input = np.stack((imgs_input, ) * 3, -1) else: imgs_input = np.expand_dims(imgs_input, 3) print('New shape of input images:') print(imgs_input.shape) if not do_prediction: imgs_mask_train = np.expand_dims(imgs_mask_train, 3) print('New shape of masks:') print(imgs_mask_train.shape) # Preprocess as per https://github.com/qubvel/segmentation_models preprocessing_fn = get_preprocessing(backbone) imgs_input = preprocessing_fn(imgs_input) # Return appropriate variables if not do_prediction: return (imgs_input, imgs_mask_train) else: return (imgs_input) # Import relevant modules and functions import sys sys.path.append(hyperparams['segmentation_models_repo']) import numpy as np from keras.models import Model from keras.layers import Input, concatenate, Conv2D, MaxPooling2D, Conv2DTranspose, Dropout from keras.optimizers import Adam from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping, CSVLogger from keras.layers.normalization import BatchNormalization from keras.backend import binary_crossentropy import keras import random import tensorflow as tf from keras.models import model_from_json from segmentation_models import Unet from segmentation_models.backbones import get_preprocessing K.set_image_data_format( 'channels_last') # TF dimension ordering in this code # Basically constants expandChannel = True modelwtsfname = 'model_weights.h5' model_json_fname = 'model.json' csvfname = 'model.csv' do_prediction = hyperparams['predict'] if not do_prediction: # Train... print('Training...') # Parameters inputnpyfname = hyperparams['images'] labels = hyperparams['labels'] initialize = hyperparams['initialize'] backbone = hyperparams['backbone'] encoder = hyperparams['encoder'] lr = float(hyperparams['lr']) batch_size = hyperparams['batch_size'] obj_return = hyperparams['obj_return'] epochs = hyperparams['epochs'] # Preprocess the data imgs_train, imgs_mask_train = preprocess_data(do_prediction, inputnpyfname, labels, expandChannel, backbone) # Load, save, and compile the model model = Unet(backbone_name=backbone, encoder_weights=encoder) save_model_to_json(model, model_json_fname) model.compile(optimizer=Adam(lr=lr), loss='binary_crossentropy', metrics=[ 'binary_crossentropy', 'mean_squared_error', dice_coef, dice_coef_batch, focal_loss() ]) # Load previous weights for restarting, if desired and possible if os.path.isfile(initialize): print('-' * 30) print('Loading previous weights ...') model.load_weights(initialize) # Set up the training callback functions model_checkpoint = ModelCheckpoint(modelwtsfname, monitor=obj_return, save_best_only=True) reduce_lr = ReduceLROnPlateau(monitor=obj_return, factor=0.1, patience=100, min_lr=0.001, verbose=1) model_es = EarlyStopping(monitor=obj_return, min_delta=0.00000001, patience=100, verbose=1, mode='auto') csv_logger = CSVLogger(csvfname, append=True) # Train the model history_callback = model.fit( imgs_train, imgs_mask_train, batch_size=batch_size, epochs=epochs, verbose=2, shuffle=True, validation_split=0.10, callbacks=[model_checkpoint, reduce_lr, model_es, csv_logger]) print("Minimum validation loss:") print(min(history_callback.history[obj_return])) else: # ...or predict print('Inferring...') # Parameters inputnpyfname = hyperparams['images'] initialize = hyperparams['initialize'] backbone = hyperparams['backbone'] # lr = float(hyperparams['lr']) # this isn't needed but we're keeping it for the U-Net, where it is "needed" # Preprocess the data imgs_infer = preprocess_data(do_prediction, inputnpyfname, '', expandChannel, backbone) # Load the model #model = get_model(model_json_fname,initialize) model = get_model( os.path.dirname(initialize) + '/' + model_json_fname, initialize) # Run inference imgs_test_predict = model.predict(imgs_infer, batch_size=1, verbose=1) # Save the predicted masks np.save('mask_predictions.npy', np.squeeze(np.round(imgs_test_predict).astype('uint8'))) history_callback = None #### End model input ############################################################################################ return (history_callback)
def predict_3D(): args = parser.parse_args() first_dataset_test = args.first_dataset_path second_dataset_test = args.second_dataset_path predictions_path = args.predictions_path model_path = args.model_path first_dataset_path = Path(first_dataset_test) second_dataset_path = Path(second_dataset_test) Path(predictions_path).mkdir(exist_ok=True, parents=True) backbone = 'resnet50' preprocess_input = get_preprocessing(backbone) # define model model = Unet(backbone, encoder_weights='imagenet', input_shape=(None, None, 3)) model.compile(optimizer=tf.keras.optimizers.Adam(lr=1e-3), loss=dice_loss, metrics=[f1_score, iou_score]) model.load_weights(model_path) for scan_path in first_dataset_path.iterdir(): if scan_path.name.endswith('mask.nii.gz'): print(nib.load(str(scan_path)).header.get_zooms()) print() for scan_path in second_dataset_path.iterdir(): print(nib.load(str(scan_path / 'T1w.nii.gz')).header.get_zooms()) first_dataset_predictions_path = Path(predictions_path + 'first') second_dataset_predictions_path = Path(predictions_path + 'second') first_dataset_predictions_path.mkdir(exist_ok=True, parents=True) second_dataset_predictions_path.mkdir(exist_ok=True, parents=True) fit = MinMaxScaler() for scan_path in first_dataset_path.iterdir(): data, affine = dm.load_raw_volume(scan_path) labels = np.zeros(data.shape, dtype=np.uint8) x_size, y_size, z_size = data.shape for y_index in range(y_size): data_slice = data[:, y_index, :] data_slice = cv2.resize(data_slice, (256, 256)) data_slice = fit.fit_transform(data_slice) data_slice = cv2.cvtColor(data_slice, cv2.COLOR_GRAY2RGB) prediction = model.predict(data_slice[None, :]) prediction[prediction < 0.5] = 0 prediction[prediction >= 0.5] = 1 prediction = prediction.squeeze() labels[:, y_index, :] = cv2.resize(prediction, (z_size, x_size)) dm.save_labels(labels, affine, first_dataset_predictions_path / scan_path.name) for scan_path in second_dataset_path.iterdir(): data, affine = dm.load_raw_volume(scan_path / 'T1w.nii.gz') labels = np.zeros(data.shape, dtype=np.uint8) x_size, y_size, z_size = data.shape for y_index in range(y_size): data_slice = data[:, y_index, :] data_slice = cv2.resize(data_slice, (256, 256)) data_slice = fit.fit_transform(data_slice) data_slice = cv2.cvtColor(data_slice, cv2.COLOR_GRAY2RGB) prediction = model.predict(data_slice[None, :]) prediction[prediction < 0.5] = 0 prediction[prediction >= 0.5] = 1 prediction = prediction.squeeze() labels[:, y_index, :] = cv2.resize(prediction, (z_size, x_size)) dm.save_labels( labels, affine, second_dataset_predictions_path / f'{scan_path.name}.nii.gz')
plt.subplot(122) plt.plot(history_unet.history['loss']) plt.plot(history_unet.history['val_loss']) plt.title('Model loss') plt.ylabel('Loss') plt.xlabel('Epoch') plt.legend(['Train', 'Test'], loc='upper left') plt.show() X_test_ex = X_test.head(10) for p in range(10): # original image image = cv2.imread(list(X_test_ex['image'])[p], cv2.IMREAD_UNCHANGED) image = cv2.resize(image, (256, 256), interpolation=cv2.INTER_NEAREST) # predicted segmentation map pred_mask = model.predict(image[np.newaxis, :, :, :]) pred_mask = tf.argmax(pred_mask, axis=-1) # original segmentation map image_mask = cv2.imread(list(X_test_ex['mask'])[p], cv2.IMREAD_UNCHANGED) image_mask = cv2.resize(image_mask, (256, 256), interpolation=cv2.INTER_NEAREST) plt.figure(figsize=(10, 6)) plt.subplot(131) plt.imshow(image) # Original Image plt.subplot(132) plt.imshow(image_mask, cmap='gray') # Original Mask plt.subplot(133) plt.imshow(pred_mask[0], cmap='gray') # Predicted Mask plt.show()
trans_list = ['hflip', 'vflip', 'rotate'] generator = custom_generator(batches, trans_list) # Train the model hist = model.fit_generator(generator, steps_per_epoch=len(train_images) // 16, epochs=125, validation_data=(val_images, val_masks), callbacks=callbacks) # Load best weights model.load_weights(filepath) # Load test data test_data = load_images(test_images_folder) # Predict the data test_data = preprocess_input(test_data) test_pred = model.predict(test_data) # Store predicted masks to files for i, sample in enumerate(sorted(test_images_folder.iterdir())): io.imsave(predictions_folder / sample.name, np.squeeze(test_pred[i])) # Create submission file pred_files = [] for file in sorted(predictions_folder.iterdir(), key=lambda x: int(str(x).split('_')[-1].split('.')[0])): pred_files.append(str(file)) masks_to_submission(submission_file, *pred_files)
def detect_margins_onnx_v3(config_path, checkpoint_path, frames_dir): from segmentation_models import Unet with open(config_path, 'r') as f: config = json.load(f) target_dir = frames_dir + '_with_masks' if os.path.isdir(target_dir): shutil.rmtree(target_dir) os.mkdir(target_dir) model = Unet( backbone_name='resnet34', input_shape=(config['image_size'][0], config['image_size'][1], 3), classes=1, activation='sigmoid', encoder_weights=None, decoder_block_type='transpose' ) model.load_weights(checkpoint_path) frmaes_num = len(os.listdir(frames_dir)) batch_size = 1 bufer = [] start = time.time() nn_time = 0 for i, image_name in tqdm(enumerate(os.listdir(frames_dir))): # load image image = cv2.imread(os.path.join(frames_dir, image_name)) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # resize it to network input shape image_resized = cv2.resize(image, (config['image_size'][1], config['image_size'][0])) image_resized = image_resized / 255.0 image_resized = image_resized.astype(np.float32) # get mask from network prediction and resize it to the original image shape bufer.append(image_resized) if len(bufer) == batch_size or i + 1 == frmaes_num: nn_start = time.time() masks = model.predict(np.asarray(bufer)) nn_time += time.time() - nn_start for mask in masks: # round prediction to 0 and 1 and convert to uint8 dtype mask = np.round(mask) mask = mask.astype(np.uint8) # resize mask to the orignal image size mask = cv2.resize(mask, (image.shape[1], image.shape[0])) # draw color mask color_mask = np.zeros((*mask.shape, 3), dtype=mask.dtype) color_mask[mask == 1] = (0, 200, 200) # draw mask on image image = cv2.addWeighted(color_mask, 0.3, image, 1, 0) image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) cv2.imwrite(os.path.join(target_dir, image_name), image) bufer = [] end = time.time() print('total execution time {}s\tnn time {}s\tbufer size {}'.format(end - start, nn_time, batch_size))
dir_mask = "Data_Test/Gr_Th/" Eval = [] Dice = [] TP = [] FP = [] width=240 height=240 channels=3 for i in os.listdir(dir_img): Image_Test = cv2.resize(cv2.imread(dir_img+i), (width, height))/255 Image = cv2.resize(cv2.imread(dir_img+i), (width, height))[:,:,2] VT=cv2.resize(cv2.imread(dir_mask+i), (width, height))[:,:,2] VT=(VT==255).astype(int) Image_Test = np.expand_dims(Image_Test, axis=0) Seg = model.predict(Image_Test) Seg = ((Seg[0,:,:,0]*255)>5).astype(int) Eval.append(SEG_EVAL(Seg,VT)) VIS_Seg,VIS_GT=VIS_EVAL(Image, Seg, VT, option = 'region') cv2.imwrite('CNN_Results/Seg_'+i, VIS_Seg) cv2.imwrite('CNN_Results/GT_'+i, VIS_GT) #cv2.imwrite('FPN_Results/Mask_Seg_'+i, Seg) np.savetxt("CNN_Results.csv", Eval, delimiter=",")
dic_times['times'] = times savemat( combinations + "_" + BACKBONE + '_' + name_model + '_times.mat', dic_times) model.save_weights(combinations + "_" + BACKBONE + "_" + name_model + "_model_wIoU" + str(size) + ".h5") ############END TRAINING############# # Load best model model.load_weights(combinations + "_" + BACKBONE + "_" + name_model + "_model_wIoU" + str(size) + ".h5") # model.evaluate(x_val, y_val, verbose=1) # Predict on train, val and test if name_model == "PSPNet": preds_train = model.predict(x_train2, verbose=1) preds_val = model.predict(x_val2, verbose=1) for k in range(0, x_val.shape[0], int(x_val.shape[0] / 100)): x_val_1 = x_val2[k, :, :, :] y_val_1 = y_val2[k, :, :, :] pred_val_1 = preds_val[k, :, :, :] ndvi = y_val_1.astype('float32') im = comb[ combinations] + "_" + BACKBONE + '_' + name_model + '_target_' + str( k) + '_wpatches' + str(size) + '.tif' imsave(im, ndvi) ndvi2 = pred_val_1.astype('float32') im2 = comb[ combinations] + "_" + BACKBONE + '_' + name_model + '_output_' + str(
ModelCheckpoint(save_name, verbose=1, save_best_only=True, mode='min', save_weights_only=True)) history = model.fit_generator(my_generator(x_train, y_train, 32), steps_per_epoch=len(x_train), validation_data=val_generator(x_val, y_val), validation_steps=len(x_val), epochs=10, verbose=1, shuffle=True, callbacks=callbacks_list) save_history(history, 1) test_image = model.predict(test_data[1].reshape(1, image_h, image_w, 3)) test_image = test_image[0, :, :, 0] cv2.imshow('test_image', test_image) cv2.waitKey(0) images_list = os.listdir(test_images_path) for i, img in enumerate(images_list): image = cv2.imread(os.path.join(test_images_path, img), cv2.IMREAD_UNCHANGED) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) image = cv2.resize(image, dsize=(image_w, image_h)) result = model.predict(image.reshape(1, image_h, image_w, 3)) res = result[0, :, :, 0] # if image sise different from 240x320 mask = cv2.resize(res, dsize=(240, 320))
# expand dims from (800,600) to (800,600,1) mask = np.expand_dims(mask, axis=-1) # insert the image into Y_train Y_test[0] = mask # Normalize the images X_test = X_test / 255 test_data = (X_test, Y_test) prediction = model.predict(X_test, batch_size=None, verbose=0, steps=None, callbacks=None, max_queue_size=10, workers=1, use_multiprocessing=False) #test_data = (X_test , Y_test) print(prediction.min()) print(prediction.max()) preds_test_thresh = (prediction >= 0.7).astype(np.uint8) preds_test_thresh.shape print(preds_test_thresh.min()) print(preds_test_thresh.max())
# validation_data=(x_val, y_val),callbacks = callbacks # ) # times = time_callback.times # dic_times = {} # dic_times['times'] = times # savemat(comb[combinations] + "_" + BACKBONE + '_' + name_model + '_times.mat', dic_times) model.save_weights(combinations + "_" + BACKBONE + "_" + name_model + "_model_wIoU" + str(size) + ".h5") ############END TRAINING############# # Load best model model.load_weights(combinations + "_" + BACKBONE + "_" + name_model + "_model_wIoU" + str(size) + ".h5") # preds_train = model.predict(x_train, verbose=1) preds_val1 = model.predict(x_test, verbose=1) # preds_val1 = model.predict([np.reshape(x_test[:,:,:,:5],newshape=(x_test.shape[0],x_test.shape[1],x_test.shape[2],5)), np.reshape(x_test[:,:,:,5],newshape=(x_test.shape[0],x_test.shape[1],x_test.shape[2],1))], verbose=1) # pp = np.squeeze(preds_val1) ## if n_shapes == 128: # preds_val = np.argmax(pp, axis = -1) # preds_val = model.predict_generator(x_val) # preds_val = np.argmax(preds_val, axis=-1) #multiple categories preds_val = preds_val1 for k in range(x_test.shape[0]): #,int(x_test.shape[0]/10)): x_val_1 = x_test[k, :, :, :] y_val_1 = y_test[k, :, :, :] pred_val_1 = preds_val[k, :, :, :] ndvi = y_val_1.astype('float32') im = combinations + "_" + BACKBONE + '_' + name_model + '_target_' + str( k) + '_wpatches' + str(size) + '.tif'
from keras.preprocessing import image from segmentation_models import Unet import numpy as np import imageio model = Unet(backbone_name='resnet101', encoder_weights='imagenet', freeze_encoder=True) model.compile('Adam', 'categorical_crossentropy', ['categorical_accuracy']) img = image.load_img('seg_images/image1.png') x = image.img_to_array(img) x = np.expand_dims(x, axis=0) mask = model.predict(x) mask = mask.astype('uint8') imageio.imwrite("result.png", mask[0])
plt.imshow(np.ma.masked_where(mask[:, :, 1] <= 0.5, mask[:, :, 1]), cmap='jet', alpha=0.5, vmin=0, vmax=1) file_descriptor = "" if prediction: file_descriptor = "_prediction-" else: file_descriptor = "_truth-" plt.savefig("/mnt/home/f0008576/Documents/Results/Sample_Predictions/" + filename + file_descriptor + str(test_idx) + ".png") plt.close() for test_id in range(10): predicted_mask = model.predict(np.expand_dims(X_test[test_id], axis=0)) #sample_iou = dice_coef(Y_test[test_id], predicted_mask) print_mask(X_test[test_id], predicted_mask, uid, test_id, title="Prediction for image {}\nIoU = {}".format( str(test_id), 1), prediction=True, figsize=(10, 10)) #print_mask(X_test[test_id], Y_test[test_id], uid, test_id, title="True mask for image {}".format(str(test_id)), prediction=False, figsize=(10,10)) #print_sample(X_test[test_id], uid, test_id, title="Sample image {}".format(str(test_id))) print(filename)
mask_datagen = ImageDataGenerator(**data_gen_args) seed = 42 bs = 32 # BacthSize nb_epochs = 100 # epoch image_generator = image_datagen.flow(X_train, seed=seed, batch_size=bs, shuffle=True) mask_generator = mask_datagen.flow(y_train, seed=seed, batch_size=bs, shuffle=True) # Just zip the two generators to get a generator that provides augmented images and masks at the same time train_generator = zip(image_generator, mask_generator) results = model.fit_generator(train_generator, steps_per_epoch=(spe), epochs=nb_epochs, validation_data=(X_valid, y_valid), callbacks=[save, lr_schedule, reduce_lr]) # save final model model.save_weights(CKPT_PATH + '{}_{}_{}_model.h5'.format(t, model_name, backbone_name)) # predicte valid data predicted = model.predict(X_valid)
class SegmentationModel(object): def __init__(self, model_path='models/unet_MoNuSeg.hdf5', target_size=(512, 512)): self.target_size = target_size ## build model self.model = Unet('resnet34', input_shape=(512, 512, 3)) ## load weights self.model.load_weights(model_path, skip_mismatch=True, by_name=True) def predict(self, image_path): ''' input: 1. image_path str return: 1. mask_pred np.array('float32') 512*512 tips: must use io.imread, model is trained with io.imread image. cv.imread is different from io.imread. Could cause a wrong predict. ''' image = io.imread(image_path) if image.shape[-1] > 3: image = image[:, :, :-1] image = trans.resize(image, self.target_size) image = image.reshape((1, ) + image.shape) mask_pred = self.model.predict(image) mask_pred = mask_pred.reshape(mask_pred.shape[1:-1]) return mask_pred def water_image(self, mask, thresh=0.3): ''' input: 1. mask np.array('float32') 512*512 2. thresh : control the sensitivity of seed. float return: 1. markers np.array('int32') 512*512 -1 indicates boundary ''' mask = mask * 255 mask = mask.astype('uint8') # gray\binary image gray = mask ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY | cv.THRESH_OTSU) # morphology operation kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3)) mb = cv.morphologyEx(binary, cv.MORPH_OPEN, kernel, iterations=2) sure_bg = cv.dilate(mb, kernel, iterations=3) # distance transform dist = cv.distanceTransform(mb, cv.DIST_L2, 3) ret, surface = cv.threshold(dist, dist.max() * thresh, 255, cv.THRESH_BINARY) surface_fg = np.uint8(surface) unknown = cv.subtract(sure_bg, surface_fg) ret, markers = cv.connectedComponents(surface_fg) # watershed transfrom markers += 1 markers[unknown == 255] = 0 mask = mask.reshape(mask.shape + (1, )) mask = np.concatenate((mask, mask, mask), axis=2) markers = cv.watershed(mask, markers=markers) # mask[markers == -1] = [0, 0, 255] return markers
validation_steps=len(X_test) // batch_size, epochs=epoch_no, callbacks=backroom.callbacks) elapsed_time = time.time()-start_time # measuring modelling time print(time.strftime("%H:%M:%S", time.gmtime(elapsed_time))) # beautifying time format #------------------------------------------------------------------------------ # 4. STEP # Prediction model.load_weights(SSD_path + "model/weights/fin_M1_unet_best.h5") y_pred = model.predict(X_test) # croping image vector extensions y_pred = y_pred[:,16:80,16:80,:] y_test = y_test[:,16:80,16:80] # original y_test is stored above # dimension reduction (uses 54x64 vector size) y_pred_twodim = backroom.dimension_reduction(y_pred, mask_label_dict) #------------------------------------------------------------------------------ # 5. STEP
dic_times['times'] = times savemat( comb[combinations] + "_" + BACKBONE + '_' + name_model + '_times.mat', dic_times) model.save_weights(comb[combinations] + "_" + BACKBONE + "_" + name_model + "_model_wIoU" + str(size) + ".h5") ############END TRAINING############# # Load best model model.load_weights(comb[combinations] + "_" + BACKBONE + "_" + name_model + "_model_wIoU" + str(size) + ".h5") # model.evaluate(x_val, y_val, verbose=1) # Predict on train, val and test if name_model == "PSPNet": preds_train = model.predict(x_train2, verbose=1) preds_val = model.predict(x_val2, verbose=1) for k in range(0, x_val.shape[0], int(x_val.shape[0] / 100)): x_val_1 = x_val2[k, :, :, :] y_val_1 = y_val2[k, :, :, :] pred_val_1 = preds_val[k, :, :, :] ndvi = y_val_1.astype('float32') im = comb[ combinations] + "_" + BACKBONE + '_' + name_model + '_target_' + str( k) + '_wpatches' + str(size) + '.tif' imsave(im, ndvi) ndvi2 = pred_val_1.astype('float32') im2 = comb[ combinations] + "_" + BACKBONE + '_' + name_model + '_output_' + str(