-
Notifications
You must be signed in to change notification settings - Fork 1
/
fast_calvo_trainer.py
240 lines (226 loc) · 7.26 KB
/
fast_calvo_trainer.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
# -----------------------------------------------------------------------------
# Program Name: calvo_trainer.py
# Program Description: Rodan wrapper for Fast Calvo's classifier training
# -----------------------------------------------------------------------------
# Core
import logging
import sys
# Third-party
from celery.utils.log import get_task_logger
# Project
from rodan.celery import app
from rodan.jobs.base import RodanTask
from rodan.jobs.Calvo_classifier.fast_trainer_lib import CalvoTrainer
"""Wrap Patchwise (Fast) Calvo classifier training in Rodan."""
logger = get_task_logger(__name__)
class FastCalvoTrainer(RodanTask):
name = "Training model for Patchwise Analysis of Music Document"
author = "Jorge Calvo-Zaragoza, Francisco J. Castellanos, Gabriel Vigliensoni, and Ichiro Fujinaga"
description = "The job performs the training of many Selection Auto-Encoder model for the pixelwise analysis of music document images."
enabled = True
category = "OMR - Layout analysis"
interactive = False
settings = {
"title": "Training parameters",
"type": "object",
"properties": {
"Batch Size": {
"type": "integer",
"minimum": 1,
"default": 16,
"maximum": 64,
},
"Maximum number of training epochs": {
"type": "integer",
"minimum": 1,
"default": 50,
},
"Maximum number of samples per label": {
"type": "integer",
"minimum": 100,
"default": 2000,
},
"Patch height": {"type": "integer", "minimum": 32, "default": 256},
"Patch width": {"type": "integer", "minimum": 32, "default": 256},
},
"job_queue": "GPU",
}
input_port_types = (
{
"name": "Image",
"minimum": 1,
"maximum": 5,
"resource_types": ["image/rgb+png", "image/rgb+jpg"],
},
{
"name": "rgba PNG - Selected regions",
"minimum": 1,
"maximum": 5,
"resource_types": ["image/rgba+png"],
},
# We did not go this route because it would be more difficult for the user to track layers
# {'name': 'rgba PNG - Layers', 'minimum': 1, 'maximum': 10, 'resource_types': ['image/rgba+png']},
{
"name": "rgba PNG - Layer 0 (Background)",
"minimum": 1,
"maximum": 5,
"resource_types": ["image/rgba+png"],
},
{
"name": "rgba PNG - Layer 1",
"minimum": 1,
"maximum": 5,
"resource_types": ["image/rgba+png"],
},
{
"name": "rgba PNG - Layer 2",
"minimum": 0,
"maximum": 5,
"resource_types": ["image/rgba+png"],
},
{
"name": "rgba PNG - Layer 3",
"minimum": 0,
"maximum": 5,
"resource_types": ["image/rgba+png"],
},
{
"name": "rgba PNG - Layer 4",
"minimum": 0,
"maximum": 5,
"resource_types": ["image/rgba+png"],
},
{
"name": "rgba PNG - Layer 5",
"minimum": 0,
"maximum": 5,
"resource_types": ["image/rgba+png"],
},
{
"name": "rgba PNG - Layer 6",
"minimum": 0,
"maximum": 5,
"resource_types": ["image/rgba+png"],
},
{
"name": "rgba PNG - Layer 7",
"minimum": 0,
"maximum": 5,
"resource_types": ["image/rgba+png"],
},
{
"name": "rgba PNG - Layer 8",
"minimum": 0,
"maximum": 5,
"resource_types": ["image/rgba+png"],
},
{
"name": "rgba PNG - Layer 9",
"minimum": 0,
"maximum": 5,
"resource_types": ["image/rgba+png"],
},
)
output_port_types = (
# We did not go this route because it would be more difficult for the user to track layers
# {'name': 'Adjustable models', 'minimum': 1, 'maximum': 10, 'resource_types': ['keras/model+hdf5']},
{
"name": "Log File",
"minimum": 1,
"maximum": 1,
"resource_types": ["text/plain"],
},
{
"name": "Model 0",
"minimum": 1,
"maximum": 1,
"resource_types": ["keras/model+hdf5"],
},
{
"name": "Model 1",
"minimum": 1,
"maximum": 1,
"resource_types": ["keras/model+hdf5"],
},
{
"name": "Model 2",
"minimum": 0,
"maximum": 1,
"resource_types": ["keras/model+hdf5"],
},
{
"name": "Model 3",
"minimum": 0,
"maximum": 1,
"resource_types": ["keras/model+hdf5"],
},
{
"name": "Model 4",
"minimum": 0,
"maximum": 1,
"resource_types": ["keras/model+hdf5"],
},
{
"name": "Model 5",
"minimum": 0,
"maximum": 1,
"resource_types": ["keras/model+hdf5"],
},
{
"name": "Model 6",
"minimum": 0,
"maximum": 1,
"resource_types": ["keras/model+hdf5"],
},
{
"name": "Model 7",
"minimum": 0,
"maximum": 1,
"resource_types": ["keras/model+hdf5"],
},
{
"name": "Model 8",
"minimum": 0,
"maximum": 1,
"resource_types": ["keras/model+hdf5"],
},
{
"name": "Model 9",
"minimum": 0,
"maximum": 1,
"resource_types": ["keras/model+hdf5"],
},
)
def run_my_task(self, inputs, settings, outputs):
oldouts = sys.stdout, sys.stderr
if "Log File" in outputs:
handler = logging.FileHandler(outputs["Log File"][0]["resource_path"])
handler.setFormatter(
logging.Formatter("%(asctime)s - %(name)s - %(message)s")
)
logger.addHandler(handler)
try:
# Settings
batch_size = settings["Batch Size"]
patch_height = settings["Patch height"]
patch_width = settings["Patch width"]
max_number_of_epochs = settings["Maximum number of training epochs"]
max_samples_per_class = settings["Maximum number of samples per label"]
rlevel = app.conf.CELERY_REDIRECT_STDOUTS_LEVEL
app.log.redirect_stdouts_to_logger(logger, rlevel)
# Fail if arbitrary layers are not equal before training occurs.
trainer = CalvoTrainer(
batch_size,
patch_height,
patch_width,
max_number_of_epochs,
max_samples_per_class,
inputs,
outputs,
)
trainer.runTrainer()
return True
finally:
sys.stdout, sys.stderr = oldouts
def my_error_information(self, exc, traceback):
pass