Spaces:
Runtime error
Runtime error
File size: 1,950 Bytes
698ee2e 19a98a2 698ee2e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
import gradio as gr
import numpy as np
from torchvision.utils import save_image
import torch.nn as nn
import torch
import torchvision.datasets as datasets
import torchvision.transforms as T
from torch.utils.data import DataLoader, ConcatDataset
import torchvision.utils as vutils
import random
LATENT_VECTOR_DIM = 16 # latent vector dimension
class Generator_128(nn.Module):
def __init__(self, GPU_COUNT):
super(Generator_128, self).__init__()
self.GPU_COUNT = GPU_COUNT
self.main = nn.Sequential(
# LATENT_VECTOR_DIM x 1 x 1
nn.ConvTranspose2d(LATENT_VECTOR_DIM, 1024, 4, 1, 0, bias=False),
nn.BatchNorm2d(1024),
nn.ReLU(True),
nn.ConvTranspose2d(1024, 512, 4, 2, 1, bias=False),
nn.BatchNorm2d(512),
nn.ReLU(True),
nn.ConvTranspose2d(512, 256, 4, 2, 1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(True),
nn.ConvTranspose2d(256, 128, 4, 2, 1, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(True),
nn.ConvTranspose2d(128,64, 4, 2, 1, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(True),
nn.ConvTranspose2d(64,3, 4, 2, 1, bias=False),
nn.Tanh()
# 128 x 128 x 3
)
def forward(self, input):
return self.main(input)
trained_gen = Generator_128(0)
trained_gen.load_state_dict(torch.load("generator_epoch_1000.h5",map_location=torch.device('cpu')))
def predict(seed, pokemon_count):
torch.manual_seed(seed)
z = torch.randn(pokemon_count, LATENT_VECTOR_DIM, 1, 1)
punks = trained_gen(z)
save_image(punks, "pokemon.png", normalize=True)
return 'pokemon.png'
gr.Interface(
predict,
inputs=[
gr.Slider(0, 1000, label='Seed', default=42),
gr.Slider(1, 8, label='Number of pokemon', step=1, default=10),
],
outputs="image",
).launch()
|