-
Notifications
You must be signed in to change notification settings - Fork 0
Add configurable Monte Carlo dropout uncertainty estimation #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
52791a3
848070b
70bec9a
4d45970
acffc06
b687837
2937be5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -134,13 +134,15 @@ def process(self, inputs, outputs): | |
| if includes_bg: | ||
| y_true = output["y_true"][..., 1:] | ||
| y_pred = output["y_pred"][..., 1:] | ||
| y_mc_dropout = None if output["y_mc_dropout"] is None else output["y_mc_dropout"][..., 1:] | ||
| labels = labels[..., 1:] | ||
| # if y_true.ndim == 3: | ||
| # y_true = y_true[..., np.newaxis] | ||
| # y_pred = y_pred[..., np.newaxis] | ||
| # labels = labels[..., np.newaxis] | ||
| output["y_true"] = y_true | ||
| output["y_pred"] = y_pred | ||
| output["y_mc_dropout"] = y_mc_dropout | ||
|
|
||
| time_elapsed = output["time_elapsed"] | ||
| if self.stream_evaluation: | ||
|
|
@@ -178,6 +180,9 @@ def eval_single_scan(self, input, output, labels, time_elapsed): | |
| with h5py.File(save_name, "w") as h5f: | ||
| h5f.create_dataset("probs", data=output["y_pred"]) | ||
| h5f.create_dataset("labels", data=labels) | ||
| h5f.create_dataset("true", data=output["y_true"]) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i would avoid saving |
||
| if output["y_mc_dropout"] is not None: | ||
| h5f.create_dataset("mc_dropout", data=output["y_mc_dropout"]) | ||
|
|
||
| def evaluate(self): | ||
| """Evaluates popular medical segmentation metrics specified in config. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,9 @@ def inference_generator( | |
| max_queue_size=10, | ||
| workers=1, | ||
| use_multiprocessing=False, | ||
| verbose=0, | ||
| mc_dropout=False, | ||
| mc_dropout_T=100, | ||
|
Comment on lines
+45
to
+46
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems like these are not used - if that's the case, delete |
||
| verbose=0 | ||
| ): | ||
| return self.inference_generator_static( | ||
| self, generator, steps, max_queue_size, workers, use_multiprocessing, verbose | ||
|
|
@@ -57,7 +59,9 @@ def inference_generator_static( | |
| max_queue_size=10, | ||
| workers=1, | ||
| use_multiprocessing=False, | ||
| verbose=0, | ||
| mc_dropout=False, | ||
| mc_dropout_T=100, | ||
| verbose=0 | ||
| ): | ||
| """Generates predictions for the input samples from a data generator | ||
| and returns inputs, ground truth, and predictions. | ||
|
|
@@ -115,6 +119,8 @@ def inference_generator_static( | |
| max_queue_size=max_queue_size, | ||
| workers=workers, | ||
| use_multiprocessing=use_multiprocessing, | ||
| mc_dropout=mc_dropout, | ||
| mc_dropout_T=mc_dropout_T, | ||
| verbose=verbose, | ||
| ) | ||
| else: | ||
|
|
@@ -252,9 +258,12 @@ def _inference_generator_tf2( | |
| max_queue_size=10, | ||
| workers=1, | ||
| use_multiprocessing=False, | ||
| mc_dropout=False, | ||
| mc_dropout_T=100 | ||
| ): | ||
| """Inference generator for TensorFlow 2.""" | ||
| outputs = [] | ||
| outputs_mc_dropout = [] | ||
| xs = [] | ||
| ys = [] | ||
| with model.distribute_strategy.scope(): | ||
|
|
@@ -295,14 +304,21 @@ def _inference_generator_tf2( | |
| batch_x, batch_y, batch_x_raw = _extract_inference_inputs(next(iterator)) | ||
| # tmp_batch_outputs = predict_function(iterator) | ||
| tmp_batch_outputs = model.predict(batch_x) | ||
|
|
||
|
|
||
| tmp_batch_outputs_mc_dropout = None | ||
| if mc_dropout: | ||
| tmp_batch_outputs_mc_dropout = np.stack([model(batch_x, training=True) for _ in range(mc_dropout_T)]) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see what you're trying to do here, but it will not be reproducible, which is necessary if we are to add this in the im not sure exactly how to account for this, potentially setting a random seed. Write a unittest to verify that this does in fact produce identical inputs when run twice. |
||
|
|
||
| if data_handler.should_sync: | ||
| context.async_wait() # noqa: F821 | ||
| batch_outputs = tmp_batch_outputs # No error, now safe to assign. | ||
| batch_outputs_mc_dropout = tmp_batch_outputs_mc_dropout | ||
|
|
||
| if batch_x_raw is not None: | ||
| batch_x = batch_x_raw | ||
| for batch, running in zip( | ||
| [batch_x, batch_y, batch_outputs], [xs, ys, outputs] | ||
| [batch_x, batch_y, batch_outputs, batch_outputs_mc_dropout], [xs, ys, outputs, outputs_mc_dropout] | ||
| ): | ||
| nest.map_structure_up_to( | ||
| batch, lambda x, batch_x: x.append(batch_x), running, batch | ||
|
|
@@ -318,7 +334,11 @@ def _inference_generator_tf2( | |
| all_xs = nest.map_structure_up_to(batch_x, np.concatenate, xs) | ||
| all_ys = nest.map_structure_up_to(batch_y, np.concatenate, ys) | ||
| all_outputs = nest.map_structure_up_to(batch_outputs, np.concatenate, outputs) | ||
| return all_xs, all_ys, all_outputs | ||
| all_outputs_mc_dropout = nest.map_structure_up_to(batch_outputs_mc_dropout, np.concatenate, outputs_mc_dropout) if mc_dropout else None | ||
|
|
||
| outputs = {'preds': all_outputs, 'preds_mc_dropout': all_outputs_mc_dropout} | ||
|
|
||
| return all_xs, all_ys, outputs | ||
|
|
||
| # all_xs = nest.map_structure_up_to(batch_x, concat, xs) | ||
| # all_ys = nest.map_structure_up_to(batch_y, concat, ys) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment indicating what MC_DROPOUT and MC_DROPOUT_T are referring to