1
1
import torch
2
2
import torch .nn as nn
3
+ import torch .nn .init as init
3
4
from torch .autograd import Variable
4
5
import unittest
5
6
import numpy as np
@@ -11,11 +12,12 @@ def convLayer(in_planes, out_planes, stride=1, padding = 1, bias = True):
11
12
return nn .Sequential (
12
13
nn .Conv2d (in_planes , out_planes , kernel_size = 3 ,
13
14
stride = stride , padding = padding , bias = bias ),
15
+ #nn.LeakyReLU(0.2),
14
16
nn .LeakyReLU (),
15
17
nn .BatchNorm2d (out_planes ),
16
- # nn.MaxPool2d(kernel_size=2, stride=2,ceil_mode=True),
18
+ #nn.MaxPool2d(kernel_size=2, stride=2,ceil_mode=True),
17
19
nn .MaxPool2d (kernel_size = 2 , stride = 2 ),
18
- nn .Dropout ()
20
+ nn .Dropout (0.1 )
19
21
)
20
22
21
23
@@ -37,32 +39,47 @@ def __init__(self, layer_sizes, num_channels = 1, keep_prob = 0.5):
37
39
self .layer3 = convLayer (layer_sizes [1 ], layer_sizes [2 ])
38
40
self .layer4 = convLayer (layer_sizes [2 ], layer_sizes [3 ])
39
41
42
+
40
43
self .weights_init (self .layer1 )
41
44
self .weights_init (self .layer2 )
42
45
self .weights_init (self .layer3 )
43
46
self .weights_init (self .layer4 )
44
47
'''
45
- # Module initialization
46
- for m in self.modules():
48
+ self.weights_init_tensorflow(self.layer1,1)
49
+ self.weights_init_tensorflow(self.layer2,2)
50
+ self.weights_init_tensorflow(self.layer3,3)
51
+ self.weights_init_tensorflow(self.layer4,4)
52
+ '''
53
+
54
+ def weights_init (self ,module ):
55
+ #for m in self.modules():
56
+ for m in module .modules ():
47
57
if isinstance (m , nn .Conv2d ):
48
- n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
49
- m.weight.data.normal_(0, math.sqrt(2. / n))
58
+ init .xavier_uniform (m .weight , gain = np .sqrt (2 ))
59
+ init .constant (m .bias , 0 )
60
+ ##m.weight.data.normal_(0.0, 0.02)
61
+ #n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
62
+ #m.weight.data.normal_(0, math.sqrt(2. / n))
50
63
elif isinstance (m , nn .BatchNorm2d ):
64
+ #m.weight.data.normal_(1.0, 0.02)
65
+ #m.bias.data.fill_(0)
51
66
m .weight .data .fill_ (1 )
52
67
m .bias .data .zero_ ()
53
- '''
54
68
55
- def weights_init (self ,m ):
56
- for m in self .modules ():
69
+ def weights_init_tensorflow (self ,module ,layer ):
70
+ #for m in self.modules():
71
+ for m in module .modules ():
57
72
if isinstance (m , nn .Conv2d ):
58
- m .weight .data . normal_ ( 0.0 , 0.02 )
59
- #n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
60
- #m.weight .data.normal_(0, math.sqrt(2. / n ))
73
+ m .weight .data = torch . from_numpy ( np . load ( '/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/conv' + str ( layer ) + '_weigths.npy' ). transpose (( 3 , 2 , 1 , 0 )) )
74
+ m . weight . data = m .weight . data . contiguous ()
75
+ m . bias .data = torch . from_numpy ( np . load ( '/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/conv' + str ( layer ) + '_bias.npy' ))
61
76
elif isinstance (m , nn .BatchNorm2d ):
62
- m .weight .data .normal_ (1.0 , 0.02 )
63
- m .bias .data .fill_ (0 )
64
- #m.weight.data.fill_(1)
65
- #m.bias.data.zero_()
77
+ m .weight .data = torch .from_numpy (np .load (
78
+ '/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/bn' + str (
79
+ layer ) + '_gamma.npy' ))
80
+ m .bias .data = torch .from_numpy (np .load (
81
+ '/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/bn' + str (
82
+ layer ) + '_beta.npy' ))
66
83
67
84
68
85
def forward (self , image_input ):
@@ -71,19 +88,20 @@ def forward(self, image_input):
71
88
:param image_input: Image input to produce embeddings for. [batch_size, 28, 28, 1]
72
89
:return: Embeddings of size [batch_size, 64]
73
90
"""
74
- # TODO: What is better 2 padding at first conv2d or 1 padding in the last layer of conv2d??
75
- #x = nn.Conv2d(1, 64, kernel_size=3, stride=1, bias=True).cuda()(image_input)
91
+
92
+ #x = nn.Conv2d(1, 64, kernel_size=3, stride=1, padding=0, bias=True).cuda()(image_input)
76
93
#x = nn.LeakyReLU().cuda()(x)
77
94
#x = nn.BatchNorm2d(64).cuda()(x)
78
95
#x = nn.MaxPool2d(kernel_size=2, stride=2).cuda()(x)
79
96
#x = nn.Dropout().cuda()(x)
80
97
98
+ #check = np.sum(self.layer1[0](image_input).data.cpu().numpy())
99
+
81
100
x = self .layer1 (image_input )
82
101
x = self .layer2 (x )
83
102
x = self .layer3 (x )
84
103
x = self .layer4 (x )
85
104
x = torch .squeeze (x )
86
- # TODO: flat output
87
105
return x
88
106
89
107
@@ -94,11 +112,38 @@ def setUp(self):
94
112
self .outputs = np .load ('../data/gen_encode.npy' )
95
113
self .layer_sizes = [64 ,64 ,64 ,64 ]
96
114
115
+ self .after_conv1 = np .load (
116
+ '/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/after_conv1_encoder.npy' )
117
+ self .after_conv2 = np .load (
118
+ '/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/after_conv2_encoder.npy' )
119
+ self .after_conv3 = np .load (
120
+ '/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/after_conv3_encoder.npy' )
121
+ self .after_conv4 = np .load (
122
+ '/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/after_conv4_encoder.npy' )
123
+ self .image_input = np .load (
124
+ '/home/aberenguel/pytorch/examples/MatchingNetworks/data/classifier/image_input.npy' )
125
+
97
126
def tearDown (self ):
98
127
pass
99
128
100
129
def test_forward (self ):
101
130
classifier = Classifier (layer_sizes = self .layer_sizes ).cuda ()
131
+
132
+ print ("sum conv1 tf: %f" % np .sum (self .after_conv1 ))
133
+ print ("sum conv2 tf: %f" % np .sum (self .after_conv2 ))
134
+ print ("sum conv3 tf: %f" % np .sum (self .after_conv3 ))
135
+ print ("sum conv4 tf: %f" % np .sum (self .after_conv4 ))
136
+ input = Variable (torch .from_numpy (self .image_input .transpose ((0 ,3 ,1 ,2 ))).cuda (), requires_grad = True )
137
+
138
+ x1 = classifier .layer1 (input )
139
+ print ("sum conv1 pytorch: %f" % torch .sum (x1 ).data [0 ])
140
+ x2 = classifier .layer2 (x1 )
141
+ print ("sum conv2 pytorch: %f" % torch .sum (x2 ).data [0 ])
142
+ x3 = classifier .layer3 (x2 )
143
+ print ("sum conv2 pytorch: %f" % torch .sum (x3 ).data [0 ])
144
+ x4 = classifier .layer4 (x3 )
145
+ print ("sum conv2 pytorch: %f" % torch .sum (x4 ).data [0 ])
146
+
102
147
input = Variable (torch .from_numpy (self .inputs ).cuda (), requires_grad = True )
103
148
output = classifier (input )
104
149
# TODO: why the output contains so many 0? The self.outputs d
0 commit comments