From fcc4581e8f6200ee1b4099691f935b859e3d060d Mon Sep 17 00:00:00 2001 From: lvmengsi Date: Sun, 23 Jun 2019 11:33:09 +0800 Subject: [PATCH] fix grad as list (#2493) * fix grad and scipy version --- PaddleCV/gan/data_reader.py | 2 +- PaddleCV/gan/infer.py | 11 ++++++----- PaddleCV/gan/trainer/AttGAN.py | 9 +++++---- PaddleCV/gan/trainer/STGAN.py | 9 +++++---- PaddleCV/gan/trainer/StarGAN.py | 2 +- PaddleCV/gan/util/utility.py | 29 +++++++++++++++-------------- 6 files changed, 33 insertions(+), 29 deletions(-) diff --git a/PaddleCV/gan/data_reader.py b/PaddleCV/gan/data_reader.py index 7536dd8b..a263ec61 100644 --- a/PaddleCV/gan/data_reader.py +++ b/PaddleCV/gan/data_reader.py @@ -296,7 +296,7 @@ class celeba_reader_creator(reader_creator): label = np.array(label).astype("float32") label = (label + 1) // 2 img = CentorCrop(img, args.crop_size, args.crop_size) - img = img.resize((args.load_size, args.load_size), + img = img.resize((args.image_size, args.image_size), Image.BILINEAR) img = (np.array(img).astype('float32') / 255.0 - 0.5) / 0.5 img = img.transpose([2, 0, 1]) diff --git a/PaddleCV/gan/infer.py b/PaddleCV/gan/infer.py index 81d6821d..dd07b1e1 100644 --- a/PaddleCV/gan/infer.py +++ b/PaddleCV/gan/infer.py @@ -23,9 +23,10 @@ from PIL import Image import paddle.fluid as fluid import paddle import numpy as np -from scipy.misc import imsave +import imageio import glob from util.config import add_arguments, print_arguments +import copy parser = argparse.ArgumentParser(description=__doc__) add_arg = functools.partial(add_arguments, argparser=parser) @@ -153,7 +154,7 @@ def infer(args): images.append(fake_temp) images_concat = np.concatenate(images, 1) images_concat = np.concatenate(images_concat, 1) - imsave(args.output + "/fake_img_" + name[0], ( + imageio.imwrite(args.output + "/fake_img_" + name[0], ( (images_concat + 1) * 127.5).astype(np.uint8)) elif args.model_net == 'StarGAN': test_reader = celeba_reader_creator( @@ -184,8 +185,8 @@ def infer(args): fake_temp = np.squeeze(out[0]).transpose([1, 2, 0]) images.append(fake_temp) images_concat = np.concatenate(images, 1) - imsave(out_path + "/fake_img" + str(epoch) + "_" + name[0], ( - (images_concat + 1) * 127.5).astype(np.uint8)) + imageio.imwrite(out_path + "/fake_img" + str(epoch) + "_" + name[0], + ((images_concat + 1) * 127.5).astype(np.uint8)) elif args.model_net == 'Pix2pix' or args.model_net == 'cyclegan': for file in glob.glob(args.input): @@ -204,7 +205,7 @@ def infer(args): fake_temp = np.squeeze(fake_temp[0]).transpose([1, 2, 0]) input_temp = np.squeeze(data).transpose([1, 2, 0]) - imsave(args.output + "/fake_" + image_name, ( + imageio.imwrite(args.output + "/fake_" + image_name, ( (fake_temp + 1) * 127.5).astype(np.uint8)) else: raise NotImplementedError("model_net {} is not support".format( diff --git a/PaddleCV/gan/trainer/AttGAN.py b/PaddleCV/gan/trainer/AttGAN.py index c979541c..3ab72119 100644 --- a/PaddleCV/gan/trainer/AttGAN.py +++ b/PaddleCV/gan/trainer/AttGAN.py @@ -161,20 +161,21 @@ class DTrainer(): if fluid.io.is_parameter(var) and var.name.startswith( "discriminator"): vars.append(var.name) - grad = fluid.gradients(pred, x, no_grad_set=vars) + grad = fluid.gradients(pred, x, no_grad_set=vars)[0] grad_shape = grad.shape grad = fluid.layers.reshape( grad, [-1, grad_shape[1] * grad_shape[2] * grad_shape[3]]) - epsilon = 1e-5 norm = fluid.layers.sqrt( fluid.layers.reduce_sum( - fluid.layers.square(grad), dim=1) + epsilon) + fluid.layers.square(grad), dim=1)) gp = fluid.layers.reduce_mean(fluid.layers.square(norm - 1.0)) return gp class AttGAN(object): def add_special_args(self, parser): + parser.add_argument( + '--image_size', type=int, default=256, help="image size") parser.add_argument( '--g_lr', type=float, @@ -241,7 +242,7 @@ class AttGAN(object): self.batch_num = batch_num def build_model(self): - data_shape = [-1, 3, self.cfg.load_size, self.cfg.load_size] + data_shape = [-1, 3, self.cfg.image_size, self.cfg.image_size] image_real = fluid.layers.data( name='image_real', shape=data_shape, dtype='float32') diff --git a/PaddleCV/gan/trainer/STGAN.py b/PaddleCV/gan/trainer/STGAN.py index d1a3b64c..775bc3b1 100644 --- a/PaddleCV/gan/trainer/STGAN.py +++ b/PaddleCV/gan/trainer/STGAN.py @@ -159,20 +159,21 @@ class DTrainer(): if fluid.io.is_parameter(var) and var.name.startswith( "discriminator"): vars.append(var.name) - grad = fluid.gradients(pred, x, no_grad_set=vars) + grad = fluid.gradients(pred, x, no_grad_set=vars)[0] grad_shape = grad.shape grad = fluid.layers.reshape( grad, [-1, grad_shape[1] * grad_shape[2] * grad_shape[3]]) - epsilon = 1e-5 norm = fluid.layers.sqrt( fluid.layers.reduce_sum( - fluid.layers.square(grad), dim=1) + epsilon) + fluid.layers.square(grad), dim=1)) gp = fluid.layers.reduce_mean(fluid.layers.square(norm - 1.0)) return gp class STGAN(object): def add_special_args(self, parser): + parser.add_argument( + '--image_size', type=int, default=256, help="image size") parser.add_argument( '--g_lr', type=float, @@ -246,7 +247,7 @@ class STGAN(object): self.batch_num = batch_num def build_model(self): - data_shape = [-1, 3, self.cfg.load_size, self.cfg.load_size] + data_shape = [-1, 3, self.cfg.image_size, self.cfg.image_size] image_real = fluid.layers.data( name='image_real', shape=data_shape, dtype='float32') diff --git a/PaddleCV/gan/trainer/StarGAN.py b/PaddleCV/gan/trainer/StarGAN.py index 09378170..8dc56460 100644 --- a/PaddleCV/gan/trainer/StarGAN.py +++ b/PaddleCV/gan/trainer/StarGAN.py @@ -185,7 +185,7 @@ class DTrainer(): for var in fluid.default_main_program().list_vars(): if fluid.io.is_parameter(var) and var.name.startswith('d_'): vars.append(var.name) - grad = fluid.gradients(pred, x, no_grad_set=vars) + grad = fluid.gradients(pred, x, no_grad_set=vars)[0] grad_shape = grad.shape grad = fluid.layers.reshape( grad, [-1, grad_shape[1] * grad_shape[2] * grad_shape[3]]) diff --git a/PaddleCV/gan/util/utility.py b/PaddleCV/gan/util/utility.py index b9242bf9..d14878a8 100644 --- a/PaddleCV/gan/util/utility.py +++ b/PaddleCV/gan/util/utility.py @@ -27,7 +27,8 @@ import six matplotlib.use('agg') import matplotlib.pyplot as plt import matplotlib.gridspec as gridspec -from scipy.misc import imsave +import imageio +import copy img_dim = 28 @@ -94,11 +95,11 @@ def save_test_image(epoch, input_A_temp = np.squeeze(data_A[0]).transpose([1, 2, 0]) input_B_temp = np.squeeze(data_A[0]).transpose([1, 2, 0]) - imsave(out_path + "/fakeB_" + str(epoch) + "_" + name, ( + imageio.imwrite(out_path + "/fakeB_" + str(epoch) + "_" + name, ( (fake_B_temp + 1) * 127.5).astype(np.uint8)) - imsave(out_path + "/inputA_" + str(epoch) + "_" + name, ( + imageio.imwrite(out_path + "/inputA_" + str(epoch) + "_" + name, ( (input_A_temp + 1) * 127.5).astype(np.uint8)) - imsave(out_path + "/inputB_" + str(epoch) + "_" + name, ( + imageio.imwrite(out_path + "/inputB_" + str(epoch) + "_" + name, ( (input_B_temp + 1) * 127.5).astype(np.uint8)) elif cfg.model_net == "StarGAN": for data in zip(A_test_reader()): @@ -127,8 +128,8 @@ def save_test_image(epoch, images.append(fake_temp) images.append(rec_temp) images_concat = np.concatenate(images, 1) - imsave(out_path + "/fake_img" + str(epoch) + "_" + name[0], ( - (images_concat + 1) * 127.5).astype(np.uint8)) + imageio.imwrite(out_path + "/fake_img" + str(epoch) + "_" + name[0], + ((images_concat + 1) * 127.5).astype(np.uint8)) elif cfg.model_net == 'AttGAN' or cfg.model_net == 'STGAN': for data in zip(A_test_reader()): real_img, label_org, name = data[0] @@ -168,8 +169,8 @@ def save_test_image(epoch, images.append(fake_temp) images_concat = np.concatenate(images, 1) images_concat = np.concatenate(images_concat, 1) - imsave(out_path + "/fake_img" + str(epoch) + '_' + name[0], ( - (images_concat + 1) * 127.5).astype(np.uint8)) + imageio.imwrite(out_path + "/fake_img" + str(epoch) + '_' + name[0], + ((images_concat + 1) * 127.5).astype(np.uint8)) else: for data_A, data_B in zip(A_test_reader(), B_test_reader()): @@ -194,17 +195,17 @@ def save_test_image(epoch, input_A_temp = np.squeeze(data_A[0][0]).transpose([1, 2, 0]) input_B_temp = np.squeeze(data_B[0][0]).transpose([1, 2, 0]) - imsave(out_path + "/fakeB_" + str(epoch) + "_" + A_name, ( + imageio.imwrite(out_path + "/fakeB_" + str(epoch) + "_" + A_name, ( (fake_B_temp + 1) * 127.5).astype(np.uint8)) - imsave(out_path + "/fakeA_" + str(epoch) + "_" + B_name, ( + imageio.imwrite(out_path + "/fakeA_" + str(epoch) + "_" + B_name, ( (fake_A_temp + 1) * 127.5).astype(np.uint8)) - imsave(out_path + "/cycA_" + str(epoch) + "_" + A_name, ( + imageio.imwrite(out_path + "/cycA_" + str(epoch) + "_" + A_name, ( (cyc_A_temp + 1) * 127.5).astype(np.uint8)) - imsave(out_path + "/cycB_" + str(epoch) + "_" + B_name, ( + imageio.imwrite(out_path + "/cycB_" + str(epoch) + "_" + B_name, ( (cyc_B_temp + 1) * 127.5).astype(np.uint8)) - imsave(out_path + "/inputA_" + str(epoch) + "_" + A_name, ( + imageio.imwrite(out_path + "/inputA_" + str(epoch) + "_" + A_name, ( (input_A_temp + 1) * 127.5).astype(np.uint8)) - imsave(out_path + "/inputB_" + str(epoch) + "_" + B_name, ( + imageio.imwrite(out_path + "/inputB_" + str(epoch) + "_" + B_name, ( (input_B_temp + 1) * 127.5).astype(np.uint8)) -- GitLab