-
Notifications
You must be signed in to change notification settings - Fork 3
/
cobmodels_test.py
136 lines (108 loc) · 3.84 KB
/
cobmodels_test.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
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
"""
Test for models constructed with layers Conv2dCOB, LinearCOB, FlattenCOB, ReLUCOB, MaxPool2dCOB, BatchNorm2dCOB,
ConvTranspose2dCOB, BatchNorm1dCOB
"""
import torch
import torch.nn as nn
from neuralteleportation.layers.activation import ReLUCOB, TanhCOB, SigmoidCOB
from neuralteleportation.layers.neuralteleportation import FlattenCOB
from neuralteleportation.layers.neuron import Conv2dCOB, LinearCOB, ConvTranspose2dCOB, BatchNorm2dCOB, \
BatchNorm1dCOB
from neuralteleportation.layers.pooling import MaxPool2dCOB
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = Conv2dCOB(1, 6, 5)
self.conv2 = Conv2dCOB(6, 16, 5)
self.fc1 = LinearCOB(16 * 20 * 20, 120)
self.fc2 = LinearCOB(120, 84)
self.fc3 = LinearCOB(84, 10)
self.flatten = FlattenCOB()
self.relu1 = ReLUCOB()
self.relu2 = ReLUCOB()
self.tanhcob = TanhCOB()
self.relu3 = ReLUCOB()
def forward(self, x):
x = self.relu1(self.conv1(x))
x = self.relu2(self.conv2(x))
x = self.flatten(x)
x = self.tanhcob(self.fc1(x))
x = self.relu3(self.fc2(x))
x = self.fc3(x)
return x
class Net2(nn.Module):
def __init__(self):
super(Net2, self).__init__()
self.conv1 = Conv2dCOB(1, 6, 5)
self.pool1 = MaxPool2dCOB(kernel_size=2)
self.pool2 = MaxPool2dCOB(kernel_size=2)
self.conv2 = Conv2dCOB(6, 16, 5)
self.flatten = FlattenCOB()
self.sigmoid = SigmoidCOB()
self.relu1 = ReLUCOB()
def forward(self, x):
x = self.pool1(self.sigmoid(self.conv1(x)))
x = self.pool2(self.relu1(self.conv2(x)))
return x
class Net3(nn.Module):
def __init__(self):
super(Net3, self).__init__()
self.conv1 = Conv2dCOB(1, 6, 5)
self.conv2 = Conv2dCOB(6, 3, 5)
self.relu1 = ReLUCOB()
self.relu2 = ReLUCOB()
self.bn1 = BatchNorm2dCOB(6)
self.bn2 = BatchNorm2dCOB(3)
def forward(self, x):
x = self.relu1(self.bn1(self.conv1(x)))
x = self.relu2(self.bn2(self.conv2(x)))
return x
class Net4(nn.Module):
def __init__(self):
super(Net4, self).__init__()
self.conv1 = Conv2dCOB(1, 6, 5)
self.conv2 = Conv2dCOB(6, 3, 5)
self.relu1 = ReLUCOB()
self.relu2 = ReLUCOB()
self.bn1 = BatchNorm2dCOB(6)
self.bn2 = BatchNorm2dCOB(3)
def forward(self, x):
x = self.relu1(self.bn1(self.conv1(x)))
x = self.relu2(self.bn2(self.conv2(x)))
return x
class ConvTransposeNet(nn.Module):
def __init__(self):
super(ConvTransposeNet, self).__init__()
self.conv1 = Conv2dCOB(1, 6, 5)
self.conv2 = ConvTranspose2dCOB(6, 3 // 2, kernel_size=2, stride=2)
self.relu1 = ReLUCOB(inplace=True)
self.relu2 = ReLUCOB(inplace=True)
def forward(self, x):
x = self.relu1(self.conv1(x))
x = self.relu2(self.conv2(x))
return x
class MLP(nn.Module):
def __init__(self):
super().__init__()
self.net = torch.nn.Sequential(
FlattenCOB(),
LinearCOB(784, 128),
BatchNorm1dCOB(128),
ReLUCOB(),
LinearCOB(128, 10)
)
def forward(self, input):
return self.net(input)
if __name__ == '__main__':
from tests.model_test import test_teleport
models = [MLP, Net, Net2, Net3, Net4, ConvTransposeNet]
input_shape = (1, 1, 28, 28)
for model in models:
try:
mod = model()
mod.eval()
diff_avg = test_teleport(mod, input_shape)
print("{} model passed with avg diff: {}".format(mod, diff_avg))
except Exception as e:
print("Teleportation failed for model: {} with error {}".format(mod, e))
print("All tests are done")