def main(): tf.random.set_seed(222) np.random.seed(222) z_dim=100 epochs=3000000 batch_size=512 learning_rate=0.002 is_training=True img_path=glob.glob(r'D:\BaiduNetdiskDownload\faces\faces\*.jpg') dataset,img_shape,_=make_anime_dataset(img_path,batch_size) print(dataset,img_shape) sample=next(iter(dataset)) print(sample.shape) dataset=dataset.repeat() db_iter=iter(dataset) generator=Genrator() generator.build(input_shape=(None,z_dim)) dis=Discriminator() dis.bulid(input_shape=(None,64,64,3)) g_optimaizer=tf.optimizers.Adam(learning_rate=learning_rate,beta_1=0.50) d_optimaer=tf.optimizers.Adam(learning_rate=learning_rate,beta_2=0.5) for epoch in range(epochs): batch_z=tf.random.uniform([batch_size,z_dim],minval=-1,maxval=1) batch_x=next(db_iter) with tf.GradientTape() as tape: d_loss=d_loss_fn(generator,dis,batch_z,batch_x,is_training) grads=tape.gradient(d_loss,dis.trainable_varables) d_optimaer.apply_gradients(zip(grads,dis.trainable_varables)) with tf.GradientTape() as tape:
def __init__(self, batch_size=16, D_lr=1e-3, G_lr=1e-3, r_dim=3072, z_dim=100, h_size=512, use_gpu=False): self.device = torch.device("cuda" if use_gpu else "cpu") self.batch_size = batch_size self.z_dim = z_dim self.D = Discriminator(r_dim, h_size).to(device=self.device) self.G = Generator(z_dim, r_dim, h_size).to(device=self.device) self.criterion = nn.BCELoss(size_average=True) self.D_optimizer = optim.Adam(self.D.parameters(), lr=D_lr) self.G_optimizer = optim.Adam(self.G.parameters(), lr=G_lr) root = '/home/lhq/PycharmProjects/gan.pytorch/datasets/data/test/' text = '/home/lhq/PycharmProjects/gan.pytorch/datasets/data/labels.txt' dataset = ListDatasets(root=root, fname_list=text) self.dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True) self.label_real = torch.ones(batch_size, 1, device=self.device) self.label_fake = torch.zeros(batch_size, 1, device=self.device)
def main(): tf.random.set_seed(22) np.random.seed(22) os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' assert tf.__version__.startswith('2.') # hyper parameters z_dim = 100 epochs = 3000000 batch_size = 512 learning_rate = 0.002 is_training = True img_path = glob.glob( r'C:\Users\Jackie Loong\Downloads\DCGAN-LSGAN-WGAN-GP-DRAGAN-Tensorflow-2-master\data\faces\*.jpg' ) dataset, img_shape, _ = make_anime_dataset(img_path, batch_size) print(dataset, img_shape) sample = next(iter(dataset)) print(sample.shape, tf.reduce_max(sample).numpy(), tf.reduce_min(sample).numpy()) dataset = dataset.repeat() db_iter = iter(dataset) generator = Generator() generator.build(input_shape=(None, z_dim)) discriminator = Discriminator() discriminator.build(input_shape=(None, 64, 64, 3)) g_optimizer = tf.optimizers.Adam(learning_rate=learning_rate, beta_1=0.5) d_optimizer = tf.optimizers.Adam(learning_rate=learning_rate, beta_1=0.5) for epoch in range(epochs): batch_z = tf.random.uniform([batch_size, z_dim], minval=-1., maxval=1.) batch_x = next(db_iter) # train D with tf.GradientTape() as tape: d_loss = d_loss_fn(generator, discriminator, batch_z, batch_x, is_training) grads = tape.gradient(d_loss, discriminator.trainable_variables) d_optimizer.apply_gradients( zip(grads, discriminator.trainable_variables)) with tf.GradientTape() as tape: g_loss = g_loss_fn(generator, discriminator, batch_z, is_training) grads = tape.gradient(g_loss, generator.trainable_variables) g_optimizer.apply_gradients(zip(grads, generator.trainable_variables)) if epoch % 100 == 0: print(epoch, 'd-loss:', float(d_loss), 'g-loss:', float(g_loss)) z = tf.random.uniform([100, z_dim]) fake_image = generator(z, training=False) img_path = os.path.join('images', 'gan-%d.png' % epoch) save_result(fake_image.numpy(), 10, img_path, color_mode='P')
def main(): tf.random.set_seed(233) np.random.seed(233) assert tf.__version__.startswith('2.') # hyper parameters z_dim = 100 epochs = 3000000 batch_size = 512 learning_rate = 0.0005 is_training = True img_path = glob.glob(r'C:\Users\Jackie\Downloads\faces\*.jpg') assert len(img_path) > 0 dataset, img_shape, _ = make_anime_dataset(img_path, batch_size) print(dataset, img_shape) sample = next(iter(dataset)) print(sample.shape, tf.reduce_max(sample).numpy(), tf.reduce_min(sample).numpy()) dataset = dataset.repeat() db_iter = iter(dataset) generator = Generator() generator.build(input_shape=(None, z_dim)) discriminator = Discriminator() discriminator.build(input_shape=(None, 64, 64, 3)) z_sample = tf.random.normal([100, z_dim]) g_optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate, beta_1=0.5) d_optimizer = tf.keras.optimizers.Adam(learning_rate=learning_rate, beta_1=0.5) for epoch in range(epochs): for _ in range(5): batch_z = tf.random.normal([batch_size, z_dim]) batch_x = next(db_iter) # train D with tf.GradientTape() as tape: d_loss, gp = d_loss_fn(generator, discriminator, batch_z, batch_x, is_training) grads = tape.gradient(d_loss, discriminator.trainable_variables) d_optimizer.apply_gradients(zip(grads, discriminator.trainable_variables)) batch_z = tf.random.normal([batch_size, z_dim]) with tf.GradientTape() as tape: g_loss = g_loss_fn(generator, discriminator, batch_z, is_training) grads = tape.gradient(g_loss, generator.trainable_variables) g_optimizer.apply_gradients(zip(grads, generator.trainable_variables)) if epoch % 100 == 0: print(epoch, 'd-loss:', float(d_loss), 'g-loss:', float(g_loss), 'gp:', float(gp)) z = tf.random.normal([100, z_dim]) fake_image = generator(z, training=False) img_path = os.path.join('images', 'wgan-%d.png' % epoch) save_result(fake_image.numpy(), 10, img_path, color_mode='P')
def train(): tf.random.set_seed(22) np.random.seed(22) data_iter = dataset.load_dataset() # 利用数组形式实现多输入模型 generator = Generator() generator.build(input_shape=[(None, z_dim), (None, 10)]) discriminator = Discriminator() discriminator.build(input_shape=[(None, 28, 28, 1), (None, 10)]) g_optimizer = tf.optimizers.Adam(learning_rate=learning_rate, beta_1=0.5) d_optimizer = tf.optimizers.Adam(learning_rate=learning_rate, beta_1=0.5) for epoch in range(epochs): for i in range(int(60000 / batch_size / epochs_d)): batch_z = tf.random.uniform([batch_size, z_dim], minval=0., maxval=1.) batch_c = [] for k in range(batch_size): batch_c.append(np.random.randint(0, 10)) batch_c = tf.one_hot(tf.convert_to_tensor(batch_c), 10) # train D for epoch_d in range(epochs_d): batch_data = next(data_iter) batch_x = batch_data[0] batch_y = batch_data[1] with tf.GradientTape() as tape: d_loss = d_loss_fn(generator, discriminator, batch_z, batch_c, batch_x, batch_y, is_training) grads = tape.gradient(d_loss, discriminator.trainable_variables) d_optimizer.apply_gradients( zip(grads, discriminator.trainable_variables)) # train G with tf.GradientTape() as tape: g_loss = g_loss_fn(generator, discriminator, batch_z, batch_c, is_training) grads = tape.gradient(g_loss, generator.trainable_variables) g_optimizer.apply_gradients( zip(grads, generator.trainable_variables)) print('epoch : {epoch} d-loss : {d_loss} g-loss : {g_loss}'.format( epoch=epoch, d_loss=d_loss, g_loss=g_loss)) z = tf.random.uniform([100, z_dim], minval=0., maxval=1.) c = [] for i in range(10): for j in range(10): c.append(i) c = tf.one_hot(tf.convert_to_tensor(c), 10) fake_image = generator([z, c], training=False) img_path = os.path.join('images', 'infogan-%d-final.png' % epoch) saver.save_image(fake_image.numpy(), img_path, 10)
class Tree: def __init__(self, location, forest, parent=None, generator=Generator(), name='unnamed'): """ Constructor for a Tree. """ self.generator = generator self.discriminator = Discriminator() self.nn = load_model('new_nn.h5') self.age = 1 self.location = location self.parent = parent self.forest = forest self.neighbors = ([parent] if parent else []) self.name = name def __repr__(self): """ Overwrite default str representation to output T(<position in list of trees>) """ return 'T(' + str(self.forest.trees.index(self)) + ')' def _newlocation(self): """ Find a suitable location for a child. """ num = randbelow(628) / 100 # A tree's roots are log10 of its age. r = math.log10(self.age) loc = (self.location[0] + round(math.cos(num) * r, 2), self.location[1] + round(math.sin(num) * r, 2)) for tree in self.forest.trees: if (loc[0]-tree.location[0]) ** 2 + (loc[1]-tree.location[1]) ** 2 <= math.log10(tree.age) ** 2: return None return loc def spawnChild(self): """ Attempt to spawn a child. If successful, return it. If not, return None. """ for _ in range(10): loc = self._newlocation() if loc: break if not loc: return None r = math.log10(self.age) child = Tree(location=loc, forest=self.forest, parent=self, name=self.forest.names.pop(0)) self.forest.connections[child] = [self] self.forest.connections[self].append(child) self.neighbors.append(child) return child def resetDiscriminator(self): """ Reset this tree's discriminator. """ self.discriminator.reset() def getnewneighbors(self): """ Check for neighbors within radius of roots. If found, add all necessary connections within both trees and within forest. """ for t in self.forest.trees: if (t is not self.parent and t is not self and t not in self.neighbors and ((t.location[0] - self.location[0]) ** 2 + (t.location[1] - self.location[1]) ** 2) <= (1.4 * math.log10(self.age)) ** 2): self.forest.connections[self].append(t) self.forest.connections[t].append(self) self.neighbors.append(t) t.neighbors.append(self)
def main(): # 设计随机种子,方便复现 tf.random.set_seed(22) np.random.seed(22) # 设定相关参数 z_dim = 100 epochs = 3000000 batch_size = 512 # 根据自己的GPU能力设计 learning_rate = 0.002 is_training = True # 加载数据(根据自己的路径更改),建立网络 img_path = glob.glob( r'C:\Users\Jackie Loong\Downloads\DCGAN-LSGAN-WGAN-GP-DRAGAN-Tensorflow-2-master\data\faces\*.jpg' ) dataset, img_shape, _ = make_anime_dataset(img_path, batch_size) # print(dataset, img_shape) # sample = next(iter(dataset)) dataset = dataset.repeat() db_iter = iter(dataset) generator = Generator() generator.build(input_shape=(None, z_dim)) discriminator = Discriminator() discriminator.build(input_shape=(None, 64, 64, 3)) # 建立优化器 g_optimizer = tf.optimizers.Adam(learning_rate=learning_rate, beta_1=0.5) d_optimizer = tf.optimizers.Adam(learning_rate=learning_rate, beta_1=0.5) for epoch in range(epochs): # 随机取样出来的结果 batch_z = tf.random.uniform([batch_size, z_dim], minval=-1., maxval=1.) batch_x = next(db_iter) # 训练检测网络 with tf.GradientTape() as tape: d_loss, gp = d_loss_fn(generator, discriminator, batch_z, batch_x, is_training) grads = tape.gradient(d_loss, discriminator.trainable_variables) d_optimizer.apply_gradients( zip(grads, discriminator.trainable_variables)) # 训练生成网络 with tf.GradientTape() as tape: g_loss = g_loss_fn(generator, discriminator, batch_z, is_training) grads = tape.gradient(g_loss, generator.trainable_variables) g_optimizer.apply_gradients(zip(grads, generator.trainable_variables)) if epoch % 100 == 0: print(epoch, 'd-loss:', float(d_loss), 'g-loss:', float(g_loss), 'gp:', float(gp)) z = tf.random.uniform([100, z_dim]) fake_image = generator(z, training=False) # 生成的图片保存,images文件夹下, 图片名为:wgan-epoch.png img_path = os.path.join('images', 'wgan-%d.png' % epoch) # 10*10, 彩色图片 save_result(fake_image.numpy(), 10, img_path, color_mode='P')
def __init__(self, location, forest, parent=None, generator=Generator(), name='unnamed'): """ Constructor for a Tree. """ self.generator = generator self.discriminator = Discriminator() self.nn = load_model('new_nn.h5') self.age = 1 self.location = location self.parent = parent self.forest = forest self.neighbors = ([parent] if parent else []) self.name = name
def main(): ''' This is a sample implementation of one generator training with three discriminators ''' # Setting up the gan network system gan1 = GAN(generator=Generator(), discriminator=Discriminator(), nn=load_model("new_nn.h5")) gan2 = GAN(generator=gan1.G, discriminator=Discriminator(), nn=load_model("new_nn.h5")) gan3 = GAN(generator=gan1.G, discriminator=Discriminator(), nn=load_model("new_nn.h5")) # Set the number of training iterations for the network training_period = 5 # Train the system for i in range(training_period): gan1.train(testid=i) gan2.train(testid=i) gan3.train(testid=i)
def init_model(config, data_generator): print('Initializing %s embedding model in %s mode...' % (config.model, config.mode)) npz = np.load(config.embedding_file) if config.load_embeddings else None if config.model == 'transe': em = EmbeddingModel.TransE(config, embeddings_dict=npz) elif config.model == 'transd': config.embedding_size = config.embedding_size / 2 em = EmbeddingModel.TransD(config, embeddings_dict=npz) elif config.model == 'distmult': em = EmbeddingModel.DistMult(config, embeddings_dict=npz) else: raise ValueError('Unrecognized model type: %s' % config.model) if config.mode == 'disc': model = Discriminator.BaseModel(config, em, data_generator) elif config.mode == 'gen': model = Generator.Generator(config, em, data_generator) else: raise ValueError('Unrecognized mode: %s' % config.mode) if npz: # noinspection PyUnresolvedReferences npz.close() model.build() print('Built model.') print('use semnet: %s' % model.use_semantic_network) return model
def __init__(self, z_dim, h_dim, learning_rate, scale, generator_output_layer): self.z_dim = z_dim self.h_dim = h_dim self.g_net = Generator(z_dim, h_dim, generator_output_layer) self.d_net = Discriminator(h_dim) self.training = tf.placeholder(tf.bool, []) self.with_text = tf.placeholder(tf.float32, [None]) self.x = tf.placeholder(tf.float32, [None, 64, 64, 3]) self.x_w_ = tf.placeholder(tf.float32, [None, 64, 64, 3]) self.z = tf.placeholder(tf.float32, [None, self.z_dim]) # true h self.h = tf.placeholder(tf.float32, [None, h_dim]) # false h self.h_ = tf.placeholder(tf.float32, [None, h_dim]) # false image self.x_ = self.g_net(self.z, self.h, self.training) # true image, true h self.d = self.d_net(self.x, self.h, self.training, reuse=False) # fake image, true h self.d_ = self.d_net(self.x_, self.h, self.training) # wrong image, true h self.d_w_ = self.d_net(self.x_w_, self.h, self.training) # true image, false h self.d_h_ = self.d_net(self.x, self.h_, self.training) self.g_loss = - tf.reduce_mean(self.d_) #+ tf.reduce_mean(tf.square(self.x - self.x_)) self.d_loss = tf.reduce_mean(self.d) \ - ( 1 * tf.reduce_mean(self.d_) + 1 * tf.reduce_mean(self.d_h_) + 1 * tf.reduce_mean(self.d_w_)) / (1 + 1 + 1) # penalty distribution for "improved wgan" epsilon = tf.random_uniform([], 0.0, 1.0) x_hat = epsilon * self.x + (1 - epsilon) * self.x_ d_hat = self.d_net(x_hat, self.h, self.training) dx = tf.gradients(d_hat, x_hat)[0] dx_norm = tf.sqrt(tf.reduce_sum(tf.square(dx), axis=[1,2,3])) ddx = scale * tf.reduce_mean(tf.square(dx_norm - 1.0)) self.d_loss = -(self.d_loss - ddx) self.d_opt, self.g_opt = None, None with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)): self.d_opt = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.5, beta2=0.9)\ .minimize(self.d_loss, var_list=self.d_net.vars) self.g_opt = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.5, beta2=0.9)\ .minimize(self.g_loss, var_list=self.g_net.vars)
def __init__(self, z_dim, h_dim, learning_rate, scale, generator_output_layer): self.z_dim = z_dim self.h_dim = h_dim self.g_net = Generator(z_dim, h_dim, generator_output_layer) self.d_net = Discriminator(h_dim) self.training = tf.placeholder(tf.bool, []) self.with_text = tf.placeholder(tf.float32, [None]) self.x = tf.placeholder(tf.float32, [None, 64, 64, 3]) self.x_w_ = tf.placeholder(tf.float32, [None, 64, 64, 3]) self.z = tf.placeholder(tf.float32, [None, self.z_dim]) # true h self.h = tf.placeholder(tf.float32, [None, h_dim]) # false h self.h_ = tf.placeholder(tf.float32, [None, h_dim]) # false image self.x_ = self.g_net(self.z, self.h, self.training) # true image, true h self.d = self.d_net(self.x, self.h, self.training, reuse=False) # fake image, true h self.d_ = self.d_net(self.x_, self.h, self.training) # wrong image, true h self.d_w_ = self.d_net(self.x_w_, self.h, self.training) # true image, false h self.d_h_ = self.d_net(self.x, self.h_, self.training) # self.g_loss = - tf.reduce_mean(self.d_) #+ tf.reduce_mean(tf.square(self.x - self.x_)) # self.d_loss = tf.reduce_mean(self.d) \ # - ( 1 * tf.reduce_mean(self.d_) + 1 * tf.reduce_mean(self.d_h_) + 1 * tf.reduce_mean(self.d_w_)) / (1 + 1 + 1) self.g_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.d_, labels=tf.ones_like(self.d_))) self.d_loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.d, labels=tf.ones_like(self.d))) \ + (tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.d_, labels=tf.zeros_like(self.d_))) + \ tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.d_w_, labels=tf.zeros_like(self.d_w_))) +\ tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.d_h_, labels=tf.zeros_like(self.d_h_))) ) / 3 self.d_opt, self.g_opt = None, None with tf.control_dependencies(tf.get_collection(tf.GraphKeys.UPDATE_OPS)): self.d_opt = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.5, beta2=0.9)\ .minimize(self.d_loss, var_list=self.d_net.vars) self.g_opt = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.5, beta2=0.9)\ .minimize(self.g_loss, var_list=self.g_net.vars)
def build_model(self): """Create a generator and a discriminator.""" self.G = Generator(self.g_conv_dim, self.c_dim, self.g_repeat_num) self.D = Discriminator(self.image_size, self.d_conv_dim, self.c_dim, self.d_repeat_num) self.g_optimizer = torch.optim.Adam(self.G.parameters(), self.g_lr, [self.beta1, self.beta2]) self.d_optimizer = torch.optim.Adam(self.D.parameters(), self.d_lr, [self.beta1, self.beta2]) self.print_network(self.G, 'G') self.print_network(self.D, 'D') self.G.to(self.device) self.D.to(self.device) """Build the feature extractor""" self.feature_model = f_model(model_path=DUMPED_MODEL, freeze_param=True).cuda() #.cuda() self.feature_model.eval()
def load_model(filename): checkpoint = torch.load(filename, map_location=device) global generator global discriminator generator = Generator(len(data['mapping']), data['max_length'], latent_size) generator.load_state_dict(checkpoint['generator_model_state_dict']) generator.eval() discriminator = Discriminator(len(data['mapping'])) discriminator.load_state_dict(checkpoint['discriminator_model_state_dict']) discriminator.eval() print('Loaded model')
parser = ArgumentParser() parser.add_argument('--data', type=str) parser.add_argument('--data-args', type=int, nargs='+') parser.add_argument('--dbs', type=int, help='Discriminator Batch Size') parser.add_argument('--dlr', type=float, help='Discriminator Learning Rate') parser.add_argument('--glr', type=float, help='Generator Learning Rate') parser.add_argument('--gbs', type=int, help='Generator Batch Size') parser.add_argument('--gpu', type=int, help='GPU') parser.add_argument('--nf', type=int, nargs='+', help='Number of Features') parser.add_argument('--ni', type=int, help='Number of Iterations') args = parser.parse_args() device = th.device('cpu') if args.gpu < 0 else None # TODO data_loader = getattr(data, args.data + 'Loader')(*args.data_args, device=device) discriminator = Discriminator().to(device) g_configs = [{'in_feats' : in_feats, 'out_feats' : out_feats, 'out_nodes' : out_nodes, 'aggregator' : 'mean'} for in_feats, out_feats, out_nodes in zip([data_loader.n_feats] + args.nf[:-1], args.nf[1:])] generator = Generator().to(device) d_optim = optim.Adam(discirminator.parameters(), args.dlr) g_optim = optim.Adam(generator.parameters(), args.glr) for i in range(args.ni): for j in range(args.gbs): generator() x, adj = next(data_loader) p, cost = discriminator(x, adj) authentic =
def main(): tf.random.set_seed(3333) np.random.seed(3333) os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' assert tf.__version__.startswith('2.') z_dim = 100 # 隐藏向量z的长度 epochs = 3000000 # 训练步数 batch_size = 64 learning_rate = 0.0002 is_training = True # 获取数据集路径 img_path = glob.glob(r'C:\Users\jay_n\.keras\datasets\faces\*.jpg') + \ glob.glob(r'C:\Users\jay_n\.keras\datasets\faces\*.png') print('images num:', len(img_path)) # 构建数据集对象 dataset, img_shape, _ = make_anime_dataset(img_path, batch_size, resize=64) print(dataset, img_shape) sample = next(iter(dataset)) # 采样 print(sample.shape, tf.reduce_max(sample).numpy(), tf.reduce_min(sample).numpy()) dataset = dataset.repeat(100) db_iter = iter(dataset) generator = Generator() generator.build(input_shape=(4, z_dim)) discriminator = Discriminator() discriminator.build(input_shape=(4, 64, 64, 3)) # 分别为生成器和判别器创建优化器 g_optimizer = keras.optimizers.Adam(learning_rate=learning_rate, beta_1=0.5) d_optimizer = keras.optimizers.Adam(learning_rate=learning_rate, beta_1=0.5) # generator.load_weights('generator.ckpt') # discriminator.load_weights('discriminator.ckpt') # print('Loaded ckpt!!') d_losses, g_losses = [], [] for epoch in range(epochs): # 1. 训练判别器 for _ in range(1): # 采样隐藏向量 batch_z = tf.random.normal([batch_size, z_dim]) batch_x = next(db_iter) # 采样真实图片 # 判别器前向计算 with tf.GradientTape() as tape: d_loss, _ = d_loss_fn(generator, discriminator, batch_z, batch_x, is_training) grads = tape.gradient(d_loss, discriminator.trainable_variables) d_optimizer.apply_gradients(zip(grads, discriminator.trainable_variables)) # 2. 训练生成器 # 采样隐藏向量 batch_z = tf.random.normal([batch_size, z_dim]) # 生成器前向计算 with tf.GradientTape() as tape: g_loss = g_loss_fn(generator, discriminator, batch_z, is_training) grads = tape.gradient(g_loss, generator.trainable_variables) g_optimizer.apply_gradients(zip(grads, generator.trainable_variables)) if epoch % 100 == 0: print(epoch, 'd-loss:', float(d_loss), 'g-loss:', float(g_loss)) # 可视化 z = tf.random.normal([100, z_dim]) fake_image = generator(z, training=False) img_path = os.path.join('gan_images', 'gan-%d.png' % epoch) save_result(fake_image.numpy(), 10, img_path, color_mode='P') d_losses.append(float(d_loss)) g_losses.append(float(g_loss)) if epoch % 10000 == 1: generator.save_weights('generator.ckpt') discriminator.save_weights('discriminator.ckpt')
def train_style_transfer(args): if not (args.train_data and args.valid_data): print("must chose train_data and valid_data") sys.exit() # make dataset trans = transforms.ToTensor() train_dataset = FaceDataset(args.train_data, transform=trans) label_dict = train_dataset.get_label_dict() valid_dataset = FaceDataset(args.valid_data, transform=trans) valid_dataset.give_label_dict(label_dict) train_loader = data_utils.DataLoader(train_dataset, batch_size=args.batch_size, shuffle=True, num_workers=1) valid_loader = data_utils.DataLoader(valid_dataset, batch_size=args.batch_size, shuffle=True, num_workers=1) train_size = len(train_dataset) valid_size = len(valid_dataset) loaders = {"train": train_loader, "valid": valid_loader} dataset_sizes = {"train": train_size, "valid": valid_size} if args.gpu: device = torch.device("cuda" if torch.cuda.is_available() else "cpu") else: device = torch.device("cpu") # make network if args.model_type == "VAE": net = Autoencoder(train_dataset.label_num()).to(device) optimizer = optim.Adam(net.parameters(), lr=args.lr, weight_decay=args.weight_decay) best_model_wts = net.state_dict() best_loss = 1e10 if args.generator_model and os.path.exists(args.generator_model): net.load_state_dict(torch.load(args.generator_model)) elif args.model_type == "VAEGAN": generator = Autoencoder(train_dataset.label_num()).to(device) discriminator = Discriminator().to(device) classifier = Classifier(train_dataset.label_num()).to(device) generator_optimizer = optim.Adam(generator.parameters(), lr=args.lr, weight_decay=args.weight_decay) discriminator_optimizer = optim.Adam(discriminator.parameters(), lr=args.lr * 0.1, weight_decay=args.weight_decay) best_generator_wts = generator.state_dict() best_discriminator_wts = discriminator.state_dict() best_generator_loss = 1e10 best_discriminator_loss = 1e10 if args.generator_model and os.path.exists(args.generator_model): generator.load_state_dict(torch.load(args.generator_model)) if args.discriminator_model and os.path.exists( args.discriminator_model): discriminator.load_state_dict(torch.load(args.discriminator_model)) if args.classifier_model: classifier.load_state_dict(torch.load(args.classifier_model)) # make loss function and optimizer criterion = nn.BCELoss(reduction="sum") classifier_criterion = nn.CrossEntropyLoss(reduction="sum") # initialize loss loss_history = {"train": [], "valid": []} # start training start_time = time.time() for epoch in range(args.epochs): print("epoch {}".format(epoch + 1)) for phase in ["train", "valid"]: if phase == "train": if args.model_type == "VAE": net.train(True) elif args.model_type == "VAEGAN": generator.train(True) discriminator.train(True) else: if args.model_type == "VAE": net.train(False) elif args.model_type == "VAEGAN": generator.train(False) discriminator.train(False) # initialize running loss generator_running_loss = 0.0 discriminator_running_loss = 0.0 for i, data in enumerate(loaders[phase]): inputs, label = data # wrap the in valiables if phase == "train": inputs = Variable(inputs).to(device) label = Variable(label).to(device) torch.set_grad_enabled(True) else: inputs = Variable(inputs).to(device) label = Variable(label).to(device) torch.set_grad_enabled(False) # zero gradients if args.model_type == "VAE": optimizer.zero_grad() mu, var, outputs = net(inputs, label) loss = loss_func(inputs, outputs, mu, var) if phase == "train": loss.backward() optimizer.step() generator_running_loss += loss.item() elif args.model_type == "VAEGAN": real_label = Variable( torch.ones((inputs.size()[0], 1), dtype=torch.float) - 0.2 * (torch.rand(inputs.size()[0], 1))).to(device) fake_label = Variable( torch.zeros((inputs.size()[0], 1), dtype=torch.float) + 0.2 * (torch.rand(inputs.size()[0], 1))).to(device) discriminator_optimizer.zero_grad() real_pred = discriminator(inputs) real_loss = criterion(real_pred, real_label) random_index = np.random.randint(0, train_dataset.label_num(), inputs.size()[0]) generate_label = Variable( torch.zeros_like(label)).to(device) for i, index in enumerate(random_index): generate_label[i][index] = 1 mu, var, outputs = generator(inputs, label) fake_pred = discriminator(outputs.detach()) fake_loss = criterion(fake_pred, fake_label) discriminator_loss = real_loss + fake_loss if phase == "train": discriminator_loss.backward() discriminator_optimizer.step() generator_optimizer.zero_grad() #class_loss = classifier_criterion(classifier(outputs), torch.max(label, 1)[1]) dis_loss = criterion(discriminator(outputs), real_label) gen_loss = loss_func(inputs, outputs, mu, var) generator_loss = dis_loss + gen_loss if phase == "train": generator_loss.backward() generator_optimizer.step() discriminator_running_loss += discriminator_loss.item() generator_running_loss += generator_loss.item() if args.model_type == "VAE": epoch_loss = generator_running_loss / dataset_sizes[ phase] * args.batch_size loss_history[phase].append(epoch_loss) print("{} loss {:.4f}".format(phase, epoch_loss)) if phase == "valid" and epoch_loss < best_loss: best_model_wts = net.state_dict() best_loss = epoch_loss elif args.model_type == "VAEGAN": epoch_generator_loss = generator_running_loss / dataset_sizes[ phase] * args.batch_size epoch_discriminator_loss = discriminator_running_loss / dataset_sizes[ phase] * args.batch_size print("{} generator loss {:.4f}".format( phase, epoch_generator_loss)) print("{} discriminator loss {:.4f}".format( phase, epoch_discriminator_loss)) if phase == "valid" and epoch_generator_loss < best_generator_loss: best_generator_wts = generator.state_dict() best_generator_loss = epoch_generator_loss if phase == "valid" and epoch_discriminator_loss < best_discriminator_loss: best_discriminator_wts = discriminator.state_dict() best_generator_loss = epoch_discriminator_loss elapsed_time = time.time() - start_time print("training complete in {:.0f}s".format(elapsed_time)) if args.model_type == "VAE": net.load_state_dict(best_model_wts) return net, label_dict elif args.model_type == "VAEGAN": generator.load_state_dict(best_generator_wts) discriminator.load_state_dict(best_discriminator_wts) return (generator, discriminator), label_dict
# root_path = "C:/Users/Lucky/PycharmProjects/finalproject/" # data_path = root_path + "face/" # work_path = root_path + "workspace/" # save_path = root_path + "workspace/new_picture/" # os.makedirs(work_path, exist_ok=True) # os.makedirs(save_path, exist_ok=True) root_path = "/Users/yuming/OneDrive/sync/semester/ML/hw/project/dataset/" data_path = root_path + "faces/" save_path = root_path + "fake-faces/" save_csv_loss_g = root_path + "csv/loss_g.csv" save_csv_loss_d = root_path + "csv/loss_d.csv" # Initialize generator and discriminator generator = Generator(opt.latent_dim, img_shape) discriminator = Discriminator(img_shape) cuda_enabled = torch.cuda.is_available() if cuda_enabled: generator.cuda() discriminator.cuda() Tensor = torch.cuda.FloatTensor else: Tensor = torch.FloatTensor # Tensor = torch.cuda.FloatTensor if cuda_enabled else torch.FloatTensor image_raw_data = fetch_dataset(data_path) data_loader = DataLoader(image_raw_data, batch_size=opt.batch_size, shuffle=True)
round(validation_split * len(dataset)) ] split = { x: y for x, y in zip(['train', 'val'], data.random_split(dataset, lengths=split_len)) } loader = { x: data.DataLoader(split[x], batch_size, shuffle=True, num_workers=4) for x in ['train', 'val'] } # Create the GAN netG = Generator(latent_size, n_features) netD = Discriminator(n_features) netG.to(device) netD.to(device) optimizerD = torch.optim.Adam(netD.parameters(), lr, betas) optimizerG = torch.optim.Adam(netG.parameters(), lr, betas) # Define penalties class RoundNoGradient(torch.autograd.Function): @staticmethod def forward(ctx, x): return x.round() @staticmethod def backward(ctx, g):
class GAN_Vanilla(object): def __init__(self, batch_size=16, D_lr=1e-3, G_lr=1e-3, r_dim=3072, z_dim=100, h_size=512, use_gpu=False): self.device = torch.device("cuda" if use_gpu else "cpu") self.batch_size = batch_size self.z_dim = z_dim self.D = Discriminator(r_dim, h_size).to(device=self.device) self.G = Generator(z_dim, r_dim, h_size).to(device=self.device) self.criterion = nn.BCELoss(size_average=True) self.D_optimizer = optim.Adam(self.D.parameters(), lr=D_lr) self.G_optimizer = optim.Adam(self.G.parameters(), lr=G_lr) root = '/home/lhq/PycharmProjects/gan.pytorch/datasets/data/test/' text = '/home/lhq/PycharmProjects/gan.pytorch/datasets/data/labels.txt' dataset = ListDatasets(root=root, fname_list=text) self.dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True) self.label_real = torch.ones(batch_size, 1, device=self.device) self.label_fake = torch.zeros(batch_size, 1, device=self.device) def train(self, epoch, step_D, step_G, print_every): for ep in range(epoch): d_loss, g_loss = 0, 0 for i, data in enumerate(self.dataloader): data = data.view(self.batch_size, -1) if i % step_D == 0: real_output = self.D(data) d_loss_1 = self.criterion(real_output, self.label_real) z = get_uniform_sampler(self.device)(self.batch_size, self.z_dim) fake_image = self.G(z).detach() fake_output = self.D(fake_image) d_loss_2 = self.criterion(fake_output, self.label_fake) d_loss = d_loss_1 + d_loss_2 self.D_optimizer.zero_grad() d_loss.backward() self.D_optimizer.step() if i % step_G == 0: z = get_normal_sampler(0, 0.1, self.device)(self.batch_size, self.z_dim) fake_image = self.G(z) fake_output = self.D(fake_image) g_loss = self.criterion(fake_output, self.label_real) self.G_optimizer.zero_grad() g_loss.backward() self.G_optimizer.step() print('{} epoch: D_loss: {}, G_loss: {}'.format( ep, d_loss[0].item(), g_loss[0].item())) if ep % print_every == 0: print('{} epoch: D_loss: {}, G_loss: {}'.format( ep, d_loss[0].item(), g_loss[0].item()))
def clip(a): return 0 if a<0 else (255 if a>255 else a) def array_to_img(im): im = im*255 im = np.vectorize(clip)(im).astype(np.uint8) im=im.transpose(1,2,0) img=Image.fromarray(im) return img def save_img(img_array,save_path): #save from np.array (3,height,width) img = array_to_img(img_array) img.save(save_path) Gen = Generator() Dis = Discriminator() gpu = -1 if gpu>=0: xp = cuda.cupy cuda.get_device(gpu).use() Gen.to_gpu() Dis.to_gpu() else: xp = np optG = Gen.make_optimizer() optD = Dis.make_optimizer() optG.setup(Gen) optD.setup(Dis)
style_size = 564 sample_batch = 4 z_size = 400 ######################################### def gen_rand_noise(batch_size, z_size, mean=0, std=0.001): z_sample = np.random.normal(mean, std, size=[batch_size, z_size]).astype(np.float32) z = torch.from_numpy(z_sample) return z ######################################### G = Generator().cuda() D = Discriminator().cuda() Vgg = Vgg16().cuda() bce = BCE_Loss() mse = torch.nn.MSELoss() optimizer_d = torch.optim.SGD(D.parameters(), lr=lr * 0.4) optimizer_g = torch.optim.Adam(G.parameters(), lr=lr, betas=(beta1, 0.9)) ######################################### ######################################### for epoch in range(max_epoch): for idx, (real_img, real_label, mask) in tqdm.tqdm(enumerate(trainloader)): # trainD make_trainable(D, True) make_trainable(G, False) D.zero_grad() optimizer_d.zero_grad()
modules = list( feature_extractor_network.children())[:-1] #Remove fully connected layer modules.append(nn.Flatten()) feature_extractor_network = nn.Sequential(*modules) feature_extractor_network = feature_extractor_network.to(device) feature_extractor_network.load_state_dict( torch.load(args.fe_model, map_location=device)) feature_extractor_network = feature_extractor_network.eval() #Setup networks generator_network = Generator(in_noise=args.noise_dem, in_features=args.feature_dem, ch=args.ch_multi, norm_type=args.norm_type).to(device) discriminator_network = Discriminator(channels=novel_images.shape[1], in_features=args.feature_dem, ch=args.ch_multi, norm_type=args.norm_type).to(device) #Setup optimizers d_optimizer = optim.Adam(discriminator_network.parameters(), lr=args.dlr, betas=(0.0, 0.999)) g_optimizer = optim.Adam(generator_network.parameters(), lr=args.glr, betas=(0.0, 0.999)) #Create the save directory if it does note exist if not os.path.isdir(args.save_dir + "/Models"): os.makedirs(args.save_dir + "/Models") if not os.path.isdir(args.save_dir + "/Samples"): os.makedirs(args.save_dir + "/Samples")
m = 3 return np.random.normal(size=[Nbatch, m, dim]).astype(dtype=np.float32) # ------- TRAIN MODELS -------- num_epochs = config_d['epochs'] print_every = config_d['print_every'] z_size = config_d['noise_dim'] n_critic = config_d['critic_iter'] lr = config_d['lr'] beta1 = config_d['beta1'] beta2 = config_d['beta2'] D = Discriminator() G = Generator(z_size=z_size) print(D) print(G) if cuda: G.cuda() D.cuda() print('GPU available for training. Models moved to GPU') else: print('Training on CPU.') d_optimizer = optim.Adam(D.parameters(), lr=lr, betas=[beta1, beta2]) g_optimizer = optim.Adam(G.parameters(), lr=lr, betas=[beta1, beta2]) losses_train = []
def main(): tf.random.set_seed(22) np.random.seed(22) os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' assert tf.__version__.startswith('2.') # hyper parameters z_dim = 100 epochs = 3000000 batch_size = 128 learning_rate = 0.0002 is_training = True # for validation purpose assets_dir = './images' if not os.path.isdir(assets_dir): os.makedirs(assets_dir) val_block_size = 10 val_size = val_block_size * val_block_size # load mnist data (x_train, _), (x_test, _) = keras.datasets.mnist.load_data() x_train = x_train.astype(np.float32) / 255. db = tf.data.Dataset.from_tensor_slices(x_train).shuffle( batch_size * 4).batch(batch_size).repeat() db_iter = iter(db) inputs_shape = [-1, 28, 28, 1] # create generator & discriminator generator = Generator() generator.build(input_shape=(batch_size, z_dim)) generator.summary() discriminator = Discriminator() discriminator.build(input_shape=(batch_size, 28, 28, 1)) discriminator.summary() # prepare optimizer d_optimizer = keras.optimizers.Adam(learning_rate=learning_rate, beta_1=0.5) g_optimizer = keras.optimizers.Adam(learning_rate=learning_rate, beta_1=0.5) for epoch in range(epochs): # no need labels batch_x = next(db_iter) # rescale images to -1 ~ 1 batch_x = tf.reshape(batch_x, shape=inputs_shape) # -1 - 1 batch_x = batch_x * 2.0 - 1.0 # Sample random noise for G batch_z = tf.random.uniform(shape=[batch_size, z_dim], minval=-1., maxval=1.) with tf.GradientTape() as tape: d_loss = d_loss_fn(generator, discriminator, batch_z, batch_x, is_training) grads = tape.gradient(d_loss, discriminator.trainable_variables) d_optimizer.apply_gradients( zip(grads, discriminator.trainable_variables)) with tf.GradientTape() as tape: g_loss = g_loss_fn(generator, discriminator, batch_z, is_training) grads = tape.gradient(g_loss, generator.trainable_variables) g_optimizer.apply_gradients(zip(grads, generator.trainable_variables)) if epoch % 100 == 0: print(epoch, 'd loss:', float(d_loss), 'g loss:', float(g_loss)) # validation results at every epoch val_z = np.random.uniform(-1, 1, size=(val_size, z_dim)) fake_image = generator(val_z, training=False) image_fn = os.path.join('images', 'gan-val-{:03d}.png'.format(epoch + 1)) save_result(fake_image.numpy(), val_block_size, image_fn, color_mode='L')
class Solver(object): """Solver for training and testing StarGAN.""" def __init__(self, data_loader, config): """Initialize configurations.""" # Data loader. self.data_loader = data_loader # Model configurations. self.c_dim = config.c_dim self.image_size = config.image_size self.g_conv_dim = config.g_conv_dim self.d_conv_dim = config.d_conv_dim self.g_repeat_num = config.g_repeat_num self.d_repeat_num = config.d_repeat_num self.lambda_app = config.lambda_app self.lambda_pose = config.lambda_pose self.lambda_rec = config.lambda_rec self.lambda_gp = config.lambda_gp # Training configurations. # self.dataset = config.dataset self.batch_size = config.batch_size self.num_iters = config.num_iters self.num_iters_decay = config.num_iters_decay self.g_lr = config.g_lr self.d_lr = config.d_lr self.n_critic = config.n_critic self.beta1 = config.beta1 self.beta2 = config.beta2 self.resume_iters = config.resume_iters # self.selected_attrs = config.selected_attrs # loss functions self.feature_loss = torch.nn.CosineEmbeddingLoss() self.pose_loss = torch.nn.PairwiseDistance() # Test configurations. self.test_iters = config.test_iters # Miscellaneous. self.use_tensorboard = config.use_tensorboard self.device = torch.device( 'cuda' if torch.cuda.is_available() else 'cpu') # Directories. self.log_dir = config.log_dir self.sample_dir = config.sample_dir self.model_save_dir = config.model_save_dir self.result_dir = config.result_dir # Step size. self.log_step = config.log_step self.sample_step = config.sample_step self.model_save_step = config.model_save_step self.lr_update_step = config.lr_update_step # Build the model and tensorboard. self.build_model() if self.use_tensorboard: self.build_tensorboard() def build_model(self): """Create a generator and a discriminator.""" self.G = Generator(self.g_conv_dim, self.c_dim, self.g_repeat_num) self.D = Discriminator(self.image_size, self.d_conv_dim, self.c_dim, self.d_repeat_num) self.g_optimizer = torch.optim.Adam(self.G.parameters(), self.g_lr, [self.beta1, self.beta2]) self.d_optimizer = torch.optim.Adam(self.D.parameters(), self.d_lr, [self.beta1, self.beta2]) self.print_network(self.G, 'G') self.print_network(self.D, 'D') self.G.to(self.device) self.D.to(self.device) """Build the feature extractor""" self.feature_model = f_model(model_path=DUMPED_MODEL, freeze_param=True).cuda() #.cuda() self.feature_model.eval() def print_network(self, model, name): """Print out the network information.""" num_params = 0 for p in model.parameters(): num_params += p.numel() print(model) print(name) print("The number of parameters: {}".format(num_params)) def restore_model(self, resume_iters): """Restore the trained generator and discriminator.""" print( 'Loading the trained models from step {}...'.format(resume_iters)) G_path = os.path.join(self.model_save_dir, '{}-G.ckpt'.format(resume_iters)) D_path = os.path.join(self.model_save_dir, '{}-D.ckpt'.format(resume_iters)) self.G.load_state_dict( torch.load(G_path, map_location=lambda storage, loc: storage)) self.D.load_state_dict( torch.load(D_path, map_location=lambda storage, loc: storage)) def build_tensorboard(self): """Build a tensorboard logger.""" from logger import Logger self.logger = Logger(self.log_dir) def update_lr(self, g_lr, d_lr): """Decay learning rates of the generator and discriminator.""" for param_group in self.g_optimizer.param_groups: param_group['lr'] = g_lr for param_group in self.d_optimizer.param_groups: param_group['lr'] = d_lr def reset_grad(self): """Reset the gradient buffers.""" self.g_optimizer.zero_grad() self.d_optimizer.zero_grad() def normalize(self, x): pass def denorm(self, x): """Convert the range from [-1, 1] to [0, 1].""" out = (x + 1) / 2 return out.clamp_(0, 1) def gradient_penalty(self, y, x): """Compute gradient penalty: (L2_norm(dy/dx) - 1)**2.""" weight = torch.ones(y.size()).to(self.device) dydx = torch.autograd.grad(outputs=y, inputs=x, grad_outputs=weight, retain_graph=True, create_graph=True, only_inputs=True)[0] dydx = dydx.view(dydx.size(0), -1) dydx_l2norm = torch.sqrt(torch.sum(dydx**2, dim=1)) return torch.mean((dydx_l2norm - 1)**2) pass def feat_extract(self, resized_data): # input: N * 3 * 224 * 224 resized_data = resized_data.to(self.device) #.cuda() # output: N * num_classes, N * inter_dim, N * C' * 7 * 7 out, inter_out, x = self.feature_model(resized_data) # return batch feature vector return out.clamp_(0, 1) def pose_extract(self, batch_img, bbx=None): # input [N, 224, 224, 3] in RGB # output [N, 2, 18] # permute = [2, 1, 0] # # crop_batch = np.zeros(shape=()) # for i in range(batch_img.shape[0]): # img = crop(batch_img[i], bbx[i]) # # RGB TO BGR # img = img[:, permute, : , :] # crop_batch[i, :, :] = img # pose_vectors = get_batch_body_vector(crop_batch.numpy()) batch_img = np.transpose(batch_img, (0, 2, 3, 1)) # print(batch_img.shape) pose_vectors = get_batch_body_vector(batch_img) pose_vectors = torch.from_numpy(pose_vectors) return pose_vectors def appreance_cos_similarity(self, feat_fake, feat_real): label = torch.ones(feat_real.shape[0]).cuda() cos_simi = self.feature_loss(feat_real, feat_fake, label) return cos_simi def compute_pose_loss(self, pose_fake, pose_real, mask): mask = mask.view(-1, 1, 18).double() pose_fake = torch.mul(pose_fake, mask) pose_real = torch.mul(pose_real.double(), mask) distance = self.pose_loss(pose_real, pose_fake) return distance.float().sum() def train(self): """Train StarGAN within a single dataset.""" # Fetch fixed inputs for debugging. data_iter = iter(self.data_loader) a_fixed, b_fixed, bbox_fixed, b_fixed_pose_feat, mask_fixed = next( data_iter) a_fixed = a_fixed.to(self.device) b_fixed = b_fixed.to(self.device) bbox_fixed = bbox_fixed.to(self.device) # c_fixed_list = self.create_labels(c_org, self.c_dim, self.dataset, self.selected_attrs) # Learning rate cache for decaying. g_lr = self.g_lr d_lr = self.d_lr # Start training from scratch or resume training. start_iters = 0 if self.resume_iters: start_iters = self.resume_iters self.restore_model(self.resume_iters) # Start training. print('Start training...') start_time = time.time() for step in range(start_iters, self.num_iters): # =================================================================================== # # 1. Preprocess input data # # =================================================================================== # # Fetch real images and labels. try: a_real, b_real, bbox, b_pose_feat, mask = next(data_iter) except: data_iter = iter(self.data_loader) a_real, b_real, bbox, b_pose_feat, mask = next(data_iter) a_real = a_real.to(self.device) # Input images. b_real = b_real.to(self.device) bbox = bbox.to(self.device) b_pose_feat = b_pose_feat.to(self.device) mask = mask.to(self.device) # extract appearance feature a_app_feat = self.feat_extract(a_real) a_app_feat = a_app_feat.to(self.device) # # extract pose feature # b_pose_feat = self.pose_extract(b_real) # =================================================================================== # # 2. Train the discriminator # # =================================================================================== # # Compute loss with real images. out_src = self.D(b_real) d_loss_real = -torch.mean(out_src) # d_loss_cls = self.classification_loss(out_cls, label_org, self.dataset) # Compute loss with fake images. # con_feat = torch.cat([a_app_feat, bbox/416.0], dim=1) con_feat = a_app_feat x_fake = self.G(b_real, con_feat) out_src = self.D(x_fake.detach()) d_loss_fake = torch.mean(out_src) # fake_app_feat = self.feat_extract(x_fake) # fake_pose_feat = self.pose_extract(x_fake, bbox) # d_loss_app = self.appreance_cos_similarity(fake_app_feat, a_app_feat) # d_loss_pose = - self.pose_loss(fake_pose_feat, b_pose_feat) # Compute loss for gradient penalty. alpha = torch.rand(b_real.size(0), 1, 1, 1).to(self.device) x_hat = (alpha * b_real.data + (1 - alpha) * x_fake.data).requires_grad_(True) out_src = self.D(x_hat) d_loss_gp = self.gradient_penalty(out_src, x_hat) # Backward and optimize. # d_loss = d_loss_real + d_loss_fake + self.lambda_app * d_loss_cls + self.lambda_gp * d_loss_gp # d_loss = d_loss_fake + d_loss_real + self.lambda_app * d_loss_app + self.lambda_pose * d_loss_pose # d_loss = d_loss_fake + d_loss_real + self.lambda_gp * d_loss_gp d_loss = d_loss_fake + d_loss_real + self.lambda_gp * d_loss_gp self.reset_grad() d_loss.backward() self.d_optimizer.step() # Logging. loss = {} loss['D/loss_real'] = d_loss_real.item() loss['D/loss_fake'] = d_loss_fake.item() # loss['D/loss_app'] = d_loss_app.item() # loss['D/loss_pose'] = d_loss_pose.item() loss['D/loss_gp'] = d_loss_gp.item() # =================================================================================== # # 3. Train the generator # # =================================================================================== # if (step + 1) % self.n_critic == 0: # Original-to-target domain. x_fake = self.G(b_real, con_feat) # print(x_fake[0,:,200:205,200:205]) out_src = self.D(x_fake) g_loss_fake = -torch.mean(out_src) crop_batch = torch.zeros((x_fake.shape[0], 3, 224, 224)) b = bbox.detach().cpu().numpy().astype(int) for i in range(x_fake.shape[0]): # img = crop(x_fake[i], bbox[i]) x1, x2, y1, y2 = b[i, 0], b[i, 0] + b[i, 2], b[ i, 1], b[i, 1] + b[i, 3] x1 = min(max(x1, 0), 416) x2 = min(max(x2, 0), 416) y1 = min(max(y1, 0), 416) y2 = min(max(y2, 0), 416) img = x_fake[i, :, x1:x2, y1:y2].cpu().data.numpy() img = img.transpose((1, 2, 0)) resized_img = np.zeros(shape=(224, 224, 3)) resized_img = cv2.resize(img, (224, 224), interpolation=cv2.INTER_AREA) crop_batch[i, :, :, :] = torch.from_numpy( resized_img.transpose((2, 0, 1))) fake_app_feat = self.feat_extract(crop_batch) fake_pose_feat = self.pose_extract(crop_batch.numpy()) # #**** debug ****# # fake_images = (x_fake.cpu().data).numpy() # permute = [2, 1, 0] # fake_images = fake_images[:, permute, :, :].transpose((0,2,3,1)) # resized_data = np.zeros(shape=(fake_images.shape[0], 224, 224, 3)) # for j in range(fake_images.shape[0]): # resized_data[j,:,:,:] = cv2.resize(fake_images[j,:,:,:], (224, 224), interpolation = cv2.INTER_AREA) # resized_data = np.transpose(resized_data, (0, 3, 1, 2)) # resized_tensor = torch.from_numpy(resized_data) # resized_tensor = resized_tensor.to(self.device, dtype=torch.float) # fake_app_feat = self.feat_extract(resized_tensor) # fake_pose_feat = self.pose_extract(resized_data, bbox) fake_app_feat = fake_app_feat.to(self.device) fake_pose_feat = fake_pose_feat.to(self.device) #**** debug ****# # g_loss_cls = self.classification_loss(out_cls, label_trg, self.dataset) g_loss_app = -self.appreance_cos_similarity( fake_app_feat, a_app_feat) # -similarity # print(fake_pose_feat.size(), b_pose_feat.size(), mask.size()) g_loss_pose = self.compute_pose_loss(fake_pose_feat, b_pose_feat, mask) # joints distance # Backward and optimize. # g_loss = g_loss_fake + self.lambda_rec * g_loss_rec + self.lambda_app * g_loss_cls # g_loss = g_loss_fake + self.lambda_app * g_loss_app + self.lambda_pose * g_loss_pose g_loss = g_loss_fake + self.lambda_app * g_loss_app + self.lambda_pose * g_loss_pose self.reset_grad() g_loss.backward() self.g_optimizer.step() # Logging. loss['G/loss_fake'] = g_loss_fake.item() # loss['G/loss_rec'] = g_loss_rec.item() loss['G/loss_app'] = g_loss_app.item() * self.lambda_app loss['G/loss_pose'] = g_loss_pose.item() * self.lambda_pose # =================================================================================== # # 4. Miscellaneous # # =================================================================================== # # Print out training information. if (step + 1) % self.log_step == 0: et = time.time() - start_time et = str(datetime.timedelta(seconds=et))[:-7] log = "Elapsed [{}], Iteration [{}/{}]".format( et, step + 1, self.num_iters) for tag, value in loss.items(): log += ", {}: {:.4f}".format(tag, value) print(log) if self.use_tensorboard: for tag, value in loss.items(): self.logger.scalar_summary(tag, value, i + 1) # Translate fixed images for debugging. if (step + 1) % self.sample_step == 0: # if (step + 1) % 1 == 0: with torch.no_grad(): # a fix: [N, 3, 224, 224] # a_real, b_real, bbox, b_pose_feat, mask a_resized = torch.zeros(size=(a_real.shape[0], 3, 416, 416)) b_drawed = torch.zeros(size=(a_real.shape[0], 3, 416, 416)) for i in range(a_real.shape[0]): img = a_real[i].cpu().data.numpy() img = img.transpose((1, 2, 0)) resized_img = np.zeros(shape=(416, 416, 3)) resized_img = cv2.resize(img, (416, 416), interpolation=cv2.INTER_AREA) a_resized[i, :, :, :] = torch.from_numpy( resized_img.transpose((2, 0, 1))) trans1 = transforms.ToPILImage() trans2 = transforms.ToTensor() b_img = trans1(b_real[i].cpu()) draw = ImageDraw.Draw(b_img) b = bbox[i].cpu().data.numpy().astype(int) x, y, w, h = b x2, y2 = x + w, y + h draw.rectangle([x, y, x2, y2], outline="green", width=20) b_drawed[i, :, :, :] = trans2(b_img) b_drawed = b_drawed.to(self.device) a_resized = a_resized.to(self.device) picture_list = [a_resized, b_drawed] a_visual_feat = self.feat_extract(a_real) # a feature: [N, 20]; bbox: [N,4] # con_visual_feat = torch.cat([a_visual_feat, bbox/416.0], dim=1) # [N, 24] con_visual_feat = a_visual_feat # print(b_real, con_visual_feat) x_fake = self.G(b_real, con_visual_feat) # [N, 3, 416, 416] # print(a_fixed.size(), b_fixed.size(), x_fake.size()) picture_list.append(x_fake) picture_concat = torch.cat(picture_list, dim=0) # print(picture_concat.size()) sample_path = os.path.join( self.sample_dir, '{}-images.jpg'.format(step + 1)) save_image(self.denorm(picture_concat.data.cpu()), sample_path, nrow=4, padding=0) print('Saved real and fake images into {}...'.format( sample_path)) # Save model checkpoints. if (step + 1) % self.model_save_step == 0: G_path = os.path.join(self.model_save_dir, '{}-G.ckpt'.format(step + 1)) D_path = os.path.join(self.model_save_dir, '{}-D.ckpt'.format(step + 1)) torch.save(self.G.state_dict(), G_path) torch.save(self.D.state_dict(), D_path) print('Saved model checkpoints into {}...'.format( self.model_save_dir)) # Decay learning rates. if (step + 1) % self.lr_update_step == 0 and (step + 1) > ( self.num_iters - self.num_iters_decay): g_lr -= (self.g_lr / float(self.num_iters_decay)) d_lr -= (self.d_lr / float(self.num_iters_decay)) self.update_lr(g_lr, d_lr) print('Decayed learning rates, g_lr: {}, d_lr: {}.'.format( g_lr, d_lr)) def test(self): """Translate images using StarGAN trained on a single dataset.""" # Load the trained generator. self.restore_model(self.test_iters) # Set data loader. data_loader = self.data_loader with torch.no_grad(): for i, (a_real, b_real) in enumerate(data_loader): # Prepare input images and target domain labels. a_real = a_real.to(self.device) b_real = b_real.to(self.device) # Translate images. a_fake_list = [a_real, b_real] a_fixed_feat = self.feat_extract(a_real) a_fake_list.append(self.G(b_real, a_fixed_feat)) # Save the translated images. x_concat = torch.cat(a_fake_list, dim=3) result_path = os.path.join(self.result_dir, '{}-images.jpg'.format(i + 1)) save_image(self.denorm(x_concat.data.cpu()), result_path, nrow=1, padding=0) print('Saved real and fake images into {}...'.format( result_path))
def main(): parser = argparse.ArgumentParser(description='DCGAN for mnist') parser.add_argument('--batchsize', '-b', type=int, default=1, help='# of each mini-batch size') parser.add_argument('--epoch', '-e', type=int, default=500, help='# of epoch') parser.add_argument( '--gpu', '-g', type=int, default=-1, help='GPU ID (if you want to use gpu, set positive value)') parser.add_argument('--dataset', '-d', type=str, default='', help='path of training dataset path.') parser.add_argument('--out_dir', '-o', type=str, default='result', help='path of output the result.') parser.add_argument('--n_hidden', '-n', type=int, default=100, help='# of hidden unit(z)') args = parser.parse_args() logger = set_logger() logger.debug('=' * 10) logger.debug('GPU: {}'.format(args.gpu)) logger.debug('#batchsize: {}'.format(args.batchsize)) logger.debug('#epoch: {}'.format(args.epoch)) logger.debug('n_hidden: {}'.format(args.n_hidden)) logger.debug('dataset: {}'.format(args.dataset)) logger.debug('out_dir: {}'.format(args.out_dir)) logger.debug('=' * 10) print() logger.debug('setup models') # Setup networks generator = Generator(z_dim=args.n_hidden) discriminator = Discriminator(z_dim=args.n_hidden) if args.gpu >= 0: chainer.backends.cuda.get_device_from_id(args.gpu).use() generator.to_gpu() discriminator.to_gpu() # Setup optimizers def make_optimizer(model, alpha=0.0002, beta1=0.5): optimizer = chainer.optimizers.Adam(alpha=alpha, beta1=beta1) optimizer.setup(model) optimizer.add_hook(chainer.optimizer_hooks.WeightDecay(0.0001), 'hook_dec') return optimizer opt_generator = make_optimizer(generator) opt_discriminator = make_optimizer(discriminator) if args.dataset == '': train, _ = chainer.datasets.get_mnist(withlabel=False, ndim=3, scale=255.) else: pass # Setup an iterator train_iter = chainer.iterators.SerialIterator(train, args.batchsize) # Setup an Updater updater = GANUpdater(models=(generator, discriminator), iterator=train_iter, optimizer={ 'gen': opt_generator, 'dis': opt_discriminator }, device=args.gpu) # Setup a trainer trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out_dir) snapshot_interval = (1000, 'iteration') display_interval = (100, 'iteration') trainer.extend( extensions.snapshot(filename='snapshot_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( generator, 'generator_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.snapshot_object( discriminator, 'discriminator_iter_{.updater.iteration}.npz'), trigger=snapshot_interval) trainer.extend(extensions.LogReport(trigger=snapshot_interval)) trainer.extend(extensions.PrintReport([ 'epoch', 'iteration', 'gen/loss', 'dis/loss', ]), trigger=display_interval) trainer.extend(extensions.ProgressBar(update_interval=10)) # Start train logger.debug('Training Start.') print() print() trainer.run()
# 1. what happens if you use larger networks? # 2. better normalization with Batch Normalization algorithm # 3. different learning rates (is there a better one?) # 4. change architecture to a CNN # Hyperparameters etc. device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') lr = 3e-4 z_dim = 64 # 128, 256 image_dim = 28 * 28 * 1 # 784 batch_size = 32 num_epochs = 200 disc_model_file = 'parameters/gan_mnist/discriminator.pth' gen_model_file = 'parameters/gan_mnist/generator.pth' disc = Discriminator(image_dim).to(device) gen = Generator(z_dim, image_dim).to(device) load_model(disc, disc_model_file, device) load_model(gen, gen_model_file, device) fixed_noise = torch.randn((batch_size, z_dim)).to(device) transforms = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5), (0.5))]) dataset = datasets.MNIST(root='../datasets/', transform=transforms, download=True) loader = DataLoader(dataset, batch_size=batch_size, shuffle=True) opt_disc = optim.Adam(disc.parameters(), lr=lr) opt_gen = optim.Adam(gen.parameters(), lr=lr) criterion = nn.BCELoss()
def main(): tf.random.set_seed(233) np.random.seed(233) z_dim = 100 epochs = 3000000 batch_size = 512 learning_rate = 2e-4 # ratios = D steps:G steps ratios = 2 img_path = glob.glob(os.path.join('faces', '*.jpg')) dataset, img_shape, _ = make_anime_dataset(img_path, batch_size) print(dataset, img_shape) sample = next(iter(dataset)) print(sample.shape, tf.reduce_max(sample).numpy(), tf.reduce_min(sample).numpy()) dataset = dataset.repeat() db_iter = iter(dataset) generator = Generator() generator.build(input_shape=(None, z_dim)) # generator.load_weights(os.path.join('checkpoints', 'generator-5000')) discriminator = Discriminator() discriminator.build(input_shape=(None, 64, 64, 3)) # discriminator.load_weights(os.path.join('checkpoints', 'discriminator-5000')) g_optimizer = tf.optimizers.Adam(learning_rate, beta_1=0.5) d_optimizer = tf.optimizers.Adam(learning_rate, beta_1=0.5) # a fixed noise for sampling z_sample = tf.random.normal([100, z_dim]) g_loss_meter = keras.metrics.Mean() d_loss_meter = keras.metrics.Mean() gp_meter = keras.metrics.Mean() for epoch in range(epochs): # train D for step in range(ratios): batch_z = tf.random.normal([batch_size, z_dim]) batch_x = next(db_iter) with tf.GradientTape() as tape: d_loss, gp = d_loss_fn(generator, discriminator, batch_z, batch_x) d_loss_meter.update_state(d_loss) gp_meter.update_state(gp) gradients = tape.gradient(d_loss, discriminator.trainable_variables) d_optimizer.apply_gradients( zip(gradients, discriminator.trainable_variables)) # train G batch_z = tf.random.normal([batch_size, z_dim]) with tf.GradientTape() as tape: g_loss = g_loss_fn(generator, discriminator, batch_z) g_loss_meter.update_state(g_loss) gradients = tape.gradient(g_loss, generator.trainable_variables) g_optimizer.apply_gradients( zip(gradients, generator.trainable_variables)) if epoch % 100 == 0: fake_image = generator(z_sample, training=False) print(epoch, 'd-loss:', d_loss_meter.result().numpy(), 'g-loss', g_loss_meter.result().numpy(), 'gp', gp_meter.result().numpy()) d_loss_meter.reset_states() g_loss_meter.reset_states() gp_meter.reset_states() # save generated image samples img_path = os.path.join('images_wgan_gp', 'wgan_gp-%d.png' % epoch) save_result(fake_image.numpy(), 10, img_path, color_mode='P') if epoch + 1 % 2000 == 0: generator.save_weights( os.path.join('checkpoints_gp', 'generator-%d' % epoch)) discriminator.save_weights( os.path.join('checkpoints_gp', 'discriminator-%d' % epoch))
dataloader = torch.utils.data.DataLoader(dataset, batch_size=args.batchSize, sampler=sampler, shuffle=True, num_workers=int(args.workers)) device = torch.device("cuda" if args.cuda else "cpu") nz = args.nz ngf = args.ngf ndf = args.ndf G = Generator().to(device) G.apply(weights_init) if args.G != '': G.load_state_dict(clean_state_dict(torch.load(args.G))) D = Discriminator().to(device) D.apply(weights_init) if args.D != '': D.load_state_dict(clean_state_dict(torch.load(args.D))) if torch.cuda.device_count() > 1 and args.cuda: print("Let's use {} GPUs".format(torch.cuda.device_count())) G = nn.DataParallel(G) D = nn.DataParallel(D) criterion = nn.BCELoss() # fixed noise & label fixed_noise = torch.randn(args.batchSize, nz, 1, 1, device=device) real_label = 1 fake_label = 0