-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
75 lines (71 loc) · 2.87 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
import math
import torch.nn as nn
import torch
class my_model(nn.Module):
def __init__(self, out_channel, mode="full_conv"):
assert out_channel % 4 == 0, "output_channel can't be divided by 4"
assert mode in ["full_conv", "full_conn"], "mode not in [full_conv , full_conn]"
super().__init__()
self.convblock1 = nn.Sequential(
nn.Conv2d(1, int(out_channel / 2), kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(int(out_channel / 2)),
nn.ReLU(True),
nn.Conv2d(int(out_channel / 2), int(out_channel / 2), kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(int(out_channel / 2)),
nn.ReLU(True)
)
self.short1 = nn.Sequential(
nn.Conv2d(1, int(out_channel / 2), 1, bias=False),
nn.BatchNorm2d(int(out_channel / 2))
)
self.maxpool = nn.MaxPool2d((3, 3), padding=1, stride=2)
self.convblock2 = nn.Sequential(
nn.Conv2d(int(out_channel / 2), out_channel, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_channel),
nn.ReLU(True),
nn.Conv2d(out_channel, out_channel, kernel_size=3, padding=1, bias=False),
nn.BatchNorm2d(out_channel),
nn.ReLU(True)
)
self.short2 = nn.Sequential(
nn.Conv2d(int(out_channel / 2), out_channel, 1, bias=False),
nn.BatchNorm2d(out_channel)
)
if mode == "full_conv":
self.classifier = nn.Sequential(
nn.Conv2d(out_channel, int(out_channel / 2), kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(int(out_channel / 2)),
nn.ReLU(True),
nn.Conv2d(int(out_channel / 2), int(out_channel / 2), kernel_size=3, padding=1, stride=2, bias=False),
nn.BatchNorm2d(int(out_channel / 2)),
nn.ReLU(True),
nn.Conv2d(int(out_channel / 2), int(out_channel / 4), kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(int(out_channel / 4)),
nn.ReLU(True),
nn.Conv2d(int(out_channel / 4), int(out_channel / 4), kernel_size=3, padding=1, stride=2, bias=False),
nn.BatchNorm2d(int(out_channel / 4)),
nn.ReLU(True),
nn.Conv2d(int(out_channel / 4), 10, kernel_size=1, stride=1, bias=False),
nn.BatchNorm2d(10),
nn.ReLU(True),
nn.Conv2d(10, 10, kernel_size=3, padding=1, stride=2)
)
else:
self.classifier = nn.Sequential(nn.Linear(7 * 7 * out_channel, 7 * 7 * int(out_channel / 2)),
nn.ReLU(True),
nn.Linear(7 * 7 * int(out_channel / 2), 7 * 7 * int(out_channel / 2)),
nn.ReLU(True),
nn.Linear(7 * 7 * int(out_channel / 2), 10)
)
def get_lr(self, iteration, epochs, decay_rate=0.01):
init_lr = 1e-2
lr = init_lr * (decay_rate ** (iteration / (epochs - 1)))
return lr
def forward(self, x):
x = self.convblock1(x) + self.short1(x)
x = self.maxpool(x)
x = self.convblock2(x) + self.short2(x)
x = self.maxpool(x)
x = self.classifier(x)
x = torch.flatten(x, 1) # [n, 10]
return x