-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathmodels.py
75 lines (63 loc) · 1.97 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
# SPDX-FileCopyrightText: Copyright (c) 2022 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: MIT
import torch
import torch.nn as nn
__all__ = [
'ModelGN',
'ModelBN',
'MODELS'
]
class ModelGN(nn.Module):
def __init__(self):
super().__init__()
self.cnn = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1),
nn.GroupNorm(8, 64),
nn.ReLU(),
nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1),
nn.GroupNorm(8, 128),
nn.ReLU(),
nn.Conv2d(128, 256, kernel_size=3, stride=2, padding=1),
nn.GroupNorm(8, 256),
nn.ReLU(),
nn.Conv2d(256, 512, kernel_size=3, stride=2, padding=1),
nn.GroupNorm(8, 512),
nn.ReLU()
)
self.pool = nn.AdaptiveAvgPool2d((1, 1))
self.linear = nn.Linear(512, 10)
def forward(self, x):
x = self.cnn(x)
x = self.pool(x)
x = x.view(x.shape[0], -1)
x = self.linear(x)
return x
class ModelBN(nn.Module):
def __init__(self):
super().__init__()
self.cnn = nn.Sequential(
nn.Conv2d(3, 64, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128, 256, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(256),
nn.ReLU(),
nn.Conv2d(256, 512, kernel_size=3, stride=2, padding=1),
nn.BatchNorm2d(512),
nn.ReLU()
)
self.pool = nn.AdaptiveAvgPool2d((1, 1))
self.linear = nn.Linear(512, 10)
def forward(self, x):
x = self.cnn(x)
x = self.pool(x)
x = x.view(x.shape[0], -1)
x = self.linear(x)
return x
MODELS = {
'model_gn': ModelGN,
'model_bn': ModelBN
}