def __init__( self, device, model, n_labels, n_channels, target, lr, l_inf_bound, alpha, beta, gamma, kappa, c, n_steps_D, n_steps_G, ): self.device = device self.n_labels = n_labels self.model = model self.target = target self.lr = lr self.l_inf_bound = l_inf_bound self.alpha = alpha self.beta = beta self.gamma = gamma self.kappa = kappa self.c = c self.n_steps_D = n_steps_D self.n_steps_G = n_steps_G self.G = models.Generator(n_channels, n_channels, target).to(device) self.D = models.Discriminator(n_channels).to(device) # initialize all weights self.G.apply(init_weights) self.D.apply(init_weights) # initialize optimizers self.optimizer_G = torch.optim.Adam(self.G.parameters(), lr=self.lr) self.optimizer_D = torch.optim.Adam(self.D.parameters(), lr=self.lr) if not os.path.exists(models_path): os.makedirs(models_path) if not os.path.exists('{}{}/'.format(losses_path, target)): os.makedirs('{}{}/'.format(losses_path, target))
def _create_models(self) -> None: cfg = self._config device = self._device self._generator = models.Generator(style_code_dim=cfg.style_code_dim, ) self._generator_ema = models.Generator( style_code_dim=cfg.style_code_dim, ) self._generator_ema.load_state_dict(self._generator.state_dict()) self._mapping = models.Mapping( latent_dim=cfg.mapper_latent_code_dim, hidden_dim=cfg.mapper_hidden_dim, out_dim=cfg.style_code_dim, num_shared_layers=cfg.mapper_shared_layers, num_heads=cfg.num_domains, ) self._mapping_ema = models.Mapping( latent_dim=cfg.mapper_latent_code_dim, hidden_dim=cfg.mapper_hidden_dim, out_dim=cfg.style_code_dim, num_shared_layers=cfg.mapper_shared_layers, num_heads=cfg.num_domains, ) self._mapping_ema.load_state_dict(self._mapping.state_dict()) self._style_encoder = models.StyleEncoder( style_code_dim=cfg.style_code_dim, num_heads=cfg.num_domains, ) self._style_encoder_ema = models.StyleEncoder( style_code_dim=cfg.style_code_dim, num_heads=cfg.num_domains, ) self._style_encoder_ema.load_state_dict( self._style_encoder.state_dict()) self._discriminator = models.Discriminator(num_heads=cfg.num_domains, ) self._generator.to(device) self._generator_ema.eval().to(device) self._mapping.to(device) self._mapping_ema.eval().to(device) self._style_encoder.to(device) self._style_encoder_ema.eval().to(device) self._discriminator.to(device)
def prepare_gan(self): self.replay = replay.ReplayBuffer(self.args, self.input_shape) self.replay_dequeue = \ self.replay_queue.dequeue_many(self.args.disc_batch_size) self.replay_thread = rl_utils.ReplayThread( self.replay, self.replay_dequeue) self.local_disc = models.Discriminator( self.args, self.input_shape, "local") self.disc_sync = ut.tf.get_sync_op( self.global_disc.var_list, self.local_disc.var_list)
def set_network(depth, ctx, ngf): # Pixel2pixel networks #netG = models.CEGenerator(in_channels=3, n_layers=depth, ndf=ngf) # UnetGenerator(in_channels=3, num_downs=8) # netEn = models.Encoder(in_channels=3, n_layers=depth, ndf=ngf) # UnetGenerator(in_channels=3, num_downs=8) # netDe = models.Decoder(in_channels=3, n_layers=depth, ndf=ngf) # UnetGenerator(in_channels=3, num_downs=8) # netD = models.Discriminator(in_channels=3, n_layers =depth, ndf=ngf, isthreeway=True) # Initialize parameters models.network_init(netDe, ctx=ctx) models.network_init(netEn, ctx=ctx) models.network_init(netD, ctx=ctx) return netEn, netDe, netD
def __init__(self, opt): self.opt = opt self.G = models.Generator(opt) self.D = models.Discriminator() self.Lsloss = torch.nn.MSELoss() self.optimizerG = torch.optim.Adam(self.G.parameters(), 1e-4, (0, 0.999)) self.optimizerD = torch.optim.Adam(self.D.parameters(), 4e-4, (0, 0.999)) self.d_losses = [] self.g_losses = [] self.G.cuda() self.D.cuda() self.G.apply(self.weights_init) self.D.apply(self.weights_init) self.latent_ebdy_generator = Get_Latent_Y(opt)
def __init__(self, device): super().__init__() self.device = device self.SZ = 128 self.C = 3 self.nMask = 2 self.latent = 32 nf = 64 self.EncM = models.EncM(sizex=self.SZ, nIn=self.C, nMasks=self.nMask, nRes=3, nf=nf, temperature=1).to(device) self.GenX = models.GenX(sizex=self.SZ, nOut=self.C, nc=self.latent, nf=nf, nMasks=self.nMask, selfAtt=False).to(device) self.RecZ = models.RecZ(sizex=self.SZ, nIn=self.C, nc=self.latent, nf=nf, nMasks=self.nMask).to(device) self.D = models.Discriminator(nIn=self.C, nf=nf, selfAtt=False).to(device) self.rd = torch.distributions.Normal(0, 1) self.optEncM = optim.Adam(self.EncM.parameters(), lr=1e-5, betas=(0, 0.9), weight_decay=1e-4, amsgrad=False) self.optGenX = optim.Adam(self.GenX.parameters(), lr=1e-4, betas=(0, 0.9), amsgrad=False) self.optRecZ = optim.Adam(self.RecZ.parameters(), lr=1e-4, betas=(0, 0.9), amsgrad=False) self.optD = optim.Adam(self.D.parameters(), lr=1e-4, betas=(0, 0.9), amsgrad=False)
def createModels(self): """ This function will create models and their optimizers """ self.gen = models.Generator().cuda() self.disc = models.Discriminator().cuda() self.gOptimizer = Adam(self.gen.parameters(), lr=self.gLR, betas=(0.0, 0.99)) self.dOptimizer = Adam(self.disc.parameters(), lr=self.dLR, betas=(0.0, 0.99)) print( 'Models Instantiated. # of trainable parameters Disc:%e; Gen:%e' % (sum([np.prod([*p.size()]) for p in self.disc.parameters()]), sum([np.prod([*p.size()]) for p in self.gen.parameters()])))
def __init__(self, opt, logger): self.opt = opt self.isTrain = opt.isTrain self.lr = opt.lr self.every = opt.every self.epoch = 0 self.best_loss = float("inf") self.idt_name = opt.idt_name self.logdir = opt.logdir self.logger = logger # loss self.criterionGAN = GANLoss(gan_mode='mse').cuda() self.criterionL1 = nn.L1Loss() # G self.netG = models.APBNet() self.netG.apply(weight_init) if opt.resume: checkpoint = torch.load('{}/{}.pth'.format( self.logdir, opt.resume_epoch if opt.resume_epoch else '{}_best'.format(self.idt_name))) self.netG.load_state_dict(checkpoint['net_G']) self.epoch = checkpoint['epoch'] self.netG.cuda() # D if self.isTrain: # define discriminators self.netD = models.Discriminator() self.netD.apply(weight_init) if opt.resume: self.netD.load_state_dict(checkpoint['net_D']) self.netD.cuda() self.netD_poseye = models.Discriminator_poseeye() self.netD_poseye.apply(weight_init) if opt.resume: self.netD_poseye.load_state_dict(checkpoint['net_D_poseeye']) self.netD_poseye.cuda() self.optimizer_G = torch.optim.Adam(self.netG.parameters(), lr=self.lr, betas=(0.99, 0.999)) self.optimizer_D = torch.optim.Adam(self.netD.parameters(), lr=self.lr, betas=(0.99, 0.999)) self.optimizer_D_poseeye = torch.optim.Adam( self.netD_poseye.parameters(), lr=self.lr, betas=(0.99, 0.999))
def __init__(self,opt): self.opt = opt self.G = models.Generator(opt) self.D = models.Discriminator(opt) self.Lsloss = torch.nn.MSELoss() self.optimizerG = torch.optim.Adam(self.G.parameters(),opt.g_lr,opt.g_betas) self.optimizerD = torch.optim.Adam(self.D.parameters(),opt.d_lr,opt.d_betas) self.d_losses = [] self.wd_losses = [] self.g_losses = [] self.wg_losses = [] self.losses = {'d':[],'g':[],'wd':[],'wg':[]} self.G.cuda() self.D.cuda() self.G.apply(self.weights_init) self.D.apply(self.weights_init) self.latent_ebdy_generator = Get_Latent_Y(opt) self.generate_imgs_count = 0 if False: self.G.share_memory() self.processes = [] self.main_to_thread1 = mp.Queue(maxsize=1) self.main_to_thread2 = mp.Queue(maxsize=1) self.main_to_thread3 = mp.Queue(maxsize=1) self.main_to_thread4 = mp.Queue(maxsize=1) self.thread1_to_main = mp.Queue(maxsize=1) self.thread2_to_main = mp.Queue(maxsize=1) self.thread3_to_main = mp.Queue(maxsize=1) self.thread4_to_main = mp.Queue(maxsize=1) self.thread1_grad = mp.Queue(maxsize=1) self.thread2_grad = mp.Queue(maxsize=1) self.thread3_grad = mp.Queue(maxsize=1) self.thread4_grad = mp.Queue(maxsize=1) p1 = mp.Process(target=func,args=(self.G,self.main_to_thread1,self.thread1_to_main,self.thread1_grad,self.optimizerG)) p1.start() self.processes.append(p1) p2 = mp.Process(target=func,args=(self.G,self.main_to_thread2,self.thread2_to_main,self.thread2_grad,self.optimizerG)) p2.start() self.processes.append(p2) p3 = mp.Process(target=func,args=(self.G,self.main_to_thread3,self.thread3_to_main,self.thread3_grad,self.optimizerG)) p3.start() self.processes.append(p3) p4 = mp.Process(target=func,args=(self.G,self.main_to_thread4,self.thread4_to_main,self.thread4_grad,self.optimizerG)) p4.start() self.processes.append(p4)
def set_network(depth, ctx, lr, beta1, ngf): # Pixel2pixel networks #netG = models.CEGenerator(in_channels=3, n_layers=depth, ndf=ngf) # UnetGenerator(in_channels=3, num_downs=8) # netEn = models.Encoder(in_channels=3, n_layers=depth, ndf=ngf) # UnetGenerator(in_channels=3, num_downs=8) # netDe = models.Decoder(in_channels=3, n_layers=depth, ndf=ngf) # UnetGenerator(in_channels=3, num_downs=8) # netD = models.Discriminator(in_channels=3, n_layers =depth, ndf=ngf, isthreeway=True) # Initialize parameters models.network_init(netDe, ctx=ctx) models.network_init(netEn, ctx=ctx) models.network_init(netD, ctx=ctx) # trainer for the generator and the discriminator trainerEn = gluon.Trainer(netEn.collect_params(), 'adam', {'learning_rate': lr, 'beta1': beta1}) trainerDe = gluon.Trainer(netDe.collect_params(), 'adam', {'learning_rate': lr, 'beta1': beta1}) trainerD = gluon.Trainer(netD.collect_params(), 'adam', {'learning_rate': lr, 'beta1': beta1}) return netEn, netDe, netD, trainerEn, trainerDe, trainerD
def set_network(): # Pixel2pixel networks netG = models.CEGenerator( in_channels=3) # UnetGenerator(in_channels=3, num_downs=8) # netD = models.Discriminator(in_channels=6) # Initialize parameters models.network_init(netG, ctx=ctx) models.network_init(netD, ctx=ctx) # trainer for the generator and the discriminator trainerG = gluon.Trainer(netG.collect_params(), 'adam', { 'learning_rate': lr, 'beta1': beta1 }) trainerD = gluon.Trainer(netD.collect_params(), 'adam', { 'learning_rate': lr, 'beta1': beta1 }) return netG, netD, trainerG, trainerD
def __init__(self, device, model, model_num_labels, box_min, box_max): self.device = device self.model_num_labels = model_num_labels self.model = model self.box_min = box_min self.box_max = box_max self.netG = models.Generator().to(device) self.netDisc = models.Discriminator().to(device) # initialize all weights self.netG.apply(weights_init) self.netDisc.apply(weights_init) # initialize optimizers self.optimizer_G = torch.optim.Adam(self.netG.parameters(), lr=0.001) self.optimizer_D = torch.optim.Adam(self.netDisc.parameters(), lr=0.001) if not os.path.exists(models_path): os.makedirs(models_path)
def inpaint(args): dataset = datasets.RandomPatchDataset(args.test_data_dir,weighted_mask=True, window_size=args.window_size) dataloader = DataLoader(dataset, batch_size=args.batch_size) # Loading trained GAN model saved_gan = torch.load(args.gan_path) generator = models.Generator(args).cuda() discriminator = models.Discriminator(args).cuda() generator.load_state_dict(saved_gan["state_dict_G"]) discriminator.load_state_dict(saved_gan["state_dict_D"]) for i, (corrupted_images, original_images, masks, weighted_masks) in enumerate(dataloader): corrupted_images, masks, weighted_masks = corrupted_images.cuda(), masks.cuda(), weighted_masks.cuda() z_optimum = nn.Parameter(torch.FloatTensor(np.random.normal(0, 1, (corrupted_images.shape[0],args.latent_dim,))).cuda()) optimizer_inpaint = optim.Adam([z_optimum]) print("Starting backprop to input ...") for epoch in range(args.optim_steps): optimizer_inpaint.zero_grad() generated_images = generator(z_optimum) discriminator_opinion = discriminator(generated_images) c_loss = context_loss(corrupted_images, generated_images, weighted_masks) prior_loss = torch.sum(-torch.log(discriminator_opinion)) inpaint_loss = c_loss + args.prior_weight*prior_loss inpaint_loss.backward() optimizer_inpaint.step() print("[Epoch: {}/{}] \t[Loss: \t[Context: {:.3f}] \t[Prior: {:.3f}] \t[Inpaint: {:.3f}]] \r".format(1+epoch, args.optim_steps, c_loss, prior_loss, inpaint_loss),end="") print("") blended_images = posisson_blending(masks, generated_images.detach(), corrupted_images) image_range = torch.min(corrupted_images), torch.max(corrupted_images) save_image(corrupted_images, "../outputs/corrupted_{}.png".format(i), normalize=True, range=image_range, nrow=5) save_image(generated_images, "../outputs/output_{}.png".format(i), normalize=True, range=image_range, nrow=5) save_image(blended_images, "../outputs/blended_{}.png".format(i), normalize=True, range=image_range, nrow=5) save_image(original_images, "../outputs/original_{}.png".format(i), normalize=True, range=image_range, nrow=5) del z_optimum, optimizer_inpaint
def __init__(self, opt, num_classes, source_train_ds, source_test_ds, target_train_ds, mean, std): super().__init__(opt, num_classes, source_train_ds, source_test_ds) self.target_train_ds = target_train_ds self.mean = mean self.std = std # Defining networks and optimizers self.generator = models.Generator(opt, num_classes) self.discriminator = models.Discriminator(opt, num_classes) # Weight initialization self.generator.apply(utils.weights_init) self.discriminator.apply(utils.weights_init) # Defining loss criterions self.criterion_c = nn.CrossEntropyLoss() self.criterion_s = nn.BCELoss() if opt.gpu >= 0: self.discriminator.cuda() self.generator.cuda() self.criterion_c.cuda() self.criterion_s.cuda() # Defining optimizers self.optimizer_discriminator = optim.Adam( self.discriminator.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999)) self.optimizer_generator = optim.Adam(self.generator.parameters(), lr=opt.lr, betas=(opt.beta1, 0.999)) # Other variables self.real_label_val = 1 self.fake_label_val = 0
BATCH_SIZE = 1 DIGIT = 1 CHANCE_DELUDE = 0.5 LR1 = 0.001 LR2 = 0.001 # define transform and dataloader transform = torchvision.transforms.Compose( [torchvision.transforms.Normalize((0.1307, ), (0.3081, ))]) dataloader = torch.utils.data.DataLoader(ds.MNISTDiscriminatorDataset( transform, digit=DIGIT), batch_size=BATCH_SIZE, shuffle=True) # define model and optimizer discriminator = m.Discriminator() generator = m.Generator() criterion = nn.CrossEntropyLoss() optimizer1 = optim.SGD(discriminator.parameters(), lr=LR1) optimizer2 = optim.SGD(generator.parameters(), lr=LR2) snaps = os.listdir('./snaps') snaps.remove('.gitkeep') if len(snaps) > 0: latest = max(snaps) path = f'./snaps/{latest}' torch_object = torch.load(path)
assert args.beta_2 > 0 assert args.n_epochs > 0 assert args.n_iters > 0 assert args.noise_std >= 0 assert args.shift_pct <= 1 and args.shift_pct >= 0 os.makedirs('run', exist_ok=True) random.seed(args.seed) np.random.seed(args.seed) torch.manual_seed(args.seed) torch.cuda.manual_seed(args.seed) torch.backends.cudnn.deterministic = True G = models.Generator(args.hid_dim, args.z_dim, args.v_dim, args.image_channels) D = models.Discriminator(args.hid_dim, args.v_dim, args.image_channels) def initialize_params(m): """ initialize neural network parameters """ if isinstance(m, nn.ConvTranspose2d) or isinstance(m, nn.Conv2d): m.weight.data.normal_(mean=0, std=args.init_std) m.bias.data.zero_() G.apply(initialize_params) D.apply(initialize_params) device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
def main(): trn_ds = loaders.SatelliteDataset(TRAIN_IMAGES_ROOT, HR_PATCH, scale_factor=SCALE) trn_dl = DataLoader(trn_ds, TRAIN_BATCH_SIZE, shuffle=True, num_workers=WORKERS) val_ds = loaders.SatelliteValDataset(VAL_IMAGES_ROOT, HR_PATCH, scale_factor=SCALE) val_dl = DataLoader(val_ds, VALID_BATCH_SIZE, shuffle=False, num_workers=WORKERS) start_epoch = 1 best_val_loss = float('inf') # Generator G = models.GeneratorBNFirst(3, 3, upscale=SCALE) opt_G = optim.Adam(G.parameters(), lr=LR_G) sched_G = optim.lr_scheduler.StepLR(opt_G, LR_STEP, gamma=LR_DECAY) # Discriminator D = models.Discriminator(48, HR_PATCH[0], sigmoid=True) opt_D = optim.Adam(D.parameters(), lr=LR_D) sched_D = optim.lr_scheduler.StepLR(opt_D, LR_STEP, gamma=LR_DECAY) if not os.path.exists(WEIGHTS_SAVE_PATH): os.mkdir(WEIGHTS_SAVE_PATH) if LOAD_CHECKPOINT is not None: checkpoint = torch.load(LOAD_CHECKPOINT, pickle_module=dill) start_epoch = checkpoint['epoch'] G.load_state_dict(checkpoint['G_state_dict']) D.load_state_dict(checkpoint['D_state_dict']) opt_G = checkpoint['optimizer_G'] opt_D = checkpoint['optimizer_D'] sched_G = checkpoint['lr_scheduler_G'] sched_D = checkpoint['lr_scheduler_D'] best_val_loss = checkpoint['best_metrics'] G.to(DEVICE) D.to(DEVICE) # Losses content_loss = losses.ContentLoss(VGG_FEATURE_LAYER, 'l2') content_loss.to(DEVICE) adv_loss = losses.AdversarialLoss() adv_loss.to(DEVICE) MSE = nn.MSELoss() MSE.to(DEVICE) train_losses = BookKeeping(TENSORBOARD_LOGDIR, suffix='trn') val_losses = BookKeeping(TENSORBOARD_LOGDIR, suffix='val') for epoch in range(start_epoch, EPOCHS + 1): # Training loop train(G, D, trn_dl, epoch, EPOCHS, content_loss, MSE, adv_loss, opt_G, opt_D, train_losses) # Validation loop best_val_loss = evaluate(G, D, val_dl, epoch, EPOCHS, content_loss, MSE, adv_loss, val_losses, best_val_loss) sched_G.step() sched_D.step() save_checkpoint(epoch, G, D, None, opt_G, sched_G, opt_D, sched_D) train_losses.update_tensorboard(epoch) val_losses.update_tensorboard(epoch) # Reset all loss for a new epoch train_losses.reset() val_losses.reset() # Save real vs fake samples for quality inspection generator = iter(val_dl) for j in range(BATCHES_TO_SAVE): lrs, hrs = next(generator) fakes = G(lrs.to(DEVICE)) # Save samples at the end save_images(END_EPOCH_SAVE_SAMPLES_PATH, lrs.detach().cpu(), fakes.detach().cpu(), hrs, epoch, j)
import numpy as np import dataset import helpers import models from config import * assert (file_name == 'vae-gan') writer, save_dir = helpers.init(gpu, file_name, experiment_name) use_ganloss = True # TODO can I use that here? use_vaeloss = True use_instancenoise = False iter_decay = 1000. # iterations after which instance noise is at 1/e # models # TODO to one model? netD = models.Discriminator().cuda() netG = models.Generator().cuda() netE = models.Encoder().cuda() # weight initialization netD.apply(models.init_weights) # xavier init netE.apply(models.init_weights) # xavier init netG.apply(models.init_weights) # xavier init criterion = nn.BCELoss().cuda() optD = torch.optim.Adam(netD.parameters(), lr=lr) optG = torch.optim.Adam(netG.parameters(), lr=lr) optE = torch.optim.Adam(netE.parameters(), lr=lr) def sample(n_samples):
def __init__(self, config): self.rank, self.world_size = 0, 1 if config['dist']: self.rank = dist.get_rank() self.world_size = dist.get_world_size() self.config = config self.mode = config['dgp_mode'] self.custom_mask = config['custom_mask'] self.update_G = config['update_G'] self.update_embed = config['update_embed'] self.iterations = config['iterations'] self.ftr_num = config['ftr_num'] self.ft_num = config['ft_num'] self.lr_ratio = config['lr_ratio'] self.G_lrs = config['G_lrs'] self.z_lrs = config['z_lrs'] self.use_in = config['use_in'] self.select_num = config['select_num'] self.factor = 4 # Downsample factor self.mask_path = config['mask_path'] #Create selective masking if self.custom_mask: self.mask = torch.ones(1, 1, 256, 256).cuda() x = Image.open(self.mask_path) pil_to_tensor = transforms.ToTensor()(x).unsqueeze_(0) t = Variable(torch.Tensor([0.9])) # threshold final_mask = F.interpolate(pil_to_tensor, size=(256, 256), mode='bilinear') self.mask = (final_mask > t).float() * 1 self.mask = self.mask[0][0].cuda() self.regions = self.get_regions(self.mask) ######################### # create model self.G = models.Generator(**config).cuda() self.D = models.Discriminator( **config).cuda() if config['ftr_type'] == 'Discriminator' else None self.G.optim = torch.optim.Adam( [{ 'params': self.G.get_params(i, self.update_embed) } for i in range(len(self.G.blocks) + 1)], lr=config['G_lr'], betas=(config['G_B1'], config['G_B2']), weight_decay=0, eps=1e-8) # load weights if config['random_G']: self.random_G() else: utils.load_weights(self.G if not (config['use_ema']) else None, self.D, config['weights_root'], name_suffix=config['load_weights'], G_ema=self.G if config['use_ema'] else None, strict=False) self.G.eval() if self.D is not None: self.D.eval() self.G_weight = deepcopy(self.G.state_dict()) # prepare latent variable and optimizer self._prepare_latent() # prepare learning rate scheduler self.G_scheduler = utils.LRScheduler(self.G.optim, config['warm_up']) self.z_scheduler = utils.LRScheduler(self.z_optim, config['warm_up']) # loss functions self.mse = torch.nn.MSELoss() if config['ftr_type'] == 'Discriminator': self.ftr_net = self.D self.criterion = utils.DiscriminatorLoss( ftr_num=config['ftr_num'][0]) else: vgg = torchvision.models.vgg16(pretrained=True).cuda().eval() self.ftr_net = models.subsequence(vgg.features, last_layer='20') self.criterion = utils.PerceptLoss() # Downsampler for producing low-resolution image self.downsampler = Downsampler(n_planes=3, factor=self.factor, kernel_type='lanczos2', phase=0.5, preserve_size=True).type( torch.cuda.FloatTensor)
def __init__(self, hyperparameters): super(Model, self).__init__() self.device = hyperparameters['device'] self.auxiliary_data_source = hyperparameters['auxiliary_data_source'] self.all_data_sources = ['resnet_features', self.auxiliary_data_source] self.DATASET = hyperparameters['dataset'] self.num_shots = hyperparameters['num_shots'] self.latent_size = hyperparameters['latent_size'] self.batch_size = hyperparameters['batch_size'] self.hidden_size_rule = hyperparameters['hidden_size_rule'] self.warmup = hyperparameters['model_specifics']['warmup'] self.generalized = hyperparameters['generalized'] self.classifier_batch_size = 32 self.img_seen_samples = hyperparameters['samples_per_class'][ self.DATASET][0] self.att_seen_samples = hyperparameters['samples_per_class'][ self.DATASET][1] self.att_unseen_samples = hyperparameters['samples_per_class'][ self.DATASET][2] self.img_unseen_samples = hyperparameters['samples_per_class'][ self.DATASET][3] self.reco_loss_function = hyperparameters['loss'] self.nepoch = hyperparameters['epochs'] self.lr_cls = hyperparameters['lr_cls'] self.cross_reconstruction = hyperparameters['model_specifics'][ 'cross_reconstruction'] self.cls_train_epochs = hyperparameters['cls_train_steps'] self.dataset = dataloader(self.DATASET, copy.deepcopy(self.auxiliary_data_source), device=self.device) self.writer = SummaryWriter() self.num_gen_iter = hyperparameters['num_gen_iter'] self.num_dis_iter = hyperparameters['num_dis_iter'] self.pretrain = hyperparameters['pretrain'] if self.DATASET == 'CUB': self.num_classes = 200 self.num_novel_classes = 50 elif self.DATASET == 'SUN': self.num_classes = 717 self.num_novel_classes = 72 elif self.DATASET == 'AWA1' or self.DATASET == 'AWA2': self.num_classes = 50 self.num_novel_classes = 10 feature_dimensions = [2048, self.dataset.aux_data.size(1)] # Here, the encoders and decoders for all modalities are created and put into dict self.encoder = {} for datatype, dim in zip(self.all_data_sources, feature_dimensions): self.encoder[datatype] = models.encoder_template( dim, self.latent_size, self.hidden_size_rule[datatype], self.device) print(str(datatype) + ' ' + str(dim)) print('latent size ' + str(self.latent_size)) self.decoder = {} for datatype, dim in zip(self.all_data_sources, feature_dimensions): self.decoder[datatype] = models.decoder_template( self.latent_size, dim, self.hidden_size_rule[datatype], self.device) # An optimizer for all encoders and decoders is defined here parameters_to_optimize = list(self.parameters()) for datatype in self.all_data_sources: parameters_to_optimize += list(self.encoder[datatype].parameters()) parameters_to_optimize += list(self.decoder[datatype].parameters()) # The discriminator network is defined here self.net_D_Att = models.Discriminator( self.dataset.aux_data.size(1) + 2048, self.device) self.net_D_Img = models.Discriminator( 2048 + self.dataset.aux_data.size(1), self.device) self.optimizer_G = optim.Adam(parameters_to_optimize, lr=hyperparameters['lr_gen_model'], betas=(0.9, 0.999), eps=1e-08, weight_decay=0.0005, amsgrad=True) self.optimizer_D = optim.Adam(itertools.chain( self.net_D_Att.parameters(), self.net_D_Img.parameters()), lr=hyperparameters['lr_gen_model'], betas=(0.5, 0.999), weight_decay=0.0005) if self.reco_loss_function == 'l2': self.reconstruction_criterion = nn.MSELoss(reduction='sum') elif self.reco_loss_function == 'l1': self.reconstruction_criterion = nn.L1Loss(reduction='sum') self.MSE = nn.MSELoss(reduction='sum') self.L1 = nn.L1Loss(reduction='sum') self.att_fake_from_att_sample = utils.Sample_from_Pool() self.att_fake_from_img_sample = utils.Sample_from_Pool() self.img_fake_from_img_sample = utils.Sample_from_Pool() self.img_fake_from_att_sample = utils.Sample_from_Pool() if self.generalized: print('mode: gzsl') self.clf = LINEAR_LOGSOFTMAX(self.latent_size, self.num_classes) else: print('mode: zsl') self.clf = LINEAR_LOGSOFTMAX(self.latent_size, self.num_novel_classes)
def main(): # Preparing dataset and loader _ = datasets.MNIST(root='data', train=True, download=True, transform=None) train_data = MNISTLayoutDataset() train_loader = DataLoader(train_data, batch_size=args.batchsize) # Defining required variables element_num = 128 feature_size = 3 feature_geo_size = 2 feature_cls_size = 1 beta1 = 0.99 beta2 = 0.95 # Setting device device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') print('Using device:', device) # Loading the models gen = models.Generator(args.batchsize, element_num, feature_geo_size, feature_cls_size).to(device) dis = models.Discriminator(args.batchsize).to(device) # Defining the optimizers g_optimizer = optim.Adam(gen.parameters(), args.lrate, (beta1, beta2)) d_optimizer = optim.Adam(dis.parameters(), args.lrate / 10, (beta1, beta2)) # Set models to training mode gen.train() dis.train() # Train for given number of epochs for epoch in range(1, args.epochs + 1): start_time = time.time() for batch_idx, real_images in enumerate(train_loader): real_images = real_images.to(device) curr_time = time.time() elp_time = curr_time - start_time print( "\rEpoch: {}/{} Batch: {}/{} Time on Epoch: {:.0f} sec".format( epoch, args.epochs, batch_idx, len(train_loader), elp_time), end="") batch_size = real_images.size(0) # TRAINING DISCRIMINATOR d_optimizer.zero_grad() # Loss for real images real_images = Variable(real_images) D_real = dis(real_images) d_real_loss = real_loss(D_real, device) # Loss for fake images z_cls = torch.FloatTensor(batch_size, element_num, feature_geo_size).uniform_(0, 1) z_geo = torch.FloatTensor(batch_size, element_num, feature_cls_size).normal_(0.5, 0.5) z = torch.cat((z_cls, z_geo), 2).to(device) fake_images_d = gen(z) D_fake = dis(fake_images_d) d_fake_loss = fake_loss(D_fake, device) # Total loss d_loss = d_real_loss + d_fake_loss d_loss.backward() d_optimizer.step() # TRAINING GENERATOR g_optimizer.zero_grad() z_cls = torch.FloatTensor(batch_size, element_num, feature_cls_size).uniform_(0, 1) z_geo = torch.FloatTensor(batch_size, element_num, feature_geo_size).normal_(0.5, 0.5) z = torch.cat((z_cls, z_geo), 2).to(device) fake_images_g = gen(z) D_out = dis(fake_images_g) g_loss = real_loss(D_out, device) g_loss.backward() g_optimizer.step() if batch_idx % 100 == 0: test_samples = 9 result_path = 'result' os.makedirs(result_path, exist_ok=True) z_cls = torch.FloatTensor(test_samples, element_num, feature_cls_size).uniform_(0, 1) z_geo = torch.FloatTensor(test_samples, element_num, feature_geo_size).normal_(0.5, 0.5) z = torch.cat((z_cls, z_geo), 2).to(device) generated_images = gen(z) generated_images = points_to_image(generated_images).view( -1, 1, 28, 28) save_image(generated_images, '{}/{}_{}.png'.format(result_path, epoch, batch_idx), nrow=3) print( "[G: Loss = {:.4f}] [D: Total Loss = {:.4f} Real Loss = {:.4f} Fake Loss {:.4f}]" .format(g_loss, d_loss, d_real_loss, d_fake_loss)) if epoch % args.save_every == 0: model_path = 'trained_models' os.makedirs(model_path, exist_ok=True) # Save Models torch.save({'state_dict': dis.state_dict()}, '{}/dis_epoch_{}.pth'.format(model_path, epoch)) torch.save({'state_dict': gen.state_dict()}, '{}/gen_epoch_{}.pth'.format(model_path, epoch))
print("GPUは使えません。") if cuda: gpu_id = input('使用するGPUの番号を入れてください : ') os.environ["CUDA_VISIBLE_DEVICES"] = gpu_id device = torch.device('cuda:' + gpu_id if cuda else 'cpu') # 推定モデルの決定 p = 7 os.makedirs("output-images/p{0}".format(p), exist_ok=True) os.makedirs("parameters/p{0}".format(p), exist_ok=True) torch.manual_seed(opt.generator_seed) generator = models.LinearGenerator(p=p, input_dim=1, is_bias=False) torch.manual_seed(opt.discriminator_seed) discriminator = models.Discriminator( q=0, discriminator_hidden_unit=opt.discriminator_hidden_unit) torch.manual_seed(opt.predictor_seed) predictor = models.LinearPredictNet(p=p, input_dim=1, is_bias=True) # 訓練データを一つ用いて学習させる dataSeed = opt.data_seed # こいつをtrain:validation=900:100に分割する Data = trainDataSets[dataSeed] Data = torch.tensor(Data, dtype=torch.float) Data = Data.view(1, -1) trainData = Data[:, :900] valData = Data[:, 900:] # trainDataとvalDataを {𝑋𝑡}𝑡0𝑡=𝑡0−𝑝 ごとに取り出しやすいようにMatrixに変換する trainMatrix = [] for i in range(trainData.shape[1] - (p)): ans = trainData[:, i:i + p + 1].view(1, Data.shape[0], -1)
parser.add_argument('--img_size', dest='img_size', type=int, default=128) parser.add_argument('--batch_size', dest='batch_size', type=int, default=1) parser.add_argument('--experiment_name', dest='experiment_name', default='stgan_128') args = parser.parse_args() # model atts = args.atts n_att = len(atts) img_size = args.img_size batch_size = args.batch_size experiment_name = args.experiment_name Gen = models.Generator() Dis = models.Discriminator(n_att) Enc = models.Encoder() Stu = models.Stu() x = tf.ones(shape=[2, 128, 128, 3], dtype=tf.float32) a = tf.ones(shape=[2, 13], dtype=tf.float32) z = Enc(x) z_stu = Stu(z, a) x_fake = Gen(z_stu, a - a) d, att = Dis(x) lr = tf.Variable(initial_value=0., trainable=False) g_opt = tf.optimizers.Adam(lr, beta_1=0., beta_2=0.99) d_opt = tf.optimizers.Adam(lr, beta_1=0., beta_2=0.99) params = tf.Variable(initial_value=[5, 0], trainable=False, dtype=tf.int64)
def train_model(): # Setup session sess = tu.setup_training_session() ########## # Innputs ########## # Setup async input queue of real images X_real = du.read_celebA() # Noise batch_size = tf.shape(X_real)[0] z_noise_for_D = tf.random_uniform(( batch_size, FLAGS.z_dim, ), minval=-1, maxval=1, name="z_input_D") z_noise_for_G = tf.random_uniform(( batch_size, FLAGS.z_dim, ), minval=-1, maxval=1, name="z_input_G") # k factor k_factor = tf.Variable(initial_value=0., trainable=False, name='anneal_factor') # learning rate lr = tf.Variable(initial_value=FLAGS.learning_rate, trainable=False, name='learning_rate') ######################## # Instantiate models ######################## G = models.Generator(nb_filters=FLAGS.nb_filters_G) D = models.Discriminator(h_dim=FLAGS.h_dim, nb_filters=FLAGS.nb_filters_D) ########## # Outputs ########## X_rec_real = D(X_real, output_name="X_rec_real") X_fake_for_D = G(z_noise_for_D, output_name="X_fake_for_D") X_rec_fake_for_D = D(X_fake_for_D, reuse=True, output_name="X_rec_fake_for_D") X_fake_for_G = G(z_noise_for_G, reuse=True, output_name="X_fake_for_G") X_rec_fake_for_G = D(X_fake_for_G, reuse=True, output_name="X_rec_fake_for_G") # output images for plots real_toplot = du.unnormalize_image(X_real, name="real_toplot") generated_toplot = du.unnormalize_image(X_fake_for_G, name="generated_toplot") real_rec_toplot = du.unnormalize_image(X_rec_real, name="rec_toplot") generated_rec_toplot = du.unnormalize_image(X_rec_fake_for_G, name="generated_rec_toplot") ########################### # Instantiate optimizers ########################### opt = tf.train.AdamOptimizer(learning_rate=lr, name='opt') ########################### # losses ########################### loss_real = losses.mae(X_real, X_rec_real) loss_fake_for_D = losses.mae(X_fake_for_D, X_rec_fake_for_D) loss_fake_for_G = losses.mae(X_fake_for_G, X_rec_fake_for_G) L_D = loss_real - k_factor * loss_fake_for_D L_G = loss_fake_for_G Convergence = loss_real + tf.abs(FLAGS.gamma * loss_real - loss_fake_for_G) ########################### # Compute updates ops ########################### dict_G_vars = G.get_trainable_variables() G_vars = [dict_G_vars[k] for k in dict_G_vars.keys()] dict_D_vars = D.get_trainable_variables() D_vars = [dict_D_vars[k] for k in dict_D_vars.keys()] G_gradvar = opt.compute_gradients(L_G, var_list=G_vars) G_update = opt.apply_gradients(G_gradvar, name='G_loss_minimize') D_gradvar = opt.compute_gradients(L_D, var_list=D_vars) D_update = opt.apply_gradients(D_gradvar, name='D_loss_minimize') update_k_factor = tf.assign( k_factor, k_factor + FLAGS.lambdak * (FLAGS.gamma * loss_real - loss_fake_for_G)) update_lr = tf.assign(lr, lr / 2) ########################## # Summary ops ########################## # Add summary for gradients tu.add_gradient_summary(G_gradvar) tu.add_gradient_summary(D_gradvar) # Add scalar symmaries for G tf.summary.scalar("G loss", L_G) # Add scalar symmaries for D tf.summary.scalar("D loss", L_D) # Add scalar symmaries for D tf.summary.scalar("k_factor", k_factor) tf.summary.scalar("Convergence", Convergence) tf.summary.scalar("learning rate", lr) summary_op = tf.summary.merge_all() ############################ # Start training ############################ # Initialize session saver = tu.initialize_session(sess) # Start queues coord, threads = du.manage_queues(sess) # Summaries writer = tu.manage_summaries(sess) # Run checks on data dimensions list_data = [z_noise_for_D, z_noise_for_G] list_data += [ X_real, X_rec_real, X_fake_for_G, X_rec_fake_for_G, X_fake_for_D, X_rec_fake_for_D ] list_data += [generated_toplot, real_toplot] output = sess.run(list_data) tu.check_data(output, list_data) for e in tqdm(range(FLAGS.nb_epoch), desc="Training progress"): # Anneal learning rate every 5 epoch if (e + 1) % 5 == 0: sess.run([update_lr]) t = tqdm(range(FLAGS.nb_batch_per_epoch), desc="Epoch %i" % e, mininterval=0.5) for batch_counter in t: output = sess.run([G_update, D_update, update_k_factor]) if batch_counter % (FLAGS.nb_batch_per_epoch // (int(0.5 * FLAGS.nb_batch_per_epoch))) == 0: output = sess.run([summary_op]) writer.add_summary( output[-1], e * FLAGS.nb_batch_per_epoch + batch_counter) t.set_description('Epoch %s:' % e) # Plot some generated images Xf, Xr, Xrrec, Xfrec = sess.run([ generated_toplot, real_toplot, real_rec_toplot, generated_rec_toplot ]) vu.save_image(Xf, Xr, title="current_batch", e=e) vu.save_image(Xrrec, Xfrec, title="reconstruction", e=e) # Save session saver.save(sess, os.path.join(FLAGS.model_dir, "model"), global_step=e) # Show data statistics output = sess.run(list_data) tu.check_data(output, list_data) # Stop threads coord.request_stop() coord.join(threads) print('Finished training!')
# pickle to avoid encoding errors with json with open(os.path.join(args.output_dir, 'charmap.pickle'), 'wb') as f: pickle.dump(charmap, f) with open(os.path.join(args.output_dir, 'inv_charmap.pickle'), 'wb') as f: pickle.dump(inv_charmap, f) real_inputs_discrete = tf.placeholder(tf.int32, shape=[args.batch_size, args.seq_length]) real_inputs = tf.one_hot(real_inputs_discrete, len(charmap)) fake_inputs = models.Generator(args.batch_size, args.seq_length, args.layer_dim, len(charmap)) fake_inputs_discrete = tf.argmax(fake_inputs, fake_inputs.get_shape().ndims - 1) disc_real = models.Discriminator(real_inputs, args.seq_length, args.layer_dim, len(charmap)) disc_fake = models.Discriminator(fake_inputs, args.seq_length, args.layer_dim, len(charmap)) disc_cost = tf.reduce_mean(disc_fake) - tf.reduce_mean(disc_real) gen_cost = -tf.reduce_mean(disc_fake) # WGAN lipschitz-penalty alpha = tf.random_uniform(shape=[args.batch_size, 1, 1], minval=0., maxval=1.) differences = fake_inputs - real_inputs interpolates = real_inputs + (alpha * differences) gradients = tf.gradients( models.Discriminator(interpolates, args.seq_length, args.layer_dim, len(charmap)), [interpolates])[0] slopes = tf.sqrt(tf.reduce_sum(tf.square(gradients), reduction_indices=[1, 2]))
import nets netG = nets.UNet11(num_classes=1, pretrained='vgg') # # ngf = 32 # use_dropout = False # norm_layer = utils.get_norm_layer(norm_type='batch') # # netG = models.ResnetGenerator(input_nc, output_nc, ngf, norm_layer=norm_layer, use_dropout=use_dropout, n_blocks=9) # netG = models.LeakyResnetGenerator(input_nc, output_nc, ngf=6, norm_layer=norm_layer, use_dropout=use_dropout, n_blocks=9) # # netG = models.LeakyResnetGenerator(input_nc, output_nc, ngf=64, norm_layer=norm_layer, use_dropout=use_dropout, n_blocks=9) # ## Unet Generator # # netG = models.UnetGenerator(input_nc, output_nc, 7, ngf=3, norm_layer=norm_layer, use_dropout=use_dropout) # utils.init_net(netG, init_type='normal', init_gain=0.02) summary(netG, input_size=(3, img_size, img_size)) ## Building Discriminator netD = models.Discriminator(input_nc=1, img_size=img_size) utils.init_net(netD, init_type='normal', init_gain=0.02) summary(netD, input_size=(1, img_size, img_size)) lr = 0.00002 G_optimizer = Adam(netG.parameters(), lr=lr, betas=(0.9, 0.999)) D_optimizer = Adam(netD.parameters(), lr=lr, betas=(0.9, 0.999)) G_model_path = root / 'G_model.pt' D_model_path = root / 'D_model.pt' if G_model_path.exists() and D_model_path.exists(): state = torch.load(str(G_model_path)) netG.load_state_dict(state['model']) state = torch.load(str(D_model_path)) epoch = state['epoch']
utils.save_image(torchvision.utils.make_grid(utils.denorm(tmp), padding=1), output_path + 'images/test_sirf_normal.png') real_image_mask_test, _ = next(iter(real_image_mask)) utils.save_image(torchvision.utils.make_grid(real_image_mask_test, padding=1), output_path + 'images/MASK_TEST.png') tmp = next(iter(sirfs_shading_val)) tmp = utils.denorm(tmp) tmp = applyMask(var(tmp).type(torch.DoubleTensor), real_image_mask_test) tmp = tmp.data utils.save_image(torchvision.utils.make_grid(tmp, padding=1), output_path + 'images/Validation_SIRFS_SHADING.png') # featureNet = ResNet(BasicBlock, [2, 2, 2, 2], 27) featureNet = models.BaseSimpleFeatureNet() lightingNet = models.LightingNet() D = models.Discriminator() # R = models.ResNet(models.BasicBlock, [2, 2, 2, 2], 27) # R = models.BaseSimpleFeatureNet() print(featureNet) print(lightingNet) featureNet = featureNet.cuda() lightingNet = lightingNet.cuda() D = D.cuda() R = R.cuda() dtype = torch.FloatTensor dtype = torch.cuda.FloatTensor ## UNCOMMENT THIS LINE IF YOU'RE ON A GPU! # Training if TrainSyn: syn_net_train(featureNet,
torchvision.transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))) transforms = torchvision.transforms.Compose(transforms_list) mnist = dataset_loader.MNIST(data_path=opt.data_path, transforms=transforms) mnist_train, mnist_test = mnist.get_MNIST() mnist_train_loader = torch.utils.data.DataLoader(mnist_train, batch_size=opt.batch_size, shuffle=True) mnist_test_loader = torch.utils.data.DataLoader(mnist_test, batch_size=opt.batch_size, shuffle=False) discriminator = models.Discriminator(ndf=opt.ndf).cuda() # discriminator.initialize() generator = models.Generator(ngf=opt.ngf).cuda() # generator.initialize() discriminator.apply(models.weights_init) generator.apply(models.weights_init) optim_discriminator = torch.optim.Adam(discriminator.parameters(), lr=opt.learning_rate, betas=(0.5, 0.999)) optim_generator = torch.optim.Adam(generator.parameters(), lr=opt.learning_rate, betas=(0.5, 0.999)) crit_discriminator = torch.nn.BCELoss().cuda() crit_generator = torch.nn.BCELoss().cuda()
def __init__(self, args, server, cluster, env, queue_shapes, trajectory_queue_size, replay_queue_size): self.env = env self.args = args self.task = args.task self.queue_shapes = queue_shapes self.trajectory_queue_size = trajectory_queue_size self.replay_queue_size = replay_queue_size self.action_sizes = env.action_sizes self.input_shape = list(self.env.observation_shape) # used for summary self._disc_step = 0 self._policy_step = 0 ################################## # Queue pipelines (ps/task=0~) ################################## with tf.device('/job:ps/task:0'): # TODO: we may need more than 1 queue #for i in range(cluster.num_tasks('ps')): if args.task != 1 or args.loss == 'l2': self.trajectory_queue = tf.FIFOQueue( self.trajectory_queue_size, [tf.float32] * len(self.queue_shapes), shapes=[shape for _, shape in self.queue_shapes], names=[name for name, _ in self.queue_shapes], shared_name='queue') self.trajectory_queue_size_op = self.trajectory_queue.size() if args.loss == 'gan': self.replay_queue = tf.FIFOQueue( self.replay_queue_size, tf.float32, shapes=dict(self.queue_shapes)['states'][1:], shared_name='replay') self.replay_queue_size_op = self.replay_queue.size() else: self.replay_queue = None self.replay_queue_size_op = None ########################### # Master policy (task!=1) ########################### device = 'gpu' if self.task == 0 else 'cpu' master_gpu = "/job:worker/task:{}/{}:0".format(self.args.task, device) master_gpu_replica = tf.train. \ replica_device_setter(1, worker_device=master_gpu) with tf.device(master_gpu_replica): with tf.variable_scope("global"): self.policy_step = tf.get_variable( "policy_step", [], tf.int32, initializer=tf.constant_initializer(0, dtype=tf.int32), trainable=False) self.disc_step = tf.get_variable( "disc_step", [], tf.int32, initializer=tf.constant_initializer(0, dtype=tf.int32), trainable=False) #master_cpu = "/job:worker/task:{}/cpu:0".format(self.args.task, device) #master_cpu_replica = tf.train. \ # replica_device_setter(1, worker_device=master_cpu) #with tf.device(master_cpu_replica): # master should initialize discriminator if args.task < 2 and args.loss == 'gan': self.global_disc = models.Discriminator( self.args, self.disc_step, self.input_shape, self.env.norm, "global") if args.task != 1 or args.loss == 'l2': logger.debug(master_gpu) with tf.device(master_gpu_replica): self.prepare_master_network() ########################### # Master policy network ########################### if self.args.task == 0: policy_batch_size = self.args.policy_batch_size # XXX: may need this if you are lack of GPU memory #policy_batch_size = int(self.args.policy_batch_size \ # / self.env.episode_length) worker_device = "/job:worker/task:{}/cpu:0".format(self.task) logger.debug(worker_device) with tf.device(worker_device): with tf.variable_scope("global"): self.trajectory_dequeue = self.trajectory_queue. \ dequeue_many(policy_batch_size) ########################### # Discriminator (task=1) ########################### elif self.args.task == 1 and self.args.loss == 'gan': device = 'gpu' if args.num_gpu > 0 else 'cpu' worker_device = "/job:worker/task:{}/{}:0".format( self.task, device) logger.debug(worker_device) with tf.device(worker_device): self.prepare_gan() worker_device = "/job:worker/task:{}/cpu:0".format(self.task) logger.debug(worker_device) with tf.device(worker_device): with tf.variable_scope("global"): self.replay_dequeue = self.replay_queue. \ dequeue_many(self.args.disc_batch_size) ##################################################### # Local policy network (task >= 2 (gan) or 1 (l2)) ##################################################### elif self.args.task >= 1: worker_device = "/job:worker/task:{}/cpu:0".format(self.task) logger.debug(worker_device) with tf.device(worker_device): self.prepare_local_network()
torch.cuda.manual_seed(args.seed) kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {} print('load data: ', args.dataset) train_loader, test_loader = data_loader.getTargetDataSet( args.dataset, args.batch_size, args.imageSize, args.dataroot) print('Load model') model = models.vgg13() print(model) print('load GAN') nz = 100 netG = models.Generator(1, nz, 64, 3) # ngpu, nz, ngf, nc netD = models.Discriminator(1, 3, 64) # ngpu, nc, ndf # Initial setup for GAN real_label = 1 fake_label = 0 criterion = nn.BCELoss() fixed_noise = torch.FloatTensor(64, nz, 1, 1).normal_(0, 1) if args.cuda: model.cuda() netD.cuda() netG.cuda() criterion.cuda() fixed_noise = fixed_noise.cuda() fixed_noise = Variable(fixed_noise) print('Setup optimizer')