def main(): # parse options parser = TestOptions() opts = parser.parse() # data loader print('\n--- load dataset ---') datasetA = dataset_single(opts, 'A', opts.input_dim_a) datasetB = dataset_single(opts, 'B', opts.input_dim_b) if opts.a2b: loader = torch.utils.data.DataLoader(datasetA, batch_size=1, num_workers=opts.nThreads) loader_attr = torch.utils.data.DataLoader(datasetB, batch_size=1, num_workers=opts.nThreads, shuffle=True) else: loader = torch.utils.data.DataLoader(datasetB, batch_size=1, num_workers=opts.nThreads) loader_attr = torch.utils.data.DataLoader(datasetA, batch_size=1, num_workers=opts.nThreads, shuffle=True) # model print('\n--- load model ---') model = DRIT(opts) model.setgpu(opts.gpu) model.resume(opts.resume, train=False) model.eval() # directory result_dir = os.path.join(opts.result_dir, opts.name) if not os.path.exists(result_dir): os.mkdir(result_dir) # test print('\n--- testing ---') for idx1, img1 in enumerate(loader): print('{}/{}'.format(idx1, len(loader))) img1 = img1.cuda(opts.gpu) imgs = [img1] names = ['input'] for idx2, img2 in enumerate(loader_attr): if idx2 == opts.num: break img2 = img2.cuda(opts.gpu) with torch.no_grad(): if opts.a2b: img = model.test_forward_transfer(img1, img2, a2b=True) else: img = model.test_forward_transfer(img2, img1, a2b=False) imgs.append(img) names.append('output_{}'.format(idx2)) save_imgs(imgs, names, os.path.join(result_dir, '{}'.format(idx1))) return
def main(): # parse options parser = TestOptions() opts = parser.parse() # data loader print('\n--- load dataset ---') datasetA = dataset_single(opts, opts.index_s) datasetB = dataset_single(opts, opts.index_t) loader = torch.utils.data.DataLoader(datasetA, batch_size=1, num_workers=1, shuffle=False) loader_attr = torch.utils.data.DataLoader(datasetB, batch_size=1, num_workers=1, shuffle=True) # model print('\n--- load model ---') model = SAVI2I(opts) model.setgpu(opts.gpu) model.resume(opts.resume, train=False) model.eval() # directory result_dir = os.path.join(opts.result_dir, opts.name) if not os.path.exists(result_dir): os.makedirs(result_dir) # test print('\n--- testing ---') for idx1, img1 in enumerate(loader): print('{}/{}'.format(idx1, len(loader))) img1 = img1.cuda() for idx2, img2 in enumerate(loader_attr): img2 = img2.cuda() if idx2 > opts.num: break with torch.no_grad(): imgs, names = model.test_interpolate_ref_save(img1, img2) imgs.append(img1.squeeze(0)) imgs.append(img2.squeeze(0)) names.append('input') names.append('reference') dir = os.path.join(result_dir, '{}'.format(idx1), '{}'.format(idx2)) os.makedirs(dir, exist_ok=True) save_imgs(imgs, names, dir) return
def main(): # parse options parser = TestOptions() opts = parser.parse() # data loader print('\n--- load dataset ---') if opts.a2b: dataset = dataset_single(opts, 'A', opts.input_dim_a) else: dataset = dataset_single(opts, 'B', opts.input_dim_b) loader = torch.utils.data.DataLoader(dataset, batch_size=1, num_workers=opts.nThreads) # model print('\n--- load model ---') model = DivCo_DRIT(opts) model.setgpu(opts.gpu) model.resume(opts.resume, train=False) model.eval() # directory result_dir = os.path.join(opts.result_dir, opts.name) if not os.path.exists(result_dir): os.mkdir(result_dir) # test print('\n--- testing ---') ep_name = opts.resume.split('/')[-1].split('.')[0] for idx1, (img1, name1) in enumerate(loader): print('{}/{}'.format(idx1, len(loader))) img1 = img1.cuda() imgs = [img1] names = ['input'] for idx2 in range(opts.num): with torch.no_grad(): if opts.random: img = model.test_forward(img1, a2b=opts.a2b) else: latent = float(idx2) / (opts.num - 1) * 2 - 1 img = model.test_givenz_forward(img1, latent, a2b=opts.a2b) imgs.append(img) names.append('output_{}'.format(idx2)) save_imgs( imgs, names, os.path.join( result_dir, 'test_ep{}_num{}_a2b{}_random{}'.format( ep_name, opts.num, opts.a2b, int(opts.random)), name1[0].split('.')[0])) return
def main(): # parse options parser = TestOptions() opts = parser.parse() domains = [chr(i) for i in range(ord('A'),ord('Z')+1)] # data loader print('\n--- load dataset ---') datasets = [None]*opts.num_domains loaders = [None]*opts.num_domains for i in range(opts.num_domains): datasets[i] = dataset_single(opts, i) loaders[i] = torch.utils.data.DataLoader(datasets[i], batch_size=1, num_workers=opts.nThreads) # model print('\n--- load model ---') model = MD_multi(opts) model.setgpu(opts.gpu) model.resume(opts.resume, train=False) model.eval() # directory result_dir = os.path.join(opts.result_dir, opts.name) if not os.path.exists(result_dir): os.mkdir(result_dir) # test print('\n--- testing ---') for d in range(opts.num_domains): for idx, data in enumerate(loaders[d]): #break img, c_org = data print('{}/{}'.format(idx, len(loaders[d]))) if idx > num: break; img, c_org = img.cuda(), c_org.cuda() imgs = [img] names = ['input'] for idx2 in range(opts.num): with torch.no_grad(): imgs_ = model.test_forward_random(img) for i in range(opts.num_domains): imgs.append(imgs_[i]) names.append('output{}_{}_{}'.format(domains[d], domains[i], idx2)) save_imgs(imgs, names, os.path.join(result_dir, '{}_{}'.format(domains[d], idx))) save_concat_imgs(imgs, 'output{}_{}'.format(domains[d], idx), result_dir) return
def main(): # parse options parser = TestOptions() opts = parser.parse() # data loader print('\n--- load dataset ---') if opts.a2b: dataset = dataset_single(opts, 'A', opts.input_dim_a) subdir = "a2b" else: dataset = dataset_single(opts, 'B', opts.input_dim_b) subdir = "b2a" loader = torch.utils.data.DataLoader(dataset, batch_size=1, num_workers=opts.nThreads) # model print('\n--- load model ---') model = DRIT(opts) model.setgpu(opts.gpu) model.resume(opts.resume, train=False) model.eval() # directory result_dir = os.path.join(opts.result_dir, opts.name, subdir) if not os.path.exists(result_dir): os.makedirs(result_dir) # test print('\n--- testing ---') for idx1, img1 in enumerate(loader): print('{}/{}'.format(idx1, len(loader))) img1 = img1.cuda() imgs = [img1] names = ['input'] for idx2 in range(opts.num): with torch.no_grad(): img = model.test_forward(img1, a2b=opts.a2b) imgs.append(img) names.append('output_{}'.format(idx2)) save_imgs(imgs, names, os.path.join(result_dir, '{}'.format(idx1))) return
def main(): # parse options parser = TestOptions() opts = parser.parse() # data loader print('\n--- load dataset ---') dataset = dataset_single(opts, opts.index_s) loader = torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=False, num_workers=1) # model print('\n--- load model ---') model = SAVI2I(opts) model.setgpu(opts.gpu) model.resume(opts.resume, train=False) model.eval() # directory result_dir = os.path.join(opts.result_dir, opts.name) if not os.path.exists(result_dir): os.makedirs(result_dir) # test print('\n--- testing ---') for idx, img in enumerate(loader): #break img = img.cuda() for idx2 in range(opts.num): with torch.no_grad(): index = ord(opts.index_t) - ord('A') imgs, names = model.test_interpolate_latent_save_rdm( img, index) dir = os.path.join(result_dir, '{}'.format(idx), '{}'.format(idx2)) os.makedirs(dir, exist_ok=True) save_imgs(imgs, names, dir) return
def main(): # parse options parser = TestOptions() opts = parser.parse() # data loader print('\n--- load dataset ---') datasetA = dataset_single(opts, 'A', opts.input_dim_a) datasetB = dataset_single(opts, 'B', opts.input_dim_b) if opts.a2b: loader = torch.utils.data.DataLoader(datasetA, batch_size=1, num_workers=opts.nThreads) loader_attr = torch.utils.data.DataLoader(datasetB, batch_size=1, num_workers=opts.nThreads, shuffle=True) else: loader = torch.utils.data.DataLoader(datasetB, batch_size=1, num_workers=opts.nThreads) loader_attr = torch.utils.data.DataLoader(datasetA, batch_size=1, num_workers=opts.nThreads, shuffle=True) # model print('\n--- load model ---') model = DRIT(opts) model.setgpu(opts.gpu) model.resume(opts.resume, train=False) model.eval() # directory result_dir = os.path.join(opts.result_dir, opts.name) if not os.path.exists(result_dir): os.mkdir(result_dir) # test print('\n--- testing ---') for idx1, (img1, img1_path) in enumerate(loader): print('{}/{}'.format(idx1, len(loader))) img1_path = img1_path[0] img1_prefix = os.path.basename(img1_path).split('.')[0] # print('img1_prefix:', img1_prefix) img1 = img1.cuda() imgs = [img1] # print('img1 type:', type(img1)) names = [f'{img1_prefix}_input'] for idx2, (img2, img2_path) in enumerate(loader_attr): img2_path = img2_path[0] img2_prefix = os.path.basename(img2_path).split('.')[0] # print('img2_prefix:', img2_prefix) if img1_prefix == img2_prefix: img2 = img2.cuda() imgs.append(img2) names.append(f'{img2_prefix}_real') # print('img2 type:', type(img2)) with torch.no_grad(): if opts.a2b: img = model.test_forward_transfer(img1, img2, a2b=True) else: img = model.test_forward_transfer(img2, img1, a2b=False) imgs.append(img) names.append(f'{img2_prefix}_fake') break save_imgs(imgs, names, result_dir) return
def main(): # parse options parser = TestOptions() opts = parser.parse() result_dir = os.path.join(opts.result_dir, opts.name) orig_dir = opts.orig_dir blur_dir = opts.dataroot if not os.path.exists(result_dir): os.mkdir(result_dir) # data loader print('\n--- load dataset ---') if opts.a2b: dataset = dataset_single(opts, 'A', opts.input_dim_a) else: dataset = dataset_single(opts, 'B', opts.input_dim_b) loader = torch.utils.data.DataLoader(dataset, batch_size=1, num_workers=opts.nThreads) # model print('\n--- load model ---') model = UID(opts) model.setgpu(opts.gpu) model.resume(opts.resume, train=False) model.eval() # test print('\n--- testing ---') for idx1, (img1,img_name) in enumerate(loader): print('{}/{}'.format(idx1, len(loader))) img1 = img1.cuda(opts.gpu).detach() with torch.no_grad(): img = model.test_forward(img1, a2b=opts.a2b) img_name = img_name[0].split('/') img_name = img_name[-1] save_imgs(img, img_name, result_dir) # evaluate metrics if opts.percep == 'default': pLoss = PerceptualLoss(nn.MSELoss(),p_layer=36) elif opts.percep == 'face': self.perceptualLoss = networks.PerceptualLoss16(nn.MSELoss(),p_layer=30) else: self.perceptualLoss = networks.MultiPerceptualLoss(nn.MSELoss()) orig_list = sorted(os.listdir(orig_dir)) deblur_list = sorted(os.listdir(result_dir)) blur_list = sorted(os.listdir(blur_dir)) psnr = [] ssim = [] percp = [] blur_psnr = [] blur_ssim = [] blur_percp = [] for (deblur_img_name, orig_img_name, blur_img_name) in zip(deblur_list, orig_list, blur_list): deblur_img_name = os.path.join(result_dir,deblur_img_name) orig_img_name = os.path.join(orig_dir,orig_img_name) blur_img_name = os.path.join(blur_dir, blur_img_name) deblur_img = imread(deblur_img_name) orig_img = imread(orig_img_name) blur_img = imread(blur_img_name) try: psnr.append(PSNR(deblur_img, orig_img)) ssim.append(SSIM(deblur_img, orig_img, multichannel=True)) blur_psnr.append(PSNR(blur_img, orig_img)) blur_ssim.append(SSIM(blur_img, orig_img, multichannel=True)) except ValueError: print(orig_img_name) with torch.no_grad(): temp = pLoss.getloss(deblur_img,orig_img) temp2 = pLoss.getloss(blur_img,orig_img) percp.append(temp) blur_percp.append(temp2) print(sum(psnr)/len(psnr)) print(sum(ssim)/len(ssim)) print(sum(percp)/len(percp)) print(sum(blur_psnr)/len(psnr)) print(sum(blur_ssim)/len(ssim)) print(sum(blur_percp)/len(percp)) return