-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
176 lines (162 loc) · 5.86 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torchvision import transforms
def weight_initialization(layer):
if layer == nn.Conv2d or layer == nn.Linear:
torch.nn.init.kaiming_normal(layer.weight, nonlinearity = 'relu')
class Binary_Classifier_One(nn.Module):
def __init__(self):
super(Binary_Classifier_One,self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(1, 16, 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.BatchNorm2d(16))
self.conv2 = nn.Sequential(
nn.Conv2d(16, 32, 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.BatchNorm2d(32))
self.conv3 = nn.Sequential(
nn.Conv2d(32, 64, 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.BatchNorm2d(64))
self.conv4 = nn.Sequential(
nn.Conv2d(64, 128, 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.BatchNorm2d(128))
self.adap = nn.Sequential(
nn.AdaptiveAvgPool2d((1,1)),
nn.Flatten())
self.fc1 = nn.Linear(128, 32)
self.fc2 = nn.Linear(32, 1)
# self.fc3 = nn.Linear(32, 1)
self.conv1.apply(weight_initialization)
self.conv2.apply(weight_initialization)
self.conv3.apply(weight_initialization)
self.conv4.apply(weight_initialization)
self.fc1.apply(weight_initialization)
self.fc2.apply(weight_initialization)
# self.fc3.apply(weight_initialization)
def forward(self,x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.adap(x)
x = self.fc1(x)
x = self.fc2(x)
# x = self.fc3(x)
return torch.sigmoid(x)
class Binary_Classifier_Two(nn.Module):
def __init__(self):
super(Binary_Classifier_Two,self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(1, 16, 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.BatchNorm2d(16))
self.conv2 = nn.Sequential(
nn.Conv2d(16, 32, 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.BatchNorm2d(32))
self.conv3 = nn.Sequential(
nn.Conv2d(32, 64, 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.BatchNorm2d(64))
self.conv4 = nn.Sequential(
nn.Conv2d(64, 128, 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.BatchNorm2d(128))
self.conv5 = nn.Sequential(
nn.Conv2d(128, 256, 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.BatchNorm2d(256))
self.conv6 = nn.Sequential(
nn.Conv2d(256, 512, 3, 1, 1),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.BatchNorm2d(512))
self.adap = nn.Sequential(
nn.AdaptiveAvgPool2d((1,1)),
nn.Flatten())
self.fc1 = nn.Linear(512, 128)
self.fc2 = nn.Linear(128, 32)
self.fc3 = nn.Linear(32, 1)
self.conv1.apply(weight_initialization)
self.conv2.apply(weight_initialization)
self.conv3.apply(weight_initialization)
self.conv4.apply(weight_initialization)
self.conv5.apply(weight_initialization)
self.conv6.apply(weight_initialization)
self.fc1.apply(weight_initialization)
self.fc2.apply(weight_initialization)
self.fc3.apply(weight_initialization)
def forward(self,x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.conv5(x)
x = self.conv6(x)
x = self.adap(x)
x = self.fc1(x)
x = self.fc2(x)
x = self.fc3(x)
return torch.sigmoid(x)
#TODO: Three-Way_Classifier
class Three_Way_Classifier_One(nn.Module):
def __init__(self):
super(Three_Way_Classifier_One,self).__init__()
self.conv1 = nn.Sequential(
nn.Conv2d(1, 64, kernel_size=3, stride=1, padding=1),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.BatchNorm2d(64),
nn.Dropout(p=0.05))
self.conv2 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.BatchNorm2d(128),
nn.Dropout(p=0.05))
self.conv3 = nn.Sequential(
nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.BatchNorm2d(256),
nn.Dropout(p=0.05))
self.conv4 = nn.Sequential(
nn.Conv2d(256, 512, kernel_size=3, stride=1, padding=1),
nn.MaxPool2d(kernel_size=2, stride=2),
nn.ReLU(),
nn.BatchNorm2d(512),
nn.Dropout(p=0.05))
self.final = nn.Sequential(
nn.AdaptiveAvgPool2d((1,1)),
nn.Flatten())
self.fc1 = nn.Linear(512, 256)
self.fc2 = nn.Linear(256, 3)
self.conv1.apply(weight_initialization)
self.conv2.apply(weight_initialization)
self.conv3.apply(weight_initialization)
self.conv4.apply(weight_initialization)
self.fc1.apply(weight_initialization)
self.fc2.apply(weight_initialization)
def forward(self,x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
x = self.conv4(x)
x = self.final(x)
x = self.fc1(x)
x = self.fc2(x)
return F.log_softmax(x, dim=1)