Spaces:
Runtime error
Runtime error
Upload utils.py
Browse files
utils.py
CHANGED
@@ -1,153 +1,141 @@
|
|
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 |
-
|
10 |
-
pascal_source = '.'
|
11 |
-
EXAMPLES_PATH = Path('/content/examples')
|
12 |
-
repo_id = "hugginglearners/fastai-style-transfer"
|
13 |
-
|
14 |
-
def get_stl_fs(fs): return fs[:-1]
|
15 |
-
|
16 |
-
def style_loss(inp:Tensor, out_feat:Tensor):
|
17 |
-
"Calculate style loss, assumes we have `im_grams`"
|
18 |
-
# Get batch size
|
19 |
-
bs = inp[0].shape[0]
|
20 |
-
loss = []
|
21 |
-
# For every item in our inputs
|
22 |
-
for y, f in zip(*map(get_stl_fs, [im_grams, inp])):
|
23 |
-
# Calculate MSE
|
24 |
-
loss.append(F.mse_loss(y.repeat(bs, 1, 1), gram(f)))
|
25 |
-
# Multiply their sum by 30000
|
26 |
-
return 3e5 * sum(loss)
|
27 |
-
|
28 |
-
class FeatureLoss(Module):
|
29 |
-
"Combines two losses and features into a useable loss function"
|
30 |
-
def __init__(self, feats, style_loss, act_loss, hooks, feat_net):
|
31 |
-
store_attr()
|
32 |
-
self.hooks = hooks
|
33 |
-
self.feat_net = feat_net
|
34 |
-
self.reset_metrics()
|
35 |
-
|
36 |
-
def forward(self, pred, targ):
|
37 |
-
# First get the features of our prediction and target
|
38 |
-
pred_feat, targ_feat = self.feats(self.feat_net, self.hooks, pred), self.feats(self.feat_net, self.hooks, targ)
|
39 |
-
# Calculate style and activation loss
|
40 |
-
style_loss = self.style_loss(pred_feat, targ_feat)
|
41 |
-
act_loss = self.act_loss(pred_feat, targ_feat)
|
42 |
-
# Store the loss
|
43 |
-
self._add_loss(style_loss, act_loss)
|
44 |
-
# Return the sum
|
45 |
-
return style_loss + act_loss
|
46 |
-
|
47 |
-
def reset_metrics(self):
|
48 |
-
# Generates a blank metric
|
49 |
-
self.metrics = dict(style = [], content = [])
|
50 |
-
|
51 |
-
def _add_loss(self, style_loss, act_loss):
|
52 |
-
# Add to our metrics
|
53 |
-
self.metrics['style'].append(style_loss)
|
54 |
-
self.metrics['content'].append(act_loss)
|
55 |
-
|
56 |
-
def act_loss(inp:Tensor, targ:Tensor):
|
57 |
-
"Calculate the MSE loss of the activation layers"
|
58 |
-
return F.mse_loss(inp[-1], targ[-1])
|
59 |
-
|
60 |
-
class ReflectionLayer(Module):
|
61 |
-
"A series of Reflection Padding followed by a ConvLayer"
|
62 |
-
def __init__(self, in_channels, out_channels, ks=3, stride=2):
|
63 |
-
reflection_padding = ks // 2
|
64 |
-
self.reflection_pad = nn.ReflectionPad2d(reflection_padding)
|
65 |
-
self.conv2d = nn.Conv2d(in_channels, out_channels, ks, stride)
|
66 |
-
|
67 |
-
def forward(self, x):
|
68 |
-
out = self.reflection_pad(x)
|
69 |
-
out = self.conv2d(out)
|
70 |
-
return out
|
71 |
-
|
72 |
-
class ResidualBlock(Module):
|
73 |
-
"Two reflection layers and an added activation function with residual"
|
74 |
-
def __init__(self, channels):
|
75 |
-
self.conv1 = ReflectionLayer(channels, channels, ks=3, stride=1)
|
76 |
-
self.in1 = nn.InstanceNorm2d(channels, affine=True)
|
77 |
-
self.conv2 = ReflectionLayer(channels, channels, ks=3, stride=1)
|
78 |
-
self.in2 = nn.InstanceNorm2d(channels, affine=True)
|
79 |
-
self.relu = nn.ReLU()
|
80 |
-
|
81 |
-
def forward(self, x):
|
82 |
-
residual = x
|
83 |
-
out = self.relu(self.in1(self.conv1(x)))
|
84 |
-
out = self.in2(self.conv2(out))
|
85 |
-
out = out + residual
|
86 |
-
return out
|
87 |
-
|
88 |
-
class UpsampleConvLayer(Module):
|
89 |
-
"Upsample with a ReflectionLayer"
|
90 |
-
def __init__(self, in_channels, out_channels, ks=3, stride=1, upsample=None):
|
91 |
-
self.upsample = upsample
|
92 |
-
reflection_padding = ks // 2
|
93 |
-
self.reflection_pad = nn.ReflectionPad2d(reflection_padding)
|
94 |
-
self.conv2d = nn.Conv2d(in_channels, out_channels, ks, stride)
|
95 |
-
|
96 |
-
def forward(self, x):
|
97 |
-
x_in = x
|
98 |
-
if self.upsample:
|
99 |
-
x_in = torch.nn.functional.interpolate(x_in, mode='nearest', scale_factor=self.upsample)
|
100 |
-
out = self.reflection_pad(x_in)
|
101 |
-
out = self.conv2d(out)
|
102 |
-
return out
|
103 |
-
|
104 |
-
class TransformerNet(Module):
|
105 |
-
"A simple network for style transfer"
|
106 |
-
def __init__(self):
|
107 |
-
# Initial convolution layers
|
108 |
-
self.conv1 = ReflectionLayer(3, 32, ks=9, stride=1)
|
109 |
-
self.in1 = nn.InstanceNorm2d(32, affine=True)
|
110 |
-
self.conv2 = ReflectionLayer(32, 64, ks=3, stride=2)
|
111 |
-
self.in2 = nn.InstanceNorm2d(64, affine=True)
|
112 |
-
self.conv3 = ReflectionLayer(64, 128, ks=3, stride=2)
|
113 |
-
self.in3 = nn.InstanceNorm2d(128, affine=True)
|
114 |
-
# Residual layers
|
115 |
-
self.res1 = ResidualBlock(128)
|
116 |
-
self.res2 = ResidualBlock(128)
|
117 |
-
self.res3 = ResidualBlock(128)
|
118 |
-
self.res4 = ResidualBlock(128)
|
119 |
-
self.res5 = ResidualBlock(128)
|
120 |
-
# Upsampling Layers
|
121 |
-
self.deconv1 = UpsampleConvLayer(128, 64, ks=3, stride=1, upsample=2)
|
122 |
-
self.in4 = nn.InstanceNorm2d(64, affine=True)
|
123 |
-
self.deconv2 = UpsampleConvLayer(64, 32, ks=3, stride=1, upsample=2)
|
124 |
-
self.in5 = nn.InstanceNorm2d(32, affine=True)
|
125 |
-
self.deconv3 = ReflectionLayer(32, 3, ks=9, stride=1)
|
126 |
-
# Non-linearities
|
127 |
-
self.relu = nn.ReLU()
|
128 |
-
|
129 |
-
def forward(self, X):
|
130 |
-
y = self.relu(self.in1(self.conv1(X)))
|
131 |
-
y = self.relu(self.in2(self.conv2(y)))
|
132 |
-
y = self.relu(self.in3(self.conv3(y)))
|
133 |
-
y = self.res1(y)
|
134 |
-
y = self.res2(y)
|
135 |
-
y = self.res3(y)
|
136 |
-
y = self.res4(y)
|
137 |
-
y = self.res5(y)
|
138 |
-
y = self.relu(self.in4(self.deconv1(y)))
|
139 |
-
y = self.relu(self.in5(self.deconv2(y)))
|
140 |
-
y = self.deconv3(y)
|
141 |
-
return y
|
142 |
-
|
143 |
-
def _inner(feat_net, hooks, x):
|
144 |
-
feat_net(x)
|
145 |
-
return hooks.stored
|
146 |
-
|
147 |
-
def _get_layers(arch:str, pretrained=True):
|
148 |
-
"Get the layers and arch for a VGG Model (16 and 19 are supported only)"
|
149 |
-
feat_net = vgg19(pretrained=pretrained).cuda() if arch.find('9') > 1 else vgg16(pretrained=pretrained).cuda()
|
150 |
-
config = _vgg_config.get(arch)
|
151 |
-
features = feat_net.features.cuda().eval()
|
152 |
-
for p in features.parameters(): p.requires_grad=False
|
153 |
-
return feat_net, [features[i] for i in config]
|
|
|
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 |
+
|
10 |
+
pascal_source = '.'
|
11 |
+
EXAMPLES_PATH = Path('/content/examples')
|
12 |
+
repo_id = "hugginglearners/fastai-style-transfer"
|
13 |
+
|
14 |
+
def get_stl_fs(fs): return fs[:-1]
|
15 |
+
|
16 |
+
def style_loss(inp:Tensor, out_feat:Tensor):
|
17 |
+
"Calculate style loss, assumes we have `im_grams`"
|
18 |
+
# Get batch size
|
19 |
+
bs = inp[0].shape[0]
|
20 |
+
loss = []
|
21 |
+
# For every item in our inputs
|
22 |
+
for y, f in zip(*map(get_stl_fs, [im_grams, inp])):
|
23 |
+
# Calculate MSE
|
24 |
+
loss.append(F.mse_loss(y.repeat(bs, 1, 1), gram(f)))
|
25 |
+
# Multiply their sum by 30000
|
26 |
+
return 3e5 * sum(loss)
|
27 |
+
|
28 |
+
class FeatureLoss(Module):
|
29 |
+
"Combines two losses and features into a useable loss function"
|
30 |
+
def __init__(self, feats, style_loss, act_loss, hooks, feat_net):
|
31 |
+
store_attr()
|
32 |
+
self.hooks = hooks
|
33 |
+
self.feat_net = feat_net
|
34 |
+
self.reset_metrics()
|
35 |
+
|
36 |
+
def forward(self, pred, targ):
|
37 |
+
# First get the features of our prediction and target
|
38 |
+
pred_feat, targ_feat = self.feats(self.feat_net, self.hooks, pred), self.feats(self.feat_net, self.hooks, targ)
|
39 |
+
# Calculate style and activation loss
|
40 |
+
style_loss = self.style_loss(pred_feat, targ_feat)
|
41 |
+
act_loss = self.act_loss(pred_feat, targ_feat)
|
42 |
+
# Store the loss
|
43 |
+
self._add_loss(style_loss, act_loss)
|
44 |
+
# Return the sum
|
45 |
+
return style_loss + act_loss
|
46 |
+
|
47 |
+
def reset_metrics(self):
|
48 |
+
# Generates a blank metric
|
49 |
+
self.metrics = dict(style = [], content = [])
|
50 |
+
|
51 |
+
def _add_loss(self, style_loss, act_loss):
|
52 |
+
# Add to our metrics
|
53 |
+
self.metrics['style'].append(style_loss)
|
54 |
+
self.metrics['content'].append(act_loss)
|
55 |
+
|
56 |
+
def act_loss(inp:Tensor, targ:Tensor):
|
57 |
+
"Calculate the MSE loss of the activation layers"
|
58 |
+
return F.mse_loss(inp[-1], targ[-1])
|
59 |
+
|
60 |
+
class ReflectionLayer(Module):
|
61 |
+
"A series of Reflection Padding followed by a ConvLayer"
|
62 |
+
def __init__(self, in_channels, out_channels, ks=3, stride=2):
|
63 |
+
reflection_padding = ks // 2
|
64 |
+
self.reflection_pad = nn.ReflectionPad2d(reflection_padding)
|
65 |
+
self.conv2d = nn.Conv2d(in_channels, out_channels, ks, stride)
|
66 |
+
|
67 |
+
def forward(self, x):
|
68 |
+
out = self.reflection_pad(x)
|
69 |
+
out = self.conv2d(out)
|
70 |
+
return out
|
71 |
+
|
72 |
+
class ResidualBlock(Module):
|
73 |
+
"Two reflection layers and an added activation function with residual"
|
74 |
+
def __init__(self, channels):
|
75 |
+
self.conv1 = ReflectionLayer(channels, channels, ks=3, stride=1)
|
76 |
+
self.in1 = nn.InstanceNorm2d(channels, affine=True)
|
77 |
+
self.conv2 = ReflectionLayer(channels, channels, ks=3, stride=1)
|
78 |
+
self.in2 = nn.InstanceNorm2d(channels, affine=True)
|
79 |
+
self.relu = nn.ReLU()
|
80 |
+
|
81 |
+
def forward(self, x):
|
82 |
+
residual = x
|
83 |
+
out = self.relu(self.in1(self.conv1(x)))
|
84 |
+
out = self.in2(self.conv2(out))
|
85 |
+
out = out + residual
|
86 |
+
return out
|
87 |
+
|
88 |
+
class UpsampleConvLayer(Module):
|
89 |
+
"Upsample with a ReflectionLayer"
|
90 |
+
def __init__(self, in_channels, out_channels, ks=3, stride=1, upsample=None):
|
91 |
+
self.upsample = upsample
|
92 |
+
reflection_padding = ks // 2
|
93 |
+
self.reflection_pad = nn.ReflectionPad2d(reflection_padding)
|
94 |
+
self.conv2d = nn.Conv2d(in_channels, out_channels, ks, stride)
|
95 |
+
|
96 |
+
def forward(self, x):
|
97 |
+
x_in = x
|
98 |
+
if self.upsample:
|
99 |
+
x_in = torch.nn.functional.interpolate(x_in, mode='nearest', scale_factor=self.upsample)
|
100 |
+
out = self.reflection_pad(x_in)
|
101 |
+
out = self.conv2d(out)
|
102 |
+
return out
|
103 |
+
|
104 |
+
class TransformerNet(Module):
|
105 |
+
"A simple network for style transfer"
|
106 |
+
def __init__(self):
|
107 |
+
# Initial convolution layers
|
108 |
+
self.conv1 = ReflectionLayer(3, 32, ks=9, stride=1)
|
109 |
+
self.in1 = nn.InstanceNorm2d(32, affine=True)
|
110 |
+
self.conv2 = ReflectionLayer(32, 64, ks=3, stride=2)
|
111 |
+
self.in2 = nn.InstanceNorm2d(64, affine=True)
|
112 |
+
self.conv3 = ReflectionLayer(64, 128, ks=3, stride=2)
|
113 |
+
self.in3 = nn.InstanceNorm2d(128, affine=True)
|
114 |
+
# Residual layers
|
115 |
+
self.res1 = ResidualBlock(128)
|
116 |
+
self.res2 = ResidualBlock(128)
|
117 |
+
self.res3 = ResidualBlock(128)
|
118 |
+
self.res4 = ResidualBlock(128)
|
119 |
+
self.res5 = ResidualBlock(128)
|
120 |
+
# Upsampling Layers
|
121 |
+
self.deconv1 = UpsampleConvLayer(128, 64, ks=3, stride=1, upsample=2)
|
122 |
+
self.in4 = nn.InstanceNorm2d(64, affine=True)
|
123 |
+
self.deconv2 = UpsampleConvLayer(64, 32, ks=3, stride=1, upsample=2)
|
124 |
+
self.in5 = nn.InstanceNorm2d(32, affine=True)
|
125 |
+
self.deconv3 = ReflectionLayer(32, 3, ks=9, stride=1)
|
126 |
+
# Non-linearities
|
127 |
+
self.relu = nn.ReLU()
|
128 |
+
|
129 |
+
def forward(self, X):
|
130 |
+
y = self.relu(self.in1(self.conv1(X)))
|
131 |
+
y = self.relu(self.in2(self.conv2(y)))
|
132 |
+
y = self.relu(self.in3(self.conv3(y)))
|
133 |
+
y = self.res1(y)
|
134 |
+
y = self.res2(y)
|
135 |
+
y = self.res3(y)
|
136 |
+
y = self.res4(y)
|
137 |
+
y = self.res5(y)
|
138 |
+
y = self.relu(self.in4(self.deconv1(y)))
|
139 |
+
y = self.relu(self.in5(self.deconv2(y)))
|
140 |
+
y = self.deconv3(y)
|
141 |
+
return y
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|