datamint 2.45.0__tar.gz → 2.46.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {datamint-2.45.0 → datamint-2.46.0}/PKG-INFO +1 -1
- {datamint-2.45.0 → datamint-2.46.0}/datamint/dataset/annotation_processor.py +71 -2
- {datamint-2.45.0 → datamint-2.46.0}/datamint/dataset/base.py +50 -11
- {datamint-2.45.0 → datamint-2.46.0}/datamint/dataset/sliced_dataset.py +5 -3
- {datamint-2.45.0 → datamint-2.46.0}/datamint/dataset/sliced_video_dataset.py +4 -1
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/lightning_modules/classification_module.py +13 -1
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/lightning_modules/segmentation_module.py +16 -3
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/lightning_modules/segmentation_modules/unetrpp.py +8 -3
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/segmentation_trainer.py +1 -1
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/vol_seg_trainer.py +1 -1
- {datamint-2.45.0 → datamint-2.46.0}/pyproject.toml +1 -1
- {datamint-2.45.0 → datamint-2.46.0}/README.md +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/__main__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/_repr_utils.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/base_api.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/client.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/dto/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/dto/annotation_dto.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/dto/inference_prompts.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/dto/save_results_options.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/endpoints/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/endpoints/annotations_api.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/endpoints/annotationsets_api.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/endpoints/datasetsinfo_api.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/endpoints/deploy_model_api.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/endpoints/inference_api.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/endpoints/model_types.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/endpoints/models_api.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/endpoints/pod_logs_api.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/endpoints/projects_api.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/endpoints/resources_api.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/endpoints/users_api.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/endpoints/validation_studies_api.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/api/entity_base_api.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/client_cmd_tools/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/client_cmd_tools/datamint_config.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/client_cmd_tools/datamint_download.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/client_cmd_tools/datamint_example.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/client_cmd_tools/datamint_import.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/client_cmd_tools/datamint_inference.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/client_cmd_tools/datamint_init.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/client_cmd_tools/datamint_train.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/client_cmd_tools/datamint_upload.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/configs.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/dataset/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/dataset/factory.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/dataset/image_dataset.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/dataset/multiframe_dataset.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/dataset/split_result.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/dataset/video_dataset.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/dataset/volume_dataset.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/default_project.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotation_worklist.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/annotation.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/annotation_spec.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/base_geometry.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/base_segmentation.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/box_annotation.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/category_annotation.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/geometry.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/image_classification.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/image_segmentation.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/label_annotation.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/line_annotation.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/numeric_annotation.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/point_annotation.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/region_annotation.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/text_annotation.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/types.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/annotations/volume_segmentation.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/base_entity.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/buildlog.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/cache_manager.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/channel.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/datasetinfo.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/deployjob.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/inferencejob.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/pod.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/project.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/project_resource_split.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/resource.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/resources/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/resources/dicom_resource.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/resources/image_resource.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/resources/nifti_resource.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/resources/video_resource.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/resources/volume_resource.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/sliced_resource.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/sliced_resource_base.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/sliced_video_resource.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/user.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/entities/validation_study.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/evaluate/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/evaluate/evaluator.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/evaluate/metrics/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/evaluate/metrics/classification.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/evaluate/metrics/detection.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/evaluate/metrics/segmentation.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/examples/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/examples/_common.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/examples/_download.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/examples/bccd_dataset.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/examples/busi_dataset.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/examples/example_projects.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/examples/fracatlas_dataset.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/examples/synapse_dataset.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/exceptions.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/importers/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/importers/_common.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/importers/_detect.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/importers/coco.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/importers/pascal_voc.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/importers/yolo.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/benchmark.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/datamodule.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/base_trainer.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/classification_trainer.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/detection_trainer.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/lightning_modules/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/lightning_modules/base.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/lightning_modules/detection_modules/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/lightning_modules/detection_modules/yolox_module.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/lightning_modules/segmentation_modules/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/lightning_modules/segmentation_modules/deeplabv3plus.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/lightning_modules/segmentation_modules/smp_module.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/lightning_modules/segmentation_modules/transunet.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/lightning_modules/segmentation_modules/unetpp.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/seg2d_trainer.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/seg3d_trainer.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/specialized/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/specialized/deeplabv3plus.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/specialized/efficientnetv2.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/specialized/nnunet/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/specialized/nnunet/_nnunet_trainer_bridge.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/specialized/nnunet/data_export.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/specialized/nnunet/data_import.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/specialized/nnunet/inference_model.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/specialized/nnunet/trainer.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/specialized/transunet.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/specialized/unetpp.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/specialized/unetrpp.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/lightning/trainers/specialized/yolox.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/logging.yaml +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/artifact/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/artifact/datamint_artifacts_repo.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/data/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/data/datamint_dataset.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/env_utils.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/env_vars.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/flavors/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/flavors/annotation_specs.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/flavors/datamint_flavor.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/flavors/model.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/flavors/model_loader.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/flavors/model_parser.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/flavors/prediction_modes.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/flavors/prediction_router.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/flavors/task_type.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/flavors/validation.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/lightning/callbacks/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/lightning/callbacks/modelcheckpoint.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/models/Dockerfile.template +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/models/__init__.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/models/datamint_model_store.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/models/docker_build.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/models/local_inference.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/models/serve.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/models/tags.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/store_utils.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/tracking/datamint_store.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/tracking/default_experiment.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/tracking/fluent.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/mlflow/tracking/offline_buffer.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/types.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/utils/annotation_agreement.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/utils/collection_utils.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/utils/date_utils.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/utils/env.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/utils/logging_utils.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/utils/nifti_utils.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/utils/torchmetrics.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/utils/uncertainty.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/utils/urls.py +0 -0
- {datamint-2.45.0 → datamint-2.46.0}/datamint/utils/visualization.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: datamint
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.46.0
|
|
4
4
|
Summary: A library for interacting with the Datamint API, designed for efficient data management, processing and Deep Learning workflows.
|
|
5
5
|
Keywords: medical-imaging,dicom,nifti,pytorch,mlflow,lightning,datasets,annotation
|
|
6
6
|
Author: SonanceAI
|
|
@@ -12,6 +12,7 @@ for any dataset type, while specialized logic is in subclasses.
|
|
|
12
12
|
import logging
|
|
13
13
|
from collections import defaultdict
|
|
14
14
|
from collections.abc import Iterable, Sequence
|
|
15
|
+
from itertools import combinations
|
|
15
16
|
from typing import TYPE_CHECKING, Literal
|
|
16
17
|
|
|
17
18
|
import numpy as np
|
|
@@ -26,7 +27,7 @@ if TYPE_CHECKING:
|
|
|
26
27
|
_LOGGER = logging.getLogger(__name__)
|
|
27
28
|
|
|
28
29
|
# Type alias for merge strategy
|
|
29
|
-
MergeStrategy = Literal['union', 'intersection', 'mode']
|
|
30
|
+
MergeStrategy = Literal['union', 'intersection', 'mode', 'consensus_soft']
|
|
30
31
|
|
|
31
32
|
|
|
32
33
|
class AnnotationProcessor:
|
|
@@ -356,6 +357,59 @@ class AnnotationProcessor:
|
|
|
356
357
|
new_segmentations = new_segmentations >= len(segmentations) / 2
|
|
357
358
|
return new_segmentations.bool().to(torch.uint8)
|
|
358
359
|
|
|
360
|
+
def _merge_consensus_soft(self, segmentations: dict[str, Tensor]) -> Tensor:
|
|
361
|
+
"""Soft merge: pixel value is the fraction of annotators that labeled it."""
|
|
362
|
+
return torch.stack(list(segmentations.values())).float().mean(dim=0)
|
|
363
|
+
|
|
364
|
+
def compute_agreement_weight(self,
|
|
365
|
+
segmentations: dict[str, Tensor] | dict[str, np.ndarray],
|
|
366
|
+
min_weight: float = 0.1) -> float:
|
|
367
|
+
"""Agreement weight from per-annotator semantic segmentations.
|
|
368
|
+
|
|
369
|
+
Mean pairwise Dice per foreground class (channel 0 is background), averaged over classes.
|
|
370
|
+
Pairs where neither annotator labeled the class are ignored.
|
|
371
|
+
|
|
372
|
+
Args:
|
|
373
|
+
segmentations: Dict of author -> semantic segmentation of shape (num_labels+1, ...).
|
|
374
|
+
min_weight: Lower bound of the returned weight.
|
|
375
|
+
|
|
376
|
+
Returns:
|
|
377
|
+
Weight in [min_weight, 1]. 1.0 with fewer than 2 annotators or no labeled class.
|
|
378
|
+
"""
|
|
379
|
+
if len(segmentations) < 2:
|
|
380
|
+
return 1.0
|
|
381
|
+
|
|
382
|
+
# One (num_classes, num_pixels) mask per annotator, without the background channel
|
|
383
|
+
masks = [torch.as_tensor(seg)[1:].flatten(1).float() for seg in segmentations.values()]
|
|
384
|
+
num_classes = masks[0].shape[0]
|
|
385
|
+
|
|
386
|
+
# Dice of every annotator pair, grouped by class
|
|
387
|
+
dice_by_class: list[list[float]] = [[] for _ in range(num_classes)]
|
|
388
|
+
for mask_a, mask_b in combinations(masks, 2):
|
|
389
|
+
for class_idx, dice in enumerate(self._pairwise_dice_per_class(mask_a, mask_b)):
|
|
390
|
+
if dice is not None:
|
|
391
|
+
dice_by_class[class_idx].append(dice)
|
|
392
|
+
|
|
393
|
+
# Average the pairs of each class, then average the classes.
|
|
394
|
+
class_means = [sum(dices) / len(dices) for dices in dice_by_class if dices]
|
|
395
|
+
if not class_means:
|
|
396
|
+
return 1.0 # no annotator labeled any class
|
|
397
|
+
return max(sum(class_means) / len(class_means), min_weight)
|
|
398
|
+
|
|
399
|
+
@staticmethod
|
|
400
|
+
def _pairwise_dice_per_class(mask_a: Tensor, mask_b: Tensor) -> list[float | None]:
|
|
401
|
+
"""Dice between two annotators for each class. ``None`` if neither labeled the class.
|
|
402
|
+
|
|
403
|
+
Args:
|
|
404
|
+
mask_a: Mask of shape (num_classes, num_pixels).
|
|
405
|
+
mask_b: Mask of shape (num_classes, num_pixels).
|
|
406
|
+
"""
|
|
407
|
+
|
|
408
|
+
overlap = torch.einsum('cp,cp->c', mask_a, mask_b)
|
|
409
|
+
total = mask_a.sum(dim=1) + mask_b.sum(dim=1)
|
|
410
|
+
|
|
411
|
+
return [(2 * o / t).item() if t > 0 else None for o, t in zip(overlap, total)]
|
|
412
|
+
|
|
359
413
|
def convert_image_labels(
|
|
360
414
|
self,
|
|
361
415
|
annotations: Sequence['Annotation'],
|
|
@@ -515,6 +569,18 @@ class AnnotationProcessor:
|
|
|
515
569
|
return (stacked.min(dim=0).values > 0).int()
|
|
516
570
|
raise ValueError(f"Unknown merge strategy: {strategy!r}")
|
|
517
571
|
|
|
572
|
+
@staticmethod
|
|
573
|
+
def compute_category_agreement_weight(categories_by_user: dict[str, Tensor], min_weight: float = 0.1) -> float:
|
|
574
|
+
"""Fraction of annotators that voted for the majority category, floored at ``min_weight``.
|
|
575
|
+
|
|
576
|
+
Annotators without a valid category (index < 0) are ignored. Returns 1.0 with fewer than 2 votes.
|
|
577
|
+
"""
|
|
578
|
+
votes = [t for t in categories_by_user.values() if t.item() >= 0]
|
|
579
|
+
if len(votes) < 2:
|
|
580
|
+
return 1.0
|
|
581
|
+
top_count = torch.bincount(torch.stack(votes)).max().item()
|
|
582
|
+
return max(top_count / len(votes), min_weight)
|
|
583
|
+
|
|
518
584
|
@overload
|
|
519
585
|
def apply_merge_strategy(
|
|
520
586
|
self,
|
|
@@ -538,7 +604,8 @@ class AnnotationProcessor:
|
|
|
538
604
|
|
|
539
605
|
Args:
|
|
540
606
|
segmentations: Dict of author -> semantic segmentation tensor.
|
|
541
|
-
strategy: Merge strategy ('union', 'intersection', 'mode').
|
|
607
|
+
strategy: Merge strategy ('union', 'intersection', 'mode', 'consensus_soft').
|
|
608
|
+
'consensus_soft' returns, per pixel, the fraction of annotators that labeled it.
|
|
542
609
|
|
|
543
610
|
Returns:
|
|
544
611
|
Merged tensor if strategy is specified, otherwise original dict.
|
|
@@ -557,6 +624,8 @@ class AnnotationProcessor:
|
|
|
557
624
|
merged = self._merge_intersection(segmentations)
|
|
558
625
|
elif strategy == 'mode':
|
|
559
626
|
merged = self._merge_mode(segmentations)
|
|
627
|
+
elif strategy == 'consensus_soft':
|
|
628
|
+
merged = self._merge_consensus_soft(segmentations)
|
|
560
629
|
else:
|
|
561
630
|
raise ValueError(f"Unknown merge strategy: {strategy}")
|
|
562
631
|
|
|
@@ -60,7 +60,10 @@ class DatamintBaseDataset(ABC, torch.utils.data.Dataset):
|
|
|
60
60
|
return_metainfo: If True, include metadata in output.
|
|
61
61
|
return_segmentations: If True, process and return segmentations.
|
|
62
62
|
return_as_semantic_segmentation: If True, convert to semantic format.
|
|
63
|
-
semantic_seg_merge_strategy: Strategy for merging multi-annotator segs
|
|
63
|
+
semantic_seg_merge_strategy: Strategy for merging multi-annotator segs:
|
|
64
|
+
'union', 'intersection', 'mode' (majority, ties positive) or 'consensus_soft'
|
|
65
|
+
(per-pixel fraction of annotators, e.g. 2 of 3 -> 0.67; built-in modules train on it
|
|
66
|
+
and compute metrics against ``>= 0.5``). Not used by nnU-Net.
|
|
64
67
|
alb_transform: Albumentations transform.
|
|
65
68
|
include_unannotated: If True, include resources without annotations.
|
|
66
69
|
include_annotators: Whitelist of annotators.
|
|
@@ -85,6 +88,11 @@ class DatamintBaseDataset(ABC, torch.utils.data.Dataset):
|
|
|
85
88
|
Pass ``None`` to disable this filtering entirely and include every source.
|
|
86
89
|
A resource whose annotations are all excluded is treated as unannotated,
|
|
87
90
|
so it is kept (with an empty mask) unless ``include_unannotated=False``.
|
|
91
|
+
label_weighting: ``'agreement'`` adds ``item['agreement_weight']``, floored at 0.1 and
|
|
92
|
+
1.0 with fewer than 2 annotators. With ``return_as_semantic_segmentation=True``: mean
|
|
93
|
+
pairwise Dice between annotators' semantic masks (after ``alb_transform``), averaged
|
|
94
|
+
over labeled classes. Otherwise requires ``image_categories_merge_strategy='mode'``:
|
|
95
|
+
fraction of annotators that voted for the majority category.
|
|
88
96
|
"""
|
|
89
97
|
|
|
90
98
|
resources: Sequence['Resource']
|
|
@@ -119,6 +127,7 @@ class DatamintBaseDataset(ABC, torch.utils.data.Dataset):
|
|
|
119
127
|
image_categories_merge_strategy: MergeStrategy | None = None,
|
|
120
128
|
worklists: Sequence[AnnotationWorklist] | Literal['all'] | None = 'all',
|
|
121
129
|
trusted_annotation_sources: Sequence[str] | None = ('imported', 'manual'),
|
|
130
|
+
label_weighting: Literal['agreement'] | None = None,
|
|
122
131
|
):
|
|
123
132
|
# Validate merge strategy values
|
|
124
133
|
_valid_strategies = ('union', 'intersection', 'mode', None)
|
|
@@ -157,6 +166,13 @@ class DatamintBaseDataset(ABC, torch.utils.data.Dataset):
|
|
|
157
166
|
raise ValueError("Cannot return semantic segmentation without returning segmentations.")
|
|
158
167
|
if semantic_seg_merge_strategy and not return_as_semantic_segmentation:
|
|
159
168
|
raise ValueError("semantic_seg_merge_strategy requires return_as_semantic_segmentation=True")
|
|
169
|
+
if label_weighting not in ('agreement', None):
|
|
170
|
+
raise ValueError(f"label_weighting must be 'agreement' or None, got {label_weighting!r}")
|
|
171
|
+
if label_weighting and not (return_as_semantic_segmentation or image_categories_merge_strategy == 'mode'):
|
|
172
|
+
raise ValueError(
|
|
173
|
+
"label_weighting requires return_as_semantic_segmentation=True "
|
|
174
|
+
"or image_categories_merge_strategy='mode'"
|
|
175
|
+
)
|
|
160
176
|
|
|
161
177
|
# Store API configuration for (possibly deferred) initialization
|
|
162
178
|
self._server_url = server_url
|
|
@@ -172,6 +188,7 @@ class DatamintBaseDataset(ABC, torch.utils.data.Dataset):
|
|
|
172
188
|
self.return_boxes = return_boxes
|
|
173
189
|
self.return_as_semantic_segmentation = return_as_semantic_segmentation
|
|
174
190
|
self.semantic_seg_merge_strategy: MergeStrategy | None = semantic_seg_merge_strategy
|
|
191
|
+
self.label_weighting: Literal['agreement'] | None = label_weighting
|
|
175
192
|
self.include_unannotated = include_unannotated
|
|
176
193
|
self.worklists = worklists
|
|
177
194
|
|
|
@@ -423,7 +440,7 @@ class DatamintBaseDataset(ABC, torch.utils.data.Dataset):
|
|
|
423
440
|
def _extract_image_categories(
|
|
424
441
|
self,
|
|
425
442
|
annotations: Sequence['Annotation'],
|
|
426
|
-
) -> dict[str, torch.Tensor] | torch.Tensor:
|
|
443
|
+
) -> tuple[dict[str, torch.Tensor] | torch.Tensor, torch.Tensor | None]:
|
|
427
444
|
"""Extract image-level category annotations.
|
|
428
445
|
|
|
429
446
|
When ``image_categories_merge_strategy`` is ``None`` (default), returns a dict
|
|
@@ -436,20 +453,28 @@ class DatamintBaseDataset(ABC, torch.utils.data.Dataset):
|
|
|
436
453
|
annotations: All annotations for the item.
|
|
437
454
|
|
|
438
455
|
Returns:
|
|
439
|
-
Dict[annotator_id, Tensor] or merged Tensor depending on
|
|
440
|
-
:attr:`image_categories_merge_strategy
|
|
456
|
+
Tuple of (Dict[annotator_id, Tensor] or merged Tensor depending on
|
|
457
|
+
:attr:`image_categories_merge_strategy`, category agreement weight as a 0-d
|
|
458
|
+
tensor if ``label_weighting='agreement'`` applies to categories, else ``None``).
|
|
441
459
|
"""
|
|
442
460
|
category_annotations = AnnotationProcessor.filter_annotations(
|
|
443
461
|
annotations, type='category', scope='image'
|
|
444
462
|
)
|
|
445
463
|
categories_dict = self.annotation_processor.convert_image_categories(category_annotations)
|
|
446
464
|
if self.image_categories_merge_strategy is None:
|
|
447
|
-
return categories_dict
|
|
448
|
-
|
|
465
|
+
return categories_dict, None
|
|
466
|
+
agreement_weight = None
|
|
467
|
+
|
|
468
|
+
# With semantic masks the weight comes from the segmentations instead.
|
|
469
|
+
if (getattr(self, 'label_weighting', None) == 'agreement' and not self.return_as_semantic_segmentation and self.image_categories_merge_strategy == 'mode'):
|
|
470
|
+
agreement_weight = torch.tensor(AnnotationProcessor.compute_category_agreement_weight(categories_dict))
|
|
471
|
+
|
|
472
|
+
merged = AnnotationProcessor.merge_image_categories(
|
|
449
473
|
categories_dict,
|
|
450
474
|
self.image_categories_merge_strategy,
|
|
451
475
|
num_categories=len(self.image_categories_set),
|
|
452
476
|
)
|
|
477
|
+
return merged, agreement_weight
|
|
453
478
|
|
|
454
479
|
def _validate_filter_params(
|
|
455
480
|
self,
|
|
@@ -1008,7 +1033,8 @@ class DatamintBaseDataset(ABC, torch.utils.data.Dataset):
|
|
|
1008
1033
|
def _process_segmentations(self,
|
|
1009
1034
|
segmentations: dict,
|
|
1010
1035
|
seg_labels: dict,
|
|
1011
|
-
output_shape: tuple | None = None
|
|
1036
|
+
output_shape: tuple | None = None
|
|
1037
|
+
) -> tuple[Tensor | np.ndarray | dict, dict | None, Tensor | None]:
|
|
1012
1038
|
"""Process segmentations by optionally converting to semantic format and applying merge strategy.
|
|
1013
1039
|
|
|
1014
1040
|
Args:
|
|
@@ -1017,8 +1043,10 @@ class DatamintBaseDataset(ABC, torch.utils.data.Dataset):
|
|
|
1017
1043
|
output_shape: Fallback output shape for semantic segmentation with no segmentations.
|
|
1018
1044
|
|
|
1019
1045
|
Returns:
|
|
1020
|
-
Tuple of (processed segmentations, seg_labels or ``None
|
|
1046
|
+
Tuple of (processed segmentations, seg_labels or ``None``,
|
|
1047
|
+
agreement weight as a 0-d tensor if ``label_weighting='agreement'``, else ``None``).
|
|
1021
1048
|
"""
|
|
1049
|
+
agreement_weight = None
|
|
1022
1050
|
# segmentations['author'] shape: (#instances, depth, H, W)
|
|
1023
1051
|
if self.return_as_semantic_segmentation:
|
|
1024
1052
|
sem_segs = {}
|
|
@@ -1028,6 +1056,11 @@ class DatamintBaseDataset(ABC, torch.utils.data.Dataset):
|
|
|
1028
1056
|
num_labels=len(self.segmentation_labels_set)
|
|
1029
1057
|
)
|
|
1030
1058
|
segmentations = sem_segs
|
|
1059
|
+
|
|
1060
|
+
if getattr(self, 'label_weighting', None) == 'agreement':
|
|
1061
|
+
# Before the merge: needs per-annotator masks
|
|
1062
|
+
agreement_weight = torch.tensor(self.annotation_processor.compute_agreement_weight(sem_segs))
|
|
1063
|
+
|
|
1031
1064
|
if self.semantic_seg_merge_strategy:
|
|
1032
1065
|
if len(segmentations) > 0:
|
|
1033
1066
|
segmentations = self.annotation_processor.apply_merge_strategy(
|
|
@@ -1052,7 +1085,7 @@ class DatamintBaseDataset(ABC, torch.utils.data.Dataset):
|
|
|
1052
1085
|
# In semantic format, we don't need `seg_labels`, as the label info is at the new dimension (axis=0) of the semantic segmentation array.
|
|
1053
1086
|
seg_labels = None
|
|
1054
1087
|
|
|
1055
|
-
return segmentations, seg_labels
|
|
1088
|
+
return segmentations, seg_labels, agreement_weight
|
|
1056
1089
|
|
|
1057
1090
|
def __getitem__(self, index: int) -> dict[str, Any]:
|
|
1058
1091
|
"""Get item with full processing."""
|
|
@@ -1092,10 +1125,13 @@ class DatamintBaseDataset(ABC, torch.utils.data.Dataset):
|
|
|
1092
1125
|
# Post-process and write to result
|
|
1093
1126
|
if self.return_segmentations:
|
|
1094
1127
|
masks = targets.get('masks', {})
|
|
1095
|
-
masks, seg_labels = self._process_segmentations(
|
|
1128
|
+
masks, seg_labels, agreement_weight = self._process_segmentations(
|
|
1129
|
+
masks, seg_labels, output_shape=img.shape[1:])
|
|
1096
1130
|
result['masks'] = masks
|
|
1097
1131
|
if seg_labels:
|
|
1098
1132
|
result['mask_labels'] = seg_labels
|
|
1133
|
+
if agreement_weight is not None:
|
|
1134
|
+
result['agreement_weight'] = agreement_weight
|
|
1099
1135
|
|
|
1100
1136
|
if self.return_boxes:
|
|
1101
1137
|
result['boxes'] = targets.get('boxes', torch.zeros((0, 4), dtype=torch.float32))
|
|
@@ -1103,7 +1139,9 @@ class DatamintBaseDataset(ABC, torch.utils.data.Dataset):
|
|
|
1103
1139
|
|
|
1104
1140
|
# Process image-level labels
|
|
1105
1141
|
result['image_labels'] = self._extract_image_labels(annotations)
|
|
1106
|
-
result['image_categories'] = self._extract_image_categories(annotations)
|
|
1142
|
+
result['image_categories'], category_weight = self._extract_image_categories(annotations)
|
|
1143
|
+
if category_weight is not None:
|
|
1144
|
+
result['agreement_weight'] = category_weight
|
|
1107
1145
|
|
|
1108
1146
|
return result
|
|
1109
1147
|
|
|
@@ -1165,6 +1203,7 @@ class DatamintBaseDataset(ABC, torch.utils.data.Dataset):
|
|
|
1165
1203
|
'return_boxes': self.return_boxes,
|
|
1166
1204
|
'return_as_semantic_segmentation': self.return_as_semantic_segmentation,
|
|
1167
1205
|
'semantic_seg_merge_strategy': str(self.semantic_seg_merge_strategy),
|
|
1206
|
+
'label_weighting': str(self.label_weighting),
|
|
1168
1207
|
'include_unannotated': self.include_unannotated,
|
|
1169
1208
|
'include_annotators': self.include_annotators,
|
|
1170
1209
|
'exclude_annotators': self.exclude_annotators,
|
|
@@ -105,7 +105,7 @@ class SlicedVolumeDataset(DatamintBaseDataset):
|
|
|
105
105
|
_attributes_to_copy = [
|
|
106
106
|
'return_metainfo', 'return_segmentations',
|
|
107
107
|
'return_as_semantic_segmentation', 'semantic_seg_merge_strategy',
|
|
108
|
-
'include_unannotated', 'allow_external_annotations',
|
|
108
|
+
'label_weighting', 'include_unannotated', 'allow_external_annotations',
|
|
109
109
|
'trusted_annotation_sources',
|
|
110
110
|
'image_labels_merge_strategy', 'image_categories_merge_strategy',
|
|
111
111
|
'split_name', 'split_source',
|
|
@@ -491,8 +491,8 @@ class SlicedVolumeDataset(DatamintBaseDataset):
|
|
|
491
491
|
result['image'] = img
|
|
492
492
|
sliced_segs = aug_result.get('masks', {})
|
|
493
493
|
|
|
494
|
-
segmentations_processed, seg_labels_out = self._process_segmentations(
|
|
495
|
-
|
|
494
|
+
segmentations_processed, seg_labels_out, agreement_weight = self._process_segmentations(
|
|
495
|
+
sliced_segs, seg_labels, output_shape=img.shape[1:])
|
|
496
496
|
# remove temporary dummy dimension: (#instances, 1, DIM1, DIM2) -> (#instances, DIM1, DIM2)
|
|
497
497
|
if isinstance(segmentations_processed, (Tensor, np.ndarray)):
|
|
498
498
|
segmentations_processed = segmentations_processed.squeeze(1)
|
|
@@ -504,6 +504,8 @@ class SlicedVolumeDataset(DatamintBaseDataset):
|
|
|
504
504
|
result['masks'] = segmentations_processed
|
|
505
505
|
if seg_labels_out:
|
|
506
506
|
result['mask_labels'] = seg_labels_out
|
|
507
|
+
if agreement_weight is not None:
|
|
508
|
+
result['agreement_weight'] = agreement_weight
|
|
507
509
|
|
|
508
510
|
# Process image-level labels
|
|
509
511
|
result['image_labels'] = self._extract_image_labels(annotations)
|
|
@@ -94,6 +94,7 @@ class SlicedVideoDataset(DatamintBaseDataset):
|
|
|
94
94
|
instance.return_segmentations = parent_dataset.return_segmentations
|
|
95
95
|
instance.return_as_semantic_segmentation = parent_dataset.return_as_semantic_segmentation
|
|
96
96
|
instance.semantic_seg_merge_strategy = parent_dataset.semantic_seg_merge_strategy
|
|
97
|
+
instance.label_weighting = parent_dataset.label_weighting
|
|
97
98
|
instance.include_unannotated = parent_dataset.include_unannotated
|
|
98
99
|
|
|
99
100
|
# Transforms
|
|
@@ -381,9 +382,11 @@ class SlicedVideoDataset(DatamintBaseDataset):
|
|
|
381
382
|
result['image'] = img
|
|
382
383
|
frame_segs = aug_result['segmentations']
|
|
383
384
|
|
|
384
|
-
segmentations_processed, seg_labels_out = self._process_segmentations(
|
|
385
|
+
segmentations_processed, seg_labels_out, agreement_weight = self._process_segmentations(
|
|
385
386
|
frame_segs, seg_labels, output_shape=img.shape[1:],
|
|
386
387
|
)
|
|
388
|
+
if agreement_weight is not None:
|
|
389
|
+
result['agreement_weight'] = agreement_weight
|
|
387
390
|
# Remove dummy depth dimension: (#instances, 1, H, W) -> (#instances, H, W)
|
|
388
391
|
if isinstance(segmentations_processed, (Tensor, np.ndarray)):
|
|
389
392
|
segmentations_processed = segmentations_processed.squeeze(1)
|
|
@@ -83,12 +83,24 @@ class ClassificationModule(DatamintLightningModule):
|
|
|
83
83
|
else:
|
|
84
84
|
return self.criterion(logits, labels).unsqueeze(0).expand(logits.shape[0])
|
|
85
85
|
|
|
86
|
+
def _per_sample_loss(self, logits: Tensor, labels: Tensor) -> Tensor:
|
|
87
|
+
"""Per-sample loss (shape ``(B,)``), calling the criterion once per sample. """
|
|
88
|
+
return torch.stack([self.criterion(logits[i:i + 1], labels[i:i + 1]) for i in range(logits.shape[0])])
|
|
89
|
+
|
|
86
90
|
def _common_step(self, batch: dict, stage: str) -> Tensor:
|
|
87
91
|
images = batch['image']
|
|
88
92
|
labels = batch['image_categories']
|
|
89
93
|
logits = self(images)
|
|
90
94
|
|
|
91
|
-
|
|
95
|
+
# Agreement weights ('label_weighting') only weight the training loss.
|
|
96
|
+
weights = batch.get('agreement_weight') if stage == 'train' else None
|
|
97
|
+
|
|
98
|
+
if weights is not None:
|
|
99
|
+
loss_per_sample = self._per_sample_loss(logits, labels)
|
|
100
|
+
loss = (loss_per_sample * weights.to(loss_per_sample)).mean()
|
|
101
|
+
if self._log_sample_metrics:
|
|
102
|
+
self._accumulate_sample_data(batch, logits, loss_per_sample, stage)
|
|
103
|
+
elif self._log_sample_metrics:
|
|
92
104
|
loss_unreduced = self._compute_unreduced_loss(logits, labels) # (B,)
|
|
93
105
|
loss = loss_unreduced.mean()
|
|
94
106
|
self._accumulate_sample_data(batch, logits, loss_unreduced, stage)
|
|
@@ -70,8 +70,16 @@ class SegmentationModule(DatamintLightningModule):
|
|
|
70
70
|
|
|
71
71
|
logits = self(images)
|
|
72
72
|
|
|
73
|
+
# Agreement weights ('label_weighting') only weight the training loss
|
|
74
|
+
weights = batch.get('agreement_weight') if stage == 'train' else None
|
|
75
|
+
|
|
73
76
|
if self.criterion:
|
|
74
|
-
if
|
|
77
|
+
if weights is not None:
|
|
78
|
+
loss_per_sample = self._per_sample_loss(logits, masks)
|
|
79
|
+
loss = (loss_per_sample * weights.to(loss_per_sample)).mean()
|
|
80
|
+
if self._log_sample_metrics:
|
|
81
|
+
self._accumulate_sample_data(batch, logits, loss_per_sample, stage)
|
|
82
|
+
elif self._log_sample_metrics:
|
|
75
83
|
loss_unreduced = self._compute_unreduced_loss(logits, masks)
|
|
76
84
|
loss = loss_unreduced.mean()
|
|
77
85
|
self._accumulate_sample_data(batch, logits, loss_unreduced, stage)
|
|
@@ -82,7 +90,8 @@ class SegmentationModule(DatamintLightningModule):
|
|
|
82
90
|
|
|
83
91
|
preds = (logits > 0).long()
|
|
84
92
|
|
|
85
|
-
|
|
93
|
+
# Threshold soft targets ('consensus_soft' merge) to the majority vote
|
|
94
|
+
getattr(self, f'{stage}_metrics').update(preds, (masks >= 0.5).long())
|
|
86
95
|
|
|
87
96
|
if loss is not None:
|
|
88
97
|
self.log(
|
|
@@ -92,6 +101,10 @@ class SegmentationModule(DatamintLightningModule):
|
|
|
92
101
|
)
|
|
93
102
|
return loss
|
|
94
103
|
|
|
104
|
+
def _per_sample_loss(self, logits: Tensor, masks: Tensor) -> Tensor:
|
|
105
|
+
"""Per-sample loss (shape ``(B,)``), calling the criterion once per sample. """
|
|
106
|
+
return torch.stack([self.criterion(logits[i:i + 1], masks[i:i + 1]) for i in range(logits.shape[0])])
|
|
107
|
+
|
|
95
108
|
def _compute_unreduced_loss(self, logits: Tensor, masks: Tensor) -> Tensor:
|
|
96
109
|
"""Compute per-sample loss (shape ``(B,)``).
|
|
97
110
|
|
|
@@ -142,7 +155,7 @@ class SegmentationModule(DatamintLightningModule):
|
|
|
142
155
|
|
|
143
156
|
def _compute_sample_metrics(self, logits: Tensor, batch: dict) -> dict[str, Tensor]:
|
|
144
157
|
"""Per-sample IoU and Dice."""
|
|
145
|
-
masks = batch['masks'][:, 1:].float()
|
|
158
|
+
masks = (batch['masks'][:, 1:] >= 0.5).float()
|
|
146
159
|
preds = (logits > 0).float()
|
|
147
160
|
intersection = (preds * masks).sum(dim=[1, 2, 3])
|
|
148
161
|
union = ((preds + masks) > 0).float().sum(dim=[1, 2, 3])
|
|
@@ -546,9 +546,14 @@ class UNETRPPModule(SegmentationModule):
|
|
|
546
546
|
images, masks = self._random_crop_3d(images, masks)
|
|
547
547
|
|
|
548
548
|
logits = self(images)
|
|
549
|
-
|
|
549
|
+
weights = batch.get('agreement_weight')
|
|
550
|
+
if self.criterion and weights is not None:
|
|
551
|
+
loss_per_sample = self._per_sample_loss(logits, masks)
|
|
552
|
+
loss = (loss_per_sample * weights.to(loss_per_sample)).mean()
|
|
553
|
+
else:
|
|
554
|
+
loss = self.criterion(logits, masks) if self.criterion else None
|
|
550
555
|
|
|
551
|
-
self.train_metrics.update((logits > 0).long(), masks.long())
|
|
556
|
+
self.train_metrics.update((logits > 0).long(), (masks >= 0.5).long())
|
|
552
557
|
|
|
553
558
|
if loss is not None:
|
|
554
559
|
self.log('train/loss', loss, on_step=True, on_epoch=True,
|
|
@@ -572,7 +577,7 @@ class UNETRPPModule(SegmentationModule):
|
|
|
572
577
|
|
|
573
578
|
logits = self._sliding_window_inference(images)
|
|
574
579
|
loss = self.criterion(logits, masks) if self.criterion else None
|
|
575
|
-
getattr(self, f'{stage}_metrics').update((logits > 0).long(), masks.long())
|
|
580
|
+
getattr(self, f'{stage}_metrics').update((logits > 0).long(), (masks >= 0.5).long())
|
|
576
581
|
|
|
577
582
|
if loss is not None:
|
|
578
583
|
self.log(f'{stage}/loss', loss, on_epoch=True, prog_bar=True,
|
|
@@ -21,7 +21,7 @@ class _BCEDiceLoss(nn.Module):
|
|
|
21
21
|
|
|
22
22
|
Expects:
|
|
23
23
|
pred: ``(B, C, H, W)`` logits
|
|
24
|
-
target: ``(B, C, H, W)``
|
|
24
|
+
target: ``(B, C, H, W)`` masks in [0, 1] (float). Soft targets allowed
|
|
25
25
|
"""
|
|
26
26
|
|
|
27
27
|
def forward(self, pred: torch.Tensor, target: torch.Tensor, reduction: str = 'mean') -> torch.Tensor:
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "datamint"
|
|
3
3
|
description = "A library for interacting with the Datamint API, designed for efficient data management, processing and Deep Learning workflows."
|
|
4
|
-
version = "2.
|
|
4
|
+
version = "2.46.0"
|
|
5
5
|
dynamic = ["dependencies"]
|
|
6
6
|
requires-python = ">=3.10"
|
|
7
7
|
readme = "README.md"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|