Spaces:
Runtime error
Runtime error
Create new file
Browse files
app.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import requests
|
4 |
+
import numpy as np
|
5 |
+
import urllib.request
|
6 |
+
from urllib.request import urlretrieve
|
7 |
+
import PIL.Image
|
8 |
+
import torchvision.transforms as T
|
9 |
+
import fastai
|
10 |
+
from fastai.vision import *
|
11 |
+
from fastai.utils.mem import *
|
12 |
+
|
13 |
+
class FeatureLoss(nn.Module):
|
14 |
+
def __init__(self, m_feat, layer_ids, layer_wgts):
|
15 |
+
super().__init__()
|
16 |
+
self.m_feat = m_feat
|
17 |
+
self.loss_features = [self.m_feat[i] for i in layer_ids]
|
18 |
+
self.hooks = hook_outputs(self.loss_features, detach=False)
|
19 |
+
self.wgts = layer_wgts
|
20 |
+
self.metric_names = ['pixel',] + [f'feat_{i}' for i in range(len(layer_ids))
|
21 |
+
] + [f'gram_{i}' for i in range(len(layer_ids))]
|
22 |
+
|
23 |
+
def make_features(self, x, clone=False):
|
24 |
+
self.m_feat(x)
|
25 |
+
return [(o.clone() if clone else o) for o in self.hooks.stored]
|
26 |
+
|
27 |
+
def forward(self, input, target):
|
28 |
+
out_feat = self.make_features(target, clone=True)
|
29 |
+
in_feat = self.make_features(input)
|
30 |
+
self.feat_losses = [base_loss(input,target)]
|
31 |
+
self.feat_losses += [base_loss(f_in, f_out)*w
|
32 |
+
for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)]
|
33 |
+
self.feat_losses += [base_loss(gram_matrix(f_in), gram_matrix(f_out))*w**2 * 5e3
|
34 |
+
for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)]
|
35 |
+
self.metrics = dict(zip(self.metric_names, self.feat_losses))
|
36 |
+
return sum(self.feat_losses)
|
37 |
+
|
38 |
+
def __del__(self): self.hooks.remove()
|
39 |
+
|
40 |
+
MODEL_URL = "https://www.dropbox.com/s/rz9nt35um1agf5y/t10T.pkl?dl=1"
|
41 |
+
urllib.request.urlretrieve(MODEL_URL, "t10T.pkl")
|
42 |
+
path = Path(".")
|
43 |
+
learn=load_learner(path, 't10T.pkl')
|
44 |
+
|
45 |
+
urlretrieve("https://www.independent.ie/incoming/714c6/29308190.ece/AUTOCROP/h530/melanie-griffiths_2391378a.jpg","socce1.jpg")
|
46 |
+
urlretrieve("https://media.okmagazine.com/brand-img/IEPXUdkY7/0x0/2015/06/celebrity-tattoos-16-splash.jpg","socce2.jpg")
|
47 |
+
urlretrieve("https://newsmeter.in/wp-content/uploads/2020/06/Ajay-Devgn-Tattoo.jpg","basebal.jpg")
|
48 |
+
urlretrieve("https://akns-images.eonline.com/eol_images/Entire_Site/2014617/rs_600x600-140717150632-600-vin-los-tattoo-model.ls.71714_copy.jpg?fit=around%7C600:600&output-quality=90&crop=600:600;center,top","baseb.jpg")
|
49 |
+
|
50 |
+
sample_images = [["socce1.jpg"],
|
51 |
+
["socce2.jpg"],
|
52 |
+
["basebal.jpg"],
|
53 |
+
["baseb.jpg"]]
|
54 |
+
|
55 |
+
|
56 |
+
def predict(input):
|
57 |
+
size = input.size
|
58 |
+
img_t = T.ToTensor()(input)
|
59 |
+
img_fast = Image(img_t)
|
60 |
+
p,img_hr,b = learn.predict(img_fast)
|
61 |
+
x = np.minimum(np.maximum(image2np(img_hr.data*255), 0), 255).astype(np.uint8)
|
62 |
+
img = PIL.Image.fromarray(x)
|
63 |
+
im1 = img.resize(size)
|
64 |
+
return im1
|
65 |
+
|
66 |
+
gr_interface = gr.Interface(fn=predict, inputs=gr.Image(type="pil"), outputs="image", title='Skin-Deep',examples=sample_images).launch();
|