-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.py
More file actions
128 lines (109 loc) · 2.91 KB
/
basic.py
File metadata and controls
128 lines (109 loc) · 2.91 KB
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
import torch
import torch.nn as nn
class BasicConv2d(nn.Module):
"""
单独的卷积模块"conv-ReLU"
"""
def __init__(
self,
input_channels : int,
output_channels : int,
kernel_size : int,
stride : int = 1,
padding : int = 0
):
super().__init__()
self.conv2d = nn.Conv2d(
in_channels=input_channels,
out_channels=output_channels,
kernel_size=kernel_size,
stride=stride,
padding=padding
)
self.relu = nn.ReLU()
self.model = nn.Sequential(
self.conv2d,
self.relu
)
def forward(self, input):
return self.model(input)
class BasicConvMaxPool(nn.Module):
"""
单独的卷积池化模块"conv-ReLU-maxpool"
"""
def __init__(
self,
inch : int,
outch : int,
convkernel : int,
convstride : int = 1,
convpadding : int = 0,
poolkernel : int = 2,
poolpadding : int = 0,
poolstride : int = 2
):
super().__init__()
self.conv2d = nn.Conv2d(
in_channels=inch,
out_channels=outch,
kernel_size=convkernel,
stride=convstride,
padding=convpadding
)
self.relu = nn.ReLU()
self.maxpool = nn.MaxPool2d(
kernel_size=poolkernel,
stride=poolstride,
padding=poolpadding
)
self.model = nn.Sequential(
self.conv2d,
self.relu,
self.maxpool
)
def forward(self, input):
return self.model(input)
class BasicConvAvgPool(nn.Module):
"""
单独的卷积池化模块"conv-ReLU-avgpool"
"""
def __init__(
self,
inch : int,
outch : int,
convkernel : int,
convstride : int = 1,
convpadding : int = 0,
poolkernel : int = 2,
poolpadding : int = 0,
poolstride : int = 2
):
super().__init__()
self.conv2d = nn.Conv2d(
in_channels=inch,
out_channels=outch,
kernel_size=convkernel,
stride=convstride,
padding=convpadding
)
self.relu = nn.ReLU()
self.maxpool = nn.AvgPool2d(
kernel_size=poolkernel,
stride=poolstride,
padding=poolpadding
)
self.model = nn.Sequential(
self.conv2d,
self.relu,
self.maxpool
)
def forward(self, input):
return self.model(input)
if __name__ == '__main__':
"""调试用"""
device = torch.device('cuda')
data = torch.randn(3,5,5,device=device)
net = BasicConvAvgPool(3,4,3)
net = net.to(device)
output = net(data)
print(output)