-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtipmask_train.py
193 lines (149 loc) · 5.45 KB
/
tipmask_train.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
from joblib import dump, load
import os
import click
import collections
import timeit
import warnings
import cv2
from sklearn.ensemble import RandomForestRegressor
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
from lib.utils import get_files_to_process, pixel_to_mm, get_attributes_from_filename
from lib.constants import STRAIGHTENED_MASKS_DIR, TIP_MASK_PSEUDO_MAX_LENGTH
from lib.tip_mask import get_width_array, get_width_array_mm, normalize_width_array
from phenotype import get_length
# 1. read in mask
# 2. find index of red line
# 3. along the length: get width
# 4. normalize width
def get_true_tip_index(row):
"""
finds a red pixel in the row of an image
Args:
row (np.array) - the image row
Returns:
index (int) or None
"""
for i, pixel in enumerate(row):
b, g, r = pixel
if r > 100:
return i
def get_index_first_white_pixel(image):
"""
finds the index of the first white pixel
Args:
image (np.array) - greyscale image as nested np.array
Returns:
index (int)
"""
for i, column in enumerate(image.T):
count = collections.Counter(column)
if count.get(255, None):
return i
def equalize_lengths(raw_data):
"""
ensure all normalized width arrays are of the same lengh
"""
# max_length = max([len(d["normalized_widths"]) for d in raw_data])
max_length = TIP_MASK_PSEUDO_MAX_LENGTH
for d in raw_data:
normalized_widths = d["normalized_widths"]
missing_len = max_length - len(normalized_widths)
d["normalized_widths"] = normalized_widths + [0] * missing_len
return raw_data
def get_mask_pairs(src):
"""
Assembles a list of raw/training mask pairs for each genotype
Args:
src (str): path to the source folder
Returns:
pairs (list)
"""
pairs = []
raw = os.path.join(src, "with-tips")
training = os.path.join(src, "without-tips")
genotypes = os.listdir(raw)
for g in genotypes:
if not g.startswith("."):
pair = {"genotype": g}
raw_mask_dir = os.path.join(raw, g, STRAIGHTENED_MASKS_DIR)
for f in os.listdir(raw_mask_dir):
if not f.startswith("."):
raw_straight_mask = os.listdir(raw_mask_dir)[0]
break
raw_straight_mask = os.path.join(raw_mask_dir, raw_straight_mask)
pair["with-tips"] = raw_straight_mask
training_mask_dir = os.path.join(training, g, STRAIGHTENED_MASKS_DIR)
for f in os.listdir(training_mask_dir):
if not f.startswith("."):
training_straight_mask = os.listdir(training_mask_dir)[0]
break
training_straight_mask = os.path.join(
training_mask_dir, training_straight_mask
)
pair["without-tips"] = training_straight_mask
pairs.append(pair)
return pairs
@click.command()
@click.option(
"--src",
"-s",
type=click.Path(exists=True),
help="source directory of images to process",
)
def run(src):
start = timeit.default_timer()
pairs = get_mask_pairs(src)
# pixel müssen vergleichbar sein.
data = []
for pair in pairs:
raw = pair["with-tips"]
training = pair["without-tips"]
# print(raw)
attributes = get_attributes_from_filename(raw)
scale = attributes.get("Scale", None)
mm_per_px = pixel_to_mm(scale)
# print(mm_per_px)
raw_mask = cv2.imread(raw, cv2.IMREAD_GRAYSCALE)
training_mask = cv2.imread(training, cv2.IMREAD_GRAYSCALE)
# reverse, so the thick end is at 0
width_array = get_width_array_mm(raw_mask, mm_per_px)[::-1]
# print(width_array)
normalized_width_array = normalize_width_array(width_array)
# length of raw carrot
# raw_length = get_length(raw_mask)
# print("length", raw_length)
# length of detipped carrot
detipped_length = get_length(training_mask)
# print("detipped length", detipped_length)
# difference in length
# length_diff = raw_length - detipped_length
# white_index_raw = get_index_first_white_pixel(raw_mask)
# tip_index = white_index_raw + length_diff
# because the widths are reversed
tip_index = detipped_length
data.append(
{"tip_index": tip_index, "normalized_widths": normalized_width_array}
)
# resampling sagt Gilles...
equalized_data = equalize_lengths(data)
X = [d["normalized_widths"] for d in equalized_data]
y = [d["tip_index"] for d in equalized_data]
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
# linreg = LinearRegression().fit(X_train, y_train)
# print('R-squared score (training): {:.3f}'
# .format(linreg.score(X_train, y_train)))
# print('R-squared score (test): {:.3f}'
# .format(linreg.score(X_test, y_test)))
regr = RandomForestRegressor(max_depth=5, random_state=0, n_estimators=10)
regr.fit(X_train, y_train)
print("score", regr.score(X_test, y_test))
# regr.predict([[feature1, feature2]])
dump(regr, "tip-mask-model.joblib")
print("model dumped")
stop = timeit.default_timer()
print(f"training: {stop - start}")
if __name__ == "__main__":
with warnings.catch_warnings():
warnings.simplefilter("ignore")
run()