def test_model(self ,global_iteration, n_sample=1000, alpha=None): block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[2048] self.inception_model = InceptionV3([block_idx]) self.inception_model.cuda() self.set_data_resolution(int(self.opt.crop_size/self.opt.aspect_ratio)) self.progressive_model_on_one_gpu.eval() print('generator res: %s'%self.progressive_model_on_one_gpu.generator.res) print('generator phase: %s'%self.progressive_model_on_one_gpu.phase) if not os.path.isdir('samples'): os.mkdir('samples') if not os.path.isdir('samples/%s'%self.opt.name): os.mkdir('samples/%s'%self.opt.name) num_bs = 1 for i in range(int(n_sample/num_bs)): print(i) z = torch.randn(num_bs, 512).cuda() global_iteration = self.opt.which_iter iteration = 0 dim_ind = int(np.log2(self.progressive_model_on_one_gpu.dim))-2 fake = self.progressive_model(iteration, global_iteration, dim_ind, z=z, mode='inference') # print(fake.size()) fake = fake.cpu() for j in range(num_bs): save_image(fake[j,:,:,:], 'samples/%s/%s_%s_%s.png'%(self.opt.name, i*num_bs+j, self.progressive_model_on_one_gpu.dim, global_iteration), nrow=1, normalize=True, range=(-1,1)) fid = self.compute_FID(alpha) print('fid: %s', fid)
def __init__(self, opt): self.opt = opt self.create_dataset() self.n_samples = len(self.dataset) self.end2end_model = ProgressiveSegEnd2EndModel(opt, self.n_samples) #end2end model if len(opt.gpu_ids) > 0: self.end2end_model = DataParallelWithCallback( self.end2end_model, device_ids=opt.gpu_ids) self.end2end_model_on_one_gpu = self.end2end_model.module else: self.end2end_model_on_one_gpu = self.end2end_model self.progressive_model = self.end2end_model_on_one_gpu.progressive_model self.pix2pix_model = self.end2end_model_on_one_gpu.pix2pix_model self.pix2pix_model.optimizer_G, self.pix2pix_model.optimizer_D = \ self.pix2pix_model.create_optimizers(opt) self.optimizer_D2 = self.end2end_model_on_one_gpu.create_optimizers() self.phase = "stabilize" block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[2048] self.inception_model = InceptionV3([block_idx]) self.inception_model.cuda()
def compute_FID(self): nums_fid = 1000 dims = 2048 batchsize = 10 all_reals = np.zeros((int(nums_fid/batchsize)*batchsize,dims)) all_fakes = np.zeros((int(nums_fid/batchsize)*batchsize,dims)) #load inception network block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[2048] self.inception_model = InceptionV3([block_idx]) self.inception_model.cuda() for i, data_i in enumerate(dataloader): if i * batchsize >= nums_fid: break generated = model(data_i, mode='inference') fake_acts = get_activations(generated, self.inception_model, batchsize, cuda=True) all_fakes[i*batchsize:i*batchsize+fake_acts.shape[0],:] = fake_acts for i, data_i in enumerate(dataloader): if i * batchsize >= nums_fid: break real_ims = Variable(data_i['image']).cuda() print(img.size()) real_acts = get_activations(real_ims, self.inception_model, batchsize, cuda=True) all_reals[i*batchsize:i*batchsize+real_acts.shape[0],:] = real_acts fid_eval = calculate_fid_given_acts(all_reals, all_fakes) return fid_eval
def calculate_fid_given_ims(ims_1, ims_2, batch_size, cuda, dims=2048): """Calculates the FID of two paths""" for p in paths: if not os.path.exists(p): raise RuntimeError('Invalid path: %s' % p) block_idx = InceptionV3.BLOCK_INDEX_BY_DIM[dims] model = InceptionV3([block_idx]) if cuda: model.cuda() m1, s1 = _compute_statistics_of_im(ims_1, model, batch_size, dims, cuda) m2, s2 = _compute_statistics_of_im(ims_2, model, batch_size, dims, cuda) fid_value = calculate_frechet_distance(m1, s1, m2, s2) return fid_value