-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
79 lines (65 loc) · 2.76 KB
/
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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
class MaskedConv2d(nn.Conv2d):
def __init__(self, in_channels, out_channels, kernel_size, stride=1, \
padding=0, dilation=1, groups=1, bias=False, use_gpu=False):
super(MaskedConv2d, self).__init__(in_channels, out_channels,
kernel_size, stride, padding,
dilation, groups, bias)
self.masked_channels = []
self.mask_flag = False
self.masks = None
self.use_gpu = use_gpu
def forward(self, x):
if self.mask_flag == True:
self._expand_masks(x.size())
weight = self.weight * self.masks
return F.conv2d(x, weight, self.bias, self.stride, self.padding,
self.dilation, self.groups)
else:
return F.conv2d(x, self.weight, self.bias, self.stride,
self.padding, self.dilation, self.groups)
def set_masked_channels(self, masked_channels):
self.masked_channels = masked_channels
if len(masked_channels) == 0:
self.mask_flag = False
else:
self.mask_flag = True
def get_masked_channels(self):
return self.masked_channels
def _expand_masks(self, input_size):
if len(self.masked_channels) == 0:
self.masks = None
masks = []
batch_size, _, height, width = [int(input_size[i].item()) for i in range(4)]
for mask_idx in range(len(self.masked_channels)):
channel = [b[i].item()] * width
channel = [channel] * height
masks.append(channel)
masks = [masks] * batch_size
masks = Tensor(masks)
if self.use_gpu:
masks = masks.cuda()
self.masks = Variable(masks, requires_grad=False, volatile=False)
class CustomNet(nn.Module):
def __init__(self, num_classes, use_gpu=False):
super(CustomNet, self).__init__()
self.conv1_1 = MaskedConv2d(3, 64, 3, padding=1, use_gpu=use_gpu)
self.conv2_1 = MaskedConv2d(64, 128, 3, padding=1, use_gpu=use_gpu)
self.conv3_1 = MaskedConv2d(128, 256, 3, padding=1, use_gpu=use_gpu)
self.fc1 = nn.Linear(4096, 4096)
self.fc2 = nn.Linear(4096, num_classes)
self.softmax = nn.Softmax(dim=1)
def forward(self, x):
out = F.relu(self.conv1_1(x))
out = F.max_pool2d(out, 2)
out = F.relu(self.conv2_1(out))
out = F.max_pool2d(out, 2)
out = F.relu(self.conv3_1(out))
out = F.max_pool2d(out, 2)
out = out.view(out.size(0), -1)
out = F.relu(self.fc1(out))
out = F.relu(self.fc2(out))
return self.softmax(out)