Skip to content

Commit 7e82735

Browse files
author
Elad Hoffer
committed
updates
1 parent 04a7dd2 commit 7e82735

File tree

7 files changed

+409
-4
lines changed

7 files changed

+409
-4
lines changed

seq2seq/datasets/vision.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
from PIL import ImageFile
44
from seq2seq.tools import batch_sequences
55
from seq2seq.tools.config import EOS, BOS, PAD, LANGUAGE_TOKENS
6+
import warnings
67

78
ImageFile.LOAD_TRUNCATED_IMAGES = True
8-
9+
warnings.filterwarnings("ignore", "(Possibly )?corrupt EXIF data", UserWarning)
10+
warnings.filterwarnings("ignore", "Palette images with Transparency", UserWarning)
911

1012
def imagenet_transform(scale_size=256, input_size=224, train=True, augmentation='inception', allow_var_size=False):
1113
normalize = {'mean': [0.485, 0.456, 0.406],
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import torch
2+
import torch.nn as nn
3+
import torch.nn.functional as F
4+
from collections import OrderedDict
5+
from .conv import MaskedConv2d, TimeNorm2d
6+
7+
8+
def init_model(model):
9+
# Official init from torch repo.
10+
for m in model.modules():
11+
if isinstance(m, nn.Conv2d):
12+
nn.init.kaiming_normal_(m.weight)
13+
elif isinstance(m, nn.BatchNorm2d):
14+
nn.init.constant_(m.weight, 1)
15+
nn.init.constant_(m.bias, 0)
16+
elif isinstance(m, nn.Linear):
17+
nn.init.constant_(m.bias, 0)
18+
19+
20+
class _DenseLayer(nn.Sequential):
21+
def __init__(self, num_input_features, growth_rate, bn_size, drop_rate):
22+
super(_DenseLayer, self).__init__()
23+
# self.norm1 = nn.BatchNorm2d(num_input_features)
24+
self.relu = nn.ReLU(inplace=True)
25+
self.conv1 = nn.Conv2d(
26+
num_input_features, bn_size * growth_rate, kernel_size=1, stride=1, bias=True)
27+
# self.norm2 = nn.BatchNorm2d(bn_size * growth_rate)
28+
self.conv2 = MaskedConv2d(bn_size * growth_rate, growth_rate,
29+
kernel_size=3, stride=1, padding=1, bias=True)
30+
self.drop_rate = drop_rate
31+
32+
def forward(self, inputs):
33+
# x = self.norm1(inputs)
34+
x = self.relu(inputs)
35+
x = self.conv1(x)
36+
# x = self.norm2(x)
37+
x = self.relu(x)
38+
new_features, _ = self.conv2(x)
39+
if self.drop_rate > 0:
40+
new_features = F.dropout(
41+
new_features, p=self.drop_rate, training=self.training)
42+
return torch.cat([inputs, new_features], 1)
43+
44+
45+
class _DenseBlock(nn.Sequential):
46+
def __init__(self, num_layers, num_input_features, bn_size, growth_rate, drop_rate):
47+
super(_DenseBlock, self).__init__()
48+
for i in range(num_layers):
49+
layer = _DenseLayer(num_input_features + i *
50+
growth_rate, growth_rate, bn_size, drop_rate)
51+
self.add_module('denselayer%d' % (i + 1), layer)
52+
53+
54+
class _Transition(nn.Sequential):
55+
def __init__(self, num_input_features, num_output_features):
56+
super(_Transition, self).__init__()
57+
# self.add_module('norm', nn.BatchNorm2d(num_input_features))
58+
self.add_module('relu', nn.ReLU(inplace=True))
59+
self.add_module('conv', nn.Conv2d(num_input_features, num_output_features,
60+
kernel_size=1, stride=1, bias=True))
61+
# self.add_module('pool', nn.AvgPool2d(kernel_size=(2,1), stride=(2, 1)))
62+
63+
64+
class DenseNet(nn.Module):
65+
r"""Densenet-BC model class, based on
66+
`"Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_
67+
68+
Args:
69+
growth_rate (int) - how many filters to add each layer (`k` in paper)
70+
block_config (list of 4 ints) - how many layers in each pooling block
71+
num_init_features (int) - the number of filters to learn in the first convolution layer
72+
bn_size (int) - multiplicative factor for number of bottle neck layers
73+
(i.e. bn_size * k features in the bottleneck layer)
74+
drop_rate (float) - dropout rate after each dense layer
75+
num_classes (int) - number of classification classes
76+
"""
77+
78+
def __init__(self, input_size, output_size, growth_rate=32, block_config=(6, 12, 24, 16),
79+
num_init_features=64, bn_size=4, drop_rate=0):
80+
81+
super(DenseNet, self).__init__()
82+
num_init_features = input_size
83+
# self.conv0 = MaskedConv2d(input_size, num_init_features,
84+
# kernel_size=7, stride=(2, 1), padding=3, bias=True)
85+
# self.norm0 = nn.BatchNorm2d(num_init_features)
86+
# self.relu0 = nn.ReLU(inplace=True)
87+
# self.pool0 = nn.MaxPool2d(kernel_size=(3, 1),
88+
# stride=(2, 1), padding=(1, 0))
89+
self.features = nn.Sequential()
90+
# Each denseblock
91+
num_features = num_init_features
92+
for i, num_layers in enumerate(block_config):
93+
block = _DenseBlock(num_layers=num_layers, num_input_features=num_features,
94+
bn_size=bn_size, growth_rate=growth_rate, drop_rate=drop_rate)
95+
self.features.add_module('denseblock%d' % (i + 1), block)
96+
num_features = num_features + num_layers * growth_rate
97+
if i != len(block_config) - 1:
98+
trans = _Transition(
99+
num_input_features=num_features, num_output_features=num_features // 2)
100+
self.features.add_module('transition%d' % (i + 1), trans)
101+
num_features = num_features // 2
102+
103+
# Final batch norm
104+
# self.features.add_module('norm5', nn.BatchNorm2d(num_features))
105+
106+
self.conv2 = nn.Conv2d(num_features, output_size,
107+
kernel_size=1, bias=True)
108+
# self.bn2 = nn.BatchNorm2d(output_size)
109+
self.relu = nn.ReLU(inplace=True)
110+
# Linear layer
111+
init_model(self)
112+
113+
def forward(self, x):
114+
# x, _ = self.conv0(x)
115+
# x = self.norm0(x)
116+
# x = self.relu(x)
117+
# x = self.pool0(x)
118+
features = self.features(x)
119+
out = F.relu(features)
120+
out = self.conv2(out)
121+
# out = self.bn2(out)
122+
out = self.relu(out)
123+
return out
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import torch
2+
import torch.nn as nn
3+
import math
4+
from .conv import MaskedConv2d, TimeNorm2d
5+
6+
7+
class Bottleneck(nn.Module):
8+
9+
def __init__(self, inplanes, planes, kernel_size=(3, 3), stride=1, expansion=4, downsample=None, groups=1, residual_block=None):
10+
super(Bottleneck, self).__init__()
11+
self.conv1 = nn.Conv2d(
12+
inplanes, planes, kernel_size=1, bias=False)
13+
self.bn1 = nn.BatchNorm2d(planes)
14+
self.conv2 = MaskedConv2d(
15+
planes, planes, 3, padding=1, stride=(stride, 1), groups=groups, bias=False)
16+
self.bn2 = nn.BatchNorm2d(planes)
17+
self.conv3 = nn.Conv2d(
18+
planes, planes * expansion, kernel_size=1, bias=False)
19+
self.bn3 = nn.BatchNorm2d(planes * expansion)
20+
self.relu = nn.ReLU(inplace=True)
21+
self.downsample = downsample
22+
self.residual_block = residual_block
23+
self.stride = stride
24+
self.expansion = expansion
25+
26+
def forward(self, x, cache=None):
27+
residual = x
28+
out = self.conv1(x)
29+
out = self.bn1(out)
30+
out = self.relu(out)
31+
out, cache = self.conv2(out, cache)
32+
out = self.bn2(out)
33+
out = self.relu(out)
34+
out = self.conv3(out)
35+
out = self.bn3(out)
36+
if self.downsample is not None:
37+
residual = self.downsample(residual)
38+
39+
if self.residual_block is not None:
40+
residual = self.residual_block(residual)
41+
out += residual
42+
out = self.relu(out)
43+
44+
return out
45+
46+
47+
def init_model(model):
48+
for m in model.modules():
49+
if isinstance(m, nn.Conv2d) or isinstance(m, MaskedConv2d):
50+
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
51+
m.weight.data.normal_(0, math.sqrt(2. / n))
52+
elif isinstance(m, nn.BatchNorm2d):
53+
m.weight.data.fill_(1)
54+
m.bias.data.zero_()
55+
for m in model.modules():
56+
if isinstance(m, Bottleneck):
57+
nn.init.constant_(m.bn3.weight, 0)
58+
59+
60+
class ResNet(nn.Module):
61+
62+
def __init__(self, input_size, output_size, inplanes=128, strided=True,
63+
block=Bottleneck, residual_block=None, layers=[1, 1, 1, 1],
64+
width=[128, 128, 128, 128], expansion=2, groups=[1, 1, 1, 1]):
65+
super(ResNet, self).__init__()
66+
self.inplanes = inplanes
67+
self.conv1 = MaskedConv2d(input_size, self.inplanes, kernel_size=7, stride=(2, 1), padding=3,
68+
bias=False)
69+
self.bn1 = nn.BatchNorm2d(self.inplanes)
70+
self.relu = nn.ReLU(inplace=True)
71+
72+
for i in range(len(layers)):
73+
if strided:
74+
stride = 1 if i == 0 else 2
75+
else:
76+
stride = 1
77+
setattr(self, 'layer%s' % str(i + 1),
78+
self._make_layer(block=block, planes=width[i], blocks=layers[i], expansion=expansion,
79+
stride=stride, residual_block=residual_block, groups=groups[i])) # 1 if i == 0 else 2
80+
81+
self.conv2 = nn.Conv2d(self.inplanes, output_size,
82+
kernel_size=1, bias=False)
83+
self.bn2 = nn.BatchNorm2d(output_size)
84+
self.relu = nn.ReLU(inplace=True)
85+
init_model(self)
86+
87+
def _make_layer(self, block, planes, blocks, expansion=1, stride=1, groups=1, residual_block=None):
88+
downsample = None
89+
out_planes = planes * expansion
90+
if stride != 1 or self.inplanes != out_planes:
91+
downsample = nn.Sequential(
92+
nn.Conv2d(self.inplanes, out_planes,
93+
kernel_size=1, stride=(stride, 1), bias=True),
94+
nn.BatchNorm2d(planes * expansion),
95+
)
96+
if residual_block is not None:
97+
residual_block = residual_block(out_planes)
98+
99+
layers = []
100+
layers.append(block(self.inplanes, planes, stride=stride, expansion=expansion,
101+
downsample=downsample, groups=groups, residual_block=residual_block))
102+
self.inplanes = planes * expansion
103+
for i in range(1, blocks):
104+
layers.append(block(self.inplanes, planes, expansion=expansion, groups=groups,
105+
residual_block=residual_block))
106+
107+
return nn.Sequential(*layers)
108+
109+
def forward(self, x):
110+
x, _ = self.conv1(x)
111+
x = self.bn1(x)
112+
x = self.relu(x)
113+
x = self.layer1(x)
114+
115+
x = self.layer2(x)
116+
x = self.layer3(x)
117+
x = self.layer4(x)
118+
x = self.conv2(x)
119+
x = self.bn2(x)
120+
x = self.relu(x)
121+
return x

0 commit comments

Comments
 (0)