5AF1 commited on
Commit
1752a14
·
1 Parent(s): 9e7a9f7

deploy torch model

Browse files
Files changed (1) hide show
  1. app.py +62 -0
app.py CHANGED
@@ -0,0 +1,62 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import importlib
3
+ net = importlib.import_module("model.HGSRCNN").Net
4
+ import torch
5
+
6
+ model = net(multi_scale=True, group=1)
7
+ model.load_state_dict(torch.load('checkpoint/HGSRCNN.pth',map_location=torch.device('cpu')))
8
+
9
+ def np2torch(img):
10
+ img = torch.tensor(img)
11
+ img = img/255
12
+ img = img.permute(2, 0, 1)
13
+ img = img.unsqueeze(0)
14
+ img = img.cpu()
15
+ return img
16
+
17
+ def torch2np(img):
18
+ return img.squeeze(0).mul(255).clamp(0, 255).byte().permute(1, 2, 0).numpy()
19
+
20
+
21
+
22
+
23
+ def inference(img, scale):
24
+ img = np2torch(img)
25
+ if scale == 'scale x2':
26
+ out = model(img, 2)
27
+ elif scale == 'scale x3':
28
+ out = model(img, 3)
29
+ else:
30
+ out = model(img, 4)
31
+ return torch2np(out),str(out.shape)
32
+
33
+ title = "AnimeGANv2"
34
+ description = "Gradio Demo for AnimeGanv2 Face Portrait. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below. Please use a cropped portrait picture for best results similar to the examples below."
35
+ article = "Github Repo Pytorch "
36
+ #examples=[['groot.jpeg','version 2 (🔺 robustness,🔻 stylization)'],['gongyoo.jpeg','version 1 (🔺 stylization, 🔻 robustness)']]
37
+
38
+ demo = gr.Interface(
39
+ fn=inference,
40
+ inputs=[
41
+ gr.inputs.Image(type="numpy"),
42
+ gr.inputs.Radio([
43
+ 'scale x2',
44
+ 'scale x3',
45
+ 'scale x4',
46
+ ],
47
+ type="value",
48
+ default='scale x2',
49
+ label='Scale'
50
+ )
51
+ ],
52
+ outputs=[
53
+ gr.outputs.Image(type="numpy"),
54
+ 'text'
55
+ ],
56
+ title=title,
57
+ description=description,
58
+ article=article,
59
+ #examples=examples
60
+ )
61
+
62
+ demo.launch()