def start(args): # listing out hyperparameters hyperparameters = {} hyperparameters['name'] = args[0] hyperparameters['device'] = torch.device('cuda:' + args[1]) hyperparameters['epochs'] = int(args[2]) hyperparameters['batch_size'] = int(args[3]) hyperparameters['augument_factor'] = int(args[4]) hyperparameters['model_depth'] = int(args[5]) hyperparameters['nonlinear_activation'] = args[6] # 'elu' or 'relu' hyperparameters['dropout_rate'] = float(args[7]) hyperparameters['learning_rate'] = float(args[8]) hyperparameters['optimizer'] = args[9] # 'adam' or 'sgd' hyperparameters['aug_tricks'] = args[10] #0/1/2 = none/standard/all masks = pickle.load(open("cache/masks.p", "rb")) oowl = pickle.load(open("cache/oowl.p", "rb")) train_keys = pickle.load(open("cache/train_keys.p", "rb")) val_keys = pickle.load(open("cache/val_keys.p", "rb")) test_keys = pickle.load(open("cache/test_keys.p", "rb")) unet = UNet(hyperparameters) trainer = Trainer(unet, hyperparameters, train_keys, val_keys, oowl, masks) trainer.train()
def predict(image_path): model = UNet.UNet(input_channels=3) model.load_state_dict(torch.load(CONFIG.MODEL_PATH)) model.eval() image = np.array(Image.open(image_path).convert('RGB')) mean = CONFIG.mean std = CONFIG.std transforms = alb.Compose([ alb.Normalize(mean, std, always_apply=True), alb.Resize(512, 512, always_apply=True) ]) image_ = transforms(image=image)['image'] image_ = torch.tensor(image_).unsqueeze(0) image_ = image_.permute(0, 3, 1, 2) with torch.no_grad(): prediction = model(image_) prediction = prediction.squeeze(0).squeeze(0) prediction = torch.sigmoid(prediction) prediction = (prediction > CONFIG.pred_threshold) * 1 prediction = prediction.detach().cpu().numpy() print('-- SAVING IMAGE --\n') image = cv2.resize(image, (prediction.shape[-1], prediction.shape[-1])) image[:, :, 1] = image[:, :, 1] * (1 - prediction) cv2.imwrite('output/segmented.jpg', image)
def __init__(self, model_file='put_your_model_file_name_here'): # You should # 1. create the model object # 2. load your state_dict # 3. call cuda() # self.model = ... # self.batch_size = 1 # set device self.device = torch.device( "cuda:0" if torch.cuda.is_available() else "cpu") #### model 1: predict Binary RoadMap #### self.model1 = UNet(in_channel=1, out_channel=1).to(self.device) self.model1.load_state_dict( torch.load('best_val_loss_road_map_labeleddata.pt', map_location=self.device)) # TODO: self.model1.load_state_dict(torch.load('classification.pth', map_location=self.device)) #### model 2: predict Bounding Boxes #### self.model2 = BoundingBox().to(self.device) # TODO: self.model2.load_state_dict(torch.load('classification.pth', map_location=self.device)) pass
def ValidRed2D(testloader, Bone, Network, path): dice_value = [] if Network == 'UNet': net = UNet().to(device) elif Network == 'ResNet': net = ResNet(BasicBlock, [3, 4, 6]).to(device) net.load_state_dict(torch.load(path)) for i, data in enumerate(testloader, 0): inputs, labels = data inputs = inputs.to(device) labels = labels.to(device) outputs = net(inputs) dice = 0.0 pr = labels[0].cpu().detach().numpy() gt = outputs[0].cpu().detach().numpy() gt[0, :, :][gt[0, :, :] <= 0.5] = 0 gt[0, :, :][gt[0, :, :] > 0.5] = 1 dice = dice + np.abs( computeQualityMeasures(pr[0, :, :].flatten(), gt[0, :, :].flatten())) if (i == 1600): plt.figure() plt.imshow(inputs[0, 0, :, :].cpu().detach().numpy(), cmap=plt.cm.gray, interpolation="nearest", vmin=-3, vmax=2) plt.imshow(outputs[0, 0, :, :].cpu().detach().numpy(), 'OrRd', interpolation='none', alpha=0.4) plt.savefig(os.path.join( ResultsDirectory, Bone, 'Images', 'patches_' + Network + '_' + Bone + str(i) + '.png'), dpi=150) plt.figure() plt.imshow(inputs[0, 0, :, :].cpu().detach().numpy(), cmap=plt.cm.gray, interpolation="nearest", vmin=-3, vmax=2) plt.imshow(labels[0, 0, :, :].cpu().detach().numpy(), 'OrRd', interpolation='none', alpha=0.4) plt.savefig(os.path.join( ResultsDirectory, Bone, 'Images', 'patches_Truth_' + Network + '_' + Bone + str(i) + '.png'), dpi=150) dice_value.append(dice) np.savetxt(os.path.join(ResultsDirectory, Bone, 'DICE_test.txt'), dice_value) print('Standard Deviation:' + str(statistics.stdev(dice_value))) print('Mean:' + str(statistics.mean(dice_value)))
def load(size,modelName): """function to load the images size -- tuple for the image dimensions required by the model modelName -- name for the model """ if not os.path.exist(savePath): model = UNet((size[0],size[1],1)) model.load_weights(modelName) return model
def ValidRed3D(testloader, Bone, Network, path, sujet): volume3D_terrain = [] volume3D_exp = [] if Network == 'UNet': net = UNet().to(device) elif Network == 'ResNet': net = ResNet(BasicBlock, [3, 4, 6]).to(device) net.load_state_dict(torch.load(path)) for i, data in enumerate(testloader, 0): inputs, labels = data inputs = inputs.to(device) labels = labels.to(device) outputs = net(inputs) gt = outputs[0].cpu().detach().numpy() gt[0, :, :][gt[0, :, :] <= 0.5] = 0 gt[0, :, :][gt[0, :, :] > 0.5] = 1 volume3D_terrain.append(labels[0, 0].cpu().detach().numpy()) volume3D_exp.append(gt[0]) if i == 30: plt.figure() plt.imshow(inputs[0, 0, :, :].cpu().detach().numpy(), 'gray', interpolation='none') plt.imshow(outputs[0, 0, :, :].cpu().detach().numpy(), 'OrRd', interpolation='none', alpha=0.6) plt.savefig(os.path.join( ResultsDirectory, Bone, 'Images', '2DSlices_' + Network + '_' + Bone + sujet + str(i) + '.png'), dpi=150) plt.figure() plt.imshow(inputs[0, 0, :, :].cpu().detach().numpy(), 'gray', interpolation='none') plt.imshow(labels[0, 0, :, :].cpu().detach().numpy(), 'OrRd', interpolation='none', alpha=0.6) plt.savefig(os.path.join( ResultsDirectory, Bone, 'Images', '2DSlices_Truth_' + Network + '_' + Bone + sujet + str(i) + '.png'), dpi=150) volume3D_terrain = np.array(volume3D_terrain) volume3D_exp = np.array(volume3D_exp) dice = np.abs( computeQualityMeasures(volume3D_terrain.flatten(), volume3D_exp.flatten())) print('dice:' + str(dice))
def main(args): ckpt_path = os.path.join(args.output_path, "Checkpoint") log_path = os.path.join(args.output_path, "Log") # this is just for my path, remember to change if you use your own dir check_dir("../output/") check_dir(args.output_path) check_dir(log_path) check_dir(ckpt_path) torch.cuda.set_device(args.gpu_id) train_list, test_list = create_list(args.data_path, ratio=args.train_ratio) # define the dataset and loader train_set = MySet(train_list) train_loader = DataLoader(train_set, batch_size=args.batch_size, shuffle=True) test_set = MySet(test_list) test_loader = DataLoader(test_set, batch_size=1, shuffle=False) # define the network and load the init weight net = UNet.UNet().cuda() if args.is_load: net.load_state_dict(torch.load(args.load_path)) # define the optimizer of the training process optimizer = torch.optim.Adam( net.parameters(), lr=args.lr, ) # define the loss function cost = torch.nn.BCELoss() best_dice = 0. for epoch in range(args.init_epoch, args.init_epoch + args.num_epoch): start_time = time.time() # train one epoch epoch_loss = train_epoch(net, train_loader, optimizer, cost) epoch_time = time.time() - start_time # eval in test data after one epoch training epoch_dice = test_epoch(net, test_loader) info_line = "Epoch {} || Loss: {:.4f} | Time: {:.2f} | Test Dice: {:.4f} ".format( str(epoch).zfill(3), epoch_loss, epoch_time, epoch_dice) print(info_line) open(os.path.join(log_path, 'train_log.txt'), 'a').write(info_line + '\n') # save the checkpoint torch.save(net.state_dict(), os.path.join(ckpt_path, "Network_{}.pth.gz".format(epoch))) if epoch_dice > best_dice: best_dice = epoch_dice torch.save(net.state_dict(), os.path.join(ckpt_path, "Best_Dice.pth.gz"))
def main(): pytorch_model = UNet.UNet(1, 1, start_filts=16, depth=2) saved_params = torch.load('../unet_2/t7/epoch_89.pth') pytorch_model.load_state_dict(saved_params['state_dict']) test_image = np.load('../pytorch_keras/test.npy').astype(np.float32) x = Variable( torch.from_numpy(test_image[np.newaxis, np.newaxis, 12:, :-14])) out = pytorch_model(x) pytorch2caffe(x, out, 'pillnet.prototxt', 'pillnet.caffemodel')
def __init__(self, **kwargs): self.kwargs = kwargs self.data = Data(kwargs['h5']) self.net = UNet.UNet(3, kwargs["nc"]) self.net = self.net.cuda() if kwargs['MD']: dic = torch.load(kwargs['MD']) self.net.load_state_dict(dic) self.loss = UNet.FocalSmoothLoss(kwargs['nc'], kwargs['sm'], kwargs['gm'], kwargs['wt']) self.opt = torch.optim.SGD(self.net.parameters(), lr = self.kwargs['lr'], momentum = 0.9, nesterov = True, weight_decay = kwargs['wd']) self.sch = torch.optim.lr_scheduler.ReduceLROnPlateau(self.opt, "min", patience = 3) self.losses = {"train": [], "val": []}
def __init__(self, output_folder, in_channels=1, out_channels=2, depth=5, epochs=350, batch_size=96, lr=0.001, do_data_augmentation=0, cuda=False, lr_decay=100, previous=False, number_filter=4, size=128, step=128): # Assign member variables self.output_folder = output_folder self.in_channels = in_channels self.out_channels = out_channels self.depth = depth self.epochs = epochs self.batch_size = batch_size self.lr = lr self.data_aug = do_data_augmentation self.cuda = cuda self.lrDecay = lr_decay self.number_filter = number_filter self.size = size self.step = step # Creation of the output folder try : os.makedirs(self.output_folder, exist_ok=False) pickle.dump(vars(self), open(os.path.join(self.output_folder, "params_trainer.pkl"), "wb")) except OSError as err: print("The name of the folder already exist! Try changing the name of the folder.") print("OSError", err) exit() # Creation of the network self.network = UNet.UNet(in_channels=self.in_channels, out_channels=self.out_channels, number_filter=self.number_filter, depth=self.depth, size=self.size) if self.cuda: self.network = self.network.cuda() # Creation of the optimizer self.optimizer = torch.optim.Adam(self.network.parameters(), lr=self.lr) # To keep track of the statistics self.stats = defaultdict(list) # Creation of the criterion self.criterion = torch.nn.CrossEntropyLoss() if self.cuda: self.criterion = self.criterion.cuda() # To keep track of the network generalizing the most self.min_valid_loss = numpy.inf
def initialNetGenerator(ks, fm, train_loader): good_net = False while not good_net: net = UNet(ks, fm, show=False).cuda(1) net.apply(weightInitialization) net.train() list_of_booleans = [] for img, label in train_loader: img, label = tensor_format(img), tensor_format(label) output = net(img) output, label = crop(output, label) cell_prob_mean = getCellProb(output).mean() # cell_prob_mean = getSigmoidProb(output).mean() diff = abs(cell_prob_mean - 0.5) list_of_booleans.append(diff > 0.2) outlier = any(list_of_booleans) if outlier: pass else: # print("One Initial Cell Probability: ",cell_prob_mean) return net
self.model.train() self.scheduler.step(epoch_loss) def predict(self): """predictions""" for i, batch in enumerate(self.valid_dataloader): images, mask_target = batch batch_preds = torch.sigmoid(self.model(images.to(self.device))) batch_preds = batch_preds.detach().cpu().numpy() for pre in range(16): fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 15)) fig.suptitle( 'predicted_mask <---------------> original_mask') ax1.imshow(np.squeeze(batch_preds[pre]), cmap='gray') ax2.imshow(np.squeeze(mask_target[pre]), cmap='gray') plt.show() break if __name__ == '__main__': df = pd.read_csv('/train_masks.csv') img_fol = '/content/gdrive/MyDrive/Project_Data/Carvan/train/train' mask_fol = '/content/gdrive/MyDrive/Project_Data/Carvan/train_masks_png' # mod = smp.Unet("resnet18", encoder_weights="imagenet", classes=1, activation=None) mod = UNet.UNet(3, 1, False) Segment = ImageSegmentation(mod, img_fol, mask_fol, df) Segment.fit(epochs=10)
nargs='+', default=[10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000]) # data augmentation parser.add_argument('--aug', dest='aug', type=int, default=0) parser.add_argument('--imgNorm', dest='imgNorm', type=float, default=0.019) parser.add_argument('--imgOffset', dest='imgOffset', type=float, default=-1) # window parser.add_argument('--vmin', dest='vmin', type=float, default=0.84) parser.add_argument('--vmax', dest='vmax', type=float, default=1.24) # In[6]: tf.reset_default_graph() net = UNet.UNet() parser = net.AddArgsToArgParser(parser) # In[7]: if sys.argv[0] != 'recon_alter_2d.py': from IPython import display import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') showPlots = True args = parser.parse_args([ '--device', '0', # '--slices', '0', '1', '--imgshape',
kf = sklearn.model_selection.KFold(n_splits=5, shuffle=True) histories = [] for lr in lrs: history_lr = [] split = 1 for train_index, val_index in kf.split(X_train): file_name = f"lr_{lr}_s_{split}" split += 1 mcp_save = ModelCheckpoint(f'weights/{file_name}.hdf5', save_best_only=True, monitor='val_loss', mode='min') csv_logger = CSVLogger(f'logs/{file_name}.log', separator=',', append=False) model = UNet.UNet(filters=filters[0], lr=lr) history = model.fit(X_train[train_index], y_train[train_index], validation_data=(X_train[val_index], y_train[val_index]), callbacks=[mcp_save, csv_logger], epochs=100, batch_size=1, verbose=2) history_lr.append(history) result = model.predict(X_test, batch_size=8) np.save(file_name + '.npy', result) histories.append([f"lr_{lr}", history_lr]) plot_history(histories)
output = net(img) output, label = crop(output, label) loss = criterion(output, label) loss_list.append(loss.data[0]) lr_list.append(group['lr']) optimizer.zero_grad() loss.backward() optimizer.step() scheduler.step() plt.subplot(1, 2, 1) plt.plot(loss_list) plt.title("Loss") plt.subplot(1, 2, 2) plt.plot(lr_list) plt.title("Learning Rate") plt.show() return loss_list, lr_list if __name__ == '__main__': kernel_size = 6 feature_maps = 16 net = UNet(kernel_size, feature_maps).cuda(1) net.apply(weightInitialization) net.train() loss_list, lr_list = learningRateFinder(net, 1)
(512, 512)) _, temp = cv2.threshold(temp, 127, 255, cv2.THRESH_BINARY) test_label.append(temp) test_data = np.array(test_data) test_label = np.array(test_label) x_test = test_data.astype('float32') / 255. y_test = test_label.astype('float32') / 255. x_test = np.reshape(x_test, (len(x_test), 512, 512, 3)) # adapt this if using `channels_first` image data format y_test = np.reshape(y_test, (len(y_test), 512, 512, 1)) # adapt this if using `channels_first` im from UNet import * model=UNet(input_size=(512,512,3)) weight="Model/Luna/UNet.h5" if os.path.isfile(weight): model.load_weights(weight) model_checkpoint = ModelCheckpoint(weight, monitor='val_acc', verbose=1, save_best_only=True) y_pred = model.predict(x_test) y_pred_threshold = [] i=0 for y in y_pred: _, temp = cv2.threshold(y, 0.5, 1, cv2.THRESH_BINARY) y_pred_threshold.append(temp) y = y * 255 cv2.imwrite('./Luna/test/result/%d.png' % i, y)
import torch from torch.autograd import Variable MEAN = 16.861 STD = 56.475 im = cv2.imread('../pytorch_keras/test.png', cv2.IMREAD_GRAYSCALE) im = im[12:, :-14] blob = cv2.dnn.blobFromImage(im, 1, (480, 640)) blob = (blob - MEAN) / STD net = cv2.dnn.readNetFromCaffe('./pillnet.prototxt', './pillnet.caffemodel') net.setInput(blob) out = net.forward().squeeze() out = 1 / (1 + np.exp(-out)) pytorch_model = UNet.UNet(1, 1, depth=2, start_filts=16) saved_params = torch.load('../unet_2/t7/epoch_89.pth') pytorch_model.load_state_dict(saved_params['state_dict']) x = Variable(torch.from_numpy(blob)) out_pytorch = pytorch_model(x).data.numpy().squeeze() out_pytorch = 1 / (1 + np.exp(-out_pytorch)) plt.subplot(1, 3, 1) plt.imshow(out.squeeze(), cmap='gray') plt.clim(0, 1) plt.subplot(1, 3, 2) plt.imshow(out_pytorch.squeeze(), cmap='gray') plt.clim(0, 1) plt.subplot(1, 3, 3) plt.imshow(np.abs(out - out_pytorch)) plt.colorbar()
########################################################## train_dataset = FakeDataset(train_images_path, train_labels_path, transform=transforms) train_dataset.fit([center]) checkTrainSetMean(train_dataset) train_loader = DataLoader(train_dataset, shuffle=True) ########################################################## test_dataset = FakeDataset(test_images_path, test_labels_path, transform=transforms) test_loader = DataLoader(test_dataset, shuffle=True) ########################################################## net = UNet(6, 32).cuda(1) net.apply(weightInitialization) net.train() learn_rate = 1e-2 momentum_rate = 0.8 cyclic_rate = 25 epochs = 50 alpha = 0.06 weight_map = getWeightMap(train_loader) # weight_map = np.array([alpha,1-alpha]) training_parameters = "Learning Rate: {} \n Momentum: {} \n Cycle Length: {} \n Number of epochs: {}\n Weight Map: {}".format( learn_rate, momentum_rate, cyclic_rate, epochs, weight_map) os.chdir("Fake") w = SummaryWriter()
for i in range(test_imgs.shape[0]): tmp = test_imgs[i].astype(np.uint8) tmp = utils.adjust_gamma(tmp, gamma=6) tmp = cv2.erode(tmp, kernel) tmp = cv2.dilate(tmp, kernel) cv2.imwrite('Q2/test_imgs/' + test_imgs_name_list[i], tmp) if mode == PREDICT: test_imgs_name_list = os.listdir('Q2/test_imgs') test_imgs_name_list.sort() imgs_test = utils.get_imgs(['Q2/test_imgs'], max_pool=True, file_type_list=['.bmp', '.png']) / 255 test_shape = imgs_test.shape X_test = imgs_test.reshape(test_shape[0], test_shape[1], test_shape[2], 1) print('****************Predict BEGIN****************') unet = UNet.UNet(img_shape=(test_shape[1], test_shape[2], 1)) unet.load_unet() Y_pred = unet.predict(X_test, batch_size=2) for i in range(Y_pred.shape[0]): cur_pred = Y_pred[i].reshape(test_shape[1], test_shape[2]) cur_pred[cur_pred >= 0.5] = 255 cur_pred[cur_pred < 0.5] = 0 cur_pred = cv2.resize(cur_pred, (640, 480), interpolation=cv2.INTER_AREA) cur_pred = cur_pred.astype(np.uint8) cur_pred = cv2.bitwise_not(cur_pred) cur_name = test_imgs_name_list[i][:-4] cv2.imwrite('Q2/predictions/' + cur_name + '_mask.bmp', cur_pred) print('****************Predict END****************') elif mode == TRAIN: imgs = utils.get_imgs(['Q2/imgs'], file_type_list=['.bmp'], max_pool=True) / 255
yy = int((shape[1] - input_width) / 2) return xx, yy #get input images and label_images image = sorted( glob.glob(image_path + '*.jpg') + glob.glob(image_path + '*.png') + glob.glob(image_path + '*.jpeg')) segmentation = sorted( glob.glob(seg_path + '*.jpg') + glob.glob(seg_path + '*.png') + glob.glob(seg_path + '*.jpeg')) #load pre-trained model weights for predict model = UNet.UNet(n_classes, input_height, input_width) save_dir = os.path.join(os.getcwd(), 'Best_Performance_Model') model_weights_name = 'keras_trained_model_weights.h5' model_weights_path = os.path.join(save_dir, model_weights_name) model.load_weights(model_weights_path) #the shape of the model output is (input_height * input_width, n_classes) im = cv2.imread(image, 1) xx, yy = getcenteroffset(im.shape, input_height, input_width) im = im[xx:xx + input_height, yy:yy + input_width, :] seg = cv2.imread(segmentation, 0) seg = seg[xx:xx + input_height, yy:yy + input_width] pr = model.predict(np.expand_dims(LoadBatches.getImageArr(im), 0))[0] pr = pr.reshape((input_height, input_width, n_classes)).argmax(axis=2)
fill_mode='nearest') myGene = DataAugmentation.trainGenerator(2, 'Data', 'image', 'label', data_gen_args, save_to_dir=None) filters = [[8, 16, 32, 64, 128], [16, 32, 64, 128, 256], [32, 64, 128, 256, 512]] Test_X = Train_X[-3:] for filter in filters: print(filter) model = UNet.UNet(filters=filter) for use_aug in [True, False]: file_name = str(filter)[1:-1].replace(', ', '-') if use_aug: history = model.fit_generator(myGene, epochs=2, steps_per_epoch=300) file_name = file_name + '_Aug' else: history = model.fit(Train_X, Train_Y, validation_split=0.2, epochs=50, batch_size=1) result = model.predict(Test_X)
help="Wheter or not to use CUDA.") parser.add_argument("--size", type=int, default=128, help="The size of the crops used for training the network.") parser.add_argument("--step", type=int, default=128, help="The step between each crops.") parser.add_argument("--show", action="store_true", default=False, help="Wheter or not to show each prediction.") print("Parsing the arguments ...") args = parser.parse_args() print("Loading the previous network ...") net_params = load(args.network_path, args.cuda) trainer_params = pickle.load(open(os.path.join(args.network_path, "params_trainer.pkl"), "rb")) network = UNet.UNet(in_channels=trainer_params["in_channels"], out_channels=trainer_params["out_channels"], number_filter=trainer_params["number_filter"], depth=trainer_params["depth"], size=trainer_params["size"]) network.load_state_dict(net_params) if args.cuda: network = network.cuda() print("Initializing loaders ...") images_names = glob.glob(os.path.join(args.images_folder, "*.tif")) dataTest = loader.BatchDatasetLoader(batch_size=96, cuda=args.cuda, size=args.size, step=args.step) print("Predicting the probability from the input images ...") network.eval() for i_name in tqdm(images_names, total=len(images_names)): # Sets the current image to predict
OUTPUT_DIR = './results/' if not os.path.exists(OUTPUT_DIR): os.makedirs(OUTPUT_DIR) ########### Arguments ########### device = torch.device("cuda") print(device) max_epoch = 1 labels = 11 dataroot = '/home/apoorv/Documents/BTP/data/subslice_f' batch = 1 save_file = '7_1000_model.pth' img_size = (128, 128) ########### Model ########### model = UNet(labels).to(device) model = nn.DataParallel(model) model.load_state_dict(torch.load(save_file)) print(model) ########### Transforms ########### mean_std = ([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]) input_transforms = transforms.Compose([ transforms.Resize(img_size, interpolation=2), transforms.ToTensor(), ]) ########### Dataloader ########### seg_path = 'segment/' img_path = 'grey/'
def UNet_grid_search(train_gen, val_gen, val_metric, hparams): """ Gridsearch over the hyper parameters :param train_gen: training generator :param val_gen: validation genrator :param hparams: dictionary with vectors for batch_size, learning rate and dropout :return best_val_metric: best validation metric value through the epochs for current set of hparams :return best_epoch: corresponding best epoch :return best_model_path: path to best model (saved by the Checkpoint callback) """ train_gen.batch_size = hparams['bs'] # make batch size a varying parameter if val_metric == 'val_loss': val_mode = 'min' elif val_metric == 'val_acc': val_mode = 'max' # Create UNet model (model.summary() shows nr of trainable parameters) model = UNet().create_model(img_shape=(PARAMS['patch_size'], PARAMS['patch_size'], PARAMS['nr_bands']), num_class=PARAMS['nr_classes'], dropout=hparams['do']) # Define optimizer and compile adam = optimizers.Adam(lr=hparams['lr']) model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['acc']) # Build string to give best models and folders different names for each hparams combination hparams_str = '' for k, v in hparams.items(): hparams_str += '%s_%g_' % (k, v) hparams_str = hparams_str[0:-1] # Delete and recreate folder for Tensorboard logs log_dir_hparams = os.path.join(PARAMS['dirs']['log'], hparams_str) if os.path.exists(log_dir_hparams): shutil.rmtree(log_dir_hparams) os.makedirs(log_dir_hparams) # Earlystopping callback with a given patience earlystop_callback = callbacks.EarlyStopping(monitor=val_metric, mode=val_mode, patience=PARAMS['patience']) # prefix 'val_' added automatically by Keras (based on name of Loss function) # Tensorboard callback to visualize network/evolution of metrics tb_callback = callbacks.TensorBoard(log_dir=log_dir_hparams, write_graph=True) # Checkpoint callback to save model each time the validation score (loss, acc, etc.) improves best_model_path = os.path.join(PARAMS['dirs']['model'], 'best_model_%s.hdf5' % hparams_str) checkpoint_callback = callbacks.ModelCheckpoint(best_model_path, monitor=val_metric, mode=val_mode, verbose=1, save_best_only=True) # Learning rate callback to reduce learning rate if val_loss does not improve after patience epochs (divide by 10 each time till a minimum of 0.000001) reduce_lr_callback = callbacks.ReduceLROnPlateau(monitor=val_metric, mode=val_mode, factor=0.1, patience=PARAMS['patience']/2, min_lr=1e-6) # TODO Only to be used with flow_from_directory() # if PARAMS['nr_bands'] == 1: # color_mode = 'grayscale' # else: # color_mode = 'rgb' # Train model history = model.fit_generator(train_gen, epochs=PARAMS['epochs'], steps_per_epoch=np.ceil(PARAMS['nr_samples_trn'] / hparams['bs']), validation_data=val_gen, callbacks=[earlystop_callback, reduce_lr_callback, checkpoint_callback, tb_callback], validation_steps=np.ceil(PARAMS['nr_samples_val'] / PARAMS['batch_size_val']), verbose=2) ## Get best accuracy and corresponding epoch if val_metric == 'val_loss': best_val_metric = np.min(history.history[val_metric]) best_epoch = np.argmin(history.history[val_metric])+1 elif val_metric == 'val_acc': best_val_metric = np.max(history.history[val_metric]) best_epoch = np.argmax(history.history[val_metric])+1 return best_val_metric, best_epoch, best_model_path
# general network training parser.add_argument('--device', type=int, default=0) parser.add_argument('--imgNormIn', type=float, default=0.15) parser.add_argument('--imgOffsetIn', type=float, default=-1) parser.add_argument('--imgNormOut', type=float, default=0.025) parser.add_argument('--imgOffsetOut', type=float, default=0) # In[16]: tf.reset_default_graph() net = UNet.UNet() parser = net.AddArgsToArgParser(parser) # In[17]: if sys.argv[0] != 'TestNetwork.py': from IPython import display import matplotlib.pyplot as plt get_ipython().run_line_magic('matplotlib', 'inline') args = parser.parse_args(['--device', '0', '--nTest', '1', '--checkPoint', '/home/dwu/trainData/Noise2Noise/train/ctp/simul/supervised_beta_25_N0_200000/99', '--outFile', '/home/dwu/trainData/Noise2Noise/train/ctp/real/supervised/test']) else:
test_data = np.array(test_data) test_label = np.array(test_label) x_test = test_data.astype('float32') / 255. y_test = test_label.astype('float32') / 255. x_test = np.reshape( x_test, (len(x_test), desired_size, desired_size, 3)) # adapt this if using `channels_first` image data format y_test = np.reshape(y_test, (len(y_test), desired_size, desired_size, 1)) # adapt this if using `channels_first` im y_test = crop_to_shape(y_test, (len(y_test), 320, 360, 1)) from UNet import * model = UNet(input_size=(desired_size, desired_size, 3)) weight = "Model/RC_SLO/UNet.h5" if os.path.isfile(weight): model.load_weights(weight) model_checkpoint = ModelCheckpoint(weight, monitor='val_acc', verbose=1, save_best_only=True) # plot_model(model, to_file='unet_resnet.png', show_shapes=False, show_layer_names=False) y_pred = model.predict(x_test) y_pred = crop_to_shape(y_pred, (40, 320, 360, 1)) y_pred_threshold = []
def main(args): model_id = str(time.time()) if args.s: save_path = '/home/fast_seg/FastSeg/model_checkpoints/' + model_id os.mkdir(save_path) else: save_path = './' print("Saving all output to ", save_path) if args.stdout: print("Routing stdout to " + save_path + "/console.txt") sys.stdout = open(save_path + "/console.txt", "w") print(args) minibatch_size = 64 num_classes = NUM_CLASSES seed = 1 #class_weights = classes.getWeights() class_weights = args.w class_weights = torch.tensor(class_weights, dtype=torch.float, requires_grad=False, device=device) learning_rate = args.lr bn = 0 dp = 0.1 wd = 0 torch.manual_seed(seed) torch.cuda.manual_seed_all(seed) dset_train = coco_custom_Dataset(train_dir, length=args.n) dset_val = coco_custom_Dataset(val_dir, length=args.nval) #dset_val = coco_custom_Dataset(train_dir, length=args.nval) train_loader = DataLoader(dset_train, batch_size=minibatch_size, shuffle=True, num_workers=0) val_loader = DataLoader(dset_val, batch_size=minibatch_size, shuffle=False, num_workers=0) if args.m == 0: model = UNet.UNet(in_channel=3, num_classes=num_classes, start_filters=args.f, num_batchnorm_layers=bn, dropout=dp) elif args.m == 1: model = U_Net_separable.U_net_separable(in_channel=3, num_classes=num_classes, start_filters=args.f, num_batchnorm_layers=bn, dropout=dp) elif args.m == 2: model = UNetSep2.UNetSep2(in_channel=3, num_classes=num_classes, start_filters=args.f, num_batchnorm_layers=bn, dropout=dp) elif args.m == 3: model = UNetSep3.UNetSep3(in_channel=3, num_classes=num_classes, start_filters=args.f, dropout=dp, nlayers=args.nl, conv_type=args.convtype) model.to(device) print("Model parameters (thousands): ", count_parameters(model) / 1000) optimizer = optim.Adam(model.parameters(), lr=learning_rate, weight_decay=wd) train_model(model, optimizer, train_loader, class_weights, val_loader, save_path, epochs=args.e, do_save=args.s)
def TrainRed(trainloader, bone, Network, n): batchs_num = [] loss_value = [] dice_value = [] if Network == 'UNet': net = UNet().to(device) elif Network == 'ResNet': net = ResNet(BasicBlock, [3, 4, 6]).to(device) criterion = nn.BCELoss() optimizer = torch.optim.Adam(net.parameters(), lr=0.0005) for epoch in range(n_epochs): running_dice = 0.0 running_loss = 0.0 for i, data in enumerate(trainloader, 0): # get the inputs; data is a list of [inputs, labels] inputs, labels = data inputs = inputs.to(device) labels = labels.to(device) # zero the parameter gradients optimizer.zero_grad() a = list(labels.size()) # forward + backward + optimize outputs = net(inputs) loss = 0 dice = 0 loss = criterion(outputs, labels.float()) loss.backward() for b in range(a[0]): pr = labels[b].cpu().detach().numpy() gt = outputs[b].cpu().detach().numpy() gt[0, :, :][gt[0, :, :] <= 0.5] = 0 gt[0, :, :][gt[0, :, :] > 0.5] = 1 dice = dice + np.abs( computeQualityMeasures(pr[0, :, :].flatten(), gt[0, :, :].flatten())) dice = dice / a[0] optimizer.step() # print statistics running_loss += loss.item() running_dice += dice if i % 200 == 199: # print every 200 mini-batches (SDG utilise des batchs des traindata) batchs_num.append((i + 1) + number * (epoch + 1)) loss_value.append(running_loss / 200) dice_value.append(running_dice / 200) print('DICE: ' + str(running_dice / 200)) print('[%d, %5d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 200)) running_loss = 0.0 running_dice = 0.0 print('Finished Training') plt.plot(batchs_num, loss_value) plt.title('Trainloss') plt.savefig(ResultsDirectory + '/' + bone + '/Images/EvolutionLoss.png') plt.close() plt.plot(batchs_num, dice_value) plt.title('DICE') plt.xlabel('Number of batches') plt.ylabel('DICE') plt.savefig(ResultsDirectory + '/' + bone + '/Images/EvolutionDICE.png') plt.close() #Save the train model PATH = os.path.join(ResultsDirectory, bone, 'pip1_' + Network + '_' + bone + '.pth') torch.save(net.state_dict(), PATH)
import os import numpy as np import cv2 from keras.callbacks import TensorBoard, ModelCheckpoint from keras.utils.vis_utils import model_to_dot from keras.utils import plot_model np.random.seed(42) import scipy.misc as mc data_location = '' training_images_loc = data_location + 'Chase/train/image/' training_label_loc = data_location + 'Chase/train/label/' testing_images_loc = data_location + 'Chase/test/image/' testing_label_loc = data_location + 'Chase/test/label/' train_files = os.listdir(training_images_loc) train_data = [] train_label = [] desired_size = 1024 for i in train_files: im = mc.imread(training_images_loc + i) label = mc.imread(training_label_loc + "Image_" + i.split('_')[1].split(".")[0] + "_1stHO.png") old_size = im.shape[:2] # old_size is in (height, width) format delta_w = desired_size - old_size[1] delta_h = desired_size - old_size[0] top, bottom = delta_h // 2, delta_h - (delta_h // 2) left, right = delta_w // 2, delta_w - (delta_w // 2) color = [0, 0, 0] color2 = [0] new_im = cv2.copyMakeBorder(im, top,
def run(): path = CONFIG.INPUT_PATH x_ray_image_names = os.listdir(path + '/CXR_png/') image_names = [] for name in x_ray_image_names: image_names.append(name.split('.')[0]) dataset_image_names = [] mask_image_names = os.listdir(path + '/masks/') for name in mask_image_names: name = name.split('.png')[0].split('_mask')[0] if name in image_names: dataset_image_names.append(name) image_transforms = alb.Compose([ alb.Normalize(CONFIG.mean, CONFIG.std, always_apply=True), alb.Resize(512, 512, always_apply=True), alb.pytorch.ToTensor() ]) mask_transforms = alb.Compose([ alb.Normalize(0, 1, always_apply=True), alb.Resize(512, 512, always_apply=True), alb.pytorch.ToTensor() ]) train_images_name, val_images_name = train_test_split(dataset_image_names) train_data = DataLoader.DataLoader( train_images_name, image_transforms, mask_transforms ) val_data = DataLoader.DataLoader( val_images_name, image_transforms, mask_transforms ) train_loader = torch.utils.data.DataLoader( train_data, num_workers=4, batch_size=CONFIG.Batch_size, pin_memory=True ) val_loader = torch.utils.data.DataLoader( val_data, num_workers=4, batch_size=CONFIG.Batch_size, pin_memory=True ) if torch.cuda.is_available(): accelarator = 'cuda' torch.backends.cudnn.benchmark = True else: accelarator = 'cpu' device = torch.device(accelarator) model = UNet.UNet(input_channels=3) model = model.to(device) optimizer = torch.optim.Adam(model.parameters(), lr=CONFIG.LR) scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau( optimizer, patience=CONFIG.patience, threshold=CONFIG.scheduler_thresh, mode="min", factor=CONFIG.decay_factor ) best_loss = 1e4 print('------ [INFO] STARTING TRAINING ------') for epoch in range(CONFIG.Epochs): train_loss = engine.train_fn(model, train_loader, optimizer, device) val_loss = engine.eval_fn(model, val_loader, device) print(f'EPOCH -> {epoch+1}/{CONFIG.Epochs} | TRAIN LOSS = {train_loss} | VAL LOSS = {val_loss} | LR = {optimizer.param_groups[0]["lr"]}\n') scheduler.step(val_loss) if best_loss > val_loss: best_loss = val_loss best_model = model.state_dict() torch.save(best_model, CONFIG.MODEL_PATH) predict.predict('input/CXR_png/CHNCXR_0001_0.png')