miittnnss's picture
Update app.py
e3004b1
raw
history blame
No virus
2 kB
import gradio as gr
import torch
import torch.nn as nn
import numpy as np
class Generator(nn.Module):
def __init__(self):
super(Generator, self).__init__()
self.main = nn.Sequential(
nn.ConvTranspose2d(100, 64 * 8, 4, 1, 0, bias=False),
nn.BatchNorm2d(64 * 8),
nn.ReLU(True),
nn.ConvTranspose2d(64 * 8, 64 * 4, 4, 2, 1, bias=False),
nn.BatchNorm2d(64 * 4),
nn.ReLU(True),
nn.ConvTranspose2d(64 * 4, 64 * 2, 4, 2, 1, bias=False),
nn.BatchNorm2d(64 * 2),
nn.ReLU(True),
nn.ConvTranspose2d(64 * 2, 64, 4, 2, 1, bias=False),
nn.BatchNorm2d(64),
nn.ReLU(True),
nn.ConvTranspose2d(64, 3, 4, 2, 1, bias=False),
nn.Tanh()
)
def forward(self, input):
return self.main(input)
netG = Generator()
device = "cuda" if torch.cuda.is_available() else "cpu"
model_file = "model.pth"
netG.load_state_dict(torch.load(model_file, map_location=device))
netG.eval()
def generate_image():
with torch.no_grad():
noise = torch.randn(1, 100, 1, 1)
fake_image = netG(noise)
generated_image = fake_image.squeeze().cpu().numpy()
generated_image = np.transpose(generated_image, (1, 2, 0))
generated_image = (generated_image + 1) / 2.0
generated_image = (generated_image * 255).astype(np.uint8)
return generated_image
title = "DCGAN Image Generator πŸ–ŒοΈπŸŽ¨"
description = "Generate non-existing images using DCGAN."
content = """
## How to Generate 🎨
To generate an image, follow these steps:
1. Click \"Generate\" button to generate a image!
2. Once the image is generated, you can save it or share it to the community!
"""
iface = gr.Interface(
fn=generate_image,
inputs=None,
outputs="image",
title=title,
description=description,
article=content,
theme="soft",
api_name="generate"
)
iface.queue()
iface.launch()