Vijish commited on
Commit
19572b1
1 Parent(s): 55b886b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +90 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import urllib.request
3
+ import PIL.Image
4
+ from PIL import Image
5
+ import requests
6
+ import fastai
7
+ from fastai.vision import *
8
+ from fastai.utils.mem import *
9
+ from fastai.vision import open_image, load_learner, image, torch
10
+ import numpy as np
11
+ from urllib.request import urlretrieve
12
+ from io import BytesIO
13
+ import numpy as np
14
+ import torchvision.transforms as T
15
+ from PIL import Image,ImageOps,ImageFilter
16
+ from io import BytesIO
17
+ import os
18
+
19
+
20
+
21
+ class FeatureLoss(nn.Module):
22
+ def __init__(self, m_feat, layer_ids, layer_wgts):
23
+ super().__init__()
24
+ self.m_feat = m_feat
25
+ self.loss_features = [self.m_feat[i] for i in layer_ids]
26
+ self.hooks = hook_outputs(self.loss_features, detach=False)
27
+ self.wgts = layer_wgts
28
+ self.metric_names = ['pixel',] + [f'feat_{i}' for i in range(len(layer_ids))
29
+ ] + [f'gram_{i}' for i in range(len(layer_ids))]
30
+
31
+ def make_features(self, x, clone=False):
32
+ self.m_feat(x)
33
+ return [(o.clone() if clone else o) for o in self.hooks.stored]
34
+
35
+ def forward(self, input, target):
36
+ out_feat = self.make_features(target, clone=True)
37
+ in_feat = self.make_features(input)
38
+ self.feat_losses = [base_loss(input,target)]
39
+ self.feat_losses += [base_loss(f_in, f_out)*w
40
+ for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)]
41
+ self.feat_losses += [base_loss(gram_matrix(f_in), gram_matrix(f_out))*w**2 * 5e3
42
+ for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)]
43
+ self.metrics = dict(zip(self.metric_names, self.feat_losses))
44
+ return sum(self.feat_losses)
45
+
46
+ def __del__(self): self.hooks.remove()
47
+
48
+
49
+ MODEL_URL = "https://www.dropbox.com/s/vxgw0s7ktpla4dk/SkinDeep2.pkl?dl=1"
50
+ urlretrieve(MODEL_URL, "SkinDeep2.pkl")
51
+ path = Path(".")
52
+ learn = load_learner(path, 'SkinDeep2.pkl')
53
+
54
+
55
+ def predict(image):
56
+ img_fast = open_image(image)
57
+ a = PIL.Image.open(image).convert('RGB')
58
+ st.image(a, caption='Input')
59
+ p,img_hr,b = learn.predict(img_fast)
60
+ x = np.minimum(np.maximum(image2np(img_hr.data*255), 0), 255).astype(np.uint8)
61
+ img = PIL.Image.fromarray(x).convert('RGB')
62
+ return st.image(img, caption='Tattoo')
63
+
64
+
65
+ SIDEBAR_OPTION_DEMO_IMAGE = "Select a Demo Image"
66
+ SIDEBAR_OPTION_UPLOAD_IMAGE = "Upload an Image"
67
+
68
+ SIDEBAR_OPTIONS = [SIDEBAR_OPTION_DEMO_IMAGE, SIDEBAR_OPTION_UPLOAD_IMAGE]
69
+
70
+ app_mode = st.sidebar.selectbox("Please select from the following", SIDEBAR_OPTIONS)
71
+ photos = ["fight.jpg","shaolin-kung-fu.jpg","unnamed.jpg","michael-jackson.png"]
72
+
73
+ if app_mode == SIDEBAR_OPTION_DEMO_IMAGE:
74
+ st.sidebar.write(" ------ ")
75
+ option = st.sidebar.selectbox('Please select a sample image and then click PoP button', photos)
76
+ pressed = st.sidebar.button('PoP')
77
+ if pressed:
78
+ st.empty()
79
+ st.sidebar.write('Please wait for the magic to happen! This may take up to a minute.')
80
+ predict(option)
81
+
82
+
83
+ elif app_mode == SIDEBAR_OPTION_UPLOAD_IMAGE:
84
+ uploaded_file = st.file_uploader("Choose an image...")
85
+ if uploaded_file is not None:
86
+ pressed = st.sidebar.button('PoP')
87
+ if pressed:
88
+ st.empty()
89
+ st.sidebar.write('Please wait for the magic to happen! This may take up to a minute.')
90
+ predict(uploaded_file)