model = DnCNN(num_layers=17) elif opt.arch == 'DnCNN-B': model = DnCNN(num_layers=20) elif opt.arch == 'DnCNN-3': model = DnCNN(num_layers=20) state_dict = model.state_dict() for n, p in torch.load(opt.weights_path, map_location=lambda storage, loc: storage).items(): if n in state_dict.keys(): state_dict[n].copy_(p) else: raise KeyError(n) model = model.to(device) model.eval() filename = os.path.basename(opt.image_path).split('.')[0] descriptions = '' input = pil_image.open(opt.image_path).convert('RGB') if opt.gaussian_noise_level is not None: noise = np.random.normal(0.0, opt.gaussian_noise_level, (input.height, input.width, 3)).astype( np.float32) input = np.array(input).astype(np.float32) + noise descriptions += '_noise_l{}'.format(opt.gaussian_noise_level) pil_image.fromarray(input.clip(0.0, 255.0).astype(np.uint8)).save( os.path.join(opt.outputs_dir, '{}{}.png'.format(filename, descriptions)))
batch_y = batch_y.unsqueeze(1) batch_x = batch_x.unsqueeze(1) # y_ = torch.cat([batch_y, High_noise[:,0].unsqueeze(1).cuda()], dim=1) # x_ = torch.cat([batch_x, High_origin[:,0].unsqueeze(1).cuda()], dim=1) y_ = High_noise[:, 0].unsqueeze(1).cuda() x_ = High_origin[:, 0].unsqueeze(1).cuda() decompose_output = decompose_model(y_.cuda()) decompose_loss = criterion(decompose_output, x_) # loss = criterion(decompose_output[:,1], x_[:,1]) optimizer_decompose.zero_grad() decompose_loss.backward() optimizer_decompose.step() decompose_model.eval() # output1 = torch.FloatTensor(decompose_output.cpu()) # compose_output = compose_model(output1.cuda()) # compose_loss = criterion(compose_output, batch_x) # optimizer_compose.zero_grad() # compose_loss.backward() # optimizer_compose.step() # compose_model.eval() fig = plt.figure() gs = GridSpec(nrows=2, ncols=3) highfreq1 = fig.add_subplot(gs[0, 0]) highfreq2 = fig.add_subplot(gs[0, 1]) highfreq3 = fig.add_subplot(gs[0, 2])
torch.load(os.path.join(args.model_dir, args.model_name))) model_lowfreq.load_state_dict( torch.load(os.path.join(args.low_model_dir, args.low_model_name))) # model = torch.load(os.path.join(args.model_dir, args.model_name)) log('load trained model') # params = model.state_dict() # print(params.values()) # print(params.keys()) # # for key, value in params.items(): # print(key) # parameter name # print(params['dncnn.12.running_mean']) # print(model.state_dict()) model_dncnn.eval() # evaluation mode model_lowfreq.eval() # model.train() if not os.path.exists(args.result_dir): os.mkdir(args.result_dir) for set_cur in args.set_names: if not os.path.exists(os.path.join(args.result_dir, set_cur)): os.mkdir(os.path.join(args.result_dir, set_cur)) psnrs = [] ssims = [] for im in os.listdir(os.path.join(args.set_dir, set_cur)): if im.endswith(".jpg") or im.endswith(".bmp") or im.endswith(
os.mkdir(save_dir) if __name__ == '__main__': # model selection print('===> Building model') model = DnCNN() low_model = DnCNN() low_model.load_state_dict(torch.load(os.path.join(args.low_model_dir, args.low_model_name))) initial_epoch = findLastCheckpoint(save_dir=save_dir) # load the last model in matconvnet style if initial_epoch > 0: print('resuming by loading epoch %03d' % initial_epoch) # model.load_state_dict(torch.load(os.path.join(save_dir, 'model_%03d.pth' % initial_epoch))) # model = torch.load(os.path.join(save_dir, 'model_%03d.pth' % initial_epoch)) model.train() low_model.eval() # criterion = nn.MSELoss(reduction = 'sum') # PyTorch 0.4.1 # criterion = sum_squared_error() criterion = nn.MSELoss() Edge_enhance = torch.FloatTensor(args.batch_size, 1, 40, 40) if cuda: model = model.cuda() low_model = low_model.cuda() # device_ids = [0] # model = nn.DataParallel(model, device_ids=device_ids).cuda() criterion = criterion.cuda() optimizer = optim.Adam(model.parameters(), lr=args.lr) scheduler = MultiStepLR(optimizer, milestones=[30, 60, 90], gamma=0.2) # learning rates for epoch in range(initial_epoch, n_epoch):
else: decompose_model.load_state_dict( torch.load(os.path.join(args.model_dir, args.model_name))) # model = torch.load(os.path.join(args.model_dir, args.model_name)) log('load trained model') # params = model.state_dict() # print(params.values()) # print(params.keys()) # # for key, value in params.items(): # print(key) # parameter name # print(params['dncnn.12.running_mean']) # print(model.state_dict()) decompose_model.eval() # evaluation mode # model.train() if torch.cuda.is_available(): decompose_model = decompose_model.cuda() if not os.path.exists(args.result_dir): os.mkdir(args.result_dir) for set_cur in args.set_names: if not os.path.exists(os.path.join(args.result_dir, set_cur)): os.mkdir(os.path.join(args.result_dir, set_cur)) psnrs = [] ssims = []
def main(): start = time.time() parser = argparse. ArgumentParser(description='Gamma-Spectra Denoising Trainer') parser.add_argument('--dettype', type=str, default='HPGe', help='detector type to train {HPGe, NaI, CZT}') parser.add_argument('--test_set', type=str, default='data/training.h5', help='h5 file with training vectors') parser.add_argument('--all', default=False, help='denoise all examples in test_set file', action='store_true') parser.add_argument('--batch_size', type=int, default=64, help='batch size for denoising') parser.add_argument('--seed', type=int, help='random seed') parser.add_argument('--model', type=str, default='models/best_model.pt', help='location of model to use') parser.add_argument('--outdir', type=str, help='location to save output plots') parser.add_argument('--outfile', type=str, help='location to save output data', default='denoised.h5') parser.add_argument('--savefigs', help='saves plots of results', default=False, action='store_true') args = parser.parse_args() # if output directory is not provided, save plots to model directory if not args.outdir: args.outdir = os.path.dirname(args.model) else: # make sure output dirs exists os.makedirs(args.outdir, exist_ok=True) # make sure data files exist assert os.path.exists(args.test_set), f'Cannot find testset vectors file {args.test_set}' # detect gpus and setup environment variables device_ids = setup_gpus() print(f'Cuda devices found: {[torch.cuda.get_device_name(i) for i in device_ids]}') print('Loading datasets') test_data = load_data(args.test_set, args.dettype.upper()) noisy_spectra = test_data['noisy_spectrum'] clean_spectra = test_data['spectrum'] spectra_keV = test_data['keV'] noisy_spectra = np.expand_dims(noisy_spectra, axis=1) clean_spectra = np.expand_dims(clean_spectra, axis=1) assert noisy_spectra.shape == clean_spectra.shape, 'Mismatch between shapes of training and target data' # load parameters for model params = pickle.load(open(args.model.replace('.pt','.npy'),'rb'))['model'] train_mean = params['train_mean'] train_std = params['train_std'] if not args.seed: args.seed = params['train_seed'] # applying random seed for reproducability np.random.seed(args.seed) torch.manual_seed(args.seed) # create dataset for denoising, if not 'all' use training seed to recreate validation set if not args.all: _, x_val, _, y_val = train_test_split(noisy_spectra, clean_spectra, test_size = 0.1, random_state=args.seed) val_dataset = TensorDataset(torch.Tensor(x_val), torch.Tensor(y_val)) else: val_dataset = TensorDataset(torch.Tensor(noisy_spectra), torch.Tensor(clean_spectra)) print(f'Number of examples to denoise: {len(val_dataset)}') # create batched data loaders for model val_loader = DataLoader(dataset=val_dataset, num_workers=os.cpu_count(), batch_size=args.batch_size, shuffle=False) print(f'Number of batches {len(val_loader)}') # create and load model if params['model_name'] == 'DnCNN': model = DnCNN(num_channels=params['num_channels'], num_layers=params['num_layers'], \ kernel_size=params['kernel_size'], stride=params['stride'], num_filters=params['num_filters']) elif params['model_name'] == 'DnCNN-res': model = DnCNN_Res(num_channels=params['num_channels'], num_layers=params['num_layers'], \ kernel_size=params['kernel_size'], stride=params['stride'], num_filters=params['num_filters']) else: print(f'Model name {params["model_name"]} is not supported.') return 1 # prepare model for data parallelism (use multiple GPUs) model = torch.nn.DataParallel(model, device_ids=device_ids).cuda() # loaded saved model print(f'Loading weights for {params["model_name"]} model from {args.model} for {params["model_type"]}') model.load_state_dict(torch.load(args.model)) # Main training loop print(f'Denoising spectra') model.eval() total_psnr_noisy = 0 total_psnr_denoised = 0 denoised = [] with torch.no_grad(): for num, (noisy_spectra, clean_spectra) in enumerate(val_loader, start=1): # move batch to GPU noisy_spectra = Variable(noisy_spectra.cuda()) clean_spectra = Variable(clean_spectra.cuda()) # make predictions preds = model((noisy_spectra-train_mean)/train_std) # calculate PSNR clean_spectra = clean_spectra.cpu().numpy().astype(np.float32) noisy_spectra = noisy_spectra.cpu().numpy().astype(np.float32) preds = preds.cpu().numpy().astype(np.float32) psnr_noisy = psnr_of_batch(clean_spectra, noisy_spectra) # save denoised spectrum if params['model_type'] == 'Gen-spectrum': denoised_spectrum = preds else: denoised_spectrum = noisy_spectra-preds # add batch of denoised spectra to list of denoised spectra denoised.extend(denoised_spectrum.tolist()) psnr_denoised = psnr_of_batch(clean_spectra, denoised_spectrum) total_psnr_noisy += psnr_noisy total_psnr_denoised += psnr_denoised print(f'[{num}/{len(val_loader)}] PSNR {psnr_noisy} --> {psnr_denoised}, increase of {psnr_denoised-psnr_noisy}') if args.savefigs: compare_results(spectra_keV, clean_spectra[0,0,:], noisy_spectra[0,0,:], preds[0,0,:], args.outdir, str(num)) # save denoised data to file, currently only supports entire dataset if args.all: assert len(test_data['noisy_spectrum']) == len(denoised), f'{len(test_data["noisy_spectrum"])} examples yet {len(denoised)} denoised' denoised = np.squeeze(np.array(denoised)) test_data['noisy_spectrum'] = denoised outfile = os.path.join(args.outdir, args.outfile) print(f'Saving denoised spectrum to {outfile}') save_dataset(args.dettype.upper(), test_data, outfile) avg_psnr_noisy = total_psnr_noisy/len(val_loader) avg_psnr_denoised = total_psnr_denoised/len(val_loader) print(f'Average PSNR: {avg_psnr_denoised}, average increase of {avg_psnr_denoised-avg_psnr_noisy}') print(f'Script completed in {time.time()-start:.2f} secs') return 0
def train_model(config): # Define hyper-parameters. depth = int(config["DnCNN"]["depth"]) n_channels = int(config["DnCNN"]["n_channels"]) img_channel = int(config["DnCNN"]["img_channel"]) kernel_size = int(config["DnCNN"]["kernel_size"]) use_bnorm = config.getboolean("DnCNN", "use_bnorm") epochs = int(config["DnCNN"]["epoch"]) batch_size = int(config["DnCNN"]["batch_size"]) train_data_dir = config["DnCNN"]["train_data_dir"] test_data_dir = config["DnCNN"]["test_data_dir"] eta_min = float(config["DnCNN"]["eta_min"]) eta_max = float(config["DnCNN"]["eta_max"]) dose = float(config["DnCNN"]["dose"]) model_save_dir = config["DnCNN"]["model_save_dir"] # Save logs to txt file. log_dir = config["DnCNN"]["log_dir"] log_dir = Path(log_dir) / "dose{}".format(str(int(dose * 100))) log_file = log_dir / "train_result.txt" # Define device. device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") # Initiate a DnCNN instance. # Load the model to device and set the model to training. model = DnCNN(depth=depth, n_channels=n_channels, img_channel=img_channel, use_bnorm=use_bnorm, kernel_size=kernel_size) model = model.to(device) model.train() # Define loss criterion and optimizer optimizer = optim.Adam(model.parameters(), lr=1e-3) scheduler = MultiStepLR(optimizer, milestones=[30, 60, 90], gamma=0.2) criterion = LossFunc(reduction="mean") # Get a validation test set and corrupt with noise for validation performance. # For every epoch, use this pre-determined noisy images. test_file_list = glob.glob(test_data_dir + "/*.png") xs_test = [] # Can't directly convert the xs_test from list to ndarray because some images are 512*512 # while the rest are 256*256. for i in range(len(test_file_list)): img = cv2.imread(test_file_list[i], 0) img = np.array(img, dtype="float32") / 255.0 img = np.expand_dims(img, axis=0) img_noisy, _ = nm(img, eta_min, eta_max, dose, t=100) xs_test.append((img_noisy, img)) # Train the model. loss_store = [] epoch_loss_store = [] psnr_store = [] ssim_store = [] psnr_tr_store = [] ssim_tr_store = [] loss_mse = torch.nn.MSELoss() dtype = torch.cuda.FloatTensor # load vgg network vgg = Vgg16().type(dtype) for epoch in range(epochs): # For each epoch, generate clean augmented patches from the training directory. # Convert the data from uint8 to float32 then scale them to make it in [0, 1]. # Then make the patches to be of shape [N, C, H, W], # where N is the batch size, C is the number of color channels. # H and W are height and width of image patches. xs = dg.datagenerator(data_dir=train_data_dir) xs = xs.astype("float32") / 255.0 xs = torch.from_numpy(xs.transpose((0, 3, 1, 2))) train_set = dg.DenoisingDatatset(xs, eta_min, eta_max, dose) train_loader = DataLoader(dataset=train_set, num_workers=4, drop_last=True, batch_size=batch_size, shuffle=True) # TODO: if drop_last=True, the dropping in the # TODO: data_generator is not necessary? # train_loader_test = next(iter(train_loader)) t_start = timer() epoch_loss = 0 for idx, data in enumerate(train_loader): inputs, labels = data inputs, labels = inputs.to(device), labels.to(device) img_batch_read = len(inputs) optimizer.zero_grad() outputs = model(inputs) # We can use labels for both style and content image # style image # style_transform = transforms.Compose([ # normalize_tensor_transform() # normalize with ImageNet values # ]) # labels_t = style_transform(labels) labels_t = labels.repeat(1, 3, 1, 1) outputs_t = outputs.repeat(1, 3, 1, 1) y_c_features = vgg(labels_t) style_gram = [gram(fmap) for fmap in y_c_features] y_hat_features = vgg(outputs_t) y_hat_gram = [gram(fmap) for fmap in y_hat_features] # calculate style loss style_loss = 0.0 for j in range(4): style_loss += loss_mse(y_hat_gram[j], style_gram[j][:img_batch_read]) style_loss = STYLE_WEIGHT*style_loss aggregate_style_loss = style_loss # calculate content loss (h_relu_2_2) recon = y_c_features[1] recon_hat = y_hat_features[1] content_loss = CONTENT_WEIGHT*loss_mse(recon_hat, recon) aggregate_content_loss = content_loss loss = aggregate_content_loss + aggregate_style_loss # loss = criterion(outputs, labels) loss_store.append(loss.item()) epoch_loss += loss.item() loss.backward() optimizer.step() if idx % 100 == 0: print("Epoch [{} / {}], step [{} / {}], loss = {:.5f}, lr = {:.6f}, elapsed time = {:.2f}s".format( epoch + 1, epochs, idx, len(train_loader), loss.item(), *scheduler.get_last_lr(), timer()-t_start)) epoch_loss_store.append(epoch_loss / len(train_loader)) # At each epoch validate the result. model = model.eval() # # Firstly validate on training sets. This takes a long time so I commented. # tr_psnr = [] # tr_ssim = [] # # t_start = timer() # with torch.no_grad(): # for idx, train_data in enumerate(train_loader): # inputs, labels = train_data # # print(inputs.shape) # # inputs = np.expand_dims(inputs, axis=0) # # inputs = torch.from_numpy(inputs).to(device) # inputs = inputs.to(device) # labels = labels.squeeze().numpy() # # outputs = model(inputs) # outputs = outputs.squeeze().cpu().detach().numpy() # # tr_psnr.append(peak_signal_noise_ratio(labels, outputs)) # tr_ssim.append(structural_similarity(outputs, labels)) # psnr_tr_store.append(sum(tr_psnr) / len(tr_psnr)) # ssim_tr_store.append(sum(tr_ssim) / len(tr_ssim)) # # print("Elapsed time = {}".format(timer() - t_start)) # # print("Validation on train set: epoch [{} / {}], aver PSNR = {:.2f}, aver SSIM = {:.4f}".format( # epoch + 1, epochs, psnr_tr_store[-1], ssim_tr_store[-1])) # Validate on test set val_psnr = [] val_ssim = [] with torch.no_grad(): for idx, test_data in enumerate(xs_test): inputs, labels = test_data inputs = np.expand_dims(inputs, axis=0) inputs = torch.from_numpy(inputs).to(device) labels = labels.squeeze() outputs = model(inputs) outputs = outputs.squeeze().cpu().detach().numpy() val_psnr.append(peak_signal_noise_ratio(labels, outputs)) val_ssim.append(structural_similarity(outputs, labels)) psnr_store.append(sum(val_psnr) / len(val_psnr)) ssim_store.append(sum(val_ssim) / len(val_ssim)) print("Validation on test set: epoch [{} / {}], aver PSNR = {:.2f}, aver SSIM = {:.4f}".format( epoch + 1, epochs, psnr_store[-1], ssim_store[-1])) # Set model to train mode again. model = model.train() scheduler.step() # Save model save_model(model, model_save_dir, epoch, dose * 100) # Save the loss and validation PSNR, SSIM. if not log_dir.exists(): Path.mkdir(log_dir) with open(log_file, "a+") as fh: # fh.write("{} Epoch [{} / {}], loss = {:.6f}, train PSNR = {:.2f}dB, train SSIM = {:.4f}, " # "validation PSNR = {:.2f}dB, validation SSIM = {:.4f}".format( # datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S:"), # epoch + 1, epochs, epoch_loss_store[-1], # psnr_tr_store[-1], ssim_tr_store[-1], # psnr_store[-1], ssim_store[-1])) fh.write("{} Epoch [{} / {}], loss = {:.6f}, " "validation PSNR = {:.2f}dB, validation SSIM = {:.4f}\n".format( datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S:"), epoch + 1, epochs, epoch_loss_store[-1], psnr_store[-1], ssim_store[-1])) # np.savetxt(log_file, np.hstack((epoch + 1, epoch_loss_store[-1], psnr_store[-1], ssim_store[-1])), fmt="%.6f", delimiter=", ") fig, ax = plt.subplots() ax.plot(loss_store[-len(train_loader):]) ax.set_title("Last 1862 losses") ax.set_xlabel("iteration") fig.show()
unet_model.load_state_dict( torch.load(os.path.join(args.three_model_dir, args.three_model_name))) # model = torch.load(os.path.join(args.model_dir, args.model_name)) log('load trained model') # params = model.state_dict() # print(params.values()) # print(params.keys()) # # for key, value in params.items(): # print(key) # parameter name # print(params['dncnn.12.running_mean']) # print(model.state_dict()) pre_model.eval() # evaluation mode # unet_model.eval() # model.train() pre_model.dncnn.register_forward_hook(get_activation('noise_level')) if not os.path.exists(args.result_dir): os.mkdir(args.result_dir) for set_cur in args.set_names: if not os.path.exists(os.path.join(args.result_dir, set_cur)): os.mkdir(os.path.join(args.result_dir, set_cur)) psnrs = [] ssims = [] for im in os.listdir(os.path.join(args.set_dir, set_cur)): if im.endswith(".jpg") or im.endswith(".bmp") or im.endswith(
args.high_model_name))) low_model.load_state_dict( torch.load(os.path.join(args.low_model_dir, args.low_model_name))) # model = torch.load(os.path.join(args.model_dir, args.model_name)) log('load trained model') # params = model.state_dict() # print(params.values()) # print(params.keys()) # # for key, value in params.items(): # print(key) # parameter name # print(params['dncnn.12.running_mean']) # print(model.state_dict()) high_model.eval() # evaluation mode low_model.eval() # model.train() if torch.cuda.is_available(): high_model = high_model.cuda() low_model = low_model.cuda() if not os.path.exists(args.result_dir): os.mkdir(args.result_dir) for set_cur in args.set_names: if not os.path.exists(os.path.join(args.result_dir, set_cur)): os.mkdir(os.path.join(args.result_dir, set_cur)) psnrs = []
res_model.load_state_dict( torch.load(os.path.join(args.res_model_dir, args.res_model_name))) # model.load_state_dict(torch.load(os.path.join(args.model_dir, args.model_name))) model = torch.load(os.path.join(args.model_dir, args.model_name)) log('load trained model') # params = model.state_dict() # print(params.values()) # print(params.keys()) # # for key, value in params.items(): # print(key) # parameter name # print(params['dncnn.12.running_mean']) # print(model.state_dict()) low_model.eval() # evaluation mode res_model.eval() model.eval() if torch.cuda.is_available(): low_model = low_model.cuda() res_model = res_model.cuda() model = model.cuda() # evaluation mode if not os.path.exists(args.result_dir): os.mkdir(args.result_dir) for set_cur in args.set_names: if not os.path.exists(os.path.join(args.result_dir, set_cur)):
two_model.load_state_dict(torch.load(os.path.join(args.two_model_dir, args.two_model_name))) three_model.load_state_dict(torch.load(os.path.join(args.three_model_dir, args.three_model_name))) # model = torch.load(os.path.join(args.model_dir, args.model_name)) log('load trained model') # params = model.state_dict() # print(params.values()) # print(params.keys()) # # for key, value in params.items(): # print(key) # parameter name # print(params['dncnn.12.running_mean']) # print(model.state_dict()) two_model.eval() # evaluation mode three_model.eval() # model.train() if not os.path.exists(args.result_dir): os.mkdir(args.result_dir) for set_cur in args.set_names: if not os.path.exists(os.path.join(args.result_dir, set_cur)): os.mkdir(os.path.join(args.result_dir, set_cur)) psnrs = [] ssims = [] for im in os.listdir(os.path.join(args.set_dir, set_cur)): if im.endswith(".jpg") or im.endswith(".bmp") or im.endswith(".png"):