-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfast_trainer_lib.py
124 lines (107 loc) · 4.16 KB
/
fast_trainer_lib.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
"""Fast Trainer Library
This file provides the code for model generation.
Can be used in the standalone file or within Rodan.
"""
import logging
import os
import sys
import cv2
import numpy as np
try:
import training_engine_sae as training
except Exception:
import rodan.jobs.Calvo_classifier.training_engine_sae as training
class CalvoTrainer:
def __init__(
self,
batch_size,
patch_height,
patch_width,
max_number_of_epochs,
max_samples_per_class,
inputs,
outputs,
):
self.batch_size = batch_size
self.patch_height = patch_height
self.patch_width = patch_width
self.max_number_of_epochs = max_number_of_epochs
self.max_samples_per_class = max_samples_per_class
self.inputs = inputs
self.outputs = outputs
def runTrainer(self):
input_ports = len([x for x in self.inputs if "Layer" in x])
output_ports = len([x for x in self.outputs if "Model" in x or "Log file" in x])
if input_ports not in [
output_ports,
output_ports - 1,
]: # So it still works if Log File is added as an output.
raise Exception(
'The number of input layers "rgba PNG - Layers" does not match the number of'
' output "Adjustable models"\n'
"input_ports: "
+ str(input_ports)
+ " output_ports: "
+ str(output_ports)
)
# Required input ports
# TODO assert that all layers have the same number of inputs (otherwise it will crack afterwards)
number_of_training_pages = len(self.inputs["Image"])
input_images = []
gts = []
# Create output models
output_models_path = {}
for idx in range(number_of_training_pages):
input_image = cv2.imread(
self.inputs["Image"][idx]["resource_path"], cv2.IMREAD_COLOR
) # 3-channel
background = cv2.imread(
self.inputs["rgba PNG - Layer 0 (Background)"][idx]["resource_path"],
cv2.IMREAD_UNCHANGED,
) # 4-channel
regions = cv2.imread(
self.inputs["rgba PNG - Selected regions"][idx]["resource_path"],
cv2.IMREAD_UNCHANGED,
) # 4-channel
# Create categorical ground-truth
gt = {}
TRANSPARENCY = 3
regions_mask = regions[:, :, TRANSPARENCY] == 255
# background is already restricted to the selected regions (based on Pixel.js' behaviour)
# Populate remaining inputs and outputs
bg_mask = background[:, :, TRANSPARENCY] == 255
gt["0"] = np.logical_and(bg_mask, regions_mask)
for i in range(1, input_ports):
file_obj = cv2.imread(
self.inputs["rgba PNG - Layer {layer_num}".format(layer_num=i)][
idx
]["resource_path"],
cv2.IMREAD_UNCHANGED,
)
file_mask = file_obj[:, :, TRANSPARENCY] == 255
gt[str(i)] = np.logical_and(file_mask, regions_mask)
input_images.append(input_image)
gts.append(gt)
for i in range(input_ports):
output_models_path[str(i)] = self.outputs["Model " + str(i)][0][
"resource_path"
]
# THIS IS NOT TAKING INTO ACCOUNT ANY FILE NOT NAMED MODEL IE BACKGROUND AND LOG!!!!
# Call in training function
status = training.train_msae(
input_images=input_images,
gts=gts,
num_labels=input_ports,
height=self.patch_height,
width=self.patch_width,
output_path=output_models_path,
epochs=self.max_number_of_epochs,
max_samples_per_class=self.max_samples_per_class,
batch_size=self.batch_size,
)
print("Finishing the Fast CM trainer job.")
for i in range(input_ports):
os.rename(
output_models_path[str(i)],
self.outputs["Model " + str(i)][0]["resource_path"],
)