if args.data == 'CelebA-HQ': from data import CelebA_HQ test_dataset = CelebA_HQ(args.data_path, args.attr_path, args.image_list_path, args.img_size, 'test', args.attrs) os.makedirs(output_path, exist_ok=True) test_dataloader = data.DataLoader( test_dataset, batch_size=1, num_workers=args.num_workers, shuffle=False, drop_last=False ) if args.num_test is None: print('Testing images:', len(test_dataset)) else: print('Testing images:', min(len(test_dataset), args.num_test)) attgan = AttGAN(args) attgan.load(find_model(join('output', args.experiment_name, 'checkpoint'), args.load_epoch)) progressbar = Progressbar() attgan.eval() for idx, (img_a, att_a) in enumerate(test_dataloader): if args.num_test is not None and idx == args.num_test: break img_a = img_a.cuda() if args.gpu else img_a att_a = att_a.cuda() if args.gpu else att_a att_a = att_a.type(torch.float) att_b_list = [att_a] if args.by_levels:
test_dataset = CelebA(args.data_path, args.attr_path, args.img_size, 'mytest', args.attrs) test_dataloader = data.DataLoader(test_dataset, batch_size=1, num_workers=args.num_workers, shuffle=False, drop_last=False) print('Testing images:', len(test_dataset)) output_path = join('output', args.experiment_name, 'attention_testing') os.makedirs(output_path, exist_ok=True) attgan = AttGAN(args) attgan.load(r'weights_unzip.17.pth') attgan.eval() for idx, (img_real, att_org) in enumerate(test_dataloader): img_real = img_real.cuda() if args.gpu else img_real att_org = att_org.cuda() if args.gpu else att_org att_org = att_org.type(torch.float) _, mc, mw, mh = img_real.shape att_list = [att_org] img_unit = img_real.view(3, mw, mh) img_unit = ((img_unit * 0.5) + 0.5) * 255 img_unit = np.uint8(img_unit) img_unit = img_unit[::-1, :, :].transpose(1, 2, 0) for i in range(args.n_attrs): tmp = att_org.clone() tmp[:, i] = 1 - tmp[:, i]
from os.path import join from attgan import AttGAN def parse(args=None): parser = argparse.ArgumentParser() parser.add_argument('--experiment_name', dest='experiment_name', type=str, default='06-36AM on March 09, 2021') parser.add_argument('--load_epoch', dest='load_epoch', type=int, default=0) parser.add_argument('--gpu', dest='gpu', type=bool, default=False) return parser.parse_args(args) args_ = parse() with open(join('output', args_.experiment_name, 'setting.txt'), 'r') as f: args = json.load(f, object_hook=lambda d: argparse.Namespace(**d)) args.gpu = args_.gpu args.experiment_name = args_.experiment_name args.load_epoch = args_.load_epoch args.betas = (args.beta1, args.beta2) model = AttGAN(args) model.load( os.path.join('output', args.experiment_name, 'checkpoint', 'weights.' + str(args.load_epoch) + '.pth')) model.saveG_D(os.path.join('output', args.experiment_name, 'checkpoint', 'weights_unzip.{:d}.pth'.format(args.load_epoch)), flag='unzip')
batch_size=1, num_workers=args.num_workers, shuffle=False, drop_last=False) # 如果没有限定处理多少张,那就把整个数据集都做迁移 if args.num_test is None: print('Testing images:', len(test_dataset)) else: print('Testing images:', min(len(test_dataset), args.num_test)) # 载入AttGAN模型 attgan = AttGAN(args) # 载入指定节点 attgan.load( find_model(join('output', args.experiment_name, 'checkpoint'), args.load_epoch) ) # 载入指定节点 output/128_shortcut1_inject1_none/checkpoint/ progressbar = Progressbar() # 进行验证 attgan.eval() # 对图片的大循环 for idx, (img_a, att_a) in enumerate(test_dataloader): ''' idx: 图像的索引 img_a: 图像 att_a: 标签 原始标签 label_a 1.文件名 '{:06d}.jpg'.format(idx + 182638) name_array[i] 2.生成标签 att_c_list[i] att_b_list[i] 3.生成的图片 samples[i] samples[i] '''