-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgrasp_models.py
142 lines (99 loc) · 3.87 KB
/
grasp_models.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
137
138
139
140
141
import sys
import numpy as np
import torch
import torch.cuda
import torch.nn as nn
import torch.nn.functional as F
# from torch.nn.utils import prune
import prune as torch_prune
import warnings
from models import PrunableNet, initialize_mask
from synflow.Layers import layers
#############################
# Subclasses of PrunableNet #
#############################
class MNISTNet(PrunableNet):
def __init__(self, *args, **kwargs):
super(MNISTNet, self).__init__(*args, **kwargs)
self.conv1 = layers.Conv2d(1, 10, 5) # "Conv 1-10"
self.conv2 = layers.Conv2d(10, 20, 5) # "Conv 10-20"
self.fc1 = layers.Linear(20 * 16 * 16, 50)
self.fc2 = layers.Linear(50, 10)
self.init_param_sizes()
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 3, stride=1))
x = F.relu(F.max_pool2d(self.conv2(x), 3, stride=1))
x = x.view(-1, self.num_flat_features(x)) # flatten
x = F.relu(self.fc1(x))
x = F.softmax(self.fc2(x), dim=1)
return x
class CIFAR10Net(PrunableNet):
def __init__(self, *args, **kwargs):
super(CIFAR10Net, self).__init__(*args, **kwargs)
self.conv1 = layers.Conv2d(3, 6, 5)
self.conv2 = layers.Conv2d(6, 16, 5)
self.fc1 = layers.Linear(16 * 20 * 20, 120)
self.fc2 = layers.Linear(120, 84)
self.fc3 = layers.Linear(84, 10)
self.init_param_sizes()
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 3, stride=1))
x = F.relu(F.max_pool2d(self.conv2(x), 3, stride=1))
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.softmax(self.fc3(x), dim=1)
return x
class CIFAR100Net(PrunableNet):
def __init__(self, *args, **kwargs):
super(CIFAR100Net, self).__init__(*args, **kwargs)
self.conv1 = layers.Conv2d(3, 6, 5)
self.conv2 = layers.Conv2d(6, 16, 5)
self.fc1 = layers.Linear(16 * 20 * 20, 120)
self.fc2 = layers.Linear(120, 100)
self.init_param_sizes()
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 3, stride=1))
x = F.relu(F.max_pool2d(self.conv2(x), 3, stride=1))
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.softmax(self.fc2(x), dim=1)
return x
class EMNISTNet(PrunableNet):
def __init__(self, *args, **kwargs):
super(EMNISTNet, self).__init__(*args, **kwargs)
self.conv1 = layers.Conv2d(1, 10, 5) # "Conv 1-10"
self.conv2 = layers.Conv2d(10, 20, 5) # "Conv 10-20"
self.fc1 = layers.Linear(20 * 16 * 16, 512)
self.fc2 = layers.Linear(512, 62)
self.init_param_sizes()
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 3, stride=1))
x = F.relu(F.max_pool2d(self.conv2(x), 3, stride=1))
x = x.view(-1, self.num_flat_features(x)) # flatten
x = F.relu(self.fc1(x))
x = F.softmax(self.fc2(x), dim=1)
return x
class Conv2(PrunableNet):
'''The EMNIST model from LEAF:
https://github.com/TalwalkarLab/leaf/blob/master/models/femnist/cnn.py
'''
def __init__(self, *args, **kwargs):
super(Conv2, self).__init__(*args, **kwargs)
self.conv1 = layers.Conv2d(1, 32, 5, padding=2)
self.conv2 = layers.Conv2d(32, 64, 5, padding=2)
self.fc1 = layers.Linear(64 * 7 * 7, 2048)
self.fc2 = layers.Linear(2048, 62)
self.init_param_sizes()
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 2, stride=2))
x = F.relu(F.max_pool2d(self.conv2(x), 2, stride=2))
x = x.view(-1, self.num_flat_features(x))
x = F.relu(self.fc1(x))
x = F.softmax(self.fc2(x), dim=1)
return x
all_models = {
'mnist': MNISTNet,
'emnist': Conv2,
'cifar10': CIFAR10Net
}