# tensor(0., device='cuda:0') # (Pdb) pp data['mask_B'].size() # torch.Size([1, 1, 256, 256]) # (Pdb) pp data['mask_B'].min() # tensor(-0.5000, device='cuda:0') # (Pdb) pp data['mask_B'].max() # tensor(-0.5000, device='cuda:0') model.set_input(data) model.test(True) # True means that losses will be computed visuals = util.get_subset_dict(model.get_current_visuals(), to_visualize) psnrs[i, pp] = util.calculate_psnr_np(util.tensor2im(visuals['real']), util.tensor2im(visuals['fake'])) save_images(webpage, visuals, img_path, aspect_ratio=opt.aspect_ratio, width=opt.display_winsize) if i % 5 == 0: print('processing (%04d)-th image... %s' % (i, img_path)) if i == opt.how_many - 1: break webpage.save()
for (pp, sample_p) in enumerate(sample_ps): img_path = [('%08d_%.3f' % (i, sample_p)).replace('.', 'p')] # data = util.get_colorization_data(data_raw[0], opt, ab_thresh=0., p=sample_p) model.set_input(data) model.test(True) # True means that losses will be computed visuals = util.get_subset_dict(model.get_current_visuals(), to_visualize) vid = [] for frames in range(0, visuals['fake_reg'].shape[1], 3): gray = util.tensor2im(visuals['gray'][:, frames:frames + 3]) real = util.tensor2im(visuals['real'][:, frames:frames + 3]) generated = util.tensor2im( visuals['fake_reg'][:, frames:frames + 3]) vid.append(np.vstack((gray, real, generated))) psnrs[i, pp] += util.calculate_psnr_np( real, generated) / (visuals['fake_reg'].shape[1] / 3) #calculate_smoothness_here vid = np.array(vid) arr2vid(vid, "vid_{}".format(i)) # psnrs[i, pp] = util.calculate_psnr_np(util.tensor2im(visuals['real']), util.tensor2im(visuals['fake_reg'])) entrs[i, pp] = model.get_current_losses()['G_entr'] # save_images(webpage, visuals, img_path, aspect_ratio=opt.aspect_ratio, width=opt.display_winsize) print("") if i % 5 == 0: print('processing (%04d)-th image... %s' % (i, img_path)) if i == opt.how_many - 1: break webpage.save()
data_raw[0] = data_raw[0].cuda() data_raw[0] = util.crop_mult(data_raw[0], mult=8) for nn in range(N): # embed() data = util.get_colorization_data(data_raw, opt, ab_thresh=0., num_points=num_points[nn]) model.set_input(data) model.test() visuals = model.get_current_visuals() psnrs[i, nn] = util.calculate_psnr_np( util.tensor2im(visuals['real']), util.tensor2im(visuals['fake_reg'])) if i == opt.how_many - 1: break bar.update(i) # Save results psnrs_mean = np.mean(psnrs, axis=0) psnrs_std = np.std(psnrs, axis=0) / np.sqrt(opt.how_many) np.save('./checkpoints/%s/psnrs_mean_%s' % (opt.name, str_now), psnrs_mean) np.save('./checkpoints/%s/psnrs_std_%s' % (opt.name, str_now), psnrs_std) np.save('./checkpoints/%s/psnrs_%s' % (opt.name, str_now), psnrs) print(', ').join(['%.2f' % psnr for psnr in psnrs_mean])
def select_photos(): to_visualize = ['gray', 'real', 'fake_reg', 'mask', 'hint'] opt.load_model = True opt.num_threads = 1 opt.batch_size = 1 opt.display_id = -1 opt.phase = 'val' opt.serial_batches = True opt.aspect_ratio = 1. model = create_model(opt) model.setup(opt) # tensor_image = tensor_image.cuda() sample_ps = 0.03125 data_raw = [None] * 4 # loader = transforms.Compose(transforms.ToTensor()) # raw_image = Image.open(args.input) # # tensor_image = TF.to_tensor(raw_image) # tensor_image = loader(raw_image.float()) # tensor_image = raw_image.unsqueeze(0) # data_raw[0] = tensor_image.cuda() # print(opt.input) tensor_image = Image.open(opt.input).convert('RGB') tensor_image = tensor_image.resize((opt.loadSize, opt.loadSize)) tensor_image = ToTensor()(tensor_image).unsqueeze(0) data_raw[0] = tensor_image.cuda() # data_raw[0] = util.crop_mult(data_raw[0], mult=8) data = util.get_colorization_data(data_raw, opt, ab_thresh=0., p=sample_ps) model.set_input(data) # model.eval() model.test(True) # gets the visuals from the model global visuals visuals = util.get_subset_dict(model.get_current_visuals(), to_visualize) # output images raw_image = Image.fromarray(util.tensor2im(visuals['real'])) image = raw_image.resize((512, 512), Image.ANTIALIAS) image = ImageTk.PhotoImage(image) label = tk.Label(text="Original Image", compound='top', image=image) label.photo = image # assign to class variable to resolve problem with bug in `PhotoImage` label.grid(row=1, column=1) all_labels.append(label) raw_image = Image.fromarray(util.tensor2im(visuals['hint'])) # lab_raw_image = util.rgb2lab(raw_image, opt) image = raw_image.resize((512, 512), Image.ANTIALIAS) image = ImageTk.PhotoImage(image) label = tk.Label(image=image, text="Hint Image", compound='top') label.bind("<Button-1>", lambda e: choose_colour()) label.photo = image label.grid(row=1, column=3) all_labels.append(label) raw_image = Image.fromarray(util.tensor2im(visuals['fake_reg'])) image = raw_image.resize((512, 512), Image.ANTIALIAS) image = ImageTk.PhotoImage(image) label = tk.Label(image=image, text="Colourised Image", compound='top') label.bind("<Button-1>", lambda e: choose_colour()) label.photo = image label.grid(row=1, column=5) all_labels.append(label) original = Image.open(opt.input) original = original.resize((opt.loadSize, opt.loadSize)) original = np.asarray(original) label = tk.Label(text="PSNR Numpy: " + str( util.calculate_psnr_np(original, util.tensor2im(visuals['fake_reg'])))) label.grid(row=4, column=1) all_labels.append(label) label = tk.Label( text="MSE : " + str(util.calculate_mse(original, util.tensor2im(visuals['fake_reg'])))) label.grid(row=4, column=3) all_labels.append(label) label = tk.Label( text="MSE : " + str(util.calculate_mae(original, util.tensor2im(visuals['fake_reg'])))) label.grid(row=4, column=5) all_labels.append(label)
print("data_raw[0].shape") print(data_raw[0].shape) for nn in range(N): # embed() data = util.get_colorization_data(data_raw, opt, ab_thresh=0., num_points=num_points[nn]) model.set_input(data) model.test() visuals = model.get_current_visuals() real = util.tensor2im(visuals['real']) generated = util.tensor2im(visuals['fake_reg']) psnrs[i, nn] = util.calculate_psnr_np(real, generated) plt.subplot(121) plt.imshow(real) plt.subplot(122) plt.imshow(generated) plt.title("PSNR: " + str(psnrs[i, nn])) plt.savefig("real_vs_generated" + str(i) + ".png") if i == opt.how_many - 1: break bar.update(i) print(psnrs) # Save results psnrs_mean = np.mean(psnrs, axis=0)
data_raw[0] = util.crop_mult(data_raw[0], mult=8) mask = util.build_mask(data_raw[0].shape, R=R, G=G).cuda() data = util.reveal_rgb_values(data_raw, mask, opt) model.set_input(data) model.test() visuals = model.get_current_visuals() if not (R,G) in psnrs: psnrs[(R,G)] = np.zeros(opt.how_many) if not (R,G) in mses: mses[(R,G)] = np.zeros(opt.how_many) mse_value = util.calculate_mse_np(util.tensor2im(visuals['real_rgb']), util.tensor2im(visuals['fake_rgb'])) psnr_value = util.calculate_psnr_np(util.tensor2im(visuals['real_rgb']), util.tensor2im(visuals['fake_rgb'])) mses[(R,G)][i] = mse_value psnrs[(R,G)][i] = psnr_value if i == opt.how_many - 1: break k = k + 1 bar.update(k) # Save results psnrs_mean = np.zeros((values_num, values_num)) mses_mean = np.zeros((values_num, values_num)) for i in range(psnrs_mean.shape[0]): for j in range(psnrs_mean.shape[1]): r,g = i*jump_number, j*jump_number