-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_vgg.py
137 lines (101 loc) · 4.24 KB
/
my_vgg.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
import torch
import torch.nn as nn
import math
import torch.nn.functional as F
from mask_module import Mask_Linear, Mask_Conv2d, Mask_BatchNorm2d, Mask_BatchNorm1d, Mask_ReLU, Mask_MaxPool2d, Mask_Sequential, Mask_Dropout
from collections import OrderedDict, namedtuple
class ConvBNReLU(nn.Module):
def __init__(self, nInputPlane, nOutputPlane):
super(ConvBNReLU, self).__init__()
self.conv = Mask_Conv2d(nInputPlane, nOutputPlane, kernel_size=3, stride=1, padding=1)
self.bn = Mask_BatchNorm2d(nOutputPlane, eps=1e-3)
self.relu = Mask_ReLU(inplace=True)
self._masks = OrderedDict()
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
x = self.relu(x)
return x
def named_masks(self, prefix='', recurse=True):
gen = self._named_members(
lambda module: module._masks.items(),
prefix=prefix, recurse=recurse)
for elem in gen:
yield elem
def to(self, *args, **kwargs):
super(vgg_cifar_sparse, self).to(*args, **kwargs)
device, dtype, non_blocking = torch._C._nn._parse_to(*args, **kwargs)
for name, para in self.named_masks():
para.data = para.to(device)
def update_mask(self, user_mask):
for name, para in self.named_masks():
para.data = user_mask[name]
def get_mask(self):
total_mask = {}
for name, para in self.named_masks():
total_mask[name] = para.clone()
return total_mask
class vgg_cifar_sparse(nn.Module):
def __init__(self):
super(vgg_cifar_sparse, self).__init__()
layers = []
layers.append(ConvBNReLU(3,64))
layers.append(Mask_Dropout(p = 0.3))
layers.append(ConvBNReLU(64,64))
layers.append(Mask_MaxPool2d(kernel_size=2, stride=2, ceil_mode=True))
layers.append(ConvBNReLU(64,128))
layers.append(Mask_Dropout(p = 0.4))
layers.append(ConvBNReLU(128,128))
layers.append(Mask_MaxPool2d(kernel_size=2, stride=2, ceil_mode=True))
layers.append(ConvBNReLU(128, 256))
layers.append(Mask_Dropout(p=0.4))
layers.append(ConvBNReLU(256, 256))
layers.append(Mask_Dropout(p=0.4))
layers.append(ConvBNReLU(256, 256))
layers.append(Mask_MaxPool2d(kernel_size=2, stride=2, ceil_mode=True))
layers.append(ConvBNReLU(256, 512))
layers.append(Mask_Dropout(p=0.4))
layers.append(ConvBNReLU(512, 512))
layers.append(Mask_Dropout(p=0.4))
layers.append(ConvBNReLU(512, 512))
layers.append(Mask_MaxPool2d(kernel_size=2, stride=2, ceil_mode=True))
layers.append(ConvBNReLU(512, 512))
layers.append(Mask_Dropout(p=0.4))
layers.append(ConvBNReLU(512, 512))
layers.append(Mask_Dropout(p=0.4))
layers.append(ConvBNReLU(512, 512))
layers.append(Mask_MaxPool2d(kernel_size=2, stride=2, ceil_mode=True))
self.conv_block = Mask_Sequential(*layers)
fc_layers = []
fc_layers.append(Mask_Dropout(p=0.4))
fc_layers.append(Mask_Linear(512, 512))
fc_layers.append(Mask_BatchNorm1d(512))
fc_layers.append(Mask_ReLU(inplace=True))
fc_layers.append(Mask_Dropout(p=0.5))
fc_layers.append(Mask_Linear(512, 10))
self.fc_block = Mask_Sequential(*fc_layers)
self._masks = OrderedDict()
def forward(self, x):
x = self.conv_block(x)
x = x.view(x.size(0), -1)
x = self.fc_block(x)
return x
def named_masks(self, prefix='', recurse=True):
gen = self._named_members(
lambda module: module._masks.items(),
prefix=prefix, recurse=recurse)
for elem in gen:
yield elem
def to(self, *args, **kwargs):
super(vgg_cifar_sparse, self).to(*args, **kwargs)
device, dtype, non_blocking = torch._C._nn._parse_to(*args, **kwargs)
for name, para in self.named_masks():
para.data = para.to(device)
def update_mask(self, user_mask):
for name, para in self.named_masks():
para.data = user_mask[name]
def get_mask(self):
total_mask = {}
for name, para in self.named_masks():
total_mask[name] = para.clone()
return total_mask