init
Browse files- app.py +3 -1
- core/__init__.py +30 -0
app.py
CHANGED
@@ -18,6 +18,8 @@ from io import BytesIO
|
|
18 |
from fastai.vision import *
|
19 |
from fastai.vision import load_learner
|
20 |
|
|
|
|
|
21 |
ORIGINAL_REPO_URL = 'https://github.com/vijishmadhavan/ArtLine'
|
22 |
TITLE = 'vijishmadhavan/ArtLine'
|
23 |
DESCRIPTION = f"""This is a demo for {ORIGINAL_REPO_URL}.
|
@@ -66,7 +68,7 @@ def run(
|
|
66 |
|
67 |
p, img_hr, b = learn.predict(img_fast)
|
68 |
r = PIL.Image(img_hr)
|
69 |
-
|
70 |
return r
|
71 |
|
72 |
learn = None
|
|
|
18 |
from fastai.vision import *
|
19 |
from fastai.vision import load_learner
|
20 |
|
21 |
+
from core import FeatureLoss
|
22 |
+
|
23 |
ORIGINAL_REPO_URL = 'https://github.com/vijishmadhavan/ArtLine'
|
24 |
TITLE = 'vijishmadhavan/ArtLine'
|
25 |
DESCRIPTION = f"""This is a demo for {ORIGINAL_REPO_URL}.
|
|
|
68 |
|
69 |
p, img_hr, b = learn.predict(img_fast)
|
70 |
r = PIL.Image(img_hr)
|
71 |
+
|
72 |
return r
|
73 |
|
74 |
learn = None
|
core/__init__.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastai.vision import *
|
2 |
+
|
3 |
+
|
4 |
+
class FeatureLoss(nn.Module):
|
5 |
+
def __init__(self, m_feat, layer_ids, layer_wgts):
|
6 |
+
super().__init__()
|
7 |
+
self.m_feat = m_feat
|
8 |
+
self.loss_features = [self.m_feat[i] for i in layer_ids]
|
9 |
+
self.hooks = hook_outputs(self.loss_features, detach=False)
|
10 |
+
self.wgts = layer_wgts
|
11 |
+
self.metric_names = ['pixel', ] + [f'feat_{i}' for i in range(len(layer_ids))
|
12 |
+
] + [f'gram_{i}' for i in range(len(layer_ids))]
|
13 |
+
|
14 |
+
def make_features(self, x, clone=False):
|
15 |
+
self.m_feat(x)
|
16 |
+
return [(o.clone() if clone else o) for o in self.hooks.stored]
|
17 |
+
|
18 |
+
def forward(self, input, target):
|
19 |
+
out_feat = self.make_features(target, clone=True)
|
20 |
+
in_feat = self.make_features(input)
|
21 |
+
self.feat_losses = [base_loss(input, target)]
|
22 |
+
self.feat_losses += [base_loss(f_in, f_out) * w
|
23 |
+
for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)]
|
24 |
+
self.feat_losses += [base_loss(gram_matrix(f_in), gram_matrix(f_out)) * w ** 2 * 5e3
|
25 |
+
for f_in, f_out, w in zip(in_feat, out_feat, self.wgts)]
|
26 |
+
self.metrics = dict(zip(self.metric_names, self.feat_losses))
|
27 |
+
return sum(self.feat_losses)
|
28 |
+
|
29 |
+
def __del__(self): self.hooks.remove()
|
30 |
+
|