-
Notifications
You must be signed in to change notification settings - Fork 274
Redesign batch construction and schema #1501
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
Open
fepegar
wants to merge
12
commits into
main
Choose a base branch
from
fepegar/batch-redesign-core
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
7004446
Fix padding mode type narrowing
fepegar e1294d2
Redesign batch construction and schema
fepegar 915b0e9
Preserve per-image history in batch round-trips
fepegar 53b09cb
Allow skipped spatial transforms with annotations
fepegar 3ad8ed6
Correct spatial annotation contract
fepegar 94e10d4
Preserve bounding box label dtype on transfer
fepegar 117e3e3
Normalize lazy and loaded image dtypes
fepegar 660dda1
Place default affines on the batch device
fepegar 017b05f
Clarify point affine transfer semantics
fepegar 4914b84
Clarify bounding box transfer semantics
fepegar c80ccaf
Clarify image affine transfer semantics
fepegar a40aeac
Clarify image annotation dtype transfers
fepegar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| """Schemas used to validate image and subject batches.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from dataclasses import dataclass | ||
| from typing import Any | ||
|
|
||
| import torch | ||
|
|
||
| from .bboxes import BoundingBoxes | ||
| from .image import Image | ||
| from .points import Points | ||
| from .subject import Subject | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class _AnnotationSchema: | ||
| """Describe one named annotation field.""" | ||
|
|
||
| value_type: type[Points] | type[BoundingBoxes] | ||
| metadata_keys: tuple[str, ...] | ||
|
|
||
| @classmethod | ||
| def from_value(cls, value: Points | BoundingBoxes) -> _AnnotationSchema: | ||
| """Build a schema from one annotation value.""" | ||
| return cls(type(value), tuple(value.metadata)) | ||
|
|
||
| def validate( | ||
| self, | ||
| value: Points | BoundingBoxes, | ||
| *, | ||
| index: int, | ||
| context: str, | ||
| ) -> None: | ||
| """Validate one annotation against this schema.""" | ||
| if type(value) is not self.value_type: | ||
| msg = ( | ||
| f"{context} at index {index} has type {type(value).__name__}," | ||
| f" expected {self.value_type.__name__}" | ||
| ) | ||
| raise ValueError(msg) | ||
| _validate_keys( | ||
| self.metadata_keys, | ||
| value.metadata, | ||
| index=index, | ||
| context=f"{context} metadata", | ||
| ) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class _ImageSchema: | ||
| """Describe one named image field.""" | ||
|
|
||
| value_type: type[Image] | ||
| shape: tuple[int, ...] | ||
| dtype: str | ||
| device: torch.device | ||
| metadata_keys: tuple[str, ...] | ||
| points: dict[str, _AnnotationSchema] | ||
| bounding_boxes: dict[str, _AnnotationSchema] | ||
|
|
||
| @classmethod | ||
| def from_image(cls, image: Image) -> _ImageSchema: | ||
| """Build a schema from one image.""" | ||
| return cls( | ||
| value_type=type(image), | ||
| shape=tuple(image.shape), | ||
| dtype=_normalize_dtype(image.dtype), | ||
| device=image.device, | ||
| metadata_keys=tuple(image.metadata), | ||
| points={ | ||
| name: _AnnotationSchema.from_value(value) | ||
| for name, value in image.points.items() | ||
| }, | ||
| bounding_boxes={ | ||
| name: _AnnotationSchema.from_value(value) | ||
| for name, value in image.bounding_boxes.items() | ||
| }, | ||
| ) | ||
|
|
||
| def validate(self, image: Image, *, index: int, name: str) -> None: | ||
| """Validate one image against this schema.""" | ||
| context = f"Image {name!r}" | ||
| if type(image) is not self.value_type: | ||
| msg = ( | ||
| f"{context} at index {index} has type {type(image).__name__}," | ||
| f" expected {self.value_type.__name__}" | ||
| ) | ||
| raise ValueError(msg) | ||
| for attribute in ("shape", "device"): | ||
| expected = getattr(self, attribute) | ||
| actual = getattr(image, attribute) | ||
| if actual != expected: | ||
| msg = ( | ||
| f"{context} at index {index} has {attribute} {actual}," | ||
| f" expected {expected}" | ||
| ) | ||
| raise ValueError(msg) | ||
| actual_dtype = _normalize_dtype(image.dtype) | ||
| if actual_dtype != self.dtype: | ||
| msg = ( | ||
| f"{context} at index {index} has dtype {actual_dtype}," | ||
| f" expected {self.dtype}" | ||
| ) | ||
| raise ValueError(msg) | ||
| _validate_keys( | ||
| self.metadata_keys, | ||
| image.metadata, | ||
| index=index, | ||
| context=f"{context} metadata", | ||
| ) | ||
| _validate_annotations( | ||
| self.points, | ||
| image.points, | ||
| index=index, | ||
| context=f"{context} points", | ||
| ) | ||
| _validate_annotations( | ||
| self.bounding_boxes, | ||
| image.bounding_boxes, | ||
| index=index, | ||
| context=f"{context} bounding boxes", | ||
| ) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class _SubjectSchema: | ||
| """Describe the fields shared by all subjects in a batch.""" | ||
|
|
||
| images: dict[str, _ImageSchema] | ||
| metadata_keys: tuple[str, ...] | ||
| points: dict[str, _AnnotationSchema] | ||
| bounding_boxes: dict[str, _AnnotationSchema] | ||
|
|
||
| @classmethod | ||
| def from_subject(cls, subject: Subject) -> _SubjectSchema: | ||
| """Build a schema from the first subject in a batch.""" | ||
| return cls( | ||
| images={ | ||
| name: _ImageSchema.from_image(image) | ||
| for name, image in subject.images.items() | ||
| }, | ||
| metadata_keys=tuple(subject.metadata), | ||
| points={ | ||
| name: _AnnotationSchema.from_value(value) | ||
| for name, value in subject.points.items() | ||
| }, | ||
| bounding_boxes={ | ||
| name: _AnnotationSchema.from_value(value) | ||
| for name, value in subject.bounding_boxes.items() | ||
| }, | ||
| ) | ||
|
|
||
| def validate(self, subject: Subject, *, index: int) -> None: | ||
| """Validate one subject against this schema.""" | ||
| _validate_keys( | ||
| self.images, subject.images, index=index, context="Subject images" | ||
| ) | ||
| _validate_keys( | ||
| self.metadata_keys, | ||
| subject.metadata, | ||
| index=index, | ||
| context="Subject metadata", | ||
| ) | ||
| _validate_annotations( | ||
| self.points, | ||
| subject.points, | ||
| index=index, | ||
| context="Subject points", | ||
| ) | ||
| _validate_annotations( | ||
| self.bounding_boxes, | ||
| subject.bounding_boxes, | ||
| index=index, | ||
| context="Subject bounding boxes", | ||
| ) | ||
| for name, schema in self.images.items(): | ||
| schema.validate(subject.images[name], index=index, name=name) | ||
|
|
||
|
|
||
| def _validate_annotations( | ||
| reference: dict[str, _AnnotationSchema], | ||
| current: dict[str, Points] | dict[str, BoundingBoxes], | ||
| *, | ||
| index: int, | ||
| context: str, | ||
| ) -> None: | ||
| """Validate a named annotation store.""" | ||
| _validate_keys(reference, current, index=index, context=context) | ||
| for name, schema in reference.items(): | ||
| schema.validate(current[name], index=index, context=f"{context} {name!r}") | ||
|
|
||
|
|
||
| def _validate_keys( | ||
| reference: Any, | ||
| current: Any, | ||
| *, | ||
| index: int, | ||
| context: str, | ||
| ) -> None: | ||
| """Validate equivalent key sets while allowing reordered keys.""" | ||
| reference_set = set(reference) | ||
| current_set = set(current) | ||
| if reference_set == current_set: | ||
| return | ||
| missing = sorted(reference_set - current_set) | ||
| unexpected = sorted(current_set - reference_set) | ||
| msg = ( | ||
| f"{context} at index {index} has incompatible keys:" | ||
| f" missing {missing}, unexpected {unexpected}" | ||
| ) | ||
| raise ValueError(msg) | ||
|
|
||
|
|
||
| def _normalize_dtype(dtype: Any) -> str: | ||
| """Return one comparable dtype name for Torch and NumPy dtypes.""" | ||
| return str(dtype).removeprefix("torch.") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.