-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
68 lines (59 loc) · 2.1 KB
/
Copy pathmain.py
File metadata and controls
68 lines (59 loc) · 2.1 KB
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
67
68
import streamlit as st
import torch.nn as nn
import torch
import numpy as np
from PIL import Image
class Generator(nn.Module):
def __init__(self, channels_noise, channels_img, img_size):
super().__init__()
self.gen = nn.Sequential(
# Input: N x channels_noise | 1 x 1 x 100
self._block(channels_noise, img_size * 36, 4, 1, 0), # img: 4x4x864
self._block(img_size * 36, img_size * 18, 4, 2, 1), # img: 8x8x432
self._block(img_size * 18, img_size * 9, 4, 2, 1), # img: 16x16x216
self._block(img_size * 9, img_size * 3, 5, 1, 0), # img: 20x20x72
nn.ConvTranspose2d(
img_size * 3, channels_img, kernel_size=5, stride=1, padding=0
),
# Output: N x channels_img | 24x24x4
nn.Tanh(),
)
def _block(self, in_channels, out_channels, kernel_size, stride, padding):
return nn.Sequential(
nn.ConvTranspose2d(
in_channels,
out_channels,
kernel_size,
stride,
padding,
bias=False,
),
nn.BatchNorm2d(out_channels),
nn.ReLU(),
)
def forward(self, x):
return self.gen(x)
st.experimental_singleton()
def load_model():
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
gan = Generator(256, 4, 24).to(device)
gan.load_state_dict(torch.load("ganPunk.pth", map_location=device))
gan.eval()
return gan
gan = load_model()
st.write("GANFTS")
btn = st.button("Generate")
columns = st.columns(5)
if btn:
noise = torch.randn(len(columns) * 4, 256, 1, 1)
fake = gan(noise)
imgs = fake.detach().cpu().numpy()
imgs = (imgs - imgs.min()) / (imgs.max() - imgs.min())
imgs = np.moveaxis(imgs, 1, -1)
imgs = imgs * 255
for idx, column in enumerate(columns):
for j in range(4):
img = imgs[idx*4+j]
img = Image.fromarray(img.astype(np.uint8))
img = img.resize((240, 240), resample=Image.NEAREST)
column.image(img, width=200)