Skip to content
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

feat: Added box coord clipping and target validation for detection tasks #355

Merged
merged 3 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doctr/datasets/detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ def __getitem__(
boxes[..., [0, 2]] /= w
boxes[..., [1, 3]] /= h

return img, dict(boxes=boxes, flags=target['flags'])
return img, dict(boxes=boxes.clip(0, 1), flags=target['flags'])
5 changes: 5 additions & 0 deletions doctr/models/detection/differentiable_binarization/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ def compute_target(
output_shape: Tuple[int, int, int],
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:

if any(t['boxes'].dtype != np.float32 for t in target):
raise AssertionError("the 'boxes' entry of the target is expected to have dtype 'np.float32'.")
if any(np.any((t['boxes'][:, :4] > 1) | (t['boxes'][:, :4] < 0)) for t in target):
raise ValueError("the 'boxes' entry of the target is expected to take values between 0 & 1.")

seg_target = np.zeros(output_shape, dtype=np.uint8)
seg_mask = np.ones(output_shape, dtype=bool)
thresh_target = np.zeros(output_shape, dtype=np.uint8)
Expand Down
5 changes: 5 additions & 0 deletions doctr/models/detection/linknet/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ def compute_target(
output_shape: Tuple[int, int, int],
) -> Tuple[np.ndarray, np.ndarray, np.ndarray]:

if any(t['boxes'].dtype != np.float32 for t in target):
raise AssertionError("the 'boxes' entry of the target is expected to have dtype 'np.float32'.")
if any(np.any((t['boxes'][:, :4] > 1) | (t['boxes'][:, :4] < 0)) for t in target):
raise ValueError("the 'boxes' entry of the target is expected to take values between 0 & 1.")

if self.rotated_bbox:
seg_target = np.zeros(output_shape, dtype=np.uint8)
else:
Expand Down
14 changes: 14 additions & 0 deletions test/tensorflow/test_models_detection_tf.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ def test_detection_models(arch_name, input_shape, output_size, out_prob):
assert np.all(boxes[:, :4] >= 0) and np.all(boxes[:, :4] <= 1)
# Check loss
assert isinstance(out['loss'], tf.Tensor)
# Target checks
target = [
dict(boxes=np.array([[0, 0, 1, 1]], dtype=np.uint8), flags=[True, False]),
dict(boxes=np.array([[0, 0, 1, 1]], dtype=np.uint8), flags=[True, False])
]
with pytest.raises(AssertionError):
out = model(input_tensor, target, training=True)

target = [
dict(boxes=np.array([[0, 0, 1.5, 1.5]], dtype=np.float32), flags=[True, False]),
dict(boxes=np.array([[-.2, -.3, 1, 1]], dtype=np.float32), flags=[True, False])
]
with pytest.raises(ValueError):
out = model(input_tensor, target, training=True)


@pytest.fixture(scope="session")
Expand Down