forked from bbrattoli/JigsawPuzzlePytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JigsawNetwork.py
90 lines (68 loc) · 3.1 KB
/
JigsawNetwork.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
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 13 15:57:01 2017
@author: Biagio Brattoli
"""
import torch
import torch.nn as nn
from torch import cat
import torch.nn.init as init
import sys
sys.path.append('Utils')
from Layers import LRN
class Network(nn.Module):
def __init__(self, classes=1000):
super(Network, self).__init__()
self.conv = nn.Sequential()
self.conv.add_module('conv1_s1',nn.Conv2d(3, 96, kernel_size=11, stride=2, padding=0))
self.conv.add_module('relu1_s1',nn.ReLU(inplace=True))
self.conv.add_module('pool1_s1',nn.MaxPool2d(kernel_size=3, stride=2))
self.conv.add_module('lrn1_s1',LRN(local_size=5, alpha=0.0001, beta=0.75))
self.conv.add_module('conv2_s1',nn.Conv2d(96, 256, kernel_size=5, padding=2, groups=2))
self.conv.add_module('relu2_s1',nn.ReLU(inplace=True))
self.conv.add_module('pool2_s1',nn.MaxPool2d(kernel_size=3, stride=2))
self.conv.add_module('lrn2_s1',LRN(local_size=5, alpha=0.0001, beta=0.75))
self.conv.add_module('conv3_s1',nn.Conv2d(256, 384, kernel_size=3, padding=1))
self.conv.add_module('relu3_s1',nn.ReLU(inplace=True))
self.conv.add_module('conv4_s1',nn.Conv2d(384, 384, kernel_size=3, padding=1, groups=2))
self.conv.add_module('relu4_s1',nn.ReLU(inplace=True))
self.conv.add_module('conv5_s1',nn.Conv2d(384, 256, kernel_size=3, padding=1, groups=2))
self.conv.add_module('relu5_s1',nn.ReLU(inplace=True))
self.conv.add_module('pool5_s1',nn.MaxPool2d(kernel_size=3, stride=2))
self.fc6 = nn.Sequential()
self.fc6.add_module('fc6_s1',nn.Linear(256*3*3, 1024))
self.fc6.add_module('relu6_s1',nn.ReLU(inplace=True))
self.fc6.add_module('drop6_s1',nn.Dropout(p=0.5))
self.fc7 = nn.Sequential()
self.fc7.add_module('fc7',nn.Linear(9*1024,4096))
self.fc7.add_module('relu7',nn.ReLU(inplace=True))
self.fc7.add_module('drop7',nn.Dropout(p=0.5))
self.classifier = nn.Sequential()
self.classifier.add_module('fc8',nn.Linear(4096, classes))
#self.apply(weights_init)
def load(self,checkpoint):
model_dict = self.state_dict()
pretrained_dict = torch.load(checkpoint)
pretrained_dict = {k: v for k, v in list(pretrained_dict.items()) if k in model_dict and 'fc8' not in k}
model_dict.update(pretrained_dict)
self.load_state_dict(model_dict)
print([k for k, v in list(pretrained_dict.items())])
def save(self,checkpoint):
torch.save(self.state_dict(), checkpoint)
def forward(self, x):
B,T,C,H,W = x.size()
x = x.transpose(0,1)
x_list = []
for i in range(9):
z = self.conv(x[i])
z = self.fc6(z.view(B,-1))
z = z.view([B,1,-1])
x_list.append(z)
x = cat(x_list,1)
x = self.fc7(x.view(B,-1))
x = self.classifier(x)
return x
def weights_init(model):
if type(model) in [nn.Conv2d,nn.Linear]:
nn.init.xavier_normal(model.weight.data)
nn.init.constant(model.bias.data, 0.1)