-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrain_resnet.py
273 lines (256 loc) · 8.91 KB
/
train_resnet.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
import argparse
import torch
from Config import hyperparameters
from DataProcessing.net_feature_extractor import net_feature_loader
from HumBugDB.runTorch import ResnetDropoutFull as ResnetDropoutBinary
from HumBugDB.runTorch import ResnetFull as ResnetBinary
from HumBugDB.runTorch import train_model as train_model_binary
from HumBugDB.runTorchMultiClass import ResnetDropoutFull as ResnetDropoutMulti
from HumBugDB.runTorchMultiClass import ResnetFull as ResnetMulti
from HumBugDB.runTorchMultiClass import train_model as train_model_multi
def create_model(model_name, num_classes, bayesian):
if model_name == "resnet50":
print("Running resnet without dropout")
if num_classes == 2:
model = ResnetBinary()
training = train_model_binary
else:
model = ResnetMulti(num_classes)
training = train_model_multi
elif model_name == "resnet50dropout":
print(f"Creating dropout model with bayesian: {bayesian}")
if num_classes == 2:
model = ResnetDropoutBinary(dropout=hyperparameters.dropout, bayesian=bayesian)
training = train_model_binary
else:
model = ResnetDropoutMulti(n_classes=num_classes, bayesian=bayesian)
training = train_model_multi
else:
raise NotImplementedError("Only implemented resnet50 and resnet50dropout")
return model, training
def run_model_training(
recalc_features,
train_data_directory,
vali_data_directory,
spectrogram_directory,
model_name,
model_label,
model_dir,
classes_name,
bayesian,
weights,
):
device = torch.device("cuda" if torch.cuda.is_available() else "mps" if torch.backends.mps.is_available() else "cpu")
print("Using device:", device)
(
spectrograms_train,
murmurs_train,
outcomes_train,
spectrograms_test,
murmurs_test,
outcomes_test,
) = net_feature_loader(
recalc_features,
train_data_directory,
vali_data_directory,
spectrogram_directory,
)
print("Data loaded")
X_train = spectrograms_train.to(device)
X_test = spectrograms_test.to(device)
if classes_name == "murmur":
y_train = murmurs_train.to(device)
y_test = murmurs_test.to(device)
model, training = create_model(model_name, 3, bayesian)
training(
X_train,
y_train,
clas_weight=weights,
x_val=X_test,
y_val=y_test,
model=model,
model_name=model_label,
model_dir=model_dir,
)
elif classes_name == "outcome_binary":
y_train = outcomes_train.to(device)
y_test = outcomes_test.to(device)
model, training = create_model(model_name, 2, bayesian)
training(
X_train,
y_train,
clas_weight=weights,
x_val=X_test,
y_val=y_test,
model=model,
model_name=model_label,
model_dir=model_dir,
)
elif classes_name == "murmur_binary":
knowledge_train = torch.zeros((murmurs_train.shape[0], 2))
for i in range(len(murmurs_train)):
if (
torch.argmax(murmurs_train[i]) == 0
or torch.argmax(murmurs_train[i]) == 1
):
knowledge_train[i, 0] = 1
else:
knowledge_train[i, 1] = 1
knowledge_test = torch.zeros((murmurs_test.shape[0], 2))
for i in range(len(murmurs_test)):
if torch.argmax(murmurs_test[i]) == 0 or torch.argmax(murmurs_test[i]) == 1:
knowledge_test[i, 0] = 1
else:
knowledge_test[i, 1] = 1
y_train = knowledge_train.to(device)
y_test = knowledge_test.to(device)
model, training = create_model(model_name, 2, bayesian)
training(
X_train,
y_train,
clas_weight=weights,
x_val=X_test,
y_val=y_test,
model=model,
model_name=model_label,
model_dir=model_dir,
)
elif classes_name == "binary_present":
knowledge_train = torch.zeros((murmurs_train.shape[0], 2))
for i in range(len(murmurs_train)):
if (
torch.argmax(murmurs_train[i]) == 1
or torch.argmax(murmurs_train[i]) == 2
):
knowledge_train[i, 1] = 1
else:
knowledge_train[i, 0] = 1
knowledge_test = torch.zeros((murmurs_test.shape[0], 2))
for i in range(len(murmurs_test)):
if torch.argmax(murmurs_test[i]) == 1 or torch.argmax(murmurs_test[i]) == 2:
knowledge_test[i, 1] = 1
else:
knowledge_test[i, 0] = 1
y_train = knowledge_train.to(device)
y_test = knowledge_test.to(device)
model, training = create_model(model_name, 2, bayesian)
training(
X_train,
y_train,
clas_weight=weights,
x_val=X_test,
y_val=y_test,
model=model,
model_name=model_label,
model_dir=model_dir,
)
elif classes_name == "binary_unknown":
knowledge_train = torch.zeros((murmurs_train.shape[0], 2))
for i in range(len(murmurs_train)):
if (
torch.argmax(murmurs_train[i]) == 0
or torch.argmax(murmurs_train[i]) == 2
):
knowledge_train[i, 1] = 1
else:
knowledge_train[i, 0] = 1
knowledge_test = torch.zeros((murmurs_test.shape[0], 2))
for i in range(len(murmurs_test)):
if torch.argmax(murmurs_test[i]) == 0 or torch.argmax(murmurs_test[i]) == 2:
knowledge_test[i, 1] = 1
else:
knowledge_test[i, 0] = 1
y_train = knowledge_train.to(device)
y_test = knowledge_test.to(device)
model, training = create_model(model_name, 2, bayesian)
training(
X_train,
y_train,
clas_weight=weights,
x_val=X_test,
y_val=y_test,
model=model,
model_name=model_label,
model_dir=model_dir,
sampler=True,
)
else:
raise ValueError("classes_name must be one of outcome, murmur or knowledge.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(prog="TrainResNet")
parser.add_argument(
"--recalc_features",
action="store_true",
help="Whether or not to recalculate the log mel spectrograms used as "
"input to the ResNet.",
)
parser.add_argument(
"--no-recalc_features", dest="recalc_features", action="store_false"
)
parser.set_defaults(recalc_features=True)
parser.add_argument(
"--train_data_directory",
type=str,
help="The directory of the training data.",
default="data/stratified_data/train_data",
)
parser.add_argument(
"--vali_data_directory",
type=str,
help="The directory of the validation data.",
default="data/stratified_data/vali_data",
)
parser.add_argument(
"--spectrogram_directory",
type=str,
help="The directory in which to save the spectrogram training data.",
default="data/spectrograms",
)
parser.add_argument(
"--model_name",
type=str,
help="The ResNet to train. Current options are resnet50 or resnet50dropout.",
choices=["resnet50", "resnet50dropout"],
default="resnet50dropout",
)
parser.add_argument(
"--model_label",
type=str,
help="The label to use when saving the model.",
default="ResNetDropout",
)
parser.add_argument(
"--model_dir",
type=str,
help="The directory to use when saving the model.",
default="data/models",
)
parser.add_argument(
"--classes_name",
type=str,
help="The name of the classes to train the model on.",
choices=["murmur", "outcome_binary", "murmur_binary", "binary_present", "binary_unknown"],
default="murmur",
)
parser.add_argument(
'--disable-bayesian',
dest='bayesian',
action='store_false',
default=True,
help='Disable Bayesian features (default: Bayesian is enabled)'
)
parser.add_argument(
"--weights_str",
type=str,
help="String containing the class weights for a weighted loss function, "
"e.g.5,3,1.",
default=None,
)
args = parser.parse_args()
weights = None
if args.weights_str:
weights = [int(x) for x in args.weights_str.split(",")]
vars(args).popitem()
print("---------------- Starting train_resnet.py for training ----------------")
print(f"---------------- Using data from {args.train_data_directory}")
run_model_training(**vars(args), weights=weights)