-
Notifications
You must be signed in to change notification settings - Fork 0
/
lenet.py
51 lines (41 loc) · 1.48 KB
/
lenet.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
import torch
import torch.nn as nn
class LeNet(nn.Module):
def __init__(self, out_dim=10, in_channel=1, img_sz = 28):
super(LeNet, self).__init__()
feat_map_sz = img_sz//4
self.n_feat = 50 * feat_map_sz * feat_map_sz
# !!! [Architecture design tip] !!!
# The KCL has much better convergence of optimization when the BN layers are added.
# MCL is robust even without BN layer.
self.conv = nn.Sequential(
nn.Conv2d(in_channel, 20, 5, padding=2),
nn.BatchNorm2d(20),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2),
nn.Conv2d(20, 50, 5, padding=2),
nn.BatchNorm2d(50),
nn.ReLU(inplace=True),
nn.MaxPool2d(2, 2)
)
self.linear = nn.Sequential(
nn.Linear(self.n_feat, 500),
nn.BatchNorm1d(500),
nn.ReLU(inplace=True),
)
self.last = nn.Linear(500, out_dim) # Subject to be replaced dependent on task
def features(self, x):
x = self.conv(x)
x = self.linear(x.view(-1, self.n_feat))
return x
def logits(self, x):
x = self.last(x)
return x
def forward(self, x):
x = self.features(x)
x = self.logits(x)
return x
def LeNet32(out_dim): # LeNet with color input
return LeNet(out_dim=out_dim, in_channel=1, img_sz=32)
def LeNetC(out_dim): # LeNet with color input
return LeNet(out_dim=out_dim, in_channel=3, img_sz=32)