Skip to content

Commit

Permalink
Merge pull request #357 from thouis/master
Browse files Browse the repository at this point in the history
Use correct min/max stretching, and guard against flat images
  • Loading branch information
jccaicedo authored Aug 17, 2024
2 parents 84c388d + 0cb8883 commit bed9d6a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
11 changes: 10 additions & 1 deletion deepprofiler/imaging/cropping.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def crop_graph(image_ph, boxes_ph, box_ind_ph, mask_ind_ph, box_size, mask_boxes
#crops = (crops - mean)/std
mini = tf.math.reduce_min(crops, axis=[1, 2], keepdims=True)
maxi = tf.math.reduce_max(crops, axis=[1, 2], keepdims=True)
crops = (crops - mini) / maxi
crops = (crops - mini) / (maxi - mini + tf.keras.backend.epsilon())

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

# check that all boxes overlap the image
ymins = boxes[:, [0, 2]].min(axis=1)
ymaxs = boxes[:, [0, 2]].max(axis=1)
xmins = boxes[:, [1, 3]].min(axis=1)
xmaxs = boxes[:, [1, 3]].max(axis=1)
if (np.any(ymins > 1) or np.any(xmins > 1) or
np.any(ymaxs < 0) or np.any(ymaxs < 0)):
print("WARNING: Some cell boxes are entirely outside the image")

for i in range(num_targets):
tname = "target_" + str(i)
feed_dict[self.input_variables["targets_phs"][tname]] = targets[i]
Expand Down
10 changes: 10 additions & 0 deletions deepprofiler/learning/profiling.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ def extract_features(self, key, image_array, meta): # key is a placeholder
if total_crops == 0:
print("No cells to profile:", output_file)
return

# check image size matches config
if (self.config["dataset"]["images"]["width"] != image_array.shape[1] or
self.config["dataset"]["images"]["height"] != image_array.shape[0]):
config_shape = (self.config["dataset"]["images"]["width"],
self.config["dataset"]["images"]["height"])
im_shape = (image_array.shape[1], image_array.shape[0])
raise ValueError("Loaded image shape WxH " + str(im_shape) +
" != configured image shape WxH " + str(config_shape))

repeats = self.config["train"]["model"]["crop_generator"] in ["repeat_channel_crop_generator", "individual_channel_cropgen"]

# Extract features
Expand Down

0 comments on commit bed9d6a

Please sign in to comment.