def eval(device, content_img, image_sz, outf, state_dict): generator_net = create_generator(device, state_dict) data, inp = create_noise_and_normalize(content_img, image_sz, 1) for index, dat in enumerate(data): data[index] = dat.to(device) for index, dat in enumerate(inp): inp[index] = dat.to(device) y = generator_net(data) y_img = utils.tensor_to_img(y.squeeze(0)) y_img.save(os.path.join(outf))
def run(config): assert config.TASK is not None, 'Please set task name: TASK' assert config.save_paths is None and config.compare_paths is None or \ config.save_paths is not None and config.compare_paths is not None if config.save_paths is None: config.save_paths = [ os.path.join(config.RESULT_PATH, config.TASK, config.NAME, 'PRE') ] else: config.save_paths = [ os.path.join(config.RESULT_PATH, config.TASK, config.NAME, path) for path in config.save_paths ] for path in config.save_paths: if not os.path.isdir(path): os.makedirs(path) # data set test_data = SuDataset(config.IMG, os.path.join(config.LIST, config.TASK, 'test_{}.csv'.format(config.SET_NAME)), low_size=config.LOW_SIZE) # GPU if config.GPU >= 0: with torch.cuda.device(config.GPU): config.model.cuda() # test i_bar = tqdm(total=len(test_data), desc='#Images') for idx, imgs in enumerate(test_data): name = os.path.basename(test_data.get_path(idx)) imgs = config.forward(imgs, config) for path, img in zip(config.save_paths, imgs): imsave(os.path.join(path, name), tensor_to_img(img, transpose=True)) i_bar.update() if config.compare_paths is None: compare(config.save_paths[0], os.path.join(config.IMG, config.TASK, str(config.SET_NAME))) else: for pre, gt in config.compare_paths: compare(pre, gt)
def process_batch(batch, show=False): output = np.clip(utils.tensor_to_img(batch)[0], 0., 1.) plt.imsave(args['output_path'], output) if show: utils.plt_show(output) return output
device = torch.device(args['device']) upscale_factor = int(args['upscale_factor']) input_path = args['input_path'] def process_batch(batch, show=False): output = np.clip(utils.tensor_to_img(batch)[0], 0., 1.) plt.imsave(args['output_path'], output) if show: utils.plt_show(output) return output batch = utils.get_test_input(paths=[input_path]) img = batch[0] img = utils.tensor_to_img(img) shutil.copy2(input_path, 'original' + Path(input_path).suffix) batch = batch.to(device) models = { 2: { 'best_model_folder': 'mlflow_pretrained_models/pretrained_super_res_f2' }, 4: { 'best_model_folder': 'mlflow_pretrained_models/pretrained_super_res_f4' } } if len(args['load_model_path']) > 0: load_model_path = args['load_model_path'] else:
else: print('Not a valid model!') exit(-1) # data set test_data = PreSuDataset(img_list, low_size=args.low_size) # GPU if args.gpu >= 0: with torch.cuda.device(args.gpu): model.cuda() # test i_bar = tqdm(total=len(test_data), desc='#Images') for idx, imgs in enumerate(test_data): name = os.path.basename(test_data.get_path(idx)) lr_x, hr_x = imgs[1].unsqueeze(0), imgs[0].unsqueeze(0) if args.gpu >= 0: with torch.cuda.device(args.gpu): lr_x = lr_x.cuda() hr_x = hr_x.cuda() imgs = model(Variable(lr_x), Variable(hr_x)).data.cpu() for img in imgs: img = tensor_to_img(img, transpose=True) if args.gray: img = img.mean(axis=2).astype(img.dtype) imsave(os.path.join(args.save_folder, name), img) i_bar.update()
def save_tensor(t: torch.Tensor, file_path: str): img = tensor_to_img(t) save_img(img=img, file_path=file_path)
def save_tensor(t: torch.Tensor, file_path: str): img = tensor_to_img(t) save_img(img=img, file_path=file_path) all_img_paths = get_all_img_paths('../image_denoise') outdir = 'output/' for i, img_path in enumerate(all_img_paths): try: org_file_name = os.path.basename(img_path)[::-1].replace('.', '__', 1)[::-1] print('processing {}[{}/{}]'.format(org_file_name, i + 1, len(all_img_paths))) org_img, norm_img = load_img(img_path=img_path) save_tensor(org_img, outdir + "{}_{}.png".format(org_file_name, 'org')) print(norm_img.shape) with torch.no_grad(): print('predict with srresnet') srresnet_out = tensor_to_img(srresnet(norm_img.unsqueeze(0))) save_img(srresnet_out, outdir + "{}_{}.png".format(org_file_name, 'srr')) print('predict with srgam') srgan_out = tensor_to_img(srgan_generator(norm_img.unsqueeze(0))) save_img(srgan_out, outdir + "{}_{}.png".format(org_file_name, 'srg')) save_img( combine_image_horizontally( [tensor_to_img(org_img), srresnet_out, srgan_out]), outdir + '{}_com.png'.format(org_file_name)) except Exception as e: print(e)