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
# 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 # Optimizer for the discriminator. optimizerD = optim.Adam(netD.parameters(), lr=params['lr'], betas=(params['beta1'], 0.999)) # Optimizer for the generator.
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] for i in range(1,101):
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!')
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)
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() # Initialize BCELoss function criterion = nn.BCELoss() fixed_noise = torch.randn(64, nz + t_in, 1, 1, device=device) real_label = 1.
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) if torch.cuda.is_available(): fixed_noise = fixed_noise.cuda() fake_images = G(fixed_noise) # z = torch.randn(batch_size, latent_size).cuda() # z = Variable(z)
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() batch_size = 25 latent_size = 100 from brancher.variables import ProbabilisticModel from brancher.standard_variables import NormalVariable, DeterministicVariable import brancher.functions as BF
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'], betas=(params['beta1'], 0.999)) optimizerD.load_state_dict(state_dict['optimizerD'])
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 optimizerD = optim.Adam(netD.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999)) optimizerG = optim.Adam(netG.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999))
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)))
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()
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)))
def train(): os.makedirs('log', exist_ok=True) ds = datasets.ImageFolder(root=data_root, transform=transforms.Compose([ transforms.Resize(image_size), transforms.CenterCrop(image_size), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ])) dataloader = DataLoader(ds, batch_size=batch_size, shuffle=True) net_g = Generator(n_latent_vector, n_g_filters).to(device) net_g.apply(weight_init) net_d = Discriminator(n_d_filters).to(device) net_d.apply(weight_init) if os.path.exists(model_save_path): all_state_dict = torch.load(model_save_path) net_d.load_state_dict(all_state_dict['d_state_dict']) net_g.load_state_dict(all_state_dict['g_state_dict']) print('model restored from {}'.format(model_save_path)) criterion = nn.BCELoss() fixed_noise = torch.randn(1, n_latent_vector, 1, 1, device=device) real_label = 1 fake_label = 0 optimizer_d = optim.Adam(net_d.parameters(), lr=lr, betas=(0.5, 0.999)) optimizer_g = optim.Adam(net_g.parameters(), lr=lr, betas=(0.5, 0.999)) print('start training...') try: for epoch in range(epochs): for i, data in enumerate(dataloader, 0): # update Discrinimator, maximize d loss net_d.zero_grad() real_cpu = data[0].to(device) b_size = real_cpu.size(0) label = torch.full((b_size,), real_label, device=device) output = net_d(real_cpu).view(-1) err_d_real = criterion(output, label) err_d_real.backward() d_x = output.mean().item() # train with fake batch noise = torch.randn(b_size, n_latent_vector, 1, 1, device=device) fake = net_g(noise) label.fill_(fake_label) output = net_d(fake.detach()).view(-1) err_d_fake = criterion(output, label) err_d_fake.backward() d_g_z1 = output.mean().item() err_d = err_d_real + err_d_fake optimizer_d.step() # update Generator net_g.zero_grad() label.fill_(real_label) output = net_d(fake).view(-1) err_g = criterion(output, label) err_g.backward() d_g_z2 = output.mean().item() optimizer_d.step() if i % 50 == 0: print(f'Epoch: {epoch}, loss_d: {err_d.item()}, loss_g: {err_g.item()}') if epoch % 2 == 0 and epoch != 0: with torch.no_grad(): fake = net_g(fixed_noise).detach().cpu().numpy() print(fake.shape) fake = np.transpose(np.squeeze(fake, axis=0), (1, 2, 0)) print(fake.shape) cv2.imwrite('log/{}_fake.png'.format(epoch), fake) print('record a fake image to local.') except KeyboardInterrupt: print('interrupted, try saving the model') all_state_dict = { 'd_state_dict': net_d.state_dict(), 'g_state_dict': net_g.state_dict(), } torch.save(all_state_dict, model_save_path) print('model saved...')