maze commited on
Commit
9ac69b4
β€’
1 Parent(s): 756e2bd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +139 -0
app.py CHANGED
@@ -1,7 +1,145 @@
 
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  def main(image, backbone, style):
 
 
4
  isize = image.size
 
5
  s = f"The output image ({str(image.size)}) is processed by {backbone} based on input image ({str(isize)}) . <br> Please <b>rate</b> the generated image through the <b>Flag</b> button below!"
6
  return image, s
7
 
@@ -20,6 +158,7 @@ gr.Interface(
20
  # []
21
  # ],
22
  # live = True, # the interface will recalculate as soon as the user input changes.
 
23
  flagging_options = ["Excellect", "Moderate", "Bad"],
24
  flagging_dir = "flagged",
25
  allow_screenshot = False,
 
1
+ from huggingface_hub import hf_hub_download
2
+
3
+
4
+ Rain_Princess = hf_hub_download(repo_id="maze/FastStyleTransfer", filename="Rain_Princess.pth")
5
+ #modelarcanev3 = hf_hub_download(repo_id="akhaliq/ArcaneGANv0.3", filename="ArcaneGANv0.3.jit")
6
+ #modelarcanev2 = hf_hub_download(repo_id="akhaliq/ArcaneGANv0.2", filename="ArcaneGANv0.2.jit")
7
+
8
+
9
+ import numpy as np
10
+ from PIL import Image
11
  import gradio as gr
12
 
13
+ import torch
14
+ import torch.nn as nn
15
+
16
+ import torchvision.transforms as transforms
17
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
18
+
19
+
20
+ class TransformerNetwork(nn.Module):
21
+ def __init__(self, tanh_multiplier=None):
22
+ super(TransformerNetwork, self).__init__()
23
+ self.ConvBlock = nn.Sequential(
24
+ ConvLayer(3, 32, 9, 1),
25
+ nn.ReLU(),
26
+ ConvLayer(32, 64, 3, 2),
27
+ nn.ReLU(),
28
+ ConvLayer(64, 128, 3, 2),
29
+ nn.ReLU()
30
+ )
31
+ self.ResidualBlock = nn.Sequential(
32
+ ResidualLayer(128, 3),
33
+ ResidualLayer(128, 3),
34
+ ResidualLayer(128, 3),
35
+ ResidualLayer(128, 3),
36
+ ResidualLayer(128, 3)
37
+ )
38
+ self.DeconvBlock = nn.Sequential(
39
+ DeconvLayer(128, 64, 3, 2, 1),
40
+ nn.ReLU(),
41
+ DeconvLayer(64, 32, 3, 2, 1),
42
+ nn.ReLU(),
43
+ ConvLayer(32, 3, 9, 1, norm="None")
44
+ )
45
+ self.tanh_multiplier = tanh_multiplier
46
+
47
+ def forward(self, x):
48
+ x = self.ConvBlock(x)
49
+ x = self.ResidualBlock(x)
50
+ x = self.DeconvBlock(x)
51
+ if isinstance(self.tanh_multiplier, int):
52
+ x = self.tanh_multiplier * F.tanh(x)
53
+ return x
54
+
55
+ class ConvLayer(nn.Module):
56
+ def __init__(self, in_channels, out_channels, kernel_size, stride, norm="instance"):
57
+ super(ConvLayer, self).__init__()
58
+ padding_size = kernel_size // 2
59
+ self.pad = nn.ReflectionPad2d(padding_size)
60
+ self.conv = nn.Conv2d(in_channels, out_channels, kernel_size, stride)
61
+ if norm == "instance":
62
+ self.norm = nn.InstanceNorm2d(out_channels, affine=True)
63
+ elif norm == "batch":
64
+ self.norm = nn.BatchNorm2d(out_channels, affine=True)
65
+ else:
66
+ self.norm = nn.Identity()
67
+
68
+ def forward(self, x):
69
+ x = self.pad(x)
70
+ x = self.conv(x)
71
+ x = self.norm(x)
72
+ return x
73
+
74
+ class ResidualLayer(nn.Module):
75
+ def __init__(self, channels=128, kernel_size=3):
76
+ super(ResidualLayer, self).__init__()
77
+ self.conv1 = ConvLayer(channels, channels, kernel_size, stride=1)
78
+ self.relu = nn.ReLU()
79
+ self.conv2 = ConvLayer(channels, channels, kernel_size, stride=1)
80
+
81
+ def forward(self, x):
82
+ identity = x
83
+ out = self.relu(self.conv1(x))
84
+ out = self.conv2(out)
85
+ out = out + identity
86
+ return out
87
+
88
+ class DeconvLayer(nn.Module):
89
+ def __init__(self, in_channels, out_channels, kernel_size, stride, output_padding, norm="instance"):
90
+ super(DeconvLayer, self).__init__()
91
+
92
+ padding_size = kernel_size // 2
93
+ self.conv_transpose = nn.ConvTranspose2d(in_channels, out_channels, kernel_size, stride, padding_size, output_padding)
94
+ if norm == "instance":
95
+ self.norm = nn.InstanceNorm2d(out_channels, affine=True)
96
+ elif norm == "batch":
97
+ self.norm = nn.BatchNorm2d(out_channels, affine=True)
98
+ else:
99
+ self.norm = nn.Identity()
100
+
101
+ def forward(self, x):
102
+ x = self.conv_transpose(x)
103
+ out = self.norm(x)
104
+ return out
105
+
106
+
107
+ mean = np.array([0.485, 0.456, 0.406])
108
+ std = np.array([0.229, 0.224, 0.225])
109
+
110
+ transformer = TransformerNetwork().to(device)
111
+
112
+ transformer.eval()
113
+
114
+ transform = transforms.Compose([
115
+ transforms.Resize(256),
116
+ transforms.ToTensor(),
117
+ transforms.Normalize(mean, std),
118
+ ])
119
+
120
+ denormalize = transforms.Normalize(
121
+ mean= [-m/s for m, s in zip(mean, std)],
122
+ std= [1/s for s in std]
123
+ )
124
+ tensor2Image = transforms.ToPILImage()
125
+
126
+ @torch.no_grad()
127
+ def process(image, model):
128
+ image = transform(image).to(device)
129
+ image = image.unsqueeze(dim=0)
130
+
131
+ image = denormalize(model(image)).cpu()
132
+ image = torch.clamp(image.squeeze(dim=0), 0, 1)
133
+ image = tensor2Image(image)
134
+
135
+ return image
136
+
137
+
138
  def main(image, backbone, style):
139
+ transformer.load_state_dict(torch.load(Rain_Princess))
140
+ image = Image.fromarray(image)
141
  isize = image.size
142
+ image = process(image, transformer)
143
  s = f"The output image ({str(image.size)}) is processed by {backbone} based on input image ({str(isize)}) . <br> Please <b>rate</b> the generated image through the <b>Flag</b> button below!"
144
  return image, s
145
 
 
158
  # []
159
  # ],
160
  # live = True, # the interface will recalculate as soon as the user input changes.
161
+ allow_flagging = "manual",
162
  flagging_options = ["Excellect", "Moderate", "Bad"],
163
  flagging_dir = "flagged",
164
  allow_screenshot = False,