forked from binzh93/fault_diagnosis_cnn_pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_net.py
99 lines (78 loc) · 3.52 KB
/
my_net.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
93
94
95
import torch
import torch.nn.functional as F
class MyNet(torch.nn.Module):
def __init__(self, init_weights=True):
super(MyNet, self).__init__()
# in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True
self.conv1_1 = torch.nn.Conv2d(3, 64, 3, 1, 1)
self.relu1_1 = torch.nn.ReLU(inplace=True)
self.conv1_2 = torch.nn.Conv2d(64, 64, 3, 1, 1)
self.relu1_2 = torch.nn.ReLU(inplace=True)
self.conv2_1 = torch.nn.Conv2d(64, 128, 3, 1, 1)
self.relu2_1 = torch.nn.ReLU(inplace=True)
self.conv2_2 = torch.nn.Conv2d(128, 128, 3, 1, 1)
self.relu2_2 = torch.nn.ReLU(inplace=True)
self.conv3_1 = torch.nn.Conv2d(128, 256, 3, 1, 1)
self.relu3_1 = torch.nn.ReLU(inplace=True)
self.conv3_2 = torch.nn.Conv2d(256, 256, 3, 1, 1)
self.relu3_2 = torch.nn.ReLU(inplace=True)
self.conv3_3 = torch.nn.Conv2d(256, 256, 3, 1, 1)
self.relu3_3 = torch.nn.ReLU(inplace=True)
self.conv4_1 = torch.nn.Conv2d(256, 512, 3, 1, 1)
self.relu4_1 = torch.nn.ReLU(inplace=True)
self.conv4_2 = torch.nn.Conv2d(512, 512, 3, 1, 1)
self.relu4_2 = torch.nn.ReLU(inplace=True)
self.conv4_3 = torch.nn.Conv2d(512, 512, 3, 1, 1)
self.relu4_3 = torch.nn.ReLU(inplace=True)
self.conv5_1 = torch.nn.Conv2d(512, 512, 3, 1, 1)
self.relu5_1 = torch.nn.ReLU(inplace=True)
self.conv5_2 = torch.nn.Conv2d(512, 512, 3, 1, 1)
self.relu5_2 = torch.nn.ReLU(inplace=True)
self.conv5_3 = torch.nn.Conv2d(512, 512, 3, 1, 1)
self.relu5_3 = torch.nn.ReLU(inplace=True)
self.fc1 = torch.nn.Linear(512*7*7, 1000)
self.relu_fc1 = torch.nn.ReLU(inplace=True)
self.fc2 = torch.nn.Linear(1000, 100)
self.relu_fc2 = torch.nn.ReLU(inplace=True)
self.fc3 = torch.nn.Linear(100, 4)
if init_weights:
self._initialize_weights()
def forward(self, x):
x = self.relu1_1(self.conv1_1(x))
x = self.relu1_2(self.conv1_2(x))
x = F.max_pool2d(x, 2)
x = self.relu2_1(self.conv2_1(x))
x = self.relu2_2(self.conv2_2(x))
x = F.max_pool2d(x, 2)
x = self.relu3_1(self.conv3_1(x))
x = self.relu3_2(self.conv3_2(x))
x = self.relu3_3(self.conv3_3(x))
x = F.max_pool2d(x, 2)
x = self.relu4_1(self.conv4_1(x))
x = self.relu4_2(self.conv4_2(x))
x = self.relu4_3(self.conv4_3(x))
x = F.max_pool2d(x, 2)
x = self.relu5_1(self.conv5_1(x))
x = self.relu5_2(self.conv5_2(x))
x = self.relu5_3(self.conv5_3(x))
x = F.max_pool2d(x, 2)
x = x.view(x.size(0), -1)
x = self.relu_fc1(self.fc1(x))
x = torch.nn.Dropout()(x)
x = self.relu_fc2(self.fc2(x))
x = torch.nn.Dropout()(x)
x = self.fc3(x)
return x
def _initialize_weights(self):
for m in self.modules():
if isinstance(m, torch.nn.Conv2d):
torch.nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
if m.bias is not None:
torch.nn.init.constant_(m.bias, 0)
# elif isinstance(m, nn.BatchNorm2d):
# nn.init.constant_(m.weight, 1)
# nn.init.constant_(m.bias, 0)
elif isinstance(m, torch.nn.Linear):
torch.nn.init.normal_(m.weight, 0, 0.01)
torch.nn.init.constant_(m.bias, 0)
# MyNet()