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

refactor: Cleaned rotation transforms #536

Merged
merged 8 commits into from
Oct 22, 2021
Merged

refactor: Cleaned rotation transforms #536

merged 8 commits into from
Oct 22, 2021

Conversation

fg-mindee
Copy link
Contributor

This PR introduces the following modifications:

  • updates interface of RandomRotate to directly take boxes as target and not a dict
  • cleans the codebase to properly handle the expand option
  • updates unittests
  • added illustration in the documentation

Please note that using expand=True changes the size of the output image.

Now, the following snippet:

import cv2
import numpy as np
import matplotlib.pyplot as plt
from doctr.file_utils import is_tf_available
from doctr.datasets import FUNSD
from doctr.transforms.functional import rotate

# Get some data
ds = FUNSD()
img, target = ds[0]
# Perform rotation
rot_img, rot_boxes = rotate(img, target['boxes'], 30)
rotbis_img, rotbis_boxes = rotate(img, target['boxes'], 30, expand=True)

# Create binary map for visualization
init_target = np.zeros(img.shape[:2] if is_tf_available() else img.shape[-2:], dtype=bool)
for box in target['boxes']:
    init_target[box[1]: box[3], box[0]: box[2]] = True

new_target = np.zeros(rot_img.shape[:2] if is_tf_available() else rot_img.shape[-2:], dtype=np.uint8)

for box in rot_boxes:
    box = cv2.boxPoints(((int(box[0]), int(box[1])), (int(box[2]), int(box[3])), -box[4]))
    box = np.int0(box)
    cv2.fillPoly(new_target, [box], 1)

exp_target = np.zeros(rotbis_img.shape[:2] if is_tf_available() else rotbis_img.shape[-2:], dtype=np.uint8)

for box in rotbis_boxes:
    box = cv2.boxPoints(((int(box[0]), int(box[1])), (int(box[2]), int(box[3])), -box[4]))
    box = np.int0(box)
    cv2.fillPoly(exp_target, [box], 1)

# Make it work for PyTorch as well
if not is_tf_available():
    img = img.permute(1, 2, 0)
    rot_img = rot_img.permute(1, 2, 0)
    rotbis_img = rotbis_img.permute(1, 2, 0)

# Plot everything
_, axes = plt.subplots(2, 3)
axes[0, 0].imshow(img)
axes[0, 0].set_title('Original image')
axes[0, 1].imshow(rot_img)
axes[0, 1].set_title('Rotated image (expand=False)')
axes[0, 2].imshow(rotbis_img)
axes[0, 2].set_title('Rotated image (expand=True)')
axes[1, 0].imshow(init_target)
axes[1, 1].imshow(new_target)
axes[1, 2].imshow(exp_target)

for axs in axes:
    for ax in axs:
        ax.axis('off')

plt.show()

yields:
new_rotation
for both Pytorch & TF

The documentation of RandomRotate will now render as follows:

Screenshot from 2021-10-22 12-30-38

Any feedback is welcome!

@fg-mindee fg-mindee added topic: documentation Improvements or additions to documentation type: enhancement Improvement module: utils Related to doctr.utils ext: tests Related to tests folder module: transforms Related to doctr.transforms framework: pytorch Related to PyTorch backend framework: tensorflow Related to TensorFlow backend labels Oct 22, 2021
@fg-mindee fg-mindee added this to the 0.5.0 milestone Oct 22, 2021
@fg-mindee fg-mindee self-assigned this Oct 22, 2021
charlesmindee
charlesmindee previously approved these changes Oct 22, 2021
Copy link
Collaborator

@charlesmindee charlesmindee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks !

@codecov
Copy link

codecov bot commented Oct 22, 2021

Codecov Report

Merging #536 (13e6b3b) into main (323d484) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main     #536   +/-   ##
=======================================
  Coverage   95.38%   95.38%           
=======================================
  Files         109      109           
  Lines        4182     4182           
=======================================
  Hits         3989     3989           
  Misses        193      193           
Flag Coverage Δ
unittests 95.38% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
doctr/transforms/functional/pytorch.py 100.00% <100.00%> (ø)
doctr/transforms/functional/tensorflow.py 100.00% <100.00%> (ø)
doctr/transforms/modules/base.py 91.22% <100.00%> (ø)
doctr/utils/geometry.py 98.78% <100.00%> (+0.09%) ⬆️
...dels/detection/differentiable_binarization/base.py 91.19% <0.00%> (-0.63%) ⬇️
doctr/models/builder.py 93.75% <0.00%> (+1.04%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 323d484...13e6b3b. Read the comment docs.

Copy link
Collaborator

@charlesmindee charlesmindee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ext: tests Related to tests folder framework: pytorch Related to PyTorch backend framework: tensorflow Related to TensorFlow backend module: transforms Related to doctr.transforms module: utils Related to doctr.utils topic: documentation Improvements or additions to documentation type: enhancement Improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants