Spaces:
Running
Running
helper files
Browse files- helpers/transform_net.py +63 -0
- helpers/vgg_loss.py +22 -0
helpers/transform_net.py
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# transform_net.py
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
|
| 5 |
+
class TransformerNet(nn.Module):
|
| 6 |
+
def __init__(self):
|
| 7 |
+
super().__init__()
|
| 8 |
+
# Convolutions (no downsampling)
|
| 9 |
+
self.conv1 = nn.Sequential(
|
| 10 |
+
nn.Conv2d(3, 32, kernel_size=9, stride=1, padding=4),
|
| 11 |
+
nn.InstanceNorm2d(32, affine=True),
|
| 12 |
+
nn.ReLU(inplace=True)
|
| 13 |
+
)
|
| 14 |
+
self.conv2 = nn.Sequential(
|
| 15 |
+
nn.Conv2d(32, 64, kernel_size=3, stride=1, padding=1),
|
| 16 |
+
nn.InstanceNorm2d(64, affine=True),
|
| 17 |
+
nn.ReLU(inplace=True)
|
| 18 |
+
)
|
| 19 |
+
self.conv3 = nn.Sequential(
|
| 20 |
+
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
|
| 21 |
+
nn.InstanceNorm2d(128, affine=True),
|
| 22 |
+
nn.ReLU(inplace=True)
|
| 23 |
+
)
|
| 24 |
+
|
| 25 |
+
# Residual blocks
|
| 26 |
+
self.res_blocks = nn.Sequential(*[ResidualBlock(128) for _ in range(5)])
|
| 27 |
+
|
| 28 |
+
# Decoder / output (NO spatial upsampling — keeps same HxW)
|
| 29 |
+
self.deconv1 = nn.Sequential(
|
| 30 |
+
nn.Conv2d(128, 64, 3, stride=1, padding=1),
|
| 31 |
+
nn.InstanceNorm2d(64, affine=True),
|
| 32 |
+
nn.ReLU(inplace=True)
|
| 33 |
+
)
|
| 34 |
+
self.deconv2 = nn.Sequential(
|
| 35 |
+
nn.Conv2d(64, 32, 3, stride=1, padding=1),
|
| 36 |
+
nn.InstanceNorm2d(32, affine=True),
|
| 37 |
+
nn.ReLU(inplace=True)
|
| 38 |
+
)
|
| 39 |
+
self.deconv3 = nn.Conv2d(32, 3, 9, stride=1, padding=4)
|
| 40 |
+
|
| 41 |
+
def forward(self, x):
|
| 42 |
+
y = self.conv1(x)
|
| 43 |
+
y = self.conv2(y)
|
| 44 |
+
y = self.conv3(y)
|
| 45 |
+
y = self.res_blocks(y)
|
| 46 |
+
y = self.deconv1(y)
|
| 47 |
+
y = self.deconv2(y)
|
| 48 |
+
y = self.deconv3(y)
|
| 49 |
+
# use tanh->scale to [0,1] (keeps stable training range)
|
| 50 |
+
return torch.tanh(y) * 0.5 + 0.5
|
| 51 |
+
|
| 52 |
+
class ResidualBlock(nn.Module):
|
| 53 |
+
def __init__(self, ch):
|
| 54 |
+
super().__init__()
|
| 55 |
+
self.block = nn.Sequential(
|
| 56 |
+
nn.Conv2d(ch, ch, 3, stride=1, padding=1),
|
| 57 |
+
nn.InstanceNorm2d(ch, affine=True),
|
| 58 |
+
nn.ReLU(inplace=True),
|
| 59 |
+
nn.Conv2d(ch, ch, 3, stride=1, padding=1),
|
| 60 |
+
nn.InstanceNorm2d(ch, affine=True)
|
| 61 |
+
)
|
| 62 |
+
def forward(self, x):
|
| 63 |
+
return x + self.block(x)
|
helpers/vgg_loss.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# vgg_loss.py
|
| 2 |
+
import torch
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torchvision.models as models
|
| 5 |
+
|
| 6 |
+
class VGG16Features(nn.Module):
|
| 7 |
+
def __init__(self, layer_ids=None):
|
| 8 |
+
super().__init__()
|
| 9 |
+
vgg = models.vgg16(pretrained=True).features
|
| 10 |
+
self.layers = vgg[:23] # up to relu4_3, adjust if needed
|
| 11 |
+
for param in self.layers.parameters():
|
| 12 |
+
param.requires_grad = False
|
| 13 |
+
|
| 14 |
+
def forward(self, x):
|
| 15 |
+
# returns features at different layers if needed
|
| 16 |
+
features = []
|
| 17 |
+
for i, layer in enumerate(self.layers):
|
| 18 |
+
x = layer(x)
|
| 19 |
+
# capture some layers:
|
| 20 |
+
if i in {3, 8, 15, 22}: # relu1_2, relu2_2, relu3_3, relu4_3
|
| 21 |
+
features.append(x)
|
| 22 |
+
return features
|