This repository has been archived by the owner on Mar 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsd_alex.py
76 lines (65 loc) · 2.13 KB
/
dsd_alex.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
import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
class DSD_Dropout(nn.module):
"""
use phase_change to change the phase in the training
"""
(DENSE, SPARSE) = (0, 1)
phase = DENSE
def __init__(self, drop_ratio)
super(DSD_Dropout, self).__init__()
self.drop_ratio = drop_ratio
def forward(self, x):
if self.phase is DSD_Dropout.DENSE:
return x
else:
# calculate the k using x's size(note that x is unidimensional)
k = drop_ratio * x.size()[0]
# get the kth smallest element
kth_value = torch.abs(x).kthvalue(k)[0]
mask = nn.Threshold(kth_value, 0)
"""
filter those smaller than kth_value
and larger than -kth_value
"""
x = mask(x) + mask(-x)
return x
class AlexNet(nn.module):
"""
Extract the feature part as a module
rewrite the classifier part
"""
def __init__(self):
super(AlexNet, self).__init__()
self.features = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(64, 192, kernel_size=5, padding=2),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(192, 384, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(384, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.Conv2d(256, 256, kernel_size=3, padding=1),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2),
)
# substitute the dropout with DSD layer
self.classifier = nn.Sequential(
DSD_Dropout(),
nn.Linear(256 * 6 * 6, 4096),
nn.ReLU(inplace=True),
DSD_Dropout(),
nn.Linear(4096, 4096),
nn.ReLU(inplace=True),
nn.Linear(4096, num_classes),
)
def forward(self, x):
x = self.features(x)
x = x.view(x.size(0), 256 * 6 * 6)
x = self.classifier(x)
return x