Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Pytorch_CNN_vanilla_binary_weights
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Julien Dejasmin
Pytorch_CNN_vanilla_binary_weights
Commits
7d7a2508
Commit
7d7a2508
authored
Apr 22, 2020
by
Julien Dejasmin
Browse files
Options
Downloads
Patches
Plain Diff
new archi omniglot CNN
parent
753028ef
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
21
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
utils/models.py
+66
-55
66 additions, 55 deletions
utils/models.py
with
66 additions
and
55 deletions
utils/models.py
+
66
−
55
View file @
7d7a2508
...
@@ -211,48 +211,53 @@ class NoBinaryNetOmniglotClassification(Net):
...
@@ -211,48 +211,53 @@ class NoBinaryNetOmniglotClassification(Net):
def
__init__
(
self
):
def
__init__
(
self
):
super
(
NoBinaryNetOmniglotClassification
,
self
).
__init__
()
super
(
NoBinaryNetOmniglotClassification
,
self
).
__init__
()
self
.
layer1
=
nn
.
Conv2d
(
1
,
128
,
kernel_size
=
3
,
padding
=
1
,
stride
=
2
)
self
.
layer1
=
nn
.
Conv2d
(
1
,
64
,
kernel_size
=
3
,
padding
=
1
,
stride
=
1
)
self
.
batchNorm1
=
nn
.
BatchNorm2d
(
128
)
self
.
batchNorm1
=
nn
.
BatchNorm2d
(
64
)
self
.
dropout1
=
nn
.
Dropout
(
0.5
)
#50 % probability
#
self.dropout1 = nn.Dropout(0.5) #50 % probability
#
self.maxPool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self
.
maxPool1
=
nn
.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)
self
.
act_layer1
=
nn
.
ReLU
()
self
.
act_layer1
=
nn
.
ReLU
()
self
.
layer2
=
nn
.
Conv2d
(
128
,
256
,
kernel_size
=
3
,
padding
=
1
,
stride
=
2
)
self
.
layer2
=
nn
.
Conv2d
(
64
,
128
,
kernel_size
=
3
,
padding
=
1
,
stride
=
1
)
self
.
batchNorm2
=
nn
.
BatchNorm2d
(
256
)
self
.
batchNorm2
=
nn
.
BatchNorm2d
(
128
)
self
.
dropout2
=
nn
.
Dropout
(
0.5
)
#
self.dropout2 = nn.Dropout(0.5)
#
self.maxPool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self
.
maxPool2
=
nn
.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)
self
.
act_layer2
=
nn
.
ReLU
()
self
.
act_layer2
=
nn
.
ReLU
()
self
.
layer3
=
nn
.
Conv2d
(
256
,
256
,
kernel_size
=
3
,
padding
=
1
,
stride
=
2
)
self
.
layer3
=
nn
.
Conv2d
(
128
,
256
,
kernel_size
=
3
,
padding
=
1
,
stride
=
1
)
self
.
batchNorm3
=
nn
.
BatchNorm2d
(
256
)
self
.
batchNorm3
=
nn
.
BatchNorm2d
(
256
)
self
.
dropout3
=
nn
.
Dropout
(
0.5
)
#
self.dropout3 = nn.Dropout(0.5)
#
self.maxPool3 = nn.MaxPool2d(kernel_size=2, stride=2)
self
.
maxPool3
=
nn
.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)
self
.
act_layer3
=
nn
.
ReLU
()
self
.
act_layer3
=
nn
.
ReLU
()
self
.
layer4
=
nn
.
Conv2d
(
256
,
256
,
kernel_size
=
3
,
padding
=
1
,
stride
=
2
)
self
.
layer4
=
nn
.
Conv2d
(
256
,
512
,
kernel_size
=
3
,
padding
=
1
,
stride
=
1
)
self
.
batchNorm4
=
nn
.
BatchNorm2d
(
256
)
self
.
batchNorm4
=
nn
.
BatchNorm2d
(
512
)
self
.
dropout4
=
nn
.
Dropout
(
0.5
)
# self.dropout4 = nn.Dropout(0.5)
self
.
maxPool4
=
nn
.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)
self
.
act_layer4
=
nn
.
ReLU
()
self
.
act_layer4
=
nn
.
ReLU
()
self
.
layer5
=
nn
.
Conv2d
(
256
,
256
,
kernel_size
=
3
,
padding
=
1
,
stride
=
2
)
self
.
layer5
=
nn
.
Conv2d
(
512
,
512
,
kernel_size
=
3
,
padding
=
1
,
stride
=
1
)
self
.
batchNorm5
=
nn
.
BatchNorm2d
(
256
)
self
.
batchNorm5
=
nn
.
BatchNorm2d
(
512
)
#
self.maxPool
4
= nn.MaxPool2d(kernel_size=2, stride=2)
self
.
maxPool
5
=
nn
.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)
self
.
act_layer5
=
nn
.
ReLU
()
self
.
act_layer5
=
nn
.
ReLU
()
self
.
fc
=
nn
.
Linear
(
3
*
3
*
256
,
1623
)
self
.
fc1
=
nn
.
Linear
(
3
*
3
*
512
,
4096
)
self
.
act_fc1
=
nn
.
ReLU
()
self
.
dropout1
=
nn
.
Dropout
(
0.5
)
self
.
fc2
=
nn
.
Linear
(
4096
,
1623
)
def
forward
(
self
,
input
):
def
forward
(
self
,
input
):
x
=
input
x
=
input
slope
=
1.0
slope
=
1.0
"""
"""
maxPool1
x_layer1 = self.act_layer1(self.maxPool1(self.batchNorm1(self.layer1(x) * slope)))
x_layer1 = self.act_layer1(self.maxPool1(self.batchNorm1(self.layer1(x) * slope)))
x_layer2 = self.act_layer2(self.maxPool2(self.batchNorm2(self.layer2(x_layer1))))
x_layer2 = self.act_layer2(self.maxPool2(self.batchNorm2(self.layer2(x_layer1))))
x_layer3 = self.act_layer3(self.maxPool3(self.batchNorm3(self.layer3(x_layer2))))
x_layer3 = self.act_layer3(self.maxPool3(self.batchNorm3(self.layer3(x_layer2))))
x_layer4 = self.act_layer4(self.maxPool4(self.batchNorm4(self.layer4(x_layer3))))
x_layer4 = self.act_layer4(self.maxPool4(self.batchNorm4(self.layer4(x_layer3))))
"""
"""
x_layer1
=
self
.
act_layer1
(
self
.
dropout
1
(
self
.
batchNorm1
(
self
.
layer1
(
x
)
*
slope
)))
x_layer1
=
self
.
act_layer1
(
self
.
maxPool
1
(
self
.
batchNorm1
(
self
.
layer1
(
x
)
*
slope
)))
x_layer2
=
self
.
act_layer2
(
self
.
dropout
2
(
self
.
batchNorm2
(
self
.
layer2
(
x_layer1
))))
x_layer2
=
self
.
act_layer2
(
self
.
maxPool
2
(
self
.
batchNorm2
(
self
.
layer2
(
x_layer1
))))
x_layer3
=
self
.
act_layer3
(
self
.
dropout
3
(
self
.
batchNorm3
(
self
.
layer3
(
x_layer2
))))
x_layer3
=
self
.
act_layer3
(
self
.
maxPool
3
(
self
.
batchNorm3
(
self
.
layer3
(
x_layer2
))))
x_layer4
=
self
.
act_layer4
(
self
.
dropout
4
(
self
.
batchNorm4
(
self
.
layer4
(
x_layer3
))))
x_layer4
=
self
.
act_layer4
(
self
.
maxPool
4
(
self
.
batchNorm4
(
self
.
layer4
(
x_layer3
))))
x_layer5
=
self
.
act_layer5
(
self
.
batchNorm5
(
self
.
layer5
(
x_layer4
)))
x_layer5
=
self
.
act_layer5
(
self
.
maxPool5
(
self
.
batchNorm5
(
self
.
layer5
(
x_layer4
)))
)
x_layer5
=
x_layer5
.
view
(
x_layer5
.
size
(
0
),
-
1
)
x_layer5
=
x_layer5
.
view
(
x_layer5
.
size
(
0
),
-
1
)
x_fc
=
self
.
fc
(
x_layer5
)
x_fc1
=
self
.
dropout1
(
self
.
act_fc1
(
self
.
fc1
(
x_layer5
)))
x_out
=
F
.
log_softmax
(
x_fc
,
dim
=
1
)
x_fc2
=
self
.
fc2
(
x_fc1
)
x_out
=
F
.
log_softmax
(
x_fc2
,
dim
=
1
)
return
x_out
return
x_out
...
@@ -274,10 +279,10 @@ class BinaryNetOmniglotClassification(Net):
...
@@ -274,10 +279,10 @@ class BinaryNetOmniglotClassification(Net):
self
.
third_conv_layer
=
third_conv_layer
self
.
third_conv_layer
=
third_conv_layer
self
.
fourth_conv_layer
=
fourth_conv_layer
self
.
fourth_conv_layer
=
fourth_conv_layer
self
.
layer1
=
nn
.
Conv2d
(
1
,
128
,
kernel_size
=
3
,
padding
=
1
,
stride
=
2
)
self
.
layer1
=
nn
.
Conv2d
(
1
,
64
,
kernel_size
=
3
,
padding
=
1
,
stride
=
1
)
self
.
batchNorm1
=
nn
.
BatchNorm2d
(
128
)
self
.
batchNorm1
=
nn
.
BatchNorm2d
(
64
)
self
.
dropout1
=
nn
.
Dropout
(
0.5
)
#
self.dropout1 = nn.Dropout(0.5)
#
self.maxPool1 = nn.MaxPool2d(kernel_size=2, stride=2)
self
.
maxPool1
=
nn
.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)
if
self
.
first_conv_layer
:
if
self
.
first_conv_layer
:
if
self
.
mode
==
'
Deterministic
'
:
if
self
.
mode
==
'
Deterministic
'
:
self
.
act_layer1
=
DeterministicBinaryActivation
(
estimator
=
estimator
)
self
.
act_layer1
=
DeterministicBinaryActivation
(
estimator
=
estimator
)
...
@@ -285,10 +290,10 @@ class BinaryNetOmniglotClassification(Net):
...
@@ -285,10 +290,10 @@ class BinaryNetOmniglotClassification(Net):
self
.
act_layer1
=
StochasticBinaryActivation
(
estimator
=
estimator
)
self
.
act_layer1
=
StochasticBinaryActivation
(
estimator
=
estimator
)
else
:
else
:
self
.
act_layer1
=
nn
.
ReLU
()
self
.
act_layer1
=
nn
.
ReLU
()
self
.
layer2
=
nn
.
Conv2d
(
128
,
256
,
kernel_size
=
3
,
padding
=
1
,
stride
=
2
)
self
.
layer2
=
nn
.
Conv2d
(
64
,
128
,
kernel_size
=
3
,
padding
=
1
,
stride
=
1
)
self
.
batchNorm2
=
nn
.
BatchNorm2d
(
256
)
self
.
batchNorm2
=
nn
.
BatchNorm2d
(
128
)
self
.
dropout2
=
nn
.
Dropout
(
0.5
)
#
self.dropout2 = nn.Dropout(0.5)
#
self.maxPool2 = nn.MaxPool2d(kernel_size=2, stride=2)
self
.
maxPool2
=
nn
.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)
if
self
.
second_conv_layer
:
if
self
.
second_conv_layer
:
if
self
.
mode
==
'
Deterministic
'
:
if
self
.
mode
==
'
Deterministic
'
:
self
.
act_layer2
=
DeterministicBinaryActivation
(
estimator
=
estimator
)
self
.
act_layer2
=
DeterministicBinaryActivation
(
estimator
=
estimator
)
...
@@ -296,10 +301,10 @@ class BinaryNetOmniglotClassification(Net):
...
@@ -296,10 +301,10 @@ class BinaryNetOmniglotClassification(Net):
self
.
act_layer2
=
StochasticBinaryActivation
(
estimator
=
estimator
)
self
.
act_layer2
=
StochasticBinaryActivation
(
estimator
=
estimator
)
else
:
else
:
self
.
act_layer2
=
nn
.
ReLU
()
self
.
act_layer2
=
nn
.
ReLU
()
self
.
layer3
=
nn
.
Conv2d
(
256
,
256
,
kernel_size
=
3
,
padding
=
1
,
stride
=
2
)
self
.
layer3
=
nn
.
Conv2d
(
128
,
256
,
kernel_size
=
3
,
padding
=
1
,
stride
=
1
)
self
.
batchNorm3
=
nn
.
BatchNorm2d
(
256
)
self
.
batchNorm3
=
nn
.
BatchNorm2d
(
256
)
self
.
dropout3
=
nn
.
Dropout
(
0.5
)
#
self.dropout3 = nn.Dropout(0.5)
#
self.maxPool3 = nn.MaxPool2d(kernel_size=2, stride=2)
self
.
maxPool3
=
nn
.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)
if
self
.
third_conv_layer
:
if
self
.
third_conv_layer
:
if
self
.
mode
==
'
Deterministic
'
:
if
self
.
mode
==
'
Deterministic
'
:
self
.
act_layer3
=
DeterministicBinaryActivation
(
estimator
=
estimator
)
self
.
act_layer3
=
DeterministicBinaryActivation
(
estimator
=
estimator
)
...
@@ -307,10 +312,10 @@ class BinaryNetOmniglotClassification(Net):
...
@@ -307,10 +312,10 @@ class BinaryNetOmniglotClassification(Net):
self
.
act_layer3
=
StochasticBinaryActivation
(
estimator
=
estimator
)
self
.
act_layer3
=
StochasticBinaryActivation
(
estimator
=
estimator
)
else
:
else
:
self
.
act_layer3
=
nn
.
ReLU
()
self
.
act_layer3
=
nn
.
ReLU
()
self
.
layer4
=
nn
.
Conv2d
(
256
,
256
,
kernel_size
=
3
,
padding
=
1
,
stride
=
2
)
self
.
layer4
=
nn
.
Conv2d
(
256
,
512
,
kernel_size
=
3
,
padding
=
1
,
stride
=
1
)
self
.
batchNorm4
=
nn
.
BatchNorm2d
(
256
)
self
.
batchNorm4
=
nn
.
BatchNorm2d
(
512
)
self
.
dropout4
=
nn
.
Dropout
(
0.5
)
#
self.dropout4 = nn.Dropout(0.5)
#
self.maxPool4 = nn.MaxPool2d(kernel_size=2, stride=2)
self
.
maxPool4
=
nn
.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)
if
self
.
fourth_conv_layer
:
if
self
.
fourth_conv_layer
:
if
self
.
mode
==
'
Deterministic
'
:
if
self
.
mode
==
'
Deterministic
'
:
self
.
act_layer4
=
DeterministicBinaryActivation
(
estimator
=
estimator
)
self
.
act_layer4
=
DeterministicBinaryActivation
(
estimator
=
estimator
)
...
@@ -318,10 +323,15 @@ class BinaryNetOmniglotClassification(Net):
...
@@ -318,10 +323,15 @@ class BinaryNetOmniglotClassification(Net):
self
.
act_layer4
=
StochasticBinaryActivation
(
estimator
=
estimator
)
self
.
act_layer4
=
StochasticBinaryActivation
(
estimator
=
estimator
)
else
:
else
:
self
.
act_layer4
=
nn
.
ReLU
()
self
.
act_layer4
=
nn
.
ReLU
()
self
.
layer5
=
nn
.
Conv2d
(
256
,
256
,
kernel_size
=
3
,
padding
=
1
,
stride
=
2
)
self
.
layer5
=
nn
.
Conv2d
(
512
,
512
,
kernel_size
=
3
,
padding
=
1
,
stride
=
1
)
self
.
batchNorm5
=
nn
.
BatchNorm2d
(
256
)
self
.
batchNorm5
=
nn
.
BatchNorm2d
(
512
)
self
.
maxPool5
=
nn
.
MaxPool2d
(
kernel_size
=
2
,
stride
=
2
)
self
.
act_layer5
=
nn
.
ReLU
()
self
.
act_layer5
=
nn
.
ReLU
()
self
.
fc
=
nn
.
Linear
(
3
*
3
*
256
,
1623
)
self
.
fc1
=
nn
.
Linear
(
3
*
3
*
512
,
4096
)
self
.
act_fc1
=
nn
.
ReLU
()
self
.
dropout1
=
nn
.
Dropout
(
0.5
)
self
.
fc2
=
nn
.
Linear
(
4096
,
1623
)
def
forward
(
self
,
input
):
def
forward
(
self
,
input
):
x
=
input
x
=
input
...
@@ -345,25 +355,26 @@ class BinaryNetOmniglotClassification(Net):
...
@@ -345,25 +355,26 @@ class BinaryNetOmniglotClassification(Net):
x_layer4 = self.act_layer4(self.maxPool4(self.batchNorm4(self.layer4(x_layer3) * slope)))
x_layer4 = self.act_layer4(self.maxPool4(self.batchNorm4(self.layer4(x_layer3) * slope)))
"""
"""
if
self
.
first_conv_layer
:
if
self
.
first_conv_layer
:
x_layer1
=
self
.
act_layer1
(((
self
.
dropout
1
(
self
.
batchNorm1
(
self
.
layer1
(
x
)))),
slope
))
x_layer1
=
self
.
act_layer1
(((
self
.
maxPool
1
(
self
.
batchNorm1
(
self
.
layer1
(
x
)))),
slope
))
else
:
else
:
x_layer1
=
self
.
act_layer1
(
self
.
dropout
1
(
self
.
batchNorm1
(
self
.
layer1
(
x
)
*
slope
)))
x_layer1
=
self
.
act_layer1
(
self
.
maxPool
1
(
self
.
batchNorm1
(
self
.
layer1
(
x
)
*
slope
)))
if
self
.
second_conv_layer
:
if
self
.
second_conv_layer
:
x_layer2
=
self
.
act_layer2
(((
self
.
dropout
2
(
self
.
batchNorm2
(
self
.
layer2
(
x_layer1
)))),
slope
))
x_layer2
=
self
.
act_layer2
(((
self
.
maxPool
2
(
self
.
batchNorm2
(
self
.
layer2
(
x_layer1
)))),
slope
))
else
:
else
:
x_layer2
=
self
.
act_layer2
(
self
.
dropout
2
(
self
.
batchNorm2
(
self
.
layer2
(
x_layer1
)
*
slope
)))
x_layer2
=
self
.
act_layer2
(
self
.
maxPool
2
(
self
.
batchNorm2
(
self
.
layer2
(
x_layer1
)
*
slope
)))
if
self
.
third_conv_layer
:
if
self
.
third_conv_layer
:
x_layer3
=
self
.
act_layer3
(((
self
.
dropout
3
(
self
.
batchNorm3
(
self
.
layer3
(
x_layer2
)))),
slope
))
x_layer3
=
self
.
act_layer3
(((
self
.
maxPool
3
(
self
.
batchNorm3
(
self
.
layer3
(
x_layer2
)))),
slope
))
else
:
else
:
x_layer3
=
self
.
act_layer3
(
self
.
dropout
3
(
self
.
batchNorm3
(
self
.
layer3
(
x_layer2
)
*
slope
)))
x_layer3
=
self
.
act_layer3
(
self
.
maxPool
3
(
self
.
batchNorm3
(
self
.
layer3
(
x_layer2
)
*
slope
)))
if
self
.
fourth_conv_layer
:
if
self
.
fourth_conv_layer
:
x_layer4
=
self
.
act_layer4
(((
self
.
dropout
4
(
self
.
batchNorm4
(
self
.
layer4
(
x_layer3
)))),
slope
))
x_layer4
=
self
.
act_layer4
(((
self
.
maxPool
4
(
self
.
batchNorm4
(
self
.
layer4
(
x_layer3
)))),
slope
))
else
:
else
:
x_layer4
=
self
.
act_layer4
(
self
.
dropout
4
(
self
.
batchNorm4
(
self
.
layer4
(
x_layer3
)
*
slope
)))
x_layer4
=
self
.
act_layer4
(
self
.
maxPool
4
(
self
.
batchNorm4
(
self
.
layer4
(
x_layer3
)
*
slope
)))
x_layer5
=
self
.
act_layer5
(
self
.
batchNorm5
(
self
.
layer5
(
x_layer4
)
*
slope
))
x_layer5
=
self
.
act_layer5
(
self
.
maxPool5
(
self
.
batchNorm5
(
self
.
layer5
(
x_layer4
)
)
))
x_layer5
=
x_layer5
.
view
(
x_layer5
.
size
(
0
),
-
1
)
x_layer5
=
x_layer5
.
view
(
x_layer5
.
size
(
0
),
-
1
)
x_fc
=
self
.
fc
(
x_layer5
)
x_fc1
=
self
.
dropout1
(
self
.
act_fc1
(
self
.
fc1
(
x_layer5
)))
x_out
=
F
.
log_softmax
(
x_fc
,
dim
=
1
)
x_fc2
=
self
.
fc2
(
x_fc1
)
x_out
=
F
.
log_softmax
(
x_fc2
,
dim
=
1
)
return
x_out
return
x_out
...
...
This diff is collapsed.
Click to expand it.
Prev
1
2
Next
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment