This repository has been archived by the owner on Jul 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
learn.py
186 lines (157 loc) · 3.82 KB
/
learn.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
#!/usr/bin/env python3
"""
Implementation of the main procedure of supervised learning. It trains and
evaluates a model.
"""
###########
# Imports #
###########
import os
from datetime import datetime
from learning.evaluation import evaluate
from learning.training import train
########
# Main #
########
def main(
outputs_pth: str = 'outputs/',
criterion_id: str = 'mse',
dataset_id: str = 'class',
train_pth: str = 'train.json',
model_id: str = 'densenet161',
augment: bool = False,
edges: bool = False,
batch_size: int = 16,
out_channels: int = 2,
num_epochs: int = 30,
test_pth: str = 'test.json',
metric_id: str = 'pr'
):
# Create output folder
now = datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
folder_pth = os.path.join(outputs_pth, now)
folder_pth += '/'
# Get weights path
weights_pth = os.path.join(folder_pth, f'{model_id}.pth')
# Train
train(
outputs_pth=folder_pth,
criterion_id=criterion_id,
dataset_id=dataset_id,
train_pth=train_pth,
model_id=model_id,
augment=augment,
edges=edges,
batch_size=batch_size,
out_channels=out_channels,
num_epochs=num_epochs,
weights_pth=weights_pth
)
# Evaluate
evaluate(
outputs_pth=folder_pth,
dataset_id=dataset_id,
test_pth=test_pth,
model_id=model_id,
edges=edges,
batch_size=batch_size,
out_channels=out_channels,
weights_pth=weights_pth,
metric_id=metric_id
)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
description='Train and evaluate a deep learning model.'
)
parser.add_argument(
'-outputs',
type=str,
default='outputs/',
help='path to outputs folder'
)
parser.add_argument(
'-criterion',
type=str,
default='mse',
choices=['mse', 'nll'],
help='criterion to use'
)
parser.add_argument(
'-dataset',
type=str,
default='class',
choices=['class', 'image'],
help='data set to use'
)
parser.add_argument(
'-train',
type=str,
default='train.json',
help='path to JSON file with training data'
)
parser.add_argument(
'-model',
type=str,
default='densenet161',
choices=['densenet121', 'densenet161', 'small', 'unet'],
help='model to train'
)
parser.add_argument(
'-augment',
default=False,
action='store_true',
help='flag to enable data augmentation'
)
parser.add_argument(
'-edges',
default=False,
action='store_true',
help='flag to work with edges'
)
parser.add_argument(
'-batch',
type=int,
default=16,
help='batch size'
)
parser.add_argument(
'-channels',
type=int,
default=2,
help='number output channels'
)
parser.add_argument(
'-epochs',
type=int,
default=30,
help='number of epochs'
)
parser.add_argument(
'-test',
type=str,
default='test.json',
help='path to JSON file with testing data'
)
parser.add_argument(
'-metric',
type=str,
default='pr',
choices=['pr'],
help='metric to use'
)
args = parser.parse_args()
main(
outputs_pth=args.outputs,
criterion_id=args.criterion,
dataset_id=args.dataset,
train_pth=args.train,
model_id=args.model,
augment=args.augment,
edges=args.edges,
batch_size=args.batch,
out_channels=args.channels,
num_epochs=args.epochs,
test_pth=args.test,
metric_id=args.metric
)