From 08aa86ac2e3ea5329e572403f95b4060d26356b1 Mon Sep 17 00:00:00 2001 From: "Thouis (Ray) Jones" Date: Thu, 15 Aug 2024 10:54:28 -0400 Subject: [PATCH 1/2] Use correct min/max stretching, and guard against flat images --- deepprofiler/imaging/cropping.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/deepprofiler/imaging/cropping.py b/deepprofiler/imaging/cropping.py index 2163707..763d7f8 100644 --- a/deepprofiler/imaging/cropping.py +++ b/deepprofiler/imaging/cropping.py @@ -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) From 0cb88839a81e2505b3d7de4be518995ee6619e68 Mon Sep 17 00:00:00 2001 From: "Thouis (Ray) Jones" Date: Thu, 15 Aug 2024 13:19:01 -0400 Subject: [PATCH 2/2] Add Exception for image size not matching configured size, and Warning for cell boxes outside the image --- deepprofiler/imaging/cropping.py | 9 +++++++++ deepprofiler/learning/profiling.py | 10 ++++++++++ 2 files changed, 19 insertions(+) diff --git a/deepprofiler/imaging/cropping.py b/deepprofiler/imaging/cropping.py index 763d7f8..04db51b 100644 --- a/deepprofiler/imaging/cropping.py +++ b/deepprofiler/imaging/cropping.py @@ -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] diff --git a/deepprofiler/learning/profiling.py b/deepprofiler/learning/profiling.py index 84264a4..5944558 100644 --- a/deepprofiler/learning/profiling.py +++ b/deepprofiler/learning/profiling.py @@ -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