-
Notifications
You must be signed in to change notification settings - Fork 1
/
model.py
92 lines (82 loc) · 3.19 KB
/
model.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
import torch
import torch.nn as nn
from torch.autograd import Function,Variable
import torch.nn.functional as F
class BinActiv(Function):
'''
Binarize the input activations and calculate the mean across channel dimension
'''
@staticmethod
def forward(ctx,input):
ctx.save_for_backward(input)
input = input.sign()
return input #tensor.Forward should has only one output, or there will be another grad
@classmethod
def Mean(cls,input):
return torch.mean(input.abs(),1,keepdim=True) #the shape of mnist data is (N,C,W,H)
@staticmethod
def backward(ctx,grad_output): #grad_output is a Variable
input,=ctx.saved_tensors
grad_input = grad_output.clone()
grad_input[input.ge(1)] = 0
grad_input[input.le(-1)] = 0
return grad_input #Variable
BinActive = BinActiv.apply
class BinConv2d(nn.Module):
def __init__(self,in_channels,out_channels,kernel_size,stride=1,padding=0,dilation=1,groups=1,bias=False):
super(BinConv2d,self).__init__()
self.in_channels = in_channels
self.out_channels = out_channels
self.kernel_size = kernel_size
self.stride = stride
self.padding = padding
self.dilation = dilation
self.groups = groups
self.bias = bias
self.layer_type = 'BinConv2d'
self.bn = nn.BatchNorm2d(in_channels,eps=1e-4,momentum=0.1,affine=True)
self.conv = nn.Conv2d(in_channels,out_channels,kernel_size=kernel_size,stride=stride,padding=padding,dilation=dilation,
groups=groups,bias=bias)
self.relu = nn.ReLU()
def forward(self,x):
#block structure is BatchNorm -> BinActiv -> BinConv -> Relu
x = self.bn(x)
A = BinActiv().Mean(x)
x = BinActive(x)
k = torch.ones(1,1,self.kernel_size,self.kernel_size).mul(1/(self.kernel_size**2)) #out_channels and in_channels are both 1.constrain kernel as square
k = Variable(k.cuda())
K = F.conv2d(A,k,bias=None,stride=self.stride,padding=self.padding,dilation=self.dilation)
x = self.conv(x)
x = torch.mul(x,K)
x = self.relu(x)
return x
class BinLinear(nn.Module):
def __init__(self,in_features,out_features):
super(BinLinear,self).__init__()
self.in_features = in_features
self.out_features = out_features
self.bn = nn.BatchNorm1d(in_features,eps=1e-4,momentum=0.1,affine=True)
self.linear = nn.Linear(in_features,out_features,bias=False)
def forward(self,x):
x = self.bn(x)
beta = BinActiv().Mean(x).expand_as(x)
x = BinActive(x)
x = torch.mul(x,beta)
x = self.linear(x)
return x
class LeNet5_Bin(nn.Module):
def __init__(self):
super(LeNet5_Bin,self).__init__()
self.conv1 = BinConv2d(1,6,kernel_size = 5)
self.conv2 = BinConv2d(6,16,kernel_size = 3)
self.fc1 = BinLinear(400,50)
self.fc2 = BinLinear(50,10)
def forward(self,x):
x = self.conv1(x)
x = F.max_pool2d(x,2)
x = self.conv2(x)
x = F.max_pool2d(x,2)
x = x.view(-1,400)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return x