def evaluation(trainer): model = load_inception_model() ims = [] xp = gen.xp n_ims = 50000 for i in range(0, n_ims, batchsize): z = Variable(xp.asarray(gen.make_hidden(batchsize))) with chainer.using_config('train', False), chainer.using_config('enable_backprop', False): x = gen(z) x = chainer.cuda.to_cpu(x.data) x = np.asarray(np.clip(x * 127.5 + 127.5, 0.0, 255.0), dtype=np.uint8) ims.append(x) ims = np.asarray(ims) _, _, _, h, w = ims.shape ims = ims.reshape((n_ims, 3, h, w)).astype("f") mean, std = inception_score(model, ims) chainer.reporter.report({ 'inception_mean': mean, 'inception_std': std }) isarray.append([mean,std]) _IS_array = np.array(isarray) _IS_array = _IS_array[:,0] plt.plot(range(len(_IS_array)), _IS_array, 'r-', label = 'Inception Score') plt.legend() plt.savefig(dir+'/inception_score.pdf', bbox_inches='tight',format="pdf", dpi = 300) plt.close() np.save(dir+"/inception_score.npy",{"IS_array":isarray})
def sample_image(model, encoder, output_image_dir, n_row, batches_done, dataloader, device): """Saves a grid of generated imagenet pictures with captions""" target_dir = os.path.join(output_image_dir, "samples/") if not os.path.isdir(target_dir): os.makedirs(target_dir) captions = [] gen_imgs = [] # get sample captions done = False while not done: for (_, labels_batch, captions_batch) in dataloader: captions += captions_batch conditional_embeddings = encoder(labels_batch.to(device), captions) imgs = model.sample(conditional_embeddings).cpu() gen_imgs.append(imgs) if len(captions) > n_row**2: done = True break break gen_imgs = torch.cat(gen_imgs).numpy() gen_imgs = np.clip(gen_imgs + 1e-5, 0, 1) score = inception_score(gen_imgs, splits=1) print("Inception score:", score) for (imgs, labels, captions) in dataloader: fid = calculate_fid(imgs, gen_imgs, batch_size=len(gen_imgs), use_multiprocessing=False) break print("FID score:", fid) fig = plt.figure(figsize=((8, 8))) grid = ImageGrid(fig, 111, nrows_ncols=(n_row, n_row), axes_pad=0.2) for i in range(n_row**2): grid[i].imshow(gen_imgs[i].transpose([1, 2, 0])) grid[i].set_title(captions[i]) grid[i].tick_params(axis='both', which='both', bottom=False, top=False, labelbottom=True) save_file = os.path.join(target_dir, "{:013d}.png".format(batches_done)) plt.savefig(save_file) print("saved {}".format(save_file)) plt.close()
def compute_is(): fake_imgs_path = os.path.join(output_dir, name, 'images', 'epoch_{}'.format(epoch)) mean, std = inception_score(fake_imgs_path) results[name][epoch]['IS'] = { 'mean': '{:.5f}'.format(mean), 'std': '{:.5f}'.format(std) } print('IS: ', mean, std)
def my_inception(gen, batchsize=100): model = load_inception_model() ims = [] n_ims = 100 for i in range(0, n_ims, batchsize): z = Variable(np.asarray(gen.make_hidden(batchsize))) # with chainer.using_config('train', False), chainer.using_config('enable_backprop', False): # x = gen(z) x = gen(z).data # x = chainer.cuda.to_cpu(x.data) x = np.asarray(np.clip(x * 127.5 + 127.5, 0.0, 255.0), dtype=np.uint8) ims.append(x) ims = np.asarray(ims) _, _, _, h, w = ims.shape ims = ims.reshape((n_ims, 3, h, w)).astype("f") mean, std = inception_score(model, ims) #FIXME ここで落ちる return mean, std
def evaluation(trainer): model = load_inception_model() ims = [] xp = gen.xp n_ims = 50000 for i in range(0, n_ims, batchsize): z = Variable(xp.asarray(gen.make_hidden(batchsize))) with chainer.using_config('train', False), chainer.using_config( 'enable_backprop', False): x = gen(z) x = chainer.cuda.to_cpu(x.data) x = np.asarray(np.clip(x * 127.5 + 127.5, 0.0, 255.0), dtype=np.uint8) ims.append(x) ims = np.asarray(ims) _, _, _, h, w = ims.shape ims = ims.reshape((n_ims, 3, h, w)).astype("f") mean, std = inception_score(model, ims) #FIXME ここで落ちる chainer.reporter.report({'inception_mean': mean, 'inception_std': std})