forked from IndigoFloyd/SoybeanWebsite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlexNet_206.py
123 lines (92 loc) · 4.04 KB
/
AlexNet_206.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
import torch
from torch import nn
from torch.nn import Module
class CA_Block(nn.Module):
def __init__(self, channel, h, w, reduction=16):
super(CA_Block, self).__init__()
self.h = h
self.w = w
self.avg_pool_x = nn.AdaptiveAvgPool2d((h, 1))
self.avg_pool_y = nn.AdaptiveAvgPool2d((1, w))
self.conv_1x1 = nn.Conv2d(in_channels=channel, out_channels=channel//reduction, kernel_size=1, stride=1, bias=False)
self.relu = nn.ReLU()
self.bn = nn.BatchNorm2d(channel//reduction)
self.F_h = nn.Conv2d(in_channels=channel//reduction, out_channels=channel, kernel_size=1, stride=1, bias=False)
self.F_w = nn.Conv2d(in_channels=channel//reduction, out_channels=channel, kernel_size=1, stride=1, bias=False)
self.sigmoid_h = nn.Sigmoid()
self.sigmoid_w = nn.Sigmoid()
def forward(self, x):
x_h = self.avg_pool_x(x).permute(0, 1, 3, 2)
x_w = self.avg_pool_y(x)
x_cat_conv_relu = self.relu(self.conv_1x1(torch.cat((x_h, x_w), 3)))
x_cat_conv_split_h, x_cat_conv_split_w = x_cat_conv_relu.split([self.h, self.w], 3)
s_h = self.sigmoid_h(self.F_h(x_cat_conv_split_h.permute(0, 1, 3, 2)))
s_w = self.sigmoid_w(self.F_w(x_cat_conv_split_w))
out = x * s_h.expand_as(x) * s_w.expand_as(x)
return out
class AlexNet(nn.Module):
def __init__(self):
super().__init__()
self.net = nn.Sequential(
nn.Conv2d(3,32,kernel_size=3,padding=1,padding_mode='reflect',stride=1,bias=False),
nn.BatchNorm2d(32),
nn.Dropout(0.3),
nn.ReLU(),
CA_Block(32,206,206,reduction=16),
nn.Conv2d(32,64,kernel_size=4,padding=1,padding_mode='reflect',stride=2,bias=False),
nn.BatchNorm2d(64),
nn.Dropout(0.3),
nn.ReLU(),
nn.Conv2d(64,64,kernel_size=3,padding=1,padding_mode='reflect',stride=2,bias=False),
nn.BatchNorm2d(64),
nn.Dropout(0.3),
nn.ReLU(),
nn.Conv2d(64,64,kernel_size=3,padding=1,padding_mode='reflect',stride=1,bias=False),
nn.BatchNorm2d(64),
nn.Dropout(0.3),
nn.ReLU(),
nn.Conv2d(64,128,kernel_size=3,padding=1,padding_mode='reflect',stride=1,bias=False),
nn.BatchNorm2d(128),
nn.Dropout(0.3),
nn.ReLU(),
nn.Conv2d(128,128,kernel_size=3,padding=1,padding_mode='reflect',stride=1,bias=False),
nn.BatchNorm2d(128),
nn.Dropout(0.3),
nn.ReLU(),
nn.Conv2d(128,256,kernel_size=2,stride=2,bias=False),
nn.BatchNorm2d(256),
nn.Dropout(0.3),
nn.ReLU(),
nn.Conv2d(256,256,kernel_size=3,padding=1,padding_mode='reflect',stride=1,bias=False),
nn.BatchNorm2d(256),
nn.Dropout(0.3),
nn.ReLU(),
nn.Conv2d(256,512,kernel_size=2,stride=2,bias=False),
nn.BatchNorm2d(512),
nn.Dropout(0.3),
nn.ReLU(),
nn.Conv2d(512,512,kernel_size=3,padding=1,padding_mode='reflect',stride=1,bias=False),
nn.BatchNorm2d(512),
nn.Dropout(0.3),
nn.ReLU(),
nn.Conv2d(512,1024,kernel_size=3,padding=1,padding_mode='reflect',stride=2,bias=False),
nn.BatchNorm2d(1024),
nn.Dropout(0.3),
nn.ReLU(),
nn.Conv2d(1024,1024,kernel_size=3,padding=1,padding_mode='reflect',stride=1,bias=False),
nn.BatchNorm2d(1024),
nn.Dropout(0.3),
nn.ReLU(),
CA_Block(1024,7,7,reduction=16),
nn.Flatten(),
nn.Dropout(0.3),
nn.ReLU(),
# nn.Linear(50176,6400),
# nn.Dropout(0.4),
# nn.ReLU(),
nn.Linear(50176,1),
# nn.Sigmoid()
)
def modules(self):
model_name = 'AlexNet_deep'
return self.net,model_name