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

Rotate page #488

Merged
merged 66 commits into from
Dec 6, 2021
Merged

Rotate page #488

merged 66 commits into from
Dec 6, 2021

Conversation

Rob192
Copy link
Contributor

@Rob192 Rob192 commented Sep 22, 2021

This is part of the solutions discussed in #225 and address the point "Integrate the feature in the predictor while being disabled by default" of the "Page-level orientation" part.

It implements the function rotate_page and tracks the 'page_angles'

@codecov
Copy link

codecov bot commented Sep 22, 2021

Codecov Report

Merging #488 (66fad67) into main (e14645a) will increase coverage by 0.17%.
The diff coverage is 96.29%.

Impacted file tree graph

@@            Coverage Diff             @@
##             main     #488      +/-   ##
==========================================
+ Coverage   96.15%   96.32%   +0.17%     
==========================================
  Files         114      117       +3     
  Lines        4447     4523      +76     
==========================================
+ Hits         4276     4357      +81     
+ Misses        171      166       -5     
Flag Coverage Δ
unittests 96.32% <96.29%> (+0.17%) ⬆️

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

Impacted Files Coverage Δ
doctr/utils/visualization.py 92.03% <0.00%> (ø)
doctr/utils/geometry.py 98.00% <96.66%> (-0.79%) ⬇️
doctr/models/_utils.py 95.34% <100.00%> (+1.30%) ⬆️
doctr/models/predictor/pytorch.py 100.00% <100.00%> (ø)
doctr/models/predictor/tensorflow.py 100.00% <100.00%> (ø)
doctr/utils/common_types.py 100.00% <100.00%> (ø)
...dels/detection/differentiable_binarization/base.py 89.44% <0.00%> (-2.49%) ⬇️
doctr/datasets/cord.py 97.43% <0.00%> (ø)
doctr/datasets/funsd.py 96.87% <0.00%> (ø)
doctr/datasets/sroie.py 94.87% <0.00%> (ø)
... and 17 more

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 e14645a...66fad67. Read the comment docs.

Copy link
Contributor

@fg-mindee fg-mindee left a comment

Choose a reason for hiding this comment

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

Thanks a lot to jump back into this 🙏

I added a few comments but for me, there is only one major part missing:

  • with this PR, we rotate the page firsthand before any DL model forward
  • the process is almost identical, but the localizations of words have to be remapped into the original pages (meaning if we detect that the page has a 30° orientation, we forward its straightened version, if we detect a straight bounding box in this, we have to reproject it correctly so that the localization refers to the original page)

Let me know what you think :)

Dockerfile Outdated Show resolved Hide resolved
doctr/models/_utils.py Outdated Show resolved Hide resolved
doctr/models/predictor/tensorflow.py Outdated Show resolved Hide resolved
doctr/models/predictor/tensorflow.py Outdated Show resolved Hide resolved
@fg-mindee fg-mindee self-assigned this Sep 24, 2021
@fg-mindee fg-mindee added the module: models Related to doctr.models label Sep 24, 2021
@fg-mindee fg-mindee added this to the 0.5.0 milestone Sep 24, 2021
@fg-mindee fg-mindee added the type: enhancement Improvement label Sep 24, 2021
@fg-mindee
Copy link
Contributor

@Rob192 also, if you don't minde merging the main branch, there seems to be some conflicts 😅

@Rob192
Copy link
Contributor Author

Rob192 commented Sep 30, 2021

@Rob192 also, if you don't minde merging the main branch, there seems to be some conflicts 😅
Yes ! you guys are mooving too fast for me 😮

@Rob192
Copy link
Contributor Author

Rob192 commented Sep 30, 2021

Hello,

Thanks for the review ! My objective is now to use geometry.rotate_image to rotate the documents before going into the predictor and then use the same function to rotate every box, compare to the center of the document previously used.

However, I am feeling a bit confused regarding the expand functionnality you added in several functions of Doctr. I hoped you could help me clarify. I think you goal was to do the same for every function, i.e. given a image of shape (10,20), the rotation by an angle of 90° with expand=True would give a result image of shape (20,10). This is not the case for all the function in utils.geometry :

import numpy as np
import matplotlib.pyplot as plt

import torch
import tensorflow as tf

from doctr.utils import geometry
from doctr.transforms.functional.tensorflow import rotate as rotate_tf
from doctr.transforms.functional.pytorch import rotate as rotate_to

_, axes = plt.subplots(3, 1)

img = np.ones((32, 64, 3), dtype=np.float32)
rotated = geometry.rotate_image(img, 45., expand=True)
print(rotated.shape)
axes[0].imshow(rotated)

input_t = torch.ones((3, 32, 64), dtype=torch.float32)
boxes = np.array([
    [15, 20, 35, 30]
])
r_img, r_boxes = rotate_to(input_t, boxes, angle=45., expand=True)
print(r_img.shape)
axes[1].imshow(r_img.permute((1,2,0)))

input_t = tf.ones((32, 64, 3), dtype=tf.float32)
boxes = np.array([
    [15, 20, 35, 30]
])
r_img, r_boxes = rotate_tf(input_t, boxes, angle=45., expand=True)
print(r_img.shape)
axes[2].imshow(r_img)

Do you think I could modify geometry.rotate_image so that expand=True has the same behaviour as the two other functions in doctr.transforms.functional ? More precisely I would add a expand_type parameter that could take values in ['crop', 'keep_original_shape', 'keep_destination_shape']

Copy link
Contributor

@fg-mindee fg-mindee left a comment

Choose a reason for hiding this comment

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

Thanks for the edits! Regarding your question about rotate_image, it's still an ongoing implementation so we can add optional features in it. But to keep the PR short, perhaps this should be implemented in the predictor directly? (we can refactor in another PR once we get this working)

Dockerfile Outdated Show resolved Hide resolved
doctr/models/predictor/tensorflow.py Outdated Show resolved Hide resolved
doctr/models/predictor/tensorflow.py Show resolved Hide resolved
test/common/test_models.py Outdated Show resolved Hide resolved
@Rob192
Copy link
Contributor Author

Rob192 commented Oct 4, 2021

Hello @fg-mindee !
Here are my modifications to remap the localizations of the words back into the original pages. Here are some details of the choices I did :

  • I created a new rotate_image function inside doctr/models/_utils.py per your recommandation. I removed the last bit of the function to keep the size of the original image. I introduced a mask functionality to crop the padding at the end of the + then - rotation.
  • I had to modify rotate_boxes to follow the expand that is done in rotate_image. The calculation for the rotation of the boxes was not right and I fixed that. There is also a mask functionality in rotate_boxes
    I hope this makes sense to you guys

Copy link
Contributor

@fg-mindee fg-mindee left a comment

Choose a reason for hiding this comment

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

Thanks again Rob!
I think there are still a few edges cases we are not covering but this is taking a very solid shape!

doctr/models/_utils.py Outdated Show resolved Hide resolved
doctr/utils/visualization.py Show resolved Hide resolved
doctr/models/predictor/tensorflow.py Outdated Show resolved Hide resolved
doctr/utils/geometry.py Outdated Show resolved Hide resolved
Copy link
Contributor

@fg-mindee fg-mindee left a comment

Choose a reason for hiding this comment

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

Still have a few comments, but the major one being about the reprojection of rotated bounding boxes

doctr/utils/geometry.py Outdated Show resolved Hide resolved
doctr/utils/geometry.py Outdated Show resolved Hide resolved
doctr/utils/geometry.py Outdated Show resolved Hide resolved
doctr/models/zoo.py Outdated Show resolved Hide resolved
Copy link
Contributor

@fg-mindee fg-mindee left a comment

Choose a reason for hiding this comment

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

There is one merge conflict to resolve, minor tweaks in the unittest and we can merge!
Sorry about this 🙃

tests/pytorch/test_models_detection_pt.py Outdated Show resolved Hide resolved
tests/pytorch/test_models_recognition_pt.py Outdated Show resolved Hide resolved
tests/pytorch/test_models_zoo_pt.py Outdated Show resolved Hide resolved
tests/pytorch/test_models_zoo_pt.py Outdated Show resolved Hide resolved
tests/pytorch/test_models_zoo_pt.py Outdated Show resolved Hide resolved
@Rob192
Copy link
Contributor Author

Rob192 commented Dec 2, 2021

OK @fg-mindee ! Now I got it for the testing !

Copy link
Contributor

@fg-mindee fg-mindee left a comment

Choose a reason for hiding this comment

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

Looks good to me, thanks a lot @Rob192 ! @charlesmindee would you mind taking a look at it as well before we merge? 🙏

charlesmindee
charlesmindee previously approved these changes Dec 3, 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 for the PR !

Copy link
Contributor

@fg-mindee fg-mindee left a comment

Choose a reason for hiding this comment

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

Sorry Robin, a minor thing I missed in my last review to adjust 😅
That shouldn't change much but especially on this feature it's important to have a good understanding of each argument influence on the final output!

doctr/utils/geometry.py Outdated Show resolved Hide resolved
doctr/utils/geometry.py Show resolved Hide resolved
Copy link
Contributor

@fg-mindee fg-mindee left a comment

Choose a reason for hiding this comment

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

There seems to be a failing test: you might have to pass preserve_origin_shape=True where I added the comment 👍

doctr/utils/geometry.py Outdated Show resolved Hide resolved
doctr/models/predictor/pytorch.py Show resolved Hide resolved
Copy link
Contributor

@fg-mindee fg-mindee left a comment

Choose a reason for hiding this comment

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

Making that last argument non-optional and we're good!

doctr/utils/geometry.py Outdated Show resolved Hide resolved
doctr/utils/geometry.py Outdated Show resolved Hide resolved
Copy link
Contributor

@fg-mindee fg-mindee left a comment

Choose a reason for hiding this comment

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

All good 🙌 Thanks a lot!

@Rob192
Copy link
Contributor Author

Rob192 commented Dec 6, 2021

Yeah ! Thanks a lot for your support in reviewing this !

@fg-mindee fg-mindee merged commit 8382896 into mindee:main Dec 6, 2021
@fg-mindee fg-mindee added ext: tests Related to tests folder module: utils Related to doctr.utils type: new feature New feature and removed type: enhancement Improvement labels Dec 31, 2021
@Rob192 Rob192 deleted the rotate_page branch January 11, 2022 13:11
@Rob192 Rob192 mentioned this pull request Jan 25, 2022
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 module: models Related to doctr.models module: utils Related to doctr.utils type: new feature New feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants