Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
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 |
-
|
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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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)
|