-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_behaviour_full_net.py
100 lines (80 loc) · 2.87 KB
/
test_behaviour_full_net.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
import sparse_conv as sp
import torch.nn as nn
import torch
import torch.nn.functional as F
import torch.nn.utils.prune as prune
import copy
class LeNet5(sp.SparseModel):
"""
A standard LeNet5 model
"""
def __init__(self, n_classes,sparse_conv_flag=True):
self._sparse_conv_flag=sparse_conv_flag
super(LeNet5, self).__init__(sparse_conv_flag)
self.conv1 = self.conv(in_channels=1, out_channels=6, kernel_size=5, stride=1,padding=1)
self.tanh1 = nn.Tanh()
self.pool1 = nn.AvgPool2d(kernel_size=2)
self.conv2 = self.conv(in_channels=6, out_channels=16, kernel_size=5, stride=1)
self.tanh2 = nn.Tanh()
self.pool2 = nn.AvgPool2d(kernel_size=2)
self.conv3 = self.conv(in_channels=16, out_channels=120, kernel_size=5, stride=1)
self.tanh3 = nn.Tanh()
self.linear1 = nn.Linear(in_features=120, out_features=84)
self.tanh4 = nn.Tanh()
self.linear2 = nn.Linear(in_features=84, out_features=n_classes)
def forward(self, x):
print("Forwarddd")
x = self.conv1(x)
x = self.tanh1(x)
x = self.pool1(x)
x = self.conv2(x)
x = self.tanh2(x)
x = self.pool2(x)
x = self.conv3(x)
x = self.tanh3(x)
x = torch.flatten(x, 1)
x = self.linear1(x)
x = self.tanh4(x)
logits = self.linear2(x)
probs = F.softmax(logits, dim=1)
return logits, probs
def pruning_model_random(model, px):
parameters_to_prune =[]
for name,m in model.named_modules():
if isinstance(m, nn.Conv2d):
parameters_to_prune.append((m,'weight'))
parameters_to_prune = tuple(parameters_to_prune)
prune.global_unstructured(
parameters_to_prune,
pruning_method=prune.RandomUnstructured,
amount=px,
)
RANDOM_SEED = 42
LEARNING_RATE = 0.001
BATCH_SIZE = 32
N_EPOCHS = 4
IMG_SIZE = 32
N_CLASSES = 10
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
# Assuming that we are on a CUDA machine, this should print a CUDA device:
print(device)
#LOAD THE MODEL
model = LeNet5(N_CLASSES,sparse_conv_flag=True)
model.to(device)
#PRUNE THE MODEL TO ADD SPARSITY
pruning_model_random(model,0.6)
#SET MODEL IN TESTING MODE (For each SparseConv compare Conv2D with SparseConv2D)
model._initialize_sparse_layers(input_shape=(1,1,IMG_SIZE,IMG_SIZE))
model._set_sparse_layers_mode(sp.Sparse_modes.Test)
#------------------------------------------
#------------------------------------------
#----------TESTING-------------------------
#------------------------------------------
#------------------------------------------
#Generate a dummy input to give the convolution
dummy_input = torch.randn(1, 1,IMG_SIZE,IMG_SIZE, dtype=torch.float).to(device)
dummy_input = dummy_input.cuda()
input = copy.deepcopy(dummy_input)
input = input.cuda()
model.forward(dummy_input)
exit()