-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
168 lines (123 loc) · 5.03 KB
/
Copy pathmain.py
File metadata and controls
168 lines (123 loc) · 5.03 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
from torchvision.models import alexnet
import torch.nn as nn
import torch.utils.model_zoo as model_zoo
from collections import OrderedDict
import torch.nn.modules.normalization as norm
import torch
from torch.autograd import Variable
class Flatten(nn.Module):
def forward(self, input):
return input.view(input.size(0), -1)
class Re3Alexnet(nn.Module):
r"""
Modified version of Alexnet. This implementation has skip layers in order to provide the lstm with information from
earlier layers
"""
def __init__(self, num_classes=1000):
super(Re3Alexnet, self).__init__()
input_channels = 3
self.conv1 = nn.Sequential(OrderedDict([
('conv1', nn.Conv2d(input_channels, out_channels=96, kernel_size=11, stride=4)),
('relu1', nn.ReLU()),
('pool1', nn.MaxPool2d(kernel_size=3, stride=2)),
('lrn1', norm.LocalResponseNorm(size=2, alpha=2e-5, beta=0.75, k=1.0))
]))
# We are reducing the first conv
self.skip1 = nn.Sequential(OrderedDict([
('conv_reduce', nn.Conv2d(96, out_channels=16, kernel_size=1, stride=1)),
('prelu', nn.PReLU()),
('conv_flatten', Flatten())
]))
# This is output of conv1
self.conv2 = nn.Sequential(OrderedDict([
('conv2', nn.Conv2d(in_channels=96, out_channels=256, groups=2, kernel_size=5, padding=2)),
('relu2', nn.ReLU()),
('pool2', nn.MaxPool2d(kernel_size=3, stride=2)),
('lrn2', norm.LocalResponseNorm(size=2, alpha=2e-5, beta=0.75, k=1.0))
]))
self.skip2 = nn.Sequential(OrderedDict([
('conv_reduce', nn.Conv2d(256, out_channels=32, kernel_size=1, stride=1)),
('prelu', nn.PReLU()),
('conv_flatten', Flatten())
]))
self.conv3 = nn.Sequential(OrderedDict([
('conv3', nn.Conv2d(in_channels=256, out_channels=384, kernel_size=3, padding=1)),
('relu3', nn.ReLU())
]))
self.conv4 = nn.Sequential(OrderedDict([
('conv4', nn.Conv2d(in_channels=384, out_channels=384, kernel_size=3, padding=1, groups=2)),
('relu4', nn.ReLU())
]))
self.conv5 = nn.Sequential(OrderedDict([
('conv5', nn.Conv2d(in_channels=384, out_channels=256, kernel_size=3, padding=1, groups=2)),
('relu5', nn.ReLU())
]))
self.pool5 = nn.Sequential(OrderedDict([
('pool5', nn.MaxPool2d(kernel_size=3, stride=2))
]))
self.conv5_flat = nn.Sequential(OrderedDict([
('conv5_flat', Flatten())
]))
self.skip5 = nn.Sequential(OrderedDict([
('conv_reduce', nn.Conv2d(256, out_channels=64, kernel_size=1, stride=1)),
('prelu', nn.PReLU()),
('conv_flatten', Flatten())
]))
self.conv6 = nn.Sequential(OrderedDict([
('fc6', nn.Linear(37104 * 2, 2048)),
('relu6', nn.ReLU())
]))
def forward(self, x, y):
x_out1 = self.conv1(x)
x_out_skip1 = self.skip1(x_out1)
x_out2 = self.conv2(x_out1)
x_out_skip2 = self.skip2(x_out2)
x_out3 = self.conv3(x_out2)
x_out4 = self.conv4(x_out3)
x_out5 = self.conv5(x_out4)
x_out_skip5 = self.skip5(x_out5)
x_out_pool =self.pool5(x_out5)
x_out_pool = self.conv5_flat( x_out_pool)
x_out = torch.cat((x_out_skip1, x_out_skip2, x_out_skip5, x_out_pool), dim=1)
y_out1 = self.conv1(x)
y_out_skip1 = self.skip1(y_out1)
y_out2 = self.conv2(y_out1)
y_out_skip2 = self.skip2(y_out2)
y_out3 = self.conv3(y_out2)
y_out4 = self.conv4(y_out3)
y_out5 = self.conv5(y_out4)
y_out_skip5 = self.skip5(y_out5)
y_out_pool =self.pool5(y_out5)
y_out_pool = self.conv5_flat(y_out_pool)
y_out = torch.cat((y_out_skip1, y_out_skip2, y_out_skip5, y_out_pool), dim=1)
final_out = torch.cat((x_out, y_out), dim=1)
lstm_input = self.conv6(final_out)
return lstm_input
class Re3Net(nn.Module):
def __init__(self):
super(Re3Net,self).__init__()
self.AlexNet=Re3Alexnet()
self.lstm1 =nn.LSTMCell(2048,1024)
self.lstm2 = nn.LSTMCell(2048+1024,1024)
self.fc_final = nn.Linear(1024,4)
self.h0=Variable(torch.rand(1,1024))
self.c0=Variable(torch.rand(1,1024))
def init_hidden(self):
self.h0 = Variable(torch.rand(1, 1024))
self.c0 = Variable(torch.rand(1, 1024))
def forward(self, x, y, prev_LSTM_state=False):
out = self.AlexNet(x, y)
lstm_out, self.h0 = self.lstm1(out, (self.h0, self.c0))
lstm2_in = torch.cat((out, lstm_out), dim=1)
lstm2_out, h1 = self.lstm2(lstm2_in, (self.h0, self.c0))
out = self.fc_final(lstm2_out)
return out
net = Re3Net()
# print(net)
bs = 1
shape = (3, 227, 227)
input1 = Variable(torch.rand(bs, *shape))
input2 = Variable(torch.rand(bs, *shape))
ot = net.forward(input1, input2)
print(net)
print(ot.data.size())