Skip to content

Commit a660595

Browse files
committed
Add ISBI output code
1 parent 69e7f37 commit a660595

File tree

1 file changed

+64
-16
lines changed

1 file changed

+64
-16
lines changed

SourceCode/RNNSeg/eval_SegNetBiGRU_ISBI.py

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import os
22
import time
3-
3+
import csv
44
import cv2
55
import numpy as np
66
import tensorflow as tf
@@ -15,7 +15,6 @@ def run_net():
1515

1616
with tf.name_scope('Data'):
1717
image_seq_fw, filename_seq_fw, image_seq_bw, filename_seq_bw = data_provider.get_sequence(params.seq_length)
18-
image_seq_fw_bw = [tf.concat([fw, bw], axis=0) for fw, bw in zip(image_seq_fw, image_seq_bw[::-1])]
1918
filename_seq_bw = filename_seq_bw[::-1]
2019
# Build Network Graph
2120
net_fw = BiGRUNetwork()
@@ -106,34 +105,83 @@ def run_net():
106105
coord.request_stop()
107106
coord.join(threads)
108107
loop = False
108+
isbi_out_dict = {}
109109
if not params.dry_run:
110110
out_dir = params.experiment_out_dir
111+
max_id = 0
112+
base_out_fname = os.path.join(params.experiment_isbi_out, 'man_seg{time:03d}.tif')
111113
for t, file_name in enumerate(all_filenames):
112114
fw_logits = np.load(os.path.join(params.experiment_tmp_fw_dir, file_name))
113115
bw_logits = np.load(os.path.join(params.experiment_tmp_bw_dir, file_name))
114116
feed_dict = {bw_ph: bw_logits, fw_ph: fw_logits}
115117
seg_out = sess.run(final_out, feed_dict)
116-
seg_cell = np.argmax(seg_out, axis=2) == 1
117-
cc_out = cv2.connectedComponentsWithStats(seg_cell, 8, cv2.CV_32S)
118+
seg_cell = (seg_out[:, :, 1] > 0.7).astype(
119+
np.float32) # (np.argmax(seg_out, axis=2) == 1).astype(np.float16)
120+
cc_out = cv2.connectedComponentsWithStats(seg_cell.astype(np.uint8), 8, cv2.CV_32S)
118121
num_cells = cc_out[0]
119122
labels = cc_out[1]
120123
stats = cc_out[2]
121-
centroids = cc_out[3]
124+
122125
if t == 0:
123-
num_cells_prev = cc_out[0]
126+
124127
labels_prev = cc_out[1]
125-
stats_prev = cc_out[2]
126-
centroids_prev = cc_out[3]
128+
max_id = num_cells
129+
isbi_out_dict = {n: [n, 0, 0, 0] for n in range(1, 1 + num_cells)}
127130
continue
131+
matched_labels = np.zeros_like(labels)
132+
unmatched_indexes = list(range(1, 1 + num_cells))
133+
continued_ids = []
134+
ids_to_split = set()
135+
split_dict = {}
136+
for p in np.arange(1, 1 + num_cells):
137+
matching_mask = labels_prev[labels == p]
138+
matching_candidates = np.unique(matching_mask)
139+
for candidate in matching_candidates:
140+
if candidate == 0:
141+
continue
142+
intersection = np.count_nonzero(matching_mask.flatten() == candidate)
143+
area = stats[p, cv2.CC_STAT_AREA].astype(np.float32)
144+
if intersection / area > 0.5:
145+
# Keep score of matched_indexes labels in order to know which are new cells
146+
unmatched_indexes.remove(p)
147+
if candidate not in continued_ids:
148+
# Keep score of matched_indexes labels in order to know which tracks to stop
149+
continued_ids.append[candidate]
150+
split_dict[candidate] = [p] # Keep for mitosis detection
151+
else:
152+
# Keep score of matched_indexes labels in order to know which tracks to stop
153+
split_dict[candidate].append[p]
154+
ids_to_split.add[candidate]
155+
continued_ids.remove(candidate)
156+
matched_labels[labels == p] = candidate
157+
continue
158+
for cont_id in continued_ids:
159+
isbi_out_dict[cont_id][2] += 1
160+
161+
out_labels = matched_labels.copy()
162+
for parent_id, idxs in split_dict.items():
163+
if len(idxs) > 1:
164+
for idx in idxs:
165+
max_id += 1
166+
out_labels[labels == idx] = max_id
167+
isbi_out_dict[max_id] = [max_id, t, t, parent_id]
168+
169+
for unmatched in unmatched_indexes:
170+
max_id += 1
171+
out_labels[labels == unmatched] = max_id
172+
isbi_out_dict[max_id] = [max_id, t, t, 0]
173+
out_fname = base_out_fname.format(time=t)
174+
cv2.imwrite(filename=out_fname, img=out_labels)
175+
176+
177+
labels_prev = out_labels
178+
179+
csv_file_name = os.path.join(params.experiment_isbi_out, 'man_track.txt')
180+
181+
with open(csv_file_name) as f:
182+
writer = csv.writer(f, delimiter=' ')
183+
writer.writerows(isbi_out_dict.values())
128184

129-
num_cells_prev = cc_out[0]
130-
labels_prev = cc_out[1]
131-
stats_prev = cc_out[2]
132-
centroids_prev = cc_out[3]
133-
134-
135-
136-
#
137185
# seg_out = (seg_out*255).astype(np.uint8)
138186
# scipy.misc.toimage(seg_out, cmin=0.0,
139187
# cmax=255.).save(os.path.join(out_dir, os.path.basename(file_name[:-4])))

0 commit comments

Comments
 (0)