geninhu commited on
Commit
01c97b6
·
1 Parent(s): a974e66

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -44
app.py CHANGED
@@ -1,44 +1,58 @@
1
- import math
2
- import numpy as np
3
- import pandas as pd
4
-
5
- import gradio as gr
6
- from huggingface_hub import from_pretrained_fastai
7
- from fastai.vision.all import *
8
- from torchvision.models import vgg19, vgg16
9
- from utils import *
10
-
11
- pascal_source = '.'
12
- EXAMPLES_PATH = Path('/content/examples')
13
- repo_id = "hugginglearners/fastai-style-transfer"
14
-
15
- _vgg_config = {
16
- 'vgg16' : [1, 11, 18, 25, 20],
17
- 'vgg19' : [1, 6, 11, 20, 29, 22]
18
- }
19
-
20
- feat_net, layers = _get_layers('vgg19', True)
21
- hooks = hook_outputs(layers, detach=False)
22
-
23
- learner = from_pretrained_fastai(repo_id)
24
-
25
- def infer(img):
26
- pred = learner.predict(img)
27
- image = pred[0].cpu().numpy()
28
- image = image.transpose((1, 2, 0))
29
- plt.imshow(image)
30
- return plt.gcf() #pred[0].show()
31
-
32
- # get the inputs
33
- inputs = gr.inputs.Image(shape=(192, 192))
34
-
35
- # the app outputs two segmented images
36
- output = gr.Plot()
37
- # it's good practice to pass examples, description and a title to guide users
38
- title = 'Style transfer'
39
- description = ''
40
- article = "Author: <a href=\"https://huggingface.co/geninhu\">Nhu Hoang</a>. "
41
- examples = [f'{EXAMPLES_PATH}/{f.name}' for f in EXAMPLES_PATH.iterdir()]
42
-
43
- gr.Interface(infer, inputs, output, examples= examples, allow_flagging='never',
44
- title=title, description=description, article=article, live=False).launch(enable_queue=True, debug=False, inbrowser=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import numpy as np
3
+ import pandas as pd
4
+
5
+ import gradio as gr
6
+ from huggingface_hub import from_pretrained_fastai
7
+ from fastai.vision.all import *
8
+ from torchvision.models import vgg19, vgg16
9
+ from utils import *
10
+
11
+ pascal_source = '.'
12
+ EXAMPLES_PATH = Path('/content/examples')
13
+ repo_id = "hugginglearners/fastai-style-transfer"
14
+
15
+
16
+ def _inner(feat_net, hooks, x):
17
+ feat_net(x)
18
+ return hooks.stored
19
+
20
+ def _get_layers(arch:str, pretrained=True):
21
+ "Get the layers and arch for a VGG Model (16 and 19 are supported only)"
22
+ feat_net = vgg19(pretrained=pretrained).cuda() if arch.find('9') > 1 else vgg16(pretrained=pretrained).cuda()
23
+ config = _vgg_config.get(arch)
24
+ features = feat_net.features.cuda().eval()
25
+ for p in features.parameters(): p.requires_grad=False
26
+ return feat_net, [features[i] for i in config]
27
+
28
+
29
+ _vgg_config = {
30
+ 'vgg16' : [1, 11, 18, 25, 20],
31
+ 'vgg19' : [1, 6, 11, 20, 29, 22]
32
+ }
33
+
34
+ feat_net, layers = _get_layers('vgg19', True)
35
+ hooks = hook_outputs(layers, detach=False)
36
+
37
+ learner = from_pretrained_fastai(repo_id)
38
+
39
+ def infer(img):
40
+ pred = learner.predict(img)
41
+ image = pred[0].cpu().numpy()
42
+ image = image.transpose((1, 2, 0))
43
+ plt.imshow(image)
44
+ return plt.gcf() #pred[0].show()
45
+
46
+ # get the inputs
47
+ inputs = gr.inputs.Image(shape=(192, 192))
48
+
49
+ # the app outputs two segmented images
50
+ output = gr.Plot()
51
+ # it's good practice to pass examples, description and a title to guide users
52
+ title = 'Style transfer'
53
+ description = ''
54
+ article = "Author: <a href=\"https://huggingface.co/geninhu\">Nhu Hoang</a>. "
55
+ examples = [f'{EXAMPLES_PATH}/{f.name}' for f in EXAMPLES_PATH.iterdir()]
56
+
57
+ gr.Interface(infer, inputs, output, examples= examples, allow_flagging='never',
58
+ title=title, description=description, article=article, live=False).launch(enable_queue=True, debug=False, inbrowser=True)