|
|
|
import gradio as gr
|
|
|
|
import torch
|
|
import torch.nn as nn
|
|
import torch.nn.parallel
|
|
import torch.backends.cudnn as cudnn
|
|
import torch.optim as optim
|
|
import torch.utils.data
|
|
import torchvision.datasets as dset
|
|
import torchvision.transforms as transforms
|
|
import torchvision.utils as vutils
|
|
import numpy as np
|
|
|
|
|
|
|
|
omit_module = True
|
|
|
|
|
|
|
|
image_size = 64
|
|
|
|
|
|
nc = 1
|
|
|
|
|
|
nz = 100
|
|
|
|
|
|
ngf = 64
|
|
|
|
|
|
ndf = 64
|
|
|
|
|
|
lr = 0.0002
|
|
|
|
|
|
beta1 = 0.5
|
|
|
|
|
|
ngpu = 1
|
|
|
|
|
|
device = torch.device("cuda:0" if (torch.cuda.is_available() and ngpu > 0) else "cpu")
|
|
|
|
|
|
|
|
def weights_init(m):
|
|
classname = m.__class__.__name__
|
|
if classname.find('Conv') != -1:
|
|
nn.init.normal_(m.weight.data, 0.0, 0.02)
|
|
elif classname.find('BatchNorm') != -1:
|
|
nn.init.normal_(m.weight.data, 1.0, 0.02)
|
|
nn.init.constant_(m.bias.data, 0)
|
|
|
|
|
|
class Generator(nn.Module):
|
|
def __init__(self, ngpu):
|
|
super(Generator, self).__init__()
|
|
self.ngpu = ngpu
|
|
self.main = nn.Sequential(
|
|
|
|
nn.ConvTranspose2d( nz, ngf * 8, 4, 1, 0, bias=False),
|
|
nn.BatchNorm2d(ngf * 8),
|
|
nn.ReLU(True),
|
|
|
|
nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False),
|
|
nn.BatchNorm2d(ngf * 4),
|
|
nn.ReLU(True),
|
|
|
|
nn.ConvTranspose2d( ngf * 4, ngf * 2, 4, 2, 1, bias=False),
|
|
nn.BatchNorm2d(ngf * 2),
|
|
nn.ReLU(True),
|
|
|
|
nn.ConvTranspose2d( ngf * 2, ngf, 4, 2, 1, bias=False),
|
|
nn.BatchNorm2d(ngf),
|
|
nn.ReLU(True),
|
|
|
|
nn.ConvTranspose2d( ngf, nc, 4, 2, 1, bias=False),
|
|
nn.Tanh()
|
|
|
|
)
|
|
|
|
def forward(self, input):
|
|
return self.main(input)
|
|
|
|
|
|
|
|
netG = Generator(ngpu).to(device)
|
|
|
|
|
|
if (device.type == 'cuda') and (ngpu > 1):
|
|
netG = nn.DataParallel(netG, list(range(ngpu)))
|
|
|
|
|
|
|
|
netG.apply(weights_init)
|
|
|
|
|
|
checkpoint = torch.load("checkpoints/epoch1100.ckpt")
|
|
|
|
|
|
if omit_module:
|
|
for i in list(checkpoint['netG_state_dict'].keys()):
|
|
if (str(i).startswith('module.')):
|
|
checkpoint['netG_state_dict'][i[7:]] = checkpoint['netG_state_dict'].pop(i)
|
|
|
|
|
|
netG.load_state_dict(checkpoint['netG_state_dict'])
|
|
|
|
|
|
def genImg():
|
|
fixed_noise = torch.randn(64, nz, 1, 1, device=device)
|
|
with torch.no_grad():
|
|
fake = netG(fixed_noise).detach().cpu()
|
|
fake_grid = vutils.make_grid(fake, padding=2, normalize=True)
|
|
return transforms.functional.to_pil_image(fake_grid)
|
|
|
|
demo = gr.Interface(fn=genImg, inputs=None, outputs="image")
|
|
demo.launch() |