-
Notifications
You must be signed in to change notification settings - Fork 0
/
Models.py
172 lines (146 loc) · 5.95 KB
/
Models.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
import torch
import torch.nn as nn
import torch.nn.functional as F
'''
MNIST简单全连接模型
'''
class Mnist_2NN(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(784, 200)
self.fc2 = nn.Linear(200, 200)
self.fc3 = nn.Linear(200, 10)
def forward(self, inputs):
tensor = F.relu(self.fc1(inputs))
tensor = F.relu(self.fc2(tensor))
tensor = self.fc3(tensor)
return tensor
'''
Cifar简单全连接模型
'''
class Cifar_2NN(nn.Module):
def __init__(self):
super().__init__()
self.fc1 = nn.Linear(3072, 200)
self.fc2 = nn.Linear(200, 200)
self.fc3 = nn.Linear(200, 10)
def forward(self, inputs):
tensor = F.relu(self.fc1(inputs))
tensor = F.relu(self.fc2(tensor))
tensor = self.fc3(tensor)
return tensor
'''
MNIST简单卷积神经网络模型
'''
class Mnist_CNN(nn.Module):
def __init__(self):
super().__init__()
# 定义每一层模型
self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=3, stride=1, padding=0)
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=0)
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
self.conv3 = nn.Conv2d(in_channels=64, out_channels=64, kernel_size=3, stride=1, padding=0)
self.fc1 = nn.Linear(3*3*64, 64)
self.fc2 = nn.Linear(64, 10)
def forward(self, inputs):
# 构造模型
tensor = inputs.view(-1, 1, 28, 28)
tensor = F.relu(self.conv1(tensor))
tensor = self.pool1(tensor)
tensor = F.relu(self.conv2(tensor))
tensor = self.pool2(tensor)
tensor = F.relu(self.conv3(tensor))
tensor = tensor.view(-1, 3*3*64)
tensor = F.relu(self.fc1(tensor))
tensor = self.fc2(tensor)
return tensor
'''
cifar简单卷积神经网络模型
'''
class Cifar_CNN(nn.Module):
def __init__(self):
super().__init__()
# 定义每一层模型
self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1)
self.pool1 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
self.pool2 = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1)
self.fc1 = nn.Linear(8*8*128, 128)
self.fc2 = nn.Linear(128, 10)
def forward(self, inputs):
# 构造模型
tensor = inputs.view(-1, 3, 32, 32)
tensor = F.relu(self.conv1(tensor))
tensor = self.pool1(tensor)
tensor = F.relu(self.conv2(tensor))
tensor = self.pool2(tensor)
tensor = F.relu(self.conv3(tensor))
# print(tensor.shape)
# raise(1)
tensor = tensor.view(-1, 8*8*128)
tensor = F.relu(self.fc1(tensor))
tensor = self.fc2(tensor)
return tensor
'''
resnet卷积神经网络模型
'''
class RestNetBasicBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride):
super(RestNetBasicBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1)
self.bn1 = nn.BatchNorm2d(out_channels)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=stride, padding=1)
self.bn2 = nn.BatchNorm2d(out_channels)
def forward(self, inputs):
tensor = self.conv1(inputs)
tensor = F.relu(self.bn1(tensor))
tensor = self.conv2(tensor)
tensor = self.bn2(tensor)
return F.relu(inputs + tensor)
class RestNetDownBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride):
super(RestNetDownBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride[0], padding=1)
self.bn1 = nn.BatchNorm2d(out_channels)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=stride[1], padding=1)
self.bn2 = nn.BatchNorm2d(out_channels)
self.extra = nn.Sequential(
nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=stride[0], padding=0),
nn.BatchNorm2d(out_channels)
)
def forward(self, input):
extra_x = self.extra(input)
tensor = self.conv1(input)
tensor = F.relu(self.bn1(tensor))
tensor = self.conv2(tensor)
tensor = self.bn2(tensor)
return F.relu(extra_x + tensor)
class RestNet18(nn.Module):
def __init__(self):
super(RestNet18, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3)
self.bn1 = nn.BatchNorm2d(64)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = nn.Sequential(RestNetBasicBlock(64, 64, 1),
RestNetBasicBlock(64, 64, 1))
self.layer2 = nn.Sequential(RestNetDownBlock(64, 128, [2, 1]),
RestNetBasicBlock(128, 128, 1))
self.layer3 = nn.Sequential(RestNetDownBlock(128, 256, [2, 1]),
RestNetBasicBlock(256, 256, 1))
self.layer4 = nn.Sequential(RestNetDownBlock(256, 512, [2, 1]),
RestNetBasicBlock(512, 512, 1))
self.avgpool = nn.AdaptiveAvgPool2d(output_size=(1, 1))
self.fc = nn.Linear(512, 10)
def forward(self, inputs):
tensor = inputs.view(-1, 3, 32, 32)
tensor = self.conv1(tensor)
tensor = self.layer1(tensor)
tensor = self.layer2(tensor)
tensor = self.layer3(tensor)
tensor = self.layer4(tensor)
tensor = self.avgpool(tensor)
tensor = tensor.reshape(tensor.shape[0], -1)
tensor = self.fc(tensor)
return tensor