-
Notifications
You must be signed in to change notification settings - Fork 2
/
mc.py
50 lines (46 loc) · 1.87 KB
/
mc.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
import torch
from torch import nn
class MCNet(nn.Module):
def __init__(self, input_size=224, in_channels=3, n_classes=3):
super(MCNet, self).__init__()
fc_in_channels = (input_size // 32) * (input_size // 32) * 64
self.conv = nn.Sequential(
nn.Conv2d(in_channels, 64, kernel_size=3, stride=2, padding=1, bias=True),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=1, bias=True),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
nn.MaxPool2d(kernel_size=3, stride=2, padding=1),
nn.Conv2d(64, 64, kernel_size=3, stride=2, padding=1, bias=True),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True)
)
self.fc = nn.Sequential(
nn.Linear(fc_in_channels, 512),
nn.BatchNorm1d(512),
nn.ReLU(inplace=True),
nn.Dropout(p=0.5),
nn.Linear(512, n_classes),
# nn.Softmax(dim=1)
)
self._init_weight()
def forward(self, x):
x = self.conv(x)
x = torch.flatten(x, 1)
out = self.fc(x)
return out
def _init_weight(self):
for m in self.modules():
if isinstance(m, nn.Conv2d):
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.Linear):
nn.init.xavier_normal_(m.weight)
if m.bias is not None:
nn.init.constant_(m.bias, 0)
elif isinstance(m, nn.BatchNorm2d):
nn.init.constant_(m.weight, 1)
nn.init.constant_(m.bias, 0)