def fit(training_images, mask_images, save_path, model): ''' This function trains the UNET model and saves it. Args: training_images: prepared training images type: np.array mask_images: prepared mask images type: np.array save_path: path where the model will be saved type: STRING model: tiramisu or unet type: STRING ''' if model is 'tiramisu': model = tiramisu.create_tiramisu( 3, nb_layers_per_block=[4, 5, 7, 10, 12, 15], p=0.2, wd=1e-4) training_images = convert_images_gray2rgb(training_images) mask_images = convert_images_gray2rgb(mask_images) else: model = unet() model.fit(training_images, mask_images, batch_size=4, epochs=100, verbose=1, shuffle=True) model.save(save_path + "/" + "model.h5")
def predict(testing_images, model_path, save_result_path): #loading model and predicting mask model = unet() model.load_weights(model_path) prediction = model.predict(testing_images, batch_size=4, verbose=1) np.save(save_result_path + '/prediction.npy', prediction)
def predict(testing_images, model_path, save_result_path, model): ''' This function predicts the test images and saves the predictions Args: testing_images: prepared testing images type: np.array model_path: path to the model type: STRING save_result_path: path where the predictions will be saved type: STRING model: tiramisu or unet type: STRING ''' if model is 'tiramisu': model = tiramisu.create_tiramisu( 3, nb_layers_per_block=[4, 5, 7, 10, 12, 15], p=0.2, wd=1e-4) testing_images = convert_images_gray2rgb(training_images) else: model = unet() model.load_weights(model_path) prediction = model.predict(testing_images, batch_size=4, verbose=1) np.save(save_result_path + '/prediction.npy', prediction)
def predict(test_nparray,model_path,save_result_path): """predict values and save then in a single numpy array.""" #loading model and predicting mask model=unet() model.load_weights(model_path) prediction = model.predict(test_nparray, batch_size=4,verbose=1) np.save(save_result_path+'/prediction.npy', prediction) return prediction
def train_model(): """ """ model = unet() #Fitting and saving model model.fit(train_nparray, masks, batch_size=1, epochs=30, verbose=1, shuffle=True) model.save("model.h5") return None
def predict(): """predicts values and save them in a single numpy array.""" #loading model and predicting mask model = unet() model.load_weights("model.h5") test_nparray = np.load('test.npy') prediction = model.predict(test_nparray, batch_size=4, verbose=1) np.save('prediction.npy', prediction) return prediction
def fit(training_images, mask_images, save_path): model = unet() #Fitting and saving model model.fit(training_images, mask_images, batch_size=4, epochs=100, verbose=1, shuffle=True) model.save(save_path + "/" + "model.h5")
def predict(testing_images, model_path, save_result_path): #loading model and predicting mask ''' Takes in the images to be tested and predicts and saves the result ''' model = unet() model.load_weights(model_path) prediction = model.predict(testing_images, batch_size=4, verbose=1) np.save(save_result_path + "/prediction.npy", prediction)
def train_unet_model(data,mask_list): ''' This function takes as input the data as list of list and mask as the list and train the unet model after some preprocessing on the data ''' train_data = prunet.shape_img_data(data) # calling the function to bring the data to a shape to be passsed for training masks = prunet.shape_mask_data(mask_list) # calling the function bring the mask to a shape to be passed for training. model = cnn.unet() #Fitting and saving model model.fit(train_data, masks, batch_size=2, epochs=15, verbose=1, shuffle=True) # fitting the model on the training data model.save("../dataset/model.h5") #saving the model created.
def test_unet_model(data): ''' This function takes as input the test data as list of list. Preprocess this data to bring it to the desired shape for prediction and performs predictions ''' test_data = shape_img_data(data) # calling the function to bring the data to the desired shape for prediction. model = cnn.unet() model.load_weights('../dataset/model.h5') #loading the model created after training prediction = model.predict(test_data, batch_size =4, verbose = 1) #performing predictions np.save('../dataset/prediction.npy', prediction) # saving the predictions
def fit(training_images, mask_images, save_path): ''' This method trains the imaging model via the unet @param training_images - input path to training dataset @param mask_images - input path to mask files @param save_path - path to save the trained resultant data ''' model = unet() #Fitting and saving model model.fit(training_images, mask_images, batch_size=4, epochs=10, verbose=1, shuffle=True) model.save(save_path + "/model.h5")
def train_model(): """ trains the unet model and saves it """ model = unet() #Fitting and saving model train_nparray = np.load("train.npy") masks = np.load("masks.npy") model.fit(train_nparray, masks, batch_size=1, epochs=20, verbose=1, shuffle=True) print("saving the model") model.save("model.h5") return None
def train(): #loading images and mask a np array images = np.load(path_image) mask = np.load(path_mask) print("-------data loaded---------") #reshaping training images and mask to 4 channels reshaped = [] for image in images: reshaped.append(resize(image, (512, 512), preserve_range=True)) reshaped = np.array(reshaped) train = reshaped[..., np.newaxis] print("-------images reshaped---------") reshaped_mask = [] for msk in mask: reshaped_mask.append(resize(msk, (512, 512), preserve_range=True)) reshaped_mask = np.array(reshaped_mask) masks = reshaped_mask[..., np.newaxis] print("-------mask reshaped---------") #augmenting data train = train.astype('float32') mask_images = masks.astype('float32') training_images = train - np.mean(train) training_images = training_images / np.std(train) mask_images = mask_images / 255 print("-------data normalized---------") print("-------loading model---------") model = unet() #Fitting and saving model model.fit(training_images, mask_images, batch_size=32, epochs=30, verbose=1, shuffle=True) model.save(args.save_path + "/model.h5")
def predict(): #loading and reshaping image images = np.load(path_image) reshaped = [] for image in images: reshaped.append(resize(image, (512, 512), preserve_range=True)) reshaped = np.array(reshaped) test = reshaped[..., np.newaxis] #Augmenting test data test = test.astype('float32') testing_images = test - np.mean(test) testing_images = testing_images / np.std(test) #loading model and predicting mask model = unet() model.load_weights('model.h5') predicted = model.predict(testing_images, verbose=1) np.save(save_result_path + '/predictedMask.npy', predicted)
def main(): # ---------- Load data ---------- # train_im_dir = './train/images/' train_ma_dir = './train/masks/' x_train, y_train, x_val, y_val = load_data.get_training_data(train_im_dir, train_ma_dir, split=0.1) test_im_dir = './test/images/' x_test = load_data.get_test_data(test_im_dir) # ---------- Define model ---------- # model = unet_model.unet() # model = unet_model_test.unet() # <-- use this model to test code (only 1 conv layer) adam = optimizers.Adam(lr=1e-4) model.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy']) model.summary() # ---------- Run model ---------- # filepath = 'weights.best.hdf5' checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max') callbacks_list = [checkpoint] model.fit(x_train, y_train, epochs=1, batch_size=64, validation_data=(x_val, y_val), callbacks=callbacks_list, verbose=1) model.load_weights('weights.best.hdf5') # ---------- Predict train results ---------- # y_train_pred = model.predict(x_train, verbose=1) y_val_pred = model.predict(x_val, verbose=1) # ---------- Evaluate results ---------- # y_train_eval = y_train[0:10, :, :, :] y_train_pred_eval = y_train_pred[0:10, :, :, :] iou_train, iou_val = evaluate_results.plot_mean_iou( y_train_eval, y_train_pred_eval, y_val, y_val_pred) threshold_adj = 0 y_train_pred = np.round(y_train_pred + threshold_adj, 0) y_val_pred = np.round(y_val_pred + threshold_adj, 0) evaluate_results.mean_iou(threshold_adj, iou_train, iou_val) # ---------- Show pictures ---------- # evaluate_results.show_pictures(x_val[0:10, :, :, :], y_val[0:10, :, :, :], y_val_pred[0:10, :, :, :]) # ---------- Predict test results ---------- # y_test_pred = model.predict(x_test, verbose=1) y_test_pred_bin = np.round(y_test_pred + threshold_adj) # ---------- Create submission ---------- # file_name = 'submission.csv' submission.create_submission(y_test_pred_bin, test_im_dir, file_name)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') data_sets_test = CNNDataLayer(data_root=data_directory, filenames=test_files, loader=CNNloader) data_loaders_test = data.DataLoader( data_sets_test, batch_size=8, shuffle=True, num_workers=0, ) """ We will load Unet model as well as our own model here """ """Loading unet""" from unet_model import unet model_unet = unet() model_unet = model_unet.to(device) model_loaded = torch.load("Unet model here", map_location=lambda storage, loc: storage) model_unet.load_state_dict(model_loaded) """Now let's load our classifier model""" from classifier_on_unet import ClassifierOnUnet model_trained = ClassifierOnUnet() model_trained = model_trained.to(device) model_loaded = torch.load("Classifier on Unet model here", map_location=lambda storage, loc: storage) model_trained.load_state_dict(model_loaded) #We will be saving the activations on the hard disk model_unet.eval() model_trained.eval()
import os import skimage.io as io import skimage.transform as trans import numpy as np from keras.models import * from keras.layers import * from keras.optimizers import * from keras import backend as keras from keras.callbacks import ModelCheckpoint, LearningRateScheduler, Callback import PIL import warnings import os import cv2 from io import StringIO import matplotlib.pyplot as plt from keras.preprocessing.image import ImageDataGenerator from keras.models import load_model from helper1 import testGenerator from unet_model import unet h1 = unet((512, 512, 3)) h1.load_weights('unet_membrane.hdf5') #path to weights of unet model testGene = testGenerator("test") #path to test folder results = h1.predict_generator(testGene, 5, verbose=1) print(results.shape) #print(results.shape) saveResult("test_results", results) #path to save results
data_loaders_train = data.DataLoader( data_sets_train, batch_size=8, shuffle=True, num_workers=0, ) data_loaders_test = data.DataLoader( data_sets_test, batch_size=8, shuffle=True, num_workers=0, ) from unet_model import unet model_to_train = unet() model_to_train = model_to_train.to(device) criterion = nn.BCELoss() optimizer_ft = optim.SGD(model_to_train.parameters(), lr=0.000001, momentum=0.9) num_epochs = 100 total_loss_examples = {} total_loss_examples['train'] = 0 total_loss_examples['test'] = 0 """Lets train model now""" for epoch in range(1, num_epochs + 1): print('Epoch is ' + str(epoch)) train_loss = 0
n_class = 7 dropout = 0 epochs = 20 valid_steps = 1 nx = 572 ny = 572 # generator = image_gen.RgbDataProvider(nx, ny, cnt=10) # train_dataset = gen_dataset(generator, 20) # test_dataset = gen_dataset(generator, 2) # train_dataset = train_dataset.shuffle(buffer_size).batch(batch_size).repeat() # train_dataset1 = train_dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE) # test_dataset = test_dataset.batch(1) net = unet(dropout, n_class, shape=(ny, nx, 3), layers=3, padding='valid') model = net.model # gen_ds = gen_from_image("./data/cj_right_all.png", "./data/cj_right_all_gt.png", nx, ny, # net.offset, buffer_size, batch_size) gen_ds = gen_from_image("./data/cj_right_all_20200402_1420.png", "./data/cj_right_all_20200402_1420_gt.png", nx, ny, net.offset, buffer_size, batch_size) train_dataset = gen_ds.dataset test_dataset = gen_ds.test_dataset checkpoint_prefix = "training_checkpoints/cp-{epoch:04d}-{val_loss:.2f}.ckpt" model_path = tf.train.latest_checkpoint(os.path.dirname(checkpoint_prefix)) model.load_weights(model_path) test_imgs = np.array([img.numpy() for img in gen_ds.images])
from unet_model import unet import torch import matplotlib.pyplot as plt from PIL import Image import numpy as np import cv2 transforms = transforms.Compose([ transforms.Resize(128), transforms.ToTensor(), transforms.Normalize(mean=[0, 0, 0], std=[0.5, 0.5, 0.5]) ]) img = Image.open('test.jpg') orig = plt.imread('test.jpg') img = transforms(img) img = Variable(img).cpu() img = img.unsqueeze(0) model = unet(1) model.load_state_dict(torch.load('unet_brats.pth', map_location='cpu')) model.cpu() model.eval() pred = model(img) pred = pred.squeeze(0).squeeze(0) pred = pred.detach().numpy() pred_up = cv2.resize(pred, dsize=(240, 240)) out = pred_up * orig cv2.imwrite('output.jpg', out) #edges = cv2.Canny(pred_up,100,200)