-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
executable file
·103 lines (77 loc) · 3.75 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import timm
import torch.nn as nn
import torch.nn.functional as F
class ResidualBlock(nn.Module):
def __init__(self, in_channels):
super(ResidualBlock, self).__init__()
self.conv = nn.Sequential(nn.Conv2d(in_channels, in_channels, 3, padding=1, padding_mode='reflect'),
nn.InstanceNorm2d(in_channels), nn.ReLU(inplace=True),
nn.Conv2d(in_channels, in_channels, 3, padding=1, padding_mode='reflect'),
nn.InstanceNorm2d(in_channels))
def forward(self, x):
return x + self.conv(x)
class Generator(nn.Module):
def __init__(self, in_channels=64, num_block=9):
super(Generator, self).__init__()
# in conv
self.in_conv = nn.Sequential(nn.Conv2d(3, in_channels, 7, padding=3, padding_mode='reflect'),
nn.InstanceNorm2d(in_channels), nn.ReLU(inplace=True))
# down sample
down_sample = []
for _ in range(2):
out_channels = in_channels * 2
down_sample += [nn.Conv2d(in_channels, out_channels, 3, stride=2, padding=1),
nn.InstanceNorm2d(out_channels), nn.ReLU(inplace=True)]
in_channels = out_channels
self.down_sample = nn.Sequential(*down_sample)
# conv blocks
self.convs = nn.Sequential(*[ResidualBlock(in_channels) for _ in range(num_block)])
# up sample
up_sample = []
for _ in range(2):
out_channels = in_channels // 2
up_sample += [nn.ConvTranspose2d(in_channels, out_channels, 3, stride=2, padding=1, output_padding=1),
nn.InstanceNorm2d(out_channels), nn.ReLU(inplace=True)]
in_channels = out_channels
self.up_sample = nn.Sequential(*up_sample)
# out conv
self.out_conv = nn.Sequential(nn.Conv2d(in_channels, 3, 7, padding=3, padding_mode='reflect'), nn.Tanh())
def forward(self, x):
x = self.in_conv(x)
x = self.down_sample(x)
x = self.convs(x)
x = self.up_sample(x)
out = self.out_conv(x)
return out
class Discriminator(nn.Module):
def __init__(self, in_channels=64):
super(Discriminator, self).__init__()
self.conv1 = nn.Sequential(nn.Conv2d(3, in_channels, 4, stride=2, padding=1), nn.LeakyReLU(0.2, inplace=True))
self.conv2 = nn.Sequential(nn.Conv2d(in_channels, in_channels * 2, 4, stride=2, padding=1),
nn.InstanceNorm2d(in_channels * 2), nn.LeakyReLU(0.2, inplace=True))
self.conv3 = nn.Sequential(nn.Conv2d(in_channels * 2, in_channels * 4, 4, stride=2, padding=1),
nn.InstanceNorm2d(in_channels * 4), nn.LeakyReLU(0.2, inplace=True))
self.conv4 = nn.Sequential(nn.Conv2d(in_channels * 4, in_channels * 8, 4, padding=1),
nn.InstanceNorm2d(in_channels * 8), nn.LeakyReLU(0.2, inplace=True))
self.conv5 = nn.Conv2d(in_channels * 8, 1, 4, padding=1)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
out = self.conv5(x)
return out
class Extractor(nn.Module):
def __init__(self, backbone_type, emb_dim):
super(Extractor, self).__init__()
# backbone
model_name = 'resnet50' if backbone_type == 'resnet50' else 'vgg16'
self.backbone = timm.create_model(model_name, pretrained=True, num_classes=emb_dim, global_pool='max')
def forward(self, x):
x = self.backbone(x)
out = F.normalize(x, dim=-1)
return out
def set_bn_eval(m):
classname = m.__class__.__name__
if classname.find('BatchNorm2d') != -1:
m.eval()