Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import torch.nn as nn
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
class Generator(nn.Module):
|
7 |
+
def __init__(self):
|
8 |
+
super(Generator, self).__init__()
|
9 |
+
self.main = nn.Sequential(
|
10 |
+
nn.ConvTranspose2d(100, 64 * 8, 4, 1, 0, bias=False),
|
11 |
+
nn.BatchNorm2d(64 * 8),
|
12 |
+
nn.ReLU(True),
|
13 |
+
|
14 |
+
nn.ConvTranspose2d(64 * 8, 64 * 4, 4, 2, 1, bias=False),
|
15 |
+
nn.BatchNorm2d(64 * 4),
|
16 |
+
nn.ReLU(True),
|
17 |
+
|
18 |
+
nn.ConvTranspose2d(64 * 4, 64 * 2, 4, 2, 1, bias=False),
|
19 |
+
nn.BatchNorm2d(64 * 2),
|
20 |
+
nn.ReLU(True),
|
21 |
+
|
22 |
+
nn.ConvTranspose2d(64 * 2, 64, 4, 2, 1, bias=False),
|
23 |
+
nn.BatchNorm2d(64),
|
24 |
+
nn.ReLU(True),
|
25 |
+
|
26 |
+
nn.ConvTranspose2d(64, 3, 4, 2, 1, bias=False),
|
27 |
+
nn.Tanh()
|
28 |
+
)
|
29 |
+
|
30 |
+
def forward(self, input):
|
31 |
+
return self.main(input)
|
32 |
+
|
33 |
+
netG = Generator()
|
34 |
+
|
35 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
36 |
+
netG.load_state_dict(torch.load("dcgan.pth", map_location=device))
|
37 |
+
netG.eval()
|
38 |
+
|
39 |
+
def generate_image():
|
40 |
+
with torch.no_grad():
|
41 |
+
noise = torch.randn(1, 100, 1, 1)
|
42 |
+
fake_image = netG(noise)
|
43 |
+
|
44 |
+
generated_image = fake_image.squeeze().cpu().numpy()
|
45 |
+
generated_image = np.transpose(generated_image, (1, 2, 0))
|
46 |
+
generated_image = (generated_image + 1) / 2.0
|
47 |
+
generated_image = (generated_image * 255).astype(np.uint8)
|
48 |
+
|
49 |
+
return generated_image
|
50 |
+
|
51 |
+
title = "DCGAN Image Generator 🖌️🎨"
|
52 |
+
description = "Generate non-existing images using DCGAN."
|
53 |
+
|
54 |
+
iface = gr.Interface(
|
55 |
+
fn=generate_image,
|
56 |
+
inputs=None,
|
57 |
+
outputs="image",
|
58 |
+
title=title,
|
59 |
+
description=description,
|
60 |
+
theme="soft"
|
61 |
+
)
|
62 |
+
|
63 |
+
iface.launch()
|
64 |
+
|