def run(): cnt = 0 for _ in tqdm.trange(len_test_dataset): # data for sampling xa_ipt, a_ipt = sess.run(test_next) b_ipt = np.copy(a_ipt) for test_att_name in args.test_att_names: i = args.att_names.index(test_att_name) b_ipt[..., i] = 1 - b_ipt[..., i] b_ipt = data.check_attribute_conflict(b_ipt, test_att_name, args.att_names) b__ipt = (b_ipt * 2 - 1).astype(np.float32) # !!! for test_att_name, test_int in zip(args.test_att_names, args.test_ints): i = args.att_names.index(test_att_name) b__ipt[..., i] = b__ipt[..., i] * test_int x_opt_list = [xa_ipt] x_opt = sess.run(x, feed_dict={xa: xa_ipt, b_: b__ipt}) x_opt_list.append(x_opt) sample = np.transpose(x_opt_list, (1, 2, 0, 3, 4)) sample = np.reshape(sample, (sample.shape[0], -1, sample.shape[2] * sample.shape[3], sample.shape[4])) for s in sample: cnt += 1 im.imwrite(s, '%s/%d.jpg' % (save_dir, cnt))
def run(): cnt = 0 for _ in tqdm.trange(len_test_dataset): # data for sampling xa_ipt, a_ipt = sess.run(test_iter.get_next()) b_ipt_list = [a_ipt] # the first is for reconstruction for i in range(n_atts): tmp = np.array(a_ipt, copy=True) tmp[:, i] = 1 - tmp[:, i] # inverse attribute tmp = data.check_attribute_conflict(tmp, args.att_names[i], args.att_names) b_ipt_list.append(tmp) x_opt_list = [xa_ipt] for i, b_ipt in enumerate(b_ipt_list): b__ipt = (b_ipt * 2 - 1).astype(np.float32) # !!! if i > 0: # i == 0 is for reconstruction b__ipt[..., i - 1] = b__ipt[..., i - 1] * args.test_int x_opt = sess.run(x, feed_dict={xa: xa_ipt, b_: b__ipt}) x_opt_list.append(x_opt) sample = np.transpose(x_opt_list, (1, 2, 0, 3, 4)) sample = np.reshape(sample, (sample.shape[0], -1, sample.shape[2] * sample.shape[3], sample.shape[4])) for s in sample: cnt += 1 im.imwrite(s, '%s/%d.jpg' % (save_dir, cnt))
def run(): cnt = 0 for _ in tqdm.trange(len_test_dataset): # data for sampling xa_ipt, a_ipt = sess.run(test_iter.get_next()) b_ipt_list = [a_ipt] # the first is for reconstruction for i in range(n_atts): tmp = np.array(a_ipt, copy=True) tmp[:, i] = 1 - tmp[:, i] # inverse attribute tmp = data.check_attribute_conflict(tmp, args.att_names[i], args.att_names) b_ipt_list.append(tmp) x_opt_list = [xa_ipt] e_opt_list = [np.full_like(xa_ipt, -1.0)] ms_opt_list = [] a__ipt = a_ipt * 2 - 1 for i, b_ipt in enumerate(b_ipt_list): b__ipt = (b_ipt * 2 - 1).astype(np.float32) # !!! if i > 0: # i == 0 is for reconstruction b__ipt[..., i - 1] = b__ipt[..., i - 1] * args.test_int x_opt, e_opt, ms_opt = sess.run([x, e, ms], feed_dict={ xa: xa_ipt, a_: a__ipt, b_: b__ipt }) x_opt_list.append(x_opt) e_opt_list.append(e_opt) ms_opt_list.append(ms_opt) if args.with_mask: # resize all masks to the same size for ms_opt in ms_opt_list: # attribute axis for i, m_opt in enumerate(ms_opt): # mask level axis m_opt_resized = [] for m_j_opt in m_opt: # batch axis m_opt_resized.append( im.imresize(m_j_opt * 2 - 1, (args.crop_size, args.crop_size))) ms_opt[i] = np.concatenate([np.array(m_opt_resized)] * 3, axis=-1) ms_opt_list = [np.full_like(ms_opt_list[0], -1.0) ] + ms_opt_list ms_opt_list = list( np.transpose(ms_opt_list, (1, 0, 2, 3, 4, 5)))[::-1] sample_m = np.transpose([x_opt_list, e_opt_list] + ms_opt_list, (2, 0, 3, 1, 4, 5)) else: sample_m = np.transpose([x_opt_list], (2, 0, 3, 1, 4, 5)) sample_m = np.reshape(sample_m, (sample_m.shape[0], -1, sample_m.shape[3] * sample_m.shape[4], sample_m.shape[5])) for s in sample_m: cnt += 1 im.imwrite(s, '%s/%d.jpg' % (save_dir, cnt))
def run(): cnt = 0 for _ in tqdm.trange(len_test_dataset): # data for sampling xa_ipt, a_ipt = sess.run(test_iter.get_next()) b_ipt = np.copy(a_ipt) for test_att_name in args.test_att_names: i = args.att_names.index(test_att_name) b_ipt[..., i] = 1 - b_ipt[..., i] b_ipt = data.check_attribute_conflict(b_ipt, test_att_name, args.att_names) a__ipt = a_ipt * 2 - 1 b__ipt = (b_ipt * 2 - 1).astype(np.float32) # !!! for test_att_name, test_int in zip(args.test_att_names, args.test_ints): i = args.att_names.index(test_att_name) b__ipt[..., i] = b__ipt[..., i] * test_int x_opt_list = [xa_ipt] e_opt_list = [np.full_like(xa_ipt, -1.0)] ms_opt_list = [] x_opt, e_opt, ms_opt = sess.run([x, e, ms], feed_dict={xa: xa_ipt, a_: a__ipt, b_: b__ipt}) x_opt_list.append(x_opt) e_opt_list.append(e_opt) ms_opt_list.append(ms_opt) if args.with_mask: # resize all masks to the same size for ms_opt in ms_opt_list: # attribute axis for i, m_opt in enumerate(ms_opt): # mask level axis m_opt_resized = [] for m_j_opt in m_opt: # batch axis m_opt_resized.append(im.imresize(m_j_opt * 2 - 1, (args.crop_size, args.crop_size))) ms_opt[i] = np.concatenate([np.array(m_opt_resized)] * 3, axis=-1) ms_opt_list = [np.full_like(ms_opt_list[0], -1.0)] + ms_opt_list ms_opt_list = list(np.transpose(ms_opt_list, (1, 0, 2, 3, 4, 5)))[::-1] sample_m = np.transpose([x_opt_list, e_opt_list] + ms_opt_list, (2, 0, 3, 1, 4, 5)) else: sample_m = np.transpose([x_opt_list], (2, 0, 3, 1, 4, 5)) sample_m = np.reshape(sample_m, (sample_m.shape[0], -1, sample_m.shape[3] * sample_m.shape[4], sample_m.shape[5])) for s in sample_m: cnt += 1 im.imwrite(s, '%s/%d.jpg' % (save_dir, cnt))
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: for i in range(args.n_attrs): tmp = att_a.clone() for j in range(args.n_attrs): tmp[:, j] = -1 tmp[:, i] = 1 tmp = check_attribute_conflict(tmp, args.attrs[i], args.attrs) att_b_list.append(tmp) else: for i in range(args.n_attrs): tmp = att_a.clone() tmp[:, i] = 1 - tmp[:, i] tmp = check_attribute_conflict(tmp, args.attrs[i], args.attrs) att_b_list.append(tmp) # a list of lists of attributes (probably). with torch.no_grad(): samples = [img_a] for i, att_b in enumerate(att_b_list): att_b_ = (att_b * 2 - 1) * args.thres_int if i > 0: att_b_[..., i - 1] = att_b_[..., i - 1] * args.test_int / args.thres_int samples.append(attgan.G(img_a, att_b_))
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 = att_a.clone() for a in args.test_atts: i = args.attrs.index(a) att_b[:, i] = 1 - att_b[:, i] att_b = check_attribute_conflict(att_b, args.attrs[i], args.attrs) with torch.no_grad(): samples = [img_a] att_b_ = (att_b * 2 - 1) * args.thres_int for a, i in zip(args.test_atts, args.test_ints): att_b_[..., args.attrs.index(a)] = att_b_[ ..., args.attrs.index(a)] * i / args.thres_int samples.append(attgan.G(img_a, att_b_)) samples = torch.cat(samples, dim=3) if args.custom_img: out_file = test_dataset.images[idx] else: out_file = '{:06d}.jpg'.format(idx + 182638) vutils.save_image(samples, join(output_path, out_file),