-
Notifications
You must be signed in to change notification settings - Fork 0
/
net5_resnet.py
85 lines (69 loc) · 2.32 KB
/
net5_resnet.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
import numpy as np
import torch
import torch.nn.functional as F
# 创建网络
# 最简单的网络
from torch import nn
class ResidualBlock(torch.nn.Module):
def __init__(self, inchannels, outchannels, use_1x1conv=False, stride=1):
super(ResidualBlock, self).__init__()
self.conv1 = torch.nn.Conv2d(inchannels, outchannels, kernel_size=3, padding=1, stride=stride)
self.conv2 = torch.nn.Conv2d(outchannels, outchannels, kernel_size=3, padding=1)
if use_1x1conv:
self.conv3 = nn.Conv2d(inchannels, outchannels, kernel_size=1, stride=stride)
else:
self.conv3 = None
self.b1 = torch.nn.BatchNorm2d(outchannels)
self.b2 = torch.nn.BatchNorm2d(outchannels)
def forward(self, x):
y = F.relu(self.b1(self.conv1(x)))
y = self.b2(self.conv2(y))
if self.conv3:
x = self.conv3(x)
return F.relu(x+y)
class net_8(torch.nn.Module):
def __init__(self):
super(net_8, self).__init__()
self.flaten = nn.Flatten()
self.layer0 = nn.Sequential(
nn.Conv2d(3, 32, kernel_size=5, padding=2),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
ResidualBlock(32, 32),
ResidualBlock(32, 32),
nn.MaxPool2d(2, 2)
)
self.layer1 = nn.Sequential(
nn.Conv2d(32, 32, kernel_size=5, padding=2),
nn.BatchNorm2d(32),
nn.ReLU(inplace=True),
ResidualBlock(32, 32),
ResidualBlock(32, 32),
nn.MaxPool2d(2, 2)
)
self.layer2 = nn.Sequential(
nn.Conv2d(32, 64, kernel_size=5, padding=2),
nn.BatchNorm2d(64),
nn.ReLU(inplace=True),
ResidualBlock(64, 64),
ResidualBlock(64, 64),
nn.MaxPool2d(2, 2)
)
self.fc = nn.Sequential(
nn.Linear(1024, 512),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(512, 64),
nn.ReLU(inplace=True),
nn.Dropout(0.5),
nn.Linear(64, 10),
nn.ReLU(inplace=True)
)
def forward(self, x):
x = self.layer0(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.flaten(x)
#print(len(x[0]))
x = self.fc(x)
return x