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) # fake_images = G(z) fake_images_np = fake_images.cpu().detach().numpy()
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. 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)) # Training Loop # Lists to keep track of progress img_list = [] G_losses = []
dped_dir, vgg_dir, eval_step = utils.process_command_args(sys.argv) # 使得随机数据可预测 np.random.seed(0) # Loss function adversarial_loss = torch.nn.BCELoss() # Initialize generator and discriminator generator = Generator() discriminator = Discriminator() cuda = True if torch.cuda.is_available() else False if cuda: generator.cuda() discriminator.cuda() adversarial_loss.cuda() # Initialize weights generator.apply(weights_init_normal) discriminator.apply(weights_init_normal) # Configure data loader nw = min([os.cpu_count(), batch_size if batch_size > 1 else 0, 8]) print('Using {} dataloader workers every process'.format(nw)) dataloader = torch.utils.data.DataLoader( LoadData( dped_dir=dped_dir, dataset_size=train_size, image_size=PATCH_SIZE, test=False,