-
Notifications
You must be signed in to change notification settings - Fork 8
/
model_constructor.py
183 lines (164 loc) · 4.29 KB
/
model_constructor.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import torch
import wandb
import models
import ckconv
import models.resnet
# typing
from omegaconf import OmegaConf
SEQ_DATASETS = [
"AddProblem",
"CopyMemory",
"SpeechCommands",
"CharTrajectories",
"sMNIST",
"sCIFAR10",
]
IMG_DATASETS = [
"MNIST",
"CIFAR10",
"CIFAR100",
"STL10",
"Imagenet",
"Imagenet64",
"Imagenet32",
"Imagenet16",
"Imagenet8",
"Cityscapes",
"VOC",
]
def construct_model(
cfg: OmegaConf,
):
"""
:param device: instance of torch.device
:return: An instance of torch.nn.Module
"""
# Define in_channels
if cfg.dataset in [
"AddProblem",
"CopyMemory",
"sMNIST",
"MNIST",
]:
in_channels = 1
elif cfg.dataset in ["AddProblem"]:
in_channels = 2
elif cfg.dataset in [
"CharTrajectories",
"sCIFAR10",
"CIFAR10",
"CIFAR100",
"STL10",
"Imagenet",
"Imagenet64",
"Imagenet32",
"Imagenet16",
"Imagenet8",
"Cityscapes",
"VOC",
]:
in_channels = 3
if cfg.dataset == "sCIFAR10" and cfg.dataset_params.noise_padded:
in_channels = 96
elif cfg.dataset in ["SpeechCommands"]:
if cfg.dataset_params.mfcc:
in_channels = 20
else:
in_channels = 1
else:
raise NotImplementedError(f"Not in_channels for dataset {cfg.dataset} found.")
# Consider the exist_mask channel for irregularly sampled cases.
if cfg.dataset_params.drop_rate != 0 and cfg.dataset in [
"CharTrajectories",
"SpeechCommands",
]:
in_channels = in_channels + 1
# Define output_channels
if cfg.dataset in ["AddProblem"]:
out_channels = 1
elif cfg.dataset in [
"CopyMemory",
"SpeechCommands",
"CharTrajectories",
"sMNIST",
"sCIFAR10",
"MNIST",
"CIFAR10",
"STL10",
]:
out_channels = 10
elif cfg.dataset in ["CIFAR100"]:
out_channels = 100
elif cfg.dataset in ["CharTrajectories"]:
out_channels = 20
elif cfg.dataset in [
"Imagenet",
"Imagenet64",
"Imagenet32",
"Imagenet16",
"Imagenet8",
]:
out_channels = 1000
else:
raise NotImplementedError(f"Not in_channels for dataset {cfg.dataset} found.")
# TODO(rjbruin): add segmentation
# Define model type names
if cfg.dataset in ["CopyMemory"]:
model_type = "CopyMemory"
elif cfg.dataset in [
"AddProblem",
"SpeechCommands",
"CharTrajectories",
"sMNIST",
"sCIFAR10",
]:
model_type = "SeqData"
elif cfg.dataset in [
"MNIST",
"CIFAR10",
"CIFAR100",
"STL10",
"Imagenet",
"Imagenet64",
"Imagenet32",
"Imagenet16",
"Imagenet8",
]:
model_type = "Img"
elif cfg.dataset in [
"Cityscapes",
"VOC",
]:
model_type = "Segmentation"
else:
raise NotImplementedError(f"Not in_channels for dataset {cfg.dataset} found.")
model_name = "%s_%s" % (model_type, cfg.net.type)
# Define dim_linear: dimensionality of the data, i.e., 1 for temporal data,
# 2 for images
if cfg.dataset in SEQ_DATASETS:
dim_linear = 1
elif cfg.dataset in IMG_DATASETS:
dim_linear = 2
else:
raise NotImplementedError(f"Not dim_linear for dataset {cfg.dataset} found.")
# Append dim_linear to the kernel_config
cfg.kernel.dim_linear = dim_linear
# Print the defined parameters.
print(
f"Automatic Parameters:\n dataset = {cfg.dataset}, model_name = {model_name}, in_channels = {in_channels}, out_chanels = {out_channels}, dim_linear = {dim_linear}"
)
model_name = getattr(models, model_name)
model = model_name(
in_channels=in_channels,
out_channels=out_channels,
net_config=cfg.net,
kernel_config=cfg.kernel,
conv_config=cfg.conv,
mask_config=cfg.mask,
)
# print number parameters
no_params = ckconv.utils.num_params(model)
print("Number of parameters:", no_params)
wandb.run.summary["no_params"] = no_params
model = torch.nn.DataParallel(model) # Required for multi-GPU
return model