Skip to content

Commit bed9d6a

Browse files
authored
Merge pull request #357 from thouis/master
Use correct min/max stretching, and guard against flat images
2 parents 84c388d + 0cb8883 commit bed9d6a

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

deepprofiler/imaging/cropping.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def crop_graph(image_ph, boxes_ph, box_ind_ph, mask_ind_ph, box_size, mask_boxes
3030
#crops = (crops - mean)/std
3131
mini = tf.math.reduce_min(crops, axis=[1, 2], keepdims=True)
3232
maxi = tf.math.reduce_max(crops, axis=[1, 2], keepdims=True)
33-
crops = (crops - mini) / maxi
33+
crops = (crops - mini) / (maxi - mini + tf.keras.backend.epsilon())
3434

3535
if export_masks:
3636
crops = tf.concat((crops[:, :, :, 0:-1], tf.expand_dims(masks, axis=-1)), axis=3)
@@ -332,6 +332,15 @@ def prepare_image(self, session, image_array, meta, sample_first_crops=False):
332332
self.input_variables["mask_ind_ph"]: mask_ind
333333
}
334334

335+
# check that all boxes overlap the image
336+
ymins = boxes[:, [0, 2]].min(axis=1)
337+
ymaxs = boxes[:, [0, 2]].max(axis=1)
338+
xmins = boxes[:, [1, 3]].min(axis=1)
339+
xmaxs = boxes[:, [1, 3]].max(axis=1)
340+
if (np.any(ymins > 1) or np.any(xmins > 1) or
341+
np.any(ymaxs < 0) or np.any(ymaxs < 0)):
342+
print("WARNING: Some cell boxes are entirely outside the image")
343+
335344
for i in range(num_targets):
336345
tname = "target_" + str(i)
337346
feed_dict[self.input_variables["targets_phs"][tname]] = targets[i]

deepprofiler/learning/profiling.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ def extract_features(self, key, image_array, meta): # key is a placeholder
8282
if total_crops == 0:
8383
print("No cells to profile:", output_file)
8484
return
85+
86+
# check image size matches config
87+
if (self.config["dataset"]["images"]["width"] != image_array.shape[1] or
88+
self.config["dataset"]["images"]["height"] != image_array.shape[0]):
89+
config_shape = (self.config["dataset"]["images"]["width"],
90+
self.config["dataset"]["images"]["height"])
91+
im_shape = (image_array.shape[1], image_array.shape[0])
92+
raise ValueError("Loaded image shape WxH " + str(im_shape) +
93+
" != configured image shape WxH " + str(config_shape))
94+
8595
repeats = self.config["train"]["model"]["crop_generator"] in ["repeat_channel_crop_generator", "individual_channel_cropgen"]
8696

8797
# Extract features

0 commit comments

Comments
 (0)