def main(): tf.random.set_seed(22) np.random.seed(22) os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2' assert tf.__version__.startswith('2.') z_dim = 100 epochs = 3000000 batch_size = 512 learning_rate = 0.005 training = True img_path = glob.glob(r'.\faces\*.jpg') dataset, img_shape, _ = make_anime_dataset(img_path, batch_size) print(dataset, img_shape) sample_picture = next(iter(dataset)) print(sample_picture.shape, tf.reduce_max(sample_picture).numpy(), tf.reduce_min(sample_picture).numpy()) dataset = dataset.repeat() ds_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.RMSprop(learning_rate=learning_rate) for epoch in range(epochs): batch_z = tf.random.uniform([batch_size, z_dim], minval=-1., maxval=1.) batch_r = next(ds_iter) # discriminator training with tf.GradientTape() as tape: d_loss = d_loss_func(generator, discriminator, batch_z, batch_r, 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_func(generator, discriminator, batch_z, training) grads = tape.gradient(g_loss, generator.trainable_variables) g_optimizer.apply_gradients(zip(grads, generator.trainable_variables)) if epoch % 100 == 0: print('Current epoch:', epoch, 'd_loss:', d_loss, 'g_loss:', g_loss) z = tf.random.uniform([100, z_dim]) g_imgs = generator(z, training=False) save_path = os.path.join('images', 'gan-%d.png' % epoch) save_result(g_imgs.numpy(), 10, save_path, color_mode='P')
def reg_init(p): if p.lambda_reg == 0: return None # load the gan gan_dir = '/accounts/projects/vision/chandan/gan/mnist_dcgan' sys.path.insert(1, gan_dir) from dcgan import Discriminator D = Discriminator( ngpu=1 if torch.cuda.is_available() else 0).to(device) D.load_state_dict( torch.load(oj(gan_dir, 'weights/netD_epoch_99.pth'), map_location=device)) D = D.eval() return D
def __init__(self, progress_dir, checkpoint_dir, z_dim=100, test_size=4, batch_size=100, learning_rate=0.0002, beta_1=0.5): self.batch_size = batch_size self.z_dim = z_dim self.test_size = test_size self.progress_dir = progress_dir self.test_points = tf.random.normal(shape=(test_size**2, z_dim)) self.ckpt = tf.train.Checkpoint( epoch=tf.Variable(1), val_loss=tf.Variable(np.inf), gan_optimizer=optimizers.Adam(lr=learning_rate, beta_1=beta_1), dis_optimizer=optimizers.Adam(lr=learning_rate, beta_1=beta_1), generator=Generator(z_dim), discriminator=Discriminator()) self.ckpt_manager = tf.train.CheckpointManager( checkpoint=self.ckpt, directory=checkpoint_dir, max_to_keep=5) self.restore_checkpoint() self.gen_loss_fn = tf.keras.losses.BinaryCrossentropy() self.dis_loss_fn = tf.keras.losses.BinaryCrossentropy() self.gen_metric = tf.metrics.Mean() self.dis_metric = tf.metrics.Mean()
def build_model(self): self.netG = Generator(nz=self.config.nz, ngf=self.config.ngf, nc=self.config.nc) self.netD = Discriminator(nz=self.config.nz, ndf=self.config.ndf, nc=self.config.nc) # 是否将网络搬运至cuda if self.config.cuda: self.netG = self.net.cuda() self.netD = self.net.cuda() cudnn.benchmark = True # self.net.train() # 设置eval状态 self.netG.eval() # use_global_stats = True self.netD.eval() # 载入预训练模型或自行训练模型 if self.config.load == '': self.netG.load_state_dict(torch.load(self.config.pretrained_model)) self.netD.load_state_dict(torch.load(self.config.pretrained_model)) else: self.netG.load_state_dict(torch.load(self.config.load)) self.netD.load_state_dict(torch.load(self.config.load)) # 设置优化器 self.optimizerD = Adam(self.netD.parameters(), lr=self.config.lr, betas=(self.config.beta1, self.config.beta2), weight_decay=self.config.wd) self.optimizerG = Adam(self.netG.parameters(), lr=self.config.lr, betas=(self.config.beta1, self.config.beta2), weight_decay=self.config.wd) # 打印网络结构 self.print_network(self.netG, 'Generator Structure') self.print_network(self.netD, 'Discriminator Structure')
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 origin neighborhood batch_x = tf.reshape(batch_x, shape=inputs_shape) # -1 -1 batch_x = batch_x * 2.0 - 1.0 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( r'C:\Users\Wojtek\Documents\Projects\DeepLearningPlayground\DCGAN\images', 'gan-val-{:03d}.png'.format(epoch + 1)) save_result(fake_image.numpy(), val_block_size, image_fn, color_mode='L')
#plt.title("Training Images") #plt.imshow(np.transpose(vutils.make_grid( # sample_batch[0].to(device)[ : 64], padding=2, normalize=True).cpu(), (1, 2, 0))) # #plt.show() # Create the generator. netG = Generator(params).to(device) # Apply the weights_init() function to randomly initialize all # weights to mean=0.0, stddev=0.2 netG.apply(weights_init) # Print the model. print(netG) # Create the discriminator. netD = Discriminator(params).to(device) # Apply the weights_init() function to randomly initialize all # weights to mean=0.0, stddev=0.2 netD.apply(weights_init) # Print the model. print(netD) # Binary Cross Entropy loss function. criterion = nn.BCELoss() fixed_noise = torch.randn(64, params['nz'], 1, 1, device=device) real_label = 1 fake_label = 0 # Optimizer for the discriminator.
import torch import torchvision import torch.nn as nn from torchvision import transforms from torchvision.utils import save_image from torch.autograd import Variable import matplotlib.pyplot as plt import pylab import numpy as np num_gpu = 1 if torch.cuda.is_available() else 0 # load the models from dcgan import Discriminator, Generator D = Discriminator(ngpu=1).eval() G = Generator(ngpu=1).eval() D = D.double() # load weights D.load_state_dict(torch.load('weights/netD_epoch_199.pth', map_location='cpu')) G.load_state_dict(torch.load('weights/netG_epoch_199.pth', map_location='cpu')) if torch.cuda.is_available(): D = D.cuda() G = G.cuda() batch_size = 25 latent_size = 100 fixed_noise = torch.randn(batch_size, latent_size, 1, 1)
batch_size = 5 class Discriminator_trimmed(nn.Module): def __init__(self,list_children): super(Discriminator_trimmed, self).__init__() self.main = nn.Sequential(*list_children[0:-2]) def forward(self, input): x = self.main(input) return x.view(-1,512*4*4) if __name__ == "__main__": model_save_path = r"./saved_data/ImageNet/discriminator_ImageNet" disc_model = Discriminator() disc_model.load_state_dict(torch.load(model_save_path)) list_children = [] for i in disc_model.children(): list_children.extend([*i]) # print(list_children) disc_new = Discriminator_trimmed(list_children) food_101_path = r"/home/data/food-101/images" index_arr = np.arange(1000) np.random.shuffle(index_arr) # print(index_arr) training_idx = index_arr[0:75] validation_idx = index_arr[75:100]
def main(): dataSize = 32 batchSize = 8 elpipsBatchSize = 1 # imageSize = 32 imageSize = 64 nz = 100 # discCheckpointPath = r'E:\projects\visus\PyTorch-GAN\implementations\dcgan\checkpoints\2020_07_10_15_53_34\disc_step4800.pth' discCheckpointPath = r'E:\projects\visus\pytorch-examples\dcgan\out\netD_epoch_24.pth' genCheckpointPath = r'E:\projects\visus\pytorch-examples\dcgan\out\netG_epoch_24.pth' gpu = torch.device('cuda') # For now we normalize the vectors to have norm 1, but don't make sure # that the data has certain mean/std. pointDataset = AuthorDataset( jsonPath=r'E:\out\scripts\metaphor-vis\authors-all.json' ) # Take top N points. points = np.asarray([pointDataset[i][0] for i in range(dataSize)]) distPointsCpu = l2_sqr_dist_matrix(torch.tensor(points)).numpy() latents = torch.tensor(np.random.normal(0.0, 1.0, (dataSize, nz)), requires_grad=True, dtype=torch.float32, device=gpu) scale = torch.tensor(2.7, requires_grad=True, dtype=torch.float32, device=gpu) # todo Re-check! bias = torch.tensor(0.0, requires_grad=True, dtype=torch.float32, device=gpu) # todo Re-check! lpips = models.PerceptualLoss(model='net-lin', net='vgg', use_gpu=True).to(gpu) # lossModel = lpips config = elpips.Config() config.batch_size = elpipsBatchSize # Ensemble size for ELPIPS. config.set_scale_levels_by_image_size(imageSize, imageSize) lossModel = elpips.ElpipsMetric(config, lpips).to(gpu) discriminator = Discriminator(3, 64, 1) if discCheckpointPath: discriminator.load_state_dict(torch.load(discCheckpointPath)) else: discriminator.init_params() discriminator = discriminator.to(gpu) generator = Generator(nz=nz, ngf=64) if genCheckpointPath: generator.load_state_dict(torch.load(genCheckpointPath)) else: generator.init_params() generator = generator.to(gpu) # optimizerImages = torch.optim.Adam([images, scale], lr=1e-2, betas=(0.9, 0.999)) optimizerScale = torch.optim.Adam([scale, bias], lr=0.001) # optimizerGen = torch.optim.Adam(generator.parameters(), lr=0.0002, betas=(0.5, 0.999)) # optimizerDisc = torch.optim.Adam(discriminator.parameters(), lr=2e-4, betas=(0.9, 0.999)) # optimizerDisc = torch.optim.Adam(discriminator.parameters(), lr=0.0002, betas=(0.5, 0.999)) optimizerLatents = torch.optim.Adam([latents], lr=5e-3, betas=(0.9, 0.999)) fig, axes = plt.subplots(nrows=2, ncols=batchSize // 2) fig2 = plt.figure() ax2 = fig2.add_subplot(1, 1, 1) outPath = os.path.join('runs', datetime.datetime.today().strftime('%Y_%m_%d_%H_%M_%S')) os.makedirs(outPath) summaryWriter = SummaryWriter(outPath) for batchIndex in range(10000): # noinspection PyTypeChecker randomIndices = np.random.randint(0, dataSize, batchSize).tolist() # type: List[int] # # randomIndices = list(range(dataSize)) # type: List[int] distTarget = torch.tensor(distPointsCpu[randomIndices, :][:, randomIndices], dtype=torch.float32, device=gpu) latentsBatch = latents[randomIndices] imageBatchFake = generator(latentsBatch[:, :, None, None].float()) # todo It's possible to compute this more efficiently, but would require re-implementing lpips. # For now, compute the full BSxBS matrix row-by-row to avoid memory issues. lossDistTotal = torch.tensor(0.0, device=gpu) distanceRows = [] for iRow in range(batchSize): distPredFlat = lossModel(imageBatchFake[iRow].repeat(repeats=(batchSize, 1, 1, 1)).contiguous(), imageBatchFake, normalize=True) distPred = distPredFlat.reshape((1, batchSize)) distanceRows.append(distPred) lossDist = torch.sum((distTarget[iRow] - (distPred * scale + bias)) ** 2) # MSE lossDistTotal += lossDist lossDistTotal /= batchSize * batchSize # Compute the mean. distPredFull = torch.cat(distanceRows, dim=0) # print('{} - {} || {} - {}'.format( # torch.min(distPred).item(), # torch.max(distPred).item(), # torch.min(distTarget).item(), # torch.max(distTarget).item() # )) # discPred = discriminator(imageBatchFake) # lossRealness = bceLoss(discPred, torch.ones(imageBatchFake.shape[0], device=gpu)) # lossGen = lossDist + 1.0 * lossRealness lossLatents = lossDistTotal # optimizerGen.zero_grad() # optimizerScale.zero_grad() # lossGen.backward() # optimizerGen.step() # optimizerScale.step() optimizerLatents.zero_grad() # optimizerScale.zero_grad() lossLatents.backward() optimizerLatents.step() # optimizerScale.step() # with torch.no_grad(): # # todo We're clamping all the images every batch, can we clamp only the ones updated? # # images = torch.clamp(images, 0, 1) # For some reason this was making the training worse. # images.data = torch.clamp(images.data, 0, 1) if batchIndex % 100 == 0: msg = 'iter {} loss dist {:.3f} scale: {:.3f} bias: {:.3f}'.format(batchIndex, lossDistTotal.item(), scale.item(), bias.item()) print(msg) summaryWriter.add_scalar('loss-dist', lossDistTotal.item(), global_step=batchIndex) def gpu_images_to_numpy(images): imagesNumpy = images.cpu().data.numpy().transpose(0, 2, 3, 1) imagesNumpy = (imagesNumpy + 1) / 2 return imagesNumpy # print(discPred.tolist()) imageBatchFakeCpu = gpu_images_to_numpy(imageBatchFake) # imageBatchRealCpu = gpu_images_to_numpy(imageBatchReal) for iCol, ax in enumerate(axes.flatten()[:batchSize]): ax.imshow(imageBatchFakeCpu[iCol]) fig.suptitle(msg) with torch.no_grad(): images = gpu_images_to_numpy(generator(latents[..., None, None])) authorVectorsProj = umap.UMAP(n_neighbors=min(5, dataSize), random_state=1337).fit_transform(points) plot_image_scatter(ax2, authorVectorsProj, images, downscaleRatio=2) fig.savefig(os.path.join(outPath, f'batch_{batchIndex}.png')) fig2.savefig(os.path.join(outPath, f'scatter_{batchIndex}.png')) plt.close(fig) plt.close(fig2) with torch.no_grad(): imagesGpu = generator(latents[..., None, None]) imageNumber = imagesGpu.shape[0] # Compute LPIPS distances, batch to avoid memory issues. bs = min(imageNumber, 8) assert imageNumber % bs == 0 distPredEval = np.zeros((imagesGpu.shape[0], imagesGpu.shape[0])) for iCol in range(imageNumber // bs): startA, endA = iCol * bs, (iCol + 1) * bs imagesA = imagesGpu[startA:endA] for j in range(imageNumber // bs): startB, endB = j * bs, (j + 1) * bs imagesB = imagesGpu[startB:endB] distBatchEval = lossModel(imagesA.repeat(repeats=(bs, 1, 1, 1)).contiguous(), imagesB.repeat_interleave(repeats=bs, dim=0).contiguous(), normalize=True).cpu().numpy() distPredEval[startA:endA, startB:endB] = distBatchEval.reshape((bs, bs)) distPredEval = (distPredEval * scale.item() + bias.item()) # Move to the CPU and append an alpha channel for rendering. images = gpu_images_to_numpy(imagesGpu) images = [np.concatenate([im, np.ones(im.shape[:-1] + (1,))], axis=-1) for im in images] distPoints = distPointsCpu assert np.abs(distPoints - distPoints.T).max() < 1e-5 distPoints = np.minimum(distPoints, distPoints.T) # Remove rounding errors, guarantee symmetry. config = DistanceMatrixConfig() config.dataRange = (0., 4.) _, pointIndicesSorted = render_distance_matrix( os.path.join(outPath, f'dist_point_{batchIndex}.png'), distPoints, images, config=config ) # print(np.abs(distPredFlat - distPredFlat.T).max()) # assert np.abs(distPredFlat - distPredFlat.T).max() < 1e-5 # todo The symmetry doesn't hold for E-LPIPS, since it's stochastic. distPredEval = np.minimum(distPredEval, distPredEval.T) # Remove rounding errors, guarantee symmetry. config = DistanceMatrixConfig() config.dataRange = (0., 4.) render_distance_matrix( os.path.join(outPath, f'dist_images_{batchIndex}.png'), distPredEval, images, config=config ) config = DistanceMatrixConfig() config.dataRange = (0., 4.) render_distance_matrix( os.path.join(outPath, f'dist_images_aligned_{batchIndex}.png'), distPredEval, images, predefinedOrder=pointIndicesSorted, config=config ) fig, axes = plt.subplots(ncols=2) axes[0].matshow(distTarget.cpu().numpy(), vmin=0, vmax=4) axes[1].matshow(distPredFull.cpu().numpy() * scale.item(), vmin=0, vmax=4) fig.savefig(os.path.join(outPath, f'batch_dist_{batchIndex}.png')) plt.close(fig) surveySize = 30 fig, axes = plt.subplots(nrows=3, ncols=surveySize, figsize=(surveySize, 3)) assert len(images) == dataSize allIndices = list(range(dataSize)) with open(os.path.join(outPath, f'survey_{batchIndex}.txt'), 'w') as file: for iCol in range(surveySize): randomIndices = random.sample(allIndices, k=3) leftToMid = distPointsCpu[randomIndices[0], randomIndices[1]] rightToMid = distPointsCpu[randomIndices[2], randomIndices[1]] correctAnswer = 'left' if leftToMid < rightToMid else 'right' file.write("{}\t{}\t{}\t{}\t{}\n".format(iCol, correctAnswer, leftToMid, rightToMid, str(tuple(randomIndices)))) for iRow in (0, 1, 2): axes[iRow][iCol].imshow(images[randomIndices[iRow]]) fig.savefig(os.path.join(outPath, f'survey_{batchIndex}.png')) plt.close(fig) torch.save(generator.state_dict(), os.path.join(outPath, 'gen_{}.pth'.format(batchIndex))) torch.save(discriminator.state_dict(), os.path.join(outPath, 'gen_{}.pth'.format(batchIndex))) summaryWriter.close()
class Solver(object): def __init__(self, train_loader, test_loader, config): # 训练集DataLoader self.train_loader = train_loader # 测试集DataLoader self.test_loader = test_loader # config配置 self.config = config # 展示信息epoch次数 self.show_every = config.show_every # 学习率衰退epoch数 self.lr_decay_epoch = [ 15, ] # 创建模型 self.build_model() # Loss function self.adversarial_loss = torch.nn.BCELoss() # 进入test模式 if config.mode == 'test': print('Loading pre-trained model from %s...' % self.config.model) # 载入预训练模型并放入相应位置 if self.config.cuda: self.netG.load_state_dict(torch.load(self.config.model)) self.netD.load_state_dict(torch.load(self.config.model)) else: self.netG.load_state_dict( torch.load(self.config.model, map_location='cpu')) self.netD.load_state_dict( torch.load(self.config.model, map_location='cpu')) # 打印网络信息和参数数量 def print_network(self, model, name): num_params = 0 for p in model.parameters(): num_params += p.numel() print(name) print(model) print("The number of parameters: {}".format(num_params)) # 建立模型 def build_model(self): self.netG = Generator(nz=self.config.nz, ngf=self.config.ngf, nc=self.config.nc) self.netD = Discriminator(nz=self.config.nz, ndf=self.config.ndf, nc=self.config.nc) # 是否将网络搬运至cuda if self.config.cuda: self.netG = self.net.cuda() self.netD = self.net.cuda() cudnn.benchmark = True # self.net.train() # 设置eval状态 self.netG.eval() # use_global_stats = True self.netD.eval() # 载入预训练模型或自行训练模型 if self.config.load == '': self.netG.load_state_dict(torch.load(self.config.pretrained_model)) self.netD.load_state_dict(torch.load(self.config.pretrained_model)) else: self.netG.load_state_dict(torch.load(self.config.load)) self.netD.load_state_dict(torch.load(self.config.load)) # 设置优化器 self.optimizerD = Adam(self.netD.parameters(), lr=self.config.lr, betas=(self.config.beta1, self.config.beta2), weight_decay=self.config.wd) self.optimizerG = Adam(self.netG.parameters(), lr=self.config.lr, betas=(self.config.beta1, self.config.beta2), weight_decay=self.config.wd) # 打印网络结构 self.print_network(self.netG, 'Generator Structure') self.print_network(self.netD, 'Discriminator Structure') # testing状态 def test(self): # 训练模式 mode_name = 'enhanced' # 开始时间 time_s = time.time() # images数量 img_num = len(self.test_loader) for i, data_batch in enumerate(self.test_loader): # 获取image数据和name phone_image, _, name = data_batch['phone_image'], data_batch[ 'dslr_image'], data_batch['name'] # testing状态 with torch.no_grad(): # 获取tensor数据并搬运指定设备 images = torch.Tensor(phone_image) if self.config.cuda: images = images.cuda() # 预测值 preds = self.netG(images).cpu().data.numpy() # 创建image cv2.imwrite( os.path.join(self.config.test_fold, name[:-4] + '_' + mode_name + '.png'), preds) # 结束时间 time_e = time.time() print('Speed: %f FPS' % (img_num / (time_e - time_s))) print('Test Done!') # training状态 def train(self): for epoch in range(self.config.epochs): for i, data_batch in enumerate(self.train_loader): # 获取image数据和name phone_image, _, _ = data_batch['phone_image'], data_batch[ 'dslr_image'], data_batch['name'] # Adversarial ground truths valid = torch.Tensor(phone_image.size(0), 1).fill_(1.0) fake = torch.Tensor(phone_image.size(0), 1).fill_(0.0) # ----------------- # Train Generator # ----------------- self.optimizerG.zero_grad() # Sample noise as generator input z = torch.Tensor( np.random.normal(0, 1, (phone_image.shape[0], self.config.nz))) # Generate a batch of images gen_imgs = self.generator(z) # Loss measures generator's ability to fool the discriminator g_loss = self.adversarial_loss(self.discriminator(gen_imgs), valid) g_loss.backward() self.optimizerG.step() # --------------------- # Train Discriminator # --------------------- self.optimizerD.zero_grad() # Measure discriminator's ability to classify real from generated samples real_loss = self.adversarial_loss( self.discriminator(phone_image), valid) fake_loss = self.adversarial_loss( self.discriminator(gen_imgs.detach()), fake) d_loss = (real_loss + fake_loss) / 2 d_loss.backward() self.optimizerD.step() # 展示此时信息 if i % (self.show_every // self.config.batch_size) == 0: print( "[Epoch %d/%d] [Batch %d/%d] [D loss: %f] [G loss: %f]" % (epoch, self.config.epochs, i, len( self.train_loader), d_loss.item(), g_loss.item())) print('Learning rate: ' + str(self.config.lr)) # 保存训练模型 if (epoch + 1) % self.config.epoch_save == 0: torch.save( self.netG.state_dict(), '%s/models/generator/epoch_%d.pth' % (self.config.save_folder, epoch + 1)) torch.save( self.netD.state_dict(), '%s/models/discriminator/epoch_%d.pth' % (self.config.save_folder, epoch + 1)) # 学习率衰退 if epoch in self.lr_decay_epoch: self.lr = self.lr * 0.1 # 设置优化器 self.optimizerG = Adam( filter(lambda p: p.requires_grad, self.netG.parameters(), lr=self.config.lr, betas=(self.config.beta1, self.config.beta2), weight_decay=self.config.wd)) self.optimizerD = Adam( filter(lambda p: p.requires_grad, self.netD.parameters(), lr=self.config.lr, betas=(self.config.beta1, self.config.beta2), weight_decay=self.config.wd)) # 保存训练模型 torch.save(self.net.state_dict(), '%s/models/generator/final.pth' % self.config.save_folder) torch.save( self.net.state_dict(), '%s/models/discriminator/final.pth' % self.config.save_folder)
transforms.Resize(image_size), transforms.CenterCrop(image_size), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ])) # Create the dataloader dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True, num_workers=workers) # Decide which device we want to run on device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu") print (device) # custom weights initialization called on netG and netD # Create the generator netG = Generator(ngpu, nz+t_in, ngf, nc).to(device) netD = Discriminator(ngpu, nc, ndf, t_in).to(device) # Handle multi-gpu if desired # if (device.type == 'cuda') and (ngpu > 1): # netG = nn.DataParallel(netG, list(range(ngpu))) # netD = nn.DataParallel(netD, list(range(ngpu))) netG.apply(weights_init) netD.apply(weights_init) netG = netG.cuda() netD = netD.cuda() # Initialize BCELoss function criterion = nn.BCELoss() fixed_noise = torch.randn(64, nz+t_in, 1, 1, device=device) real_label = 1. fake_label = 0.
m.weight.data.normal_(1.0, 0.02) m.bias.data.fill_(0) netG = Generator(ngpu, ngf, nc, imageSize, nz).to(device) netG.apply(weights_init) if opt.netG != '': netG.load_state_dict(torch.load(opt.netG)) print(netG) def count_parameters(model): return sum(p.numel() for p in model.parameters() if p.requires_grad) netD = Discriminator(ngpu, ndf, nc, imageSize).to(device) netD.apply(weights_init) if opt.netD != '': netD.load_state_dict(torch.load(opt.netD)) print(netD) print("Generator n. params: {}".format(count_parameters(netG))) print("Discriminator n. params: {}".format(count_parameters(netD))) criterion = nn.BCELoss() fixed_noise = torch.randn(opt.batchSize, nz, 1, 1, device=device) real_label = 1 fake_label = 0 # setup optimizer
plt.imshow( np.transpose( vutils.make_grid(sample_batch[0].to(device)[:64], padding=2, normalize=True).cpu(), (1, 2, 0))) plt.show() # Create the generator network. netG = Generator(params).to(device) # Load the trained generator weights. netG.load_state_dict(state_dict['generator']) print(netG) # Create the discriminator network. netD = Discriminator(params).to(device) # Load the trained discriminator weights. netD.load_state_dict(state_dict['discriminator']) print(netD) # Binary Cross Entropy loss function. criterion = nn.BCELoss() fixed_noise = torch.randn(64, params['nz'], 1, 1, device=device) real_label = 1 fake_label = 0 # Optimizer for the discriminator. optimizerD = optim.Adam(netD.parameters(), lr=params['lr'],
def main(): dataSize = 128 batchSize = 8 # imageSize = 32 imageSize = 64 initWithCats = True # discCheckpointPath = r'E:\projects\visus\PyTorch-GAN\implementations\dcgan\checkpoints\2020_07_10_15_53_34\disc_step4800.pth' # discCheckpointPath = r'E:\projects\visus\pytorch-examples\dcgan\out\netD_epoch_24.pth' discCheckpointPath = None gpu = torch.device('cuda') imageRootPath = r'E:\data\cat-vs-dog\cat' catDataset = CatDataset( imageSubdirPath=imageRootPath, transform=transforms.Compose([ transforms.Resize((imageSize, imageSize)), # torchvision.transforms.functional.to_grayscale, transforms.ToTensor(), # transforms.Lambda(lambda x: torch.reshape(x, x.shape[1:])), transforms.Normalize([0.5], [0.5]) ])) sampler = InfiniteSampler(catDataset) catLoader = DataLoader(catDataset, batch_size=batchSize, sampler=sampler) # Generate a random distance matrix. # # Make a matrix with positive values. # distancesCpu = np.clip(np.random.normal(0.5, 1.0 / 3, (dataSize, dataSize)), 0, 1) # # Make it symmetrical. # distancesCpu = np.matmul(distancesCpu, distancesCpu.T) # Generate random points and compute distances, guaranteeing that the triangle rule isn't broken. randomPoints = generate_points(dataSize) distancesCpu = scipy.spatial.distance_matrix(randomPoints, randomPoints, p=2) if initWithCats: imagePaths = random.choices(glob.glob(os.path.join(imageRootPath, '*')), k=dataSize) catImages = [] for p in imagePaths: image = skimage.transform.resize(imageio.imread(p), (imageSize, imageSize), 1).transpose(2, 0, 1) catImages.append(image) imagesInitCpu = np.asarray(catImages) else: imagesInitCpu = np.clip( np.random.normal(0.5, 0.5 / 3, (dataSize, 3, imageSize, imageSize)), 0, 1) images = torch.tensor(imagesInitCpu, requires_grad=True, dtype=torch.float32, device=gpu) scale = torch.tensor(1.0, requires_grad=True, dtype=torch.float32, device=gpu) lossModel = models.PerceptualLoss(model='net-lin', net='vgg', use_gpu=True).to(gpu) lossBce = torch.nn.BCELoss() # discriminator = Discriminator(imageSize, 3) discriminator = Discriminator(3, 64, 1) if discCheckpointPath: discriminator.load_state_dict(torch.load(discCheckpointPath)) else: discriminator.init_params() discriminator = discriminator.to(gpu) optimizerImages = torch.optim.Adam([images, scale], lr=1e-3, betas=(0.9, 0.999)) # optimizerDisc = torch.optim.Adam(discriminator.parameters(), lr=2e-4, betas=(0.9, 0.999)) optimizerDisc = torch.optim.Adam(discriminator.parameters(), lr=0.0002, betas=(0.5, 0.999)) import matplotlib.pyplot as plt fig, axes = plt.subplots(nrows=2, ncols=batchSize // 2) fig2 = plt.figure() ax2 = fig2.add_subplot(1, 1, 1) outPath = os.path.join( 'images', datetime.datetime.today().strftime('%Y_%m_%d_%H_%M_%S')) os.makedirs(outPath) catIter = iter(catLoader) for batchIndex in range(10000): realImageBatch, _ = next(catIter) # type: Tuple(torch.Tensor, Any) realImageBatch = realImageBatch.to(gpu) # realImageBatch = torch.tensor(realImageBatchCpu, device=gpu) # noinspection PyTypeChecker randomIndices = np.random.randint( 0, dataSize, batchSize).tolist() # type: List[int] # randomIndices = list(range(dataSize)) # type: List[int] distanceBatch = torch.tensor( distancesCpu[randomIndices, :][:, randomIndices], dtype=torch.float32, device=gpu) imageBatch = images[randomIndices].contiguous() distPred = lossModel.forward( imageBatch.repeat(repeats=(batchSize, 1, 1, 1)).contiguous(), imageBatch.repeat_interleave(repeats=batchSize, dim=0).contiguous(), normalize=True) distPredMat = distPred.reshape((batchSize, batchSize)) lossDist = torch.sum((distanceBatch - distPredMat * scale)**2) # MSE discPred = discriminator(imageBatch) lossRealness = lossBce(discPred, torch.ones(imageBatch.shape[0], 1, device=gpu)) lossImages = lossDist + 100.0 * lossRealness # todo # lossImages = lossRealness # todo optimizerImages.zero_grad() lossImages.backward() optimizerImages.step() lossDiscReal = lossBce( discriminator(realImageBatch), torch.ones(realImageBatch.shape[0], 1, device=gpu)) lossDiscFake = lossBce(discriminator(imageBatch.detach()), torch.zeros(imageBatch.shape[0], 1, device=gpu)) lossDisc = (lossDiscFake + lossDiscReal) / 2 # lossDisc = torch.tensor(0) optimizerDisc.zero_grad() lossDisc.backward() optimizerDisc.step() with torch.no_grad(): # todo We're clamping all the images every batch, can we do clamp only the ones updated? # images = torch.clamp(images, 0, 1) # For some reason this was making the training worse. images.data = torch.clamp(images.data, 0, 1) if batchIndex % 100 == 0: msg = 'iter {}, loss images {:.3f}, loss dist {:.3f}, loss real {:.3f}, loss disc {:.3f}, scale: {:.3f}'.format( batchIndex, lossImages.item(), lossDist.item(), lossRealness.item(), lossDisc.item(), scale.item()) print(msg) # print(discPred.tolist()) imageBatchCpu = imageBatch.cpu().data.numpy().transpose(0, 2, 3, 1) for i, ax in enumerate(axes.flatten()): ax.imshow(imageBatchCpu[i]) fig.suptitle(msg) imagesAllCpu = images.cpu().data.numpy().transpose(0, 2, 3, 1) plot_image_scatter(ax2, randomPoints, imagesAllCpu, downscaleRatio=2) fig.savefig( os.path.join(outPath, 'batch_{}.png'.format(batchIndex))) fig2.savefig( os.path.join(outPath, 'scatter_{}.png'.format(batchIndex)))
gen_file = 'parameters/dcgan_mnist/generator.pth' transforms = transforms.Compose([ transforms.Resize(image_size), transforms.ToTensor(), transforms.Normalize([0.5 for _ in range(channels_img)], [0.5 for _ in range(channels_img)]), ]) dataset = datasets.MNIST(root='../datasets/', train=True, transform=transforms, download=True) loader = DataLoader(dataset, batch_size=batch_size, shuffle=True) gen = Generator(z_dim, channels_img, features_gen).to(device) disc = Discriminator(channels_img, features_disc).to(device) load_model(disc, disc_file, device) load_model(gen, gen_file, device) opt_gen = optim.Adam(gen.parameters(), lr=learning_rate, betas=(0.5, 0.999)) opt_disc = optim.Adam(disc.parameters(), lr=learning_rate, betas=(0.5, 0.999)) criterion = nn.BCELoss() fixed_noise = torch.randn(32, z_dim, 1, 1).to(device) writer_real = SummaryWriter('runs/dcgan_mnist/real') writer_fake = SummaryWriter('runs/dcgan_mnist/fake') step = 0 gen.train() disc.train()
def main(): parser = Flags() parser.set_arguments() parser.add_argument('--z_dim', type=int, default=100) FG = parser.parse_args() vis = Visdom(port=FG.vis_port, env=FG.model) report = parser.report(end='<br>') vis.text(report, win='report f{}'.format(FG.cur_fold)) transform = transforms.Compose([ transforms.RandomResizedCrop(64, scale=(0.5, 1.0)), transforms.ToTensor(), transforms.Normalize((0.5, ), (0.5, )) ]) trainset = datasets.MNIST(root='./mnist', train=True, transform=transform) trainloader = torch.utils.data.DataLoader( trainset, batch_size=FG.batch_size, worker_init_fn=lambda _: torch.initial_seed(), num_workers=5, shuffle=True, pin_memory=True, drop_last=True) # trainloader, _ = get_dataloader(FG.fold, FG.cur_fold, FG.data_root, FG.modality, # labels=FG.labels, batch_size=FG.batch_size, # balancing=FG.data_balancing) torch.cuda.set_device(FG.devices[0]) device = torch.device(FG.devices[0]) netG = nn.DataParallel(Generator(FG.ckpt_dir, FG.z_dim).weight_init(), device_ids=FG.devices) netD = nn.DataParallel(Discriminator(FG.ckpt_dir).weight_init(), device_ids=FG.devices) optimG = Adam(netG.parameters(), lr=FG.lr, amsgrad=True) optimD = Adam(netD.parameters(), lr=FG.lr, amsgrad=True) z_sampler = ZSampler(torch.randn, (FG.batch_size, FG.z_dim, 1, 1), device=device) trainer = create_gan_trainer(netG, netD, optimG, optimD, F.binary_cross_entropy, z_sampler, device=device, non_blocking=True) monitoring_metrics = ['LD', 'LG', 'Dx', 'DGz1', 'DGz2'] RunningAverage(alpha=0.98, output_transform=lambda x: x['LD']).attach(trainer, 'LD') RunningAverage(alpha=0.98, output_transform=lambda x: x['LG']).attach(trainer, 'LG') RunningAverage(alpha=0.98, output_transform=lambda x: x['Dx']).attach(trainer, 'Dx') RunningAverage(alpha=0.98, output_transform=lambda x: x['DGz1']).attach( trainer, 'DGz1') RunningAverage(alpha=0.98, output_transform=lambda x: x['DGz2']).attach( trainer, 'DGz2') real_rate = Accuracy() fake_rate = Accuracy() trackers = dict() for monitoring_metric in monitoring_metrics: trackers[monitoring_metric] = Scalar(vis, monitoring_metric, monitoring_metric, opts=dict( title=monitoring_metric, y_label=monitoring_metric, xlabel='epoch', showlegend=True)) trackers['real_rate'] = Scalar(vis, 'real_rate', 'real_rate', opts=dict(title='real_rate', y_label='real_rate', ytickmin=0, ytickmax=1, xlabel='epoch', showlegend=True)) trackers['fake_rate'] = Scalar(vis, 'fake_rate', 'fake_rate', opts=dict(title='fake_rate', y_label='fake_rate', ytickmin=0, ytickmax=1, xlabel='epoch', showlegend=True)) fakeshow = Image2D(vis, 'fake') realshow = Image2D(vis, 'real') @trainer.on(Events.ITERATION_COMPLETED) def track_logs(engine): i = engine.state.iteration / len(trainloader) metrics = engine.state.metrics for key in metrics.keys(): trackers[key](i, metrics[key]) y_pred_real = (engine.state.output['output_real'] >= 0.5).long() y_pred_fake = (engine.state.output['output_fake'] < 0.5).long() real_rate.update((y_pred_real, z_sampler.real_label.long())) fake_rate.update((y_pred_fake, z_sampler.fake_label.long())) @trainer.on(Events.EPOCH_COMPLETED) def show_fake_example(engine): netG.eval() fake = netG(z_sampler.fixed_noise) fakeshow('fake_images', fake * 0.5 + 0.5) realshow('real_images', engine.state.batch[0] * 0.5 + 0.5) trackers['real_rate'](engine.state.epoch, real_rate.compute()) trackers['fake_rate'](engine.state.epoch, fake_rate.compute()) real_rate.reset() fake_rate.reset() trainer.run(trainloader, FG.num_epoch)
def run_dcgan(device, image_size, noise_size, batch_size, config, run_dir, saved_dir, run_name, num_epochs, val_dataset, train_dataset=None, checkpoints=None, mode='train', gpu_num=1): #Run DCGAN type = 'DCGAN' dcgan_generator = Generator(noise_size=noise_size, image_size=image_size).to(device) dcgan_discriminator = Discriminator(image_size=image_size).to(device) #Parallel for improved performence if device.type == 'cuda' and gpu_num > 1: dcgan_generator = nn.DataParallel(dcgan_generator, list(range(gpu_num))) dcgan_discriminator = nn.DataParallel(dcgan_discriminator, list(range(gpu_num))) #Print networks print('Discriminator') summary(dcgan_discriminator, (3, image_size, image_size)) print('Generator') summary(dcgan_generator, (noise_size, 1, 1)) if checkpoints is not None: utils.load_from_checkpoint(dcgan_generator, saved_dir, checkpoints["generator"]) utils.load_from_checkpoint(dcgan_discriminator, saved_dir, checkpoints["discriminator"]) run_name = 'DCGAN' + '_' + run_name #We train the model in train phase and only calculate scores in test mode if mode == 'train': inception_FID_scores, inception_scores = train_gan(num_epochs, batch_size, noise_size, device, train_dataset, val_dataset, dcgan_generator, dcgan_discriminator, type='DCGAN', config=config, run_dir=run_dir, saved_dir=saved_dir, run_name=run_name) elif mode == 'test': inception_FID_scores = [ calc_inception_FID_score(batch_size, device, val_dataset, dcgan_generator, type, noise_size) ] inception_scores = [ calc_inception_score(device, noise_size, dcgan_generator, eval_size=len(val_dataset)) ] #Return list of all score accumulated in epochs date_str = datetime.datetime.now().strftime("%m%d%Y%H") save_to_pickle( inception_FID_scores, os.path.join(saved_dir, 'dcgan_fid_' + run_name + date_str + ".pickle")) save_to_pickle( inception_scores, os.path.join(saved_dir, 'dcgan_IS_' + run_name + date_str + ".pickle")) return inception_FID_scores, inception_scores
def run_wsgan(device, image_size, noise_size, batch_size, config, run_dir, saved_dir, run_name, num_epochs, val_dataset, train_dataset=None, checkpoints=None, mode='train', gpu_num=1): type = 'WSGAN' wsgan_generator = Generator(noise_size=noise_size, image_size=image_size).to(device) wsgan_critic = Discriminator(image_size=image_size, as_critic=True).to(device) # Parallel for improved performence if ((device.type == 'cuda') and (gpu_num > 1)): wsgan_generator = nn.DataParallel(wsgan_generator, list(range(gpu_num))) wsgan_critic = nn.DataParallel(wsgan_critic, list(range(gpu_num))) # Print networks print('Critic') summary(wsgan_critic, (3, image_size, image_size)) print('Generator') summary(wsgan_generator, (noise_size, 1, 1)) if checkpoints is not None: utils.load_from_checkpoint(wsgan_generator, saved_dir, checkpoints["generator"]) utils.load_from_checkpoint(wsgan_critic, saved_dir, checkpoints["discriminator"]) run_name = 'WSGAN' + '_' + run_name if mode == 'train': inception_FID_scores, inception_scores = train_gan(num_epochs, batch_size, noise_size, device, train_dataset, val_dataset, wsgan_generator, wsgan_critic, type='WSGAN', config=config, run_dir=run_dir, saved_dir=saved_dir, run_name=run_name) elif mode == 'test': inception_FID_scores = [ calc_inception_FID_score(batch_size, device, val_dataset, wsgan_generator, type, noise_size) ] inception_scores = [ calc_inception_score(device, noise_size, wsgan_generator, eval_size=len(val_dataset)) ] date_str = datetime.datetime.now().strftime("%m%d%Y%H") save_to_pickle( inception_FID_scores, os.path.join(saved_dir, 'wsgan_fid_' + run_name + date_str + ".pickle")) save_to_pickle( inception_scores, os.path.join(saved_dir, 'wsgan_IS_' + run_name + date_str + ".pickle")) return inception_FID_scores, inception_scores
def main(): # Loss function adversarial_loss = torch.nn.BCELoss() # Initialize generator and discriminator generator = Generator() discriminator = Discriminator() # Initialize weights generator.apply(weights_init_normal) discriminator.apply(weights_init_normal) # DataParallel generator = nn.DataParallel(generator).to(device) discriminator = nn.DataParallel(discriminator).to(device) # Dataloader # data preparation, loaders normalize = transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # cudnn.benchmark = True # preparing the training laoder train_loader = torch.utils.data.DataLoader( ImageLoader( opt.img_path, transforms.Compose([ transforms.Scale( 128 ), # rescale the image keeping the original aspect ratio transforms.CenterCrop( 128), # we get only the center of that rescaled transforms.RandomCrop( 128), # random crop within the center crop transforms.RandomHorizontalFlip(), transforms.ToTensor(), normalize, ]), data_path=opt.data_path, partition='train'), batch_size=opt.batch_size, shuffle=True, num_workers=opt.workers, pin_memory=True) print('Training loader prepared.') # preparing validation loader val_loader = torch.utils.data.DataLoader( ImageLoader( opt.img_path, transforms.Compose([ transforms.Scale( 128 ), # rescale the image keeping the original aspect ratio transforms.CenterCrop( 128), # we get only the center of that rescaled transforms.ToTensor(), normalize, ]), data_path=opt.data_path, partition='val'), batch_size=opt.batch_size, shuffle=False, num_workers=opt.workers, pin_memory=True) print('Validation loader prepared.') # Optimizers optimizer_G = torch.optim.Adam(generator.parameters(), lr=opt.lr, betas=(opt.b1, opt.b2)) optimizer_D = torch.optim.Adam(discriminator.parameters(), lr=opt.lr, betas=(opt.b1, opt.b2)) # ---------- # Training # ---------- for epoch in range(opt.n_epochs): pbar = tqdm(total=len(train_loader)) start_time = time.time() for i, data in enumerate(train_loader): input_var = list() for j in range(len(data)): # if j>1: input_var.append(data[j].to(device)) imgs = input_var[0] # Adversarial ground truths valid = np.ones((imgs.shape[0], 1)) valid = torch.FloatTensor(valid).to(device) fake = np.zeros((imgs.shape[0], 1)) fake = torch.FloatTensor(fake).to(device) # ----------------- # Train Generator # ----------------- optimizer_G.zero_grad() # Sample noise as generator input z = np.random.normal(0, 1, (imgs.shape[0], opt.latent_dim)) z = torch.FloatTensor(z).to(device) # Generate a batch of images gen_imgs = generator(z, input_var[1], input_var[2], input_var[3], input_var[4]) # Loss measures generator's ability to fool the discriminator g_loss = adversarial_loss(discriminator(gen_imgs), valid) g_loss.backward() optimizer_G.step() # --------------------- # Train Discriminator # --------------------- optimizer_D.zero_grad() # Measure discriminator's ability to classify real from generated samples real_loss = adversarial_loss(discriminator(imgs), valid) fake_loss = adversarial_loss(discriminator(gen_imgs.detach()), fake) d_loss = (real_loss + fake_loss) / 2 d_loss.backward() optimizer_D.step() pbar.update(1) pbar.close() print( "[Epoch %d/%d] [Batch %d/%d] [D loss: %f] [G loss: %f] [Time Elapsed: %f]" % (epoch, opt.n_epochs, i, len(train_loader), d_loss.item(), g_loss.item(), time.time() - start_time)) if epoch % opt.sample_interval == 0: save_samples(epoch, gen_imgs.data[:25]) save_model(epoch, generator.state_dict(), discriminator.state_dict())
def main(): # load training data trainset = Dataset('./data/brilliant_blue') trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size, shuffle=True) # init netD and netG netD = Discriminator().to(device) netD.apply(weights_init) netG = Generator(nz).to(device) netG.apply(weights_init) # used for visualizing training process fixed_noise = torch.randn(16, nz, 1, device=device) # optimizers optimizerD = optim.RMSprop(netD.parameters(), lr=lr) optimizerG = optim.RMSprop(netG.parameters(), lr=lr) for epoch in range(epoch_num): for step, (data, _) in enumerate(trainloader): # training netD real_cpu = data.to(device) b_size = real_cpu.size(0) netD.zero_grad() noise = torch.randn(b_size, nz, 1, device=device) fake = netG(noise) loss_D = -torch.mean(netD(real_cpu)) + torch.mean(netD(fake)) loss_D.backward() optimizerD.step() for p in netD.parameters(): p.data.clamp_(-clip_value, clip_value) if step % n_critic == 0: # training netG noise = torch.randn(b_size, nz, 1, device=device) netG.zero_grad() fake = netG(noise) loss_G = -torch.mean(netD(fake)) netD.zero_grad() netG.zero_grad() loss_G.backward() optimizerG.step() if step % 5 == 0: print('[%d/%d][%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f' % (epoch, epoch_num, step, len(trainloader), loss_D.item(), loss_G.item())) # save training process with torch.no_grad(): fake = netG(fixed_noise).detach().cpu() f, a = plt.subplots(4, 4, figsize=(8, 8)) for i in range(4): for j in range(4): a[i][j].plot(fake[i * 4 + j].view(-1)) a[i][j].set_xticks(()) a[i][j].set_yticks(()) plt.savefig('./img/wgan_epoch_%d.png' % epoch) plt.close() # save model torch.save(netG, './nets/wgan_netG.pkl') torch.save(netD, './nets/wgan_netD.pkl')
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ])) # Create the dataloader dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True, num_workers=workers) # Decide which device we want to run on device = torch.device("cuda:0" if ( torch.cuda.is_available() and ngpu > 0) else "cpu") print(device) # custom weights initialization called on netG and netD # Create the generator netG = Generator(ngpu, nz + t_in, ngf, nc).to(device) netD = Discriminator(ngpu, nc, ndf, t_in).to(device) net_g_path = '/content/drive/MyDrive/Deep_Learning/projects/dcgan/continue_train/checkpoints/netG_latest.pth' net_d_path = '/content/drive/MyDrive/Deep_Learning/projects/dcgan/continue_train/checkpoints/netD_latest.pth' netG.load_state_dict(torch.load(net_g_path)) netD.load_state_dict(torch.load(net_d_path)) print("Done loading model!") # Handle multi-gpu if desired # if (device.type == 'cuda') and (ngpu > 1): # netG = nn.DataParallel(netG, list(range(ngpu))) # netD = nn.DataParallel(netD, list(range(ngpu))) # netG.apply(weights_init) # netD.apply(weights_init) netG = netG.cuda() netD = netD.cuda()
def main(): # load training data trainset = Dataset('./data/brilliant_blue') trainloader = torch.utils.data.DataLoader( trainset, batch_size=batch_size, shuffle=True ) # init netD and netG netD = Discriminator().to(device) netD.apply(weights_init) netG = Generator(nz).to(device) netG.apply(weights_init) criterion = nn.BCELoss() # used for visualzing training process fixed_noise = torch.randn(16, nz, 1, device=device) real_label = 1. fake_label = 0. optimizerD = optim.Adam(netD.parameters(), lr=lr, betas=(beta1, 0.999)) optimizerG = optim.Adam(netG.parameters(), lr=lr, betas=(beta1, 0.999)) for epoch in range(epoch_num): for step, (data, _) in enumerate(trainloader): real_cpu = data.to(device) b_size = real_cpu.size(0) # train netD label = torch.full((b_size,), real_label, dtype=torch.float, device=device) netD.zero_grad() output = netD(real_cpu).view(-1) errD_real = criterion(output, label) errD_real.backward() D_x = output.mean().item() # train netG noise = torch.randn(b_size, nz, 1, device=device) fake = netG(noise) label.fill_(fake_label) output = netD(fake.detach()).view(-1) errD_fake = criterion(output, label) errD_fake.backward() D_G_z1 = output.mean().item() errD = errD_real + errD_fake optimizerD.step() netG.zero_grad() label.fill_(real_label) output = netD(fake).view(-1) errG = criterion(output, label) errG.backward() D_G_z2 = output.mean().item() optimizerG.step() print('[%d/%d][%d/%d]\tLoss_D: %.4f\tLoss_G: %.4f\tD(x): %.4f\tD(G(z)): %.4f / %.4f' % (epoch, epoch_num, step, len(trainloader), errD.item(), errG.item(), D_x, D_G_z1, D_G_z2)) # save training process with torch.no_grad(): fake = netG(fixed_noise).detach().cpu() f, a = plt.subplots(4, 4, figsize=(8, 8)) for i in range(4): for j in range(4): a[i][j].plot(fake[i * 4 + j].view(-1)) a[i][j].set_xticks(()) a[i][j].set_yticks(()) plt.savefig('./img/dcgan_epoch_%d.png' % epoch) plt.close() # save models torch.save(netG, './nets/dcgan_netG.pkl') torch.save(netD, './nets/dcgan_netD.pkl')
transforms.Normalize((0.5, ), (0.5, )), ]) # prepare the data train_data = datasets.MNIST(root='../input/data', train=True, download=True, transform=transform) mini_train_data, mnist_restset = torch.utils.data.random_split( train_data, [int(0.9 * len(train_data)), int(0.1 * len(train_data))]) train_loader = DataLoader(mini_train_data, batch_size=batch_size, shuffle=True) # initialize models generator = Generator(nz).to(device) discriminator = Discriminator().to(device) # initialize generator weights generator.apply(weights_init) # initialize discriminator weights discriminator.apply(weights_init) print('##### GENERATOR #####') print(generator) params = list(generator.parameters()) for i in range(13): print(params[i].size()) # conv1's .weight print('######################') print('\n##### DISCRIMINATOR #####') print(discriminator) print('######################')
import engine import torch.nn.init as init # from config import * import config import torch.optim as optim import data if __name__ == "__main__": # Create the nets device = torch.device("cuda" if torch.cuda.is_available() else "cpu") dataset, dataloader = data.create_dataset() gen_net = Generator().to(device) gen_net.apply(weights_init) dis_net = Discriminator().to(device) dis_net.apply(weights_init) # Imporant. We need to add noise to images to learn properly fixed_noise = torch.randn(config.batchSize, config.nz, 1, 1, device=device) real_label = 1 fake_label = 0 criterion = nn.BCELoss() # We need 2 seperate optimizers, the Generator and the Discriminator gen_opt = optim.Adam(gen_net.parameters(), lr=config.lr, betas=(config.beta1, 0.999)) dis_opt = optim.Adam(dis_net.parameters(),
dataloader = torch.utils.data.DataLoader(dataset, batch_size=batch_size, shuffle=True, num_workers=workers) # Decide which device we want to run on device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu") # Plot some training images #real_batch = next(iter(dataloader)) #plt.figure(figsize=(8,8)) #plt.axis("off") #plt.title("Training Images") #plt.imshow(np.transpose(vutils.make_grid(real_batch[0].to(device)[:64], padding=2, normalize=True).cpu(),(1,2,0))) # Create the networks netG = Generator(ngpu).to(device) netD = Discriminator(ngpu).to(device) # Handle multi-gpu if desired if (device.type == 'cuda') and (ngpu > 1): netG = nn.DataParallel(netG, list(range(ngpu))) netD = nn.DataParallel(netD, list(range(ngpu))) # Apply the weights_init function to randomly initialize all weights # to mean=0, stdev=0.2. netG.apply(weights_init) netD.apply(weights_init) # Initialize BCELoss function criterion = nn.BCELoss() # Create batch of latent vectors that we will use to visualize
def train(z_channels, c_channels, epoch_num, batch_size, lr=0.0002, beta1=0.5, model_path='models/dcgan_checkpoint.pth'): use_cuda = torch.cuda.is_available() device = torch.device('cuda' if use_cuda else 'cpu') if use_cuda: cudnn.benchmark = True else: print("***** Warning: Cuda isn't available! *****") loader = load_mnist(batch_size) generator = Generator(z_channels, c_channels).to(device) discriminator = Discriminator(c_channels).to(device) g_optimizer = optim.Adam(generator.parameters(), lr=lr, betas=(beta1, 0.999)) d_optimizer = optim.Adam(discriminator.parameters(), lr=lr, betas=(beta1, 0.999)) start_epoch = 0 if os.path.exists(model_path): checkpoint = torch.load(model_path) generator.load_state_dict(checkpoint['g']) discriminator.load_state_dict(checkpoint['d']) g_optimizer.load_state_dict(checkpoint['g_optim']) d_optimizer.load_state_dict(checkpoint['d_optim']) start_epoch = checkpoint['epoch'] + 1 criterion = nn.BCELoss().to(device) generator.train() discriminator.train() std = 0.1 for epoch in range(start_epoch, start_epoch + epoch_num): d_loss_sum, g_loss_sum = 0, 0 print('---- epoch: %d ----' % (epoch, )) for i, (real_image, number) in enumerate(loader): real_image = real_image.to(device) image_noise = torch.randn(real_image.size(), device=device).normal_(0, std) d_optimizer.zero_grad() real_label = torch.randn(number.size(), device=device).normal_(0.9, 0.1) real_image.add_(image_noise) out = discriminator(real_image) d_real_loss = criterion(out, real_label) d_real_loss.backward() noise_z = torch.randn((number.size(0), z_channels, 1, 1), device=device) fake_image = generator(noise_z) fake_label = torch.zeros(number.size(), device=device) fake_image = fake_image.add(image_noise) out = discriminator(fake_image.detach()) d_fake_loss = criterion(out, fake_label) d_fake_loss.backward() d_optimizer.step() g_optimizer.zero_grad() out = discriminator(fake_image) g_loss = criterion(out, real_label) g_loss.backward() g_optimizer.step() d_loss_sum += d_real_loss.item() + d_fake_loss.item() g_loss_sum += g_loss.item() # if i % 10 == 0: # print(d_loss, g_loss) print('d_loss: %f \t\t g_loss: %f' % (d_loss_sum / (i + 1), g_loss_sum / (i + 1))) std *= 0.9 if epoch % 1 == 0: checkpoint = { 'g': generator.state_dict(), 'd': discriminator.state_dict(), 'g_optim': g_optimizer.state_dict(), 'd_optim': d_optimizer.state_dict(), 'epoch': epoch, } save_image(fake_image, 'out/fake_samples_epoch_%03d.png' % (epoch, ), normalize=False) torch.save(checkpoint, model_path) os.system('cp ' + model_path + ' models/model%d' % (epoch, )) print('saved!')
from torch.autograd import Variable import matplotlib.pyplot as plt import pylab import numpy as np import sys sys.path sys.path.append( '/home/luca/GitRepositories/gan-pretrained-pytorch/cifar10_dcgan') num_gpu = 1 if torch.cuda.is_available() else 0 # load the models from dcgan import Discriminator, Generator D = Discriminator(ngpu=1).eval() G = Generator(ngpu=1).eval() # load weights D.load_state_dict( torch.load( '/home/luca/GitRepositories/gan-pretrained-pytorch/cifar10_dcgan/weights/netD_epoch_199.pth', map_location=torch.device('cpu'))) G.load_state_dict( torch.load( '/home/luca/GitRepositories/gan-pretrained-pytorch/cifar10_dcgan/weights/netG_epoch_199.pth', map_location=torch.device('cpu'))) if torch.cuda.is_available(): D = D.cuda() G = G.cuda()
checkpoint =torch.load('./model/model_final.pth') # Create the generator. netG = Generator(params).to(device) # Apply the weights_init() function to randomly initialize all # weights to mean=0.0, stddev=0.2 ############# #netG.apply(weights_init) netG.load_state_dict(checkpoint['generator']) #print(netGt) # Print the model. #print(netG) ############ # Create the discriminator. netD = Discriminator(params).to(device) # Apply the weights_init() function to randomly initialize all # weights to mean=0.0, stddev=0.2 netD.load_state_dict(checkpoint['discriminator']) #netD.apply(weights_init) # Print the model. #print(netD) # Binary Cross Entropy loss function. criterion = nn.BCELoss() fixed_noise = torch.randn(128, params['nz'], 1, 1, device=device) real_label = 1 fake_label = 0
def main(): dataSize = 128 batchSize = 8 # imageSize = 32 imageSize = 64 # discCheckpointPath = r'E:\projects\visus\PyTorch-GAN\implementations\dcgan\checkpoints\2020_07_10_15_53_34\disc_step4800.pth' # discCheckpointPath = r'E:\projects\visus\pytorch-examples\dcgan\out\netD_epoch_24.pth' discCheckpointPath = None gpu = torch.device('cuda') # imageDataset = CatDataset( # imageSubdirPath=r'E:\data\cat-vs-dog\cat', # transform=transforms.Compose( # [ # transforms.Resize((imageSize, imageSize)), # transforms.ToTensor(), # transforms.Normalize([0.5], [0.5]) # ] # ) # ) imageDataset = datasets.CIFAR10(root=r'e:\data\images\cifar10', download=True, transform=transforms.Compose([ transforms.Resize((imageSize, imageSize)), transforms.ToTensor(), # transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), transforms.Normalize([0.5], [0.5]), ])) # For now we normalize the vectors to have norm 1, but don't make sure # that the data has certain mean/std. pointDataset = AuthorDataset( jsonPath=r'E:\out\scripts\metaphor-vis\authors-all.json' ) imageLoader = DataLoader(imageDataset, batch_size=batchSize, sampler=InfiniteSampler(imageDataset)) pointLoader = DataLoader(pointDataset, batch_size=batchSize, sampler=InfiniteSampler(pointDataset)) # Generate a random distance matrix. # # Make a matrix with positive values. # distancesCpu = np.clip(np.random.normal(0.5, 1.0 / 3, (dataSize, dataSize)), 0, 1) # # Make it symmetrical. # distancesCpu = np.matmul(distancesCpu, distancesCpu.T) # Generate random points and compute distances, guaranteeing that the triangle rule isn't broken. # randomPoints = generate_points(dataSize) # distancesCpu = scipy.spatial.distance_matrix(randomPoints, randomPoints, p=2) # catImagePath = os.path.expandvars(r'${DEV_METAPHOR_DATA_PATH}/cats/cat.247.jpg') # catImage = skimage.transform.resize(imageio.imread(catImagePath), (64, 64), 1).transpose(2, 0, 1) # imagesInitCpu = np.clip(np.random.normal(0.5, 0.5 / 3, (dataSize, 3, imageSize, imageSize)), 0, 1) # imagesInitCpu = np.clip(np.tile(catImage, (dataSize, 1, 1, 1)) + np.random.normal(0., 0.5 / 6, (dataSize, 3, 64, 64)), 0, 1) # images = torch.tensor(imagesInitCpu, requires_grad=True, dtype=torch.float32, device=gpu) scale = torch.tensor(4.0, requires_grad=True, dtype=torch.float32, device=gpu) lossModel = models.PerceptualLoss(model='net-lin', net='vgg', use_gpu=True).to(gpu) bceLoss = torch.nn.BCELoss() # discriminator = Discriminator(imageSize, 3) discriminator = Discriminator(3, 64, 1) if discCheckpointPath: discriminator.load_state_dict(torch.load(discCheckpointPath)) else: discriminator.init_params() discriminator = discriminator.to(gpu) generator = Generator(nz=pointDataset[0][0].shape[0], ngf=64) generator.init_params() generator = generator.to(gpu) # todo init properly, if training # discriminator.apply(weights_init_normal) # optimizerImages = torch.optim.Adam([images, scale], lr=1e-2, betas=(0.9, 0.999)) optimizerScale = torch.optim.Adam([scale], lr=0.001) optimizerGen = torch.optim.Adam(generator.parameters(), lr=0.0002, betas=(0.5, 0.999)) # optimizerDisc = torch.optim.Adam(discriminator.parameters(), lr=2e-4, betas=(0.9, 0.999)) optimizerDisc = torch.optim.Adam(discriminator.parameters(), lr=0.0002, betas=(0.5, 0.999)) import matplotlib.pyplot as plt fig, axes = plt.subplots(nrows=2 * 2, ncols=batchSize // 2) fig2 = plt.figure() ax2 = fig2.add_subplot(1, 1, 1) outPath = os.path.join('runs', datetime.datetime.today().strftime('%Y_%m_%d_%H_%M_%S')) os.makedirs(outPath) imageIter = iter(imageLoader) pointIter = iter(pointLoader) for batchIndex in range(10000): imageBatchReal, _ = next(imageIter) # type: Tuple(torch.Tensor, Any) imageBatchReal = imageBatchReal.to(gpu) # imageBatchReal = torch.tensor(realImageBatchCpu, device=gpu) # noinspection PyTypeChecker # randomIndices = np.random.randint(0, dataSize, batchSize).tolist() # type: List[int] # # randomIndices = list(range(dataSize)) # type: List[int] # distanceBatch = torch.tensor(distancesCpu[randomIndices, :][:, randomIndices], dtype=torch.float32, device=gpu) # imageBatchFake = images[randomIndices].contiguous() vectorBatch, _ = next(pointIter) vectorBatch = vectorBatch.to(gpu) distanceBatch = l2_sqr_dist_matrix(vectorBatch) # In-batch vector distances. imageBatchFake = generator(vectorBatch[:, :, None, None].float()) # todo It's possible to compute this more efficiently, but would require re-implementing lpips. distImages = lossModel.forward(imageBatchFake.repeat(repeats=(batchSize, 1, 1, 1)).contiguous(), imageBatchFake.repeat_interleave(repeats=batchSize, dim=0).contiguous(), normalize=True) distPredMat = distImages.reshape((batchSize, batchSize)) lossDist = torch.sum((distanceBatch - distPredMat * scale) ** 2) # MSE discPred = discriminator(imageBatchFake) lossRealness = bceLoss(discPred, torch.ones(imageBatchFake.shape[0], device=gpu)) lossGen = lossDist + 1.0 * lossRealness optimizerGen.zero_grad() optimizerScale.zero_grad() lossGen.backward() optimizerGen.step() optimizerScale.step() lossDiscReal = bceLoss(discriminator(imageBatchReal), torch.ones(imageBatchReal.shape[0], device=gpu)) lossDiscFake = bceLoss(discriminator(imageBatchFake.detach()), torch.zeros(imageBatchFake.shape[0], device=gpu)) lossDisc = (lossDiscFake + lossDiscReal) / 2 # lossDisc = torch.tensor(0) optimizerDisc.zero_grad() lossDisc.backward() optimizerDisc.step() # with torch.no_grad(): # # todo We're clamping all the images every batch, can we clamp only the ones updated? # # images = torch.clamp(images, 0, 1) # For some reason this was making the training worse. # images.data = torch.clamp(images.data, 0, 1) if batchIndex % 100 == 0: msg = 'iter {}, loss gen {:.3f}, loss dist {:.3f}, loss real {:.3f}, loss disc {:.3f}, scale: {:.3f}'.format( batchIndex, lossGen.item(), lossDist.item(), lossRealness.item(), lossDisc.item(), scale.item() ) print(msg) def gpu_images_to_numpy(images): imagesNumpy = images.cpu().data.numpy().transpose(0, 2, 3, 1) imagesNumpy = (imagesNumpy + 1) / 2 return imagesNumpy # print(discPred.tolist()) imageBatchFakeCpu = gpu_images_to_numpy(imageBatchFake) imageBatchRealCpu = gpu_images_to_numpy(imageBatchReal) for i, ax in enumerate(axes.flatten()[:batchSize]): ax.imshow(imageBatchFakeCpu[i]) for i, ax in enumerate(axes.flatten()[batchSize:]): ax.imshow(imageBatchRealCpu[i]) fig.suptitle(msg) with torch.no_grad(): points = np.asarray([pointDataset[i][0] for i in range(200)], dtype=np.float32) images = gpu_images_to_numpy(generator(torch.tensor(points[..., None, None], device=gpu))) authorVectorsProj = umap.UMAP(n_neighbors=5, random_state=1337).fit_transform(points) plot_image_scatter(ax2, authorVectorsProj, images, downscaleRatio=2) fig.savefig(os.path.join(outPath, f'batch_{batchIndex}.png')) fig2.savefig(os.path.join(outPath, f'scatter_{batchIndex}.png')) plt.close(fig) plt.close(fig2) with torch.no_grad(): imageNumber = 48 points = np.asarray([pointDataset[i][0] for i in range(imageNumber)], dtype=np.float32) imagesGpu = generator(torch.tensor(points[..., None, None], device=gpu)) # Compute LPIPS distances, batch to avoid memory issues. bs = 8 assert imageNumber % bs == 0 distImages = np.zeros((imagesGpu.shape[0], imagesGpu.shape[0])) for i in range(imageNumber // bs): startA, endA = i * bs, (i + 1) * bs imagesA = imagesGpu[startA:endA] for j in range(imageNumber // bs): startB, endB = j * bs, (j + 1) * bs imagesB = imagesGpu[startB:endB] distBatch = lossModel.forward(imagesA.repeat(repeats=(bs, 1, 1, 1)).contiguous(), imagesB.repeat_interleave(repeats=bs, dim=0).contiguous(), normalize=True).cpu().numpy() distImages[startA:endA, startB:endB] = distBatch.reshape((bs, bs)) # Move to the CPU and append an alpha channel for rendering. images = gpu_images_to_numpy(imagesGpu) images = [np.concatenate([im, np.ones(im.shape[:-1] + (1,))], axis=-1) for im in images] distPoints = l2_sqr_dist_matrix(torch.tensor(points, dtype=torch.double)).numpy() assert np.abs(distPoints - distPoints.T).max() < 1e-5 distPoints = np.minimum(distPoints, distPoints.T) # Remove rounding errors, guarantee symmetry. config = DistanceMatrixConfig() config.dataRange = (0., 4.) render_distance_matrix(os.path.join(outPath, f'dist_point_{batchIndex}.png'), distPoints, images, config) assert np.abs(distImages - distImages.T).max() < 1e-5 distImages = np.minimum(distImages, distImages.T) # Remove rounding errors, guarantee symmetry. config = DistanceMatrixConfig() config.dataRange = (0., 1.) render_distance_matrix(os.path.join(outPath, f'dist_images_{batchIndex}.png'), distImages, images, config) torch.save(generator.state_dict(), os.path.join(outPath, 'gen_{}.pth'.format(batchIndex))) torch.save(discriminator.state_dict(), os.path.join(outPath, 'disc_{}.pth'.format(batchIndex)))