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

Detection Model TextNet (TF+PT) #1292

Closed
wants to merge 44 commits into from
Closed

Detection Model TextNet (TF+PT) #1292

wants to merge 44 commits into from

Conversation

nikokks
Copy link
Contributor

@nikokks nikokks commented Aug 29, 2023

make test-torch
USE_TORCH='1' coverage run -m pytest tests/pytorch/
=========================================================================================== test session starts ===========================================================================================
platform linux -- Python 3.8.10, pytest-5.4.1, py-1.11.0, pluggy-0.13.1
rootdir: /home/nikkokks/Desktop/github/doctr_FAST
plugins: hydra-core-1.2.0, typeguard-2.13.3
collected 216 items                                                                                                                                                                                       

tests/pytorch/test_datasets_pt.py ...................................................                                                                                                               [ 23%]
tests/pytorch/test_file_utils_pt.py .                                                                                                                                                               [ 24%]
tests/pytorch/test_io_image_pt.py ...                                                                                                                                                               [ 25%]
tests/pytorch/test_models_classification_pt.py ................................                                                                                                                     [ 40%]
tests/pytorch/test_models_detection_pt.py ........................                                                                                                                                  [ 51%]
tests/pytorch/test_models_factory.py ..........................                                                                                                                                     [ 63%]
tests/pytorch/test_models_obj_detection_pt.py ..                                                                                                                                                    [ 64%]
tests/pytorch/test_models_preprocessor_pt.py ..........                                                                                                                                             [ 68%]
tests/pytorch/test_models_recognition_pt.py ................................ss..                                                                                                                    [ 85%]
tests/pytorch/test_models_utils_pt.py ....                                                                                                                                                          [ 87%]
tests/pytorch/test_models_zoo_pt.py .........                                                                                                                                                       [ 91%]
tests/pytorch/test_transforms_pt.py ..................                                                                                                                                              [100%]

============================================================================================ warnings summary =============================================================================================
tests/pytorch/test_models_classification_pt.py::test_models_onnx_export[vit_b-input_shape10-output_size10]
tests/pytorch/test_models_recognition_pt.py::test_models_onnx_export[vitstr_small-input_shape5]
tests/pytorch/test_models_recognition_pt.py::test_models_onnx_export[parseq-input_shape6]
  /home/nikkokks/Desktop/github/doctr_FAST/doctr/models/modules/vision_transformer/pytorch.py:71: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
    assert H % self.patch_size[0] == 0, "Image height must be divisible by patch height"

tests/pytorch/test_models_classification_pt.py::test_models_onnx_export[vit_b-input_shape10-output_size10]
tests/pytorch/test_models_recognition_pt.py::test_models_onnx_export[vitstr_small-input_shape5]
tests/pytorch/test_models_recognition_pt.py::test_models_onnx_export[parseq-input_shape6]
  /home/nikkokks/Desktop/github/doctr_FAST/doctr/models/modules/vision_transformer/pytorch.py:72: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
    assert W % self.patch_size[1] == 0, "Image width must be divisible by patch width"

tests/pytorch/test_models_classification_pt.py::test_models_onnx_export[vit_b-input_shape10-output_size10]
  /home/nikkokks/Desktop/github/doctr_FAST/doctr/models/modules/vision_transformer/pytorch.py:44: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
    if num_patches == num_positions and height == width:

tests/pytorch/test_models_classification_pt.py::test_models_onnx_export[vit_b-input_shape10-output_size10]
tests/pytorch/test_models_recognition_pt.py::test_models_onnx_export[vitstr_small-input_shape5]
tests/pytorch/test_models_recognition_pt.py::test_models_onnx_export[parseq-input_shape6]
  /home/nikkokks/Desktop/github/doctr_FAST/doctr/models/modules/transformer/pytorch.py:49: TracerWarning: Converting a tensor to a Python float might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
    scores = torch.matmul(query, key.transpose(-2, -1)) / math.sqrt(query.size(-1))

tests/pytorch/test_models_detection_pt.py: 12 tests with warnings
  /home/nikkokks/Desktop/github/doctr_FAST/doctr/models/detection/differentiable_binarization/base.py:197: RuntimeWarning: invalid value encountered in sqrt
    result = np.sqrt(square_dist_1 * square_dist_2 * square_sin / square_dist)

tests/pytorch/test_models_preprocessor_pt.py: 5 tests with warnings
tests/pytorch/test_models_recognition_pt.py: 8 tests with warnings
  /home/nikkokks/.local/lib/python3.8/site-packages/torchvision/transforms/functional.py:1603: UserWarning: The default value of the antialias parameter of all the resizing transforms (Resize(), RandomResizedCrop(), etc.) will change from None to True in v0.17, in order to be consistent across the PIL and Tensor backends. To suppress this warning, directly pass antialias=True (recommended, future default), antialias=None (current default, which means False for Tensors and True for PIL), or antialias=False (only works on Tensors - PIL will still use antialiasing). This also applies if you are using the inference transforms from the models weights: update the call to weights.transforms(antialias=True).
    warnings.warn(

tests/pytorch/test_models_recognition_pt.py::test_models_onnx_export[crnn_vgg16_bn-input_shape0]
tests/pytorch/test_models_recognition_pt.py::test_models_onnx_export[crnn_mobilenet_v3_small-input_shape1]
tests/pytorch/test_models_recognition_pt.py::test_models_onnx_export[crnn_mobilenet_v3_large-input_shape2]
  /home/nikkokks/.local/lib/python3.8/site-packages/torch/onnx/symbolic_opset9.py:4476: UserWarning: Exporting a model to ONNX with a batch_size other than 1, with a variable length with LSTM can cause an error when running the ONNX model with a different batch size. Make sure to save the model with a batch size of 1, or define the initial states (h0/c0) as inputs of the model. 
    warnings.warn(

tests/pytorch/test_models_recognition_pt.py::test_models_onnx_export[parseq-input_shape6]
  /home/nikkokks/Desktop/github/doctr_FAST/doctr/models/recognition/parseq/pytorch.py:283: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!
    if max_len is None and (ys == self.vocab_size).any(dim=-1).all():

tests/pytorch/test_models_recognition_pt.py::test_models_onnx_export[parseq-input_shape6]
  /home/nikkokks/.local/lib/python3.8/site-packages/torch/onnx/symbolic_opset9.py:3825: DeprecationWarning: an integer is required (got type float).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python.
    value_t=torch.tensor([fill_value], dtype=scalar_type.dtype()),

-- Docs: https://docs.pytest.org/en/latest/warnings.html
========================================================================= 214 passed, 2 skipped, 40 warnings in 533.02s (0:08:53) =========================================================================
 make test-tf
USE_TF='1' coverage run -m pytest tests/tensorflow/
=========================================================================================== test session starts ===========================================================================================
platform linux -- Python 3.8.10, pytest-5.4.1, py-1.11.0, pluggy-0.13.1
rootdir: /home/nikkokks/Desktop/github/doctr_FAST
plugins: hydra-core-1.2.0, typeguard-2.13.3
collected 222 items                                                                                                                                                                                       

tests/tensorflow/test_datasets_loader_tf.py .                                                                                                                                                       [  0%]
tests/tensorflow/test_datasets_tf.py ...................................................                                                                                                            [ 23%]
tests/tensorflow/test_file_utils_tf.py .                                                                                                                                                            [ 23%]
tests/tensorflow/test_io_image_tf.py ...                                                                                                                                                            [ 25%]
tests/tensorflow/test_models_classification_tf.py ......................ssssss...                                                                                                                   [ 39%]
tests/tensorflow/test_models_detection_tf.py ..................sss                                                                                                                                  [ 48%]
tests/tensorflow/test_models_factory.py ......................                                                                                                                                      [ 58%]
tests/tensorflow/test_models_preprocessor_tf.py ........                                                                                                                                            [ 62%]
tests/tensorflow/test_models_recognition_tf.py .....................................sss                                                                                                             [ 80%]
tests/tensorflow/test_models_utils_tf.py ....                                                                                                                                                       [ 81%]
tests/tensorflow/test_models_zoo_tf.py .........                                                                                                                                                    [ 86%]
tests/tensorflow/test_transforms_tf.py ...............................                                                                                                                              [100%]

============================================================================================ warnings summary =============================================================================================
tests/tensorflow/test_models_zoo_tf.py::test_ocrpredictor[False-False]
tests/tensorflow/test_models_zoo_tf.py::test_kiepredictor[False-False]
  /home/nikkokks/Desktop/github/doctr_FAST/doctr/utils/geometry.py:286: RuntimeWarning: divide by zero encountered in true_divide
    return float(np.median(np.arctan((yleft - yright) / (xright - xleft))) * 180 / np.pi)  # Y axis from top to bottom!

-- Docs: https://docs.pytest.org/en/latest/warnings.html
======================================================================== 210 passed, 12 skipped, 2 warnings in 1302.87s (0:21:42) =========================================================================

@felixdittrich92 felixdittrich92 self-assigned this Aug 30, 2023
@felixdittrich92 felixdittrich92 added module: models Related to doctr.models framework: pytorch Related to PyTorch backend framework: tensorflow Related to TensorFlow backend topic: character classification Related to the task of character classification type: new feature New feature labels Aug 30, 2023
@felixdittrich92 felixdittrich92 added this to the 0.7.0 milestone Aug 30, 2023
Copy link
Contributor

@felixdittrich92 felixdittrich92 left a comment

Choose a reason for hiding this comment

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

Hi @nikokks 👋,

Thanks a lot for starting with this looks pretty good for a first draft 👍

I left some comments :)

BTW: To fix the style issues you can run mmake style and to check the mypy issues make quality to fix them afterwards manually

Documentation: you can add the models in:
https://github.com/mindee/doctr/blob/main/docs/source/modules/models.rst

doctr/models/classification/__init__.py Outdated Show resolved Hide resolved
doctr/models/classification/textnetFast/pytorch.py Outdated Show resolved Hide resolved
doctr/models/classification/textnetFast/pytorch.py Outdated Show resolved Hide resolved
doctr/models/classification/textnetFast/pytorch.py Outdated Show resolved Hide resolved
doctr/models/classification/textnetFast/pytorch.py Outdated Show resolved Hide resolved
doctr/models/classification/textnetFast/tensorflow.py Outdated Show resolved Hide resolved
doctr/models/modules/layers/pytorch.py Outdated Show resolved Hide resolved
doctr/models/modules/layers/tensorflow.py Outdated Show resolved Hide resolved
doctr/models/utils/tensorflow.py Outdated Show resolved Hide resolved
tests/tensorflow/test_models_classification_tf.py Outdated Show resolved Hide resolved
@nikokks
Copy link
Contributor Author

nikokks commented Aug 30, 2023

make style
isort .
Skipped 44 files
black .
reformatted /home/nikkokks/Desktop/github/doctr_FAST/doctr/models/classification/textnet_fast/tensorflow.py

All done! ✨ 🍰 ✨
1 file reformatted, 251 files left unchanged.
ruff --fix .

make quality
isort . -c
Skipped 44 files
ruff check .
black --check .
All done! ✨ 🍰 ✨
252 files would be left unchanged.
mypy doctr/
doctr/models/artefacts/face.py:32: error: Unused "type: ignore" comment
                cv2.data.haarcascades + "haarcascade_frontalface_default.xml"  # type: ignore[attr-defined]
                ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
doctr/models/modules/layers/pytorch.py:39: error: Incompatible types in assignment (expression has type "None", variable has type "Conv2d")  [assignment]
                self.ver_conv, self.ver_bn = None, None
                                             ^~~~
doctr/models/modules/layers/pytorch.py:39: error: Incompatible types in assignment (expression has type "None", variable has type "BatchNorm2d")  [assignment]
                self.ver_conv, self.ver_bn = None, None
                                                   ^~~~
doctr/models/modules/layers/pytorch.py:54: error: Incompatible types in assignment (expression has type "None", variable has type "Conv2d")  [assignment]
                self.hor_conv, self.hor_bn = None, None
                                             ^~~~
doctr/models/modules/layers/pytorch.py:54: error: Incompatible types in assignment (expression has type "None", variable has type "BatchNorm2d")  [assignment]
                self.hor_conv, self.hor_bn = None, None
                                                   ^~~~
Found 5 errors in 2 files (checked 161 source files)
make: *** [Makefile:7: quality] Error 1

@felixdittrich92 felixdittrich92 marked this pull request as draft August 30, 2023 16:25
@felixT2K
Copy link
Contributor

felixT2K commented Sep 5, 2023

@nikokks Could you please remove all the TF parts and let the CI run onces ? :)

There are some changes required we need to do.
Unfortunately i don't have the time to review it careful this week.
So i would suggest you continue with the neck + head and post processing.

@felixdittrich92 felixdittrich92 modified the milestones: 0.7.0, 0.7.1 Sep 8, 2023
@nikokks
Copy link
Contributor Author

nikokks commented Sep 8, 2023

make style
isort .
Skipped 45 files
black .
Skipping .ipynb files as Jupyter dependencies are not installed.
You can fix this by running ``pip install "black[jupyter]"``
All done! ✨ 🍰 ✨
253 files left unchanged.
ruff --fix .
make quality
isort . -c
Skipped 45 files
ruff check .
black --check .
Skipping .ipynb files as Jupyter dependencies are not installed.
You can fix this by running ``pip install "black[jupyter]"``
All done! ✨ 🍰 ✨
253 files would be left unchanged.
mypy doctr/
Success: no issues found in 161 source files
pydocstyle doctr/

make test-tf
USE_TF='1' coverage run -m pytest tests/tensorflow/
=========================================================================================== test session starts ===========================================================================================
platform linux -- Python 3.8.10, pytest-5.4.1, py-1.11.0, pluggy-0.13.1
rootdir: /home/nikkokks/Desktop/github/doctr_FAST
plugins: hydra-core-1.2.0, typeguard-2.13.3
collected 222 items                                                                                                                                                                                       

tests/tensorflow/test_datasets_loader_tf.py .                                                                                                      [  0%]
tests/tensorflow/test_datasets_tf.py ...................................................                                                           [ 23%]
tests/tensorflow/test_file_utils_tf.py .                                                                                                           [ 23%]
tests/tensorflow/test_io_image_tf.py ...                                                                                                           [ 25%]
tests/tensorflow/test_models_classification_tf.py ......................ssssss...                                                                  [ 39%]
tests/tensorflow/test_models_detection_tf.py ..................sss                                                                                 [ 48%]
tests/tensorflow/test_models_factory.py ......................                                                                                     [ 58%]
tests/tensorflow/test_models_preprocessor_tf.py ........                                                                                           [ 62%]
tests/tensorflow/test_models_recognition_tf.py .....................................sss                                                            [ 80%]
tests/tensorflow/test_transforms_tf.py ...............................                                                                             [100%]

============================================================================================ warnings summary =============================================================================================
tests/tensorflow/test_models_zoo_tf.py::test_ocrpredictor[False-False]
tests/tensorflow/test_models_zoo_tf.py::test_kiepredictor[False-False]
  /home/nikkokks/Desktop/github/doctr_FAST/doctr/utils/geometry.py:286: RuntimeWarning: divide by zero encountered in true_divide
    return float(np.median(np.arctan((yleft - yright) / (xright - xleft))) * 180 / np.pi)  # Y axis from top to bottom!

-- Docs: https://docs.pytest.org/en/latest/warnings.html
========================================================================= 210 passed, 12 skipped, 2 warnings in 907.17s (0:15:07)
==========================================================================
make test-torch
USE_TORCH='1' coverage run -m pytest tests/pytorch/
=========================================================================================== test session starts ===========================================================================================
platform linux -- Python 3.8.10, pytest-5.4.1, py-1.11.0, pluggy-0.13.1
rootdir: /home/nikkokks/Desktop/github/doctr_FAST
plugins: hydra-core-1.2.0, typeguard-2.13.3
collected 216 items                                                                                                                                                                                       

tests/pytorch/test_datasets_pt.py ...................................................                                                                   [ 23%]
tests/pytorch/test_file_utils_pt.py .                                                                                                                   [ 24%]
tests/pytorch/test_io_image_pt.py ...                                                                                                                   [ 25%]
tests/pytorch/test_models_classification_pt.py ................................                                                                         [ 40%]
tests/pytorch/test_models_detection_pt.py ........................                                                                                      [ 51%]
tests/pytorch/test_models_factory.py ..........................                                                                                         [ 63%]
tests/pytorch/test_models_obj_detection_pt.py ..                                                                                                        [ 64%]
tests/pytorch/test_models_preprocessor_pt.py ..........                                                                                                 [ 68%]
tests/pytorch/test_models_recognition_pt.py ................................ss..                                                                        [ 85%]
tests/pytorch/test_models_utils_pt.py ....                                                                                                              [ 87%]
tests/pytorch/test_models_zoo_pt.py .........                                                                                                           [ 91%]
tests/pytorch/test_transforms_pt.py ..................                                                                                                  [100%]

======================================================================== 214 passed, 2 skipped, 268 warnings in 234.90s (0:03:54) =========================================================================

@nikokks
Copy link
Contributor Author

nikokks commented Sep 8, 2023

I still have a problem to switch textNetFast tensorflow model to eval, I think the problem is due to a problem of init weights.
Can someone have a look at it plz ?

@nikokks
Copy link
Contributor Author

nikokks commented Sep 8, 2023

I think maybe there is a second problem initializing block of layers in TF.

@felixdittrich92
Copy link
Contributor

As discussed closing this up to december :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
framework: pytorch Related to PyTorch backend framework: tensorflow Related to TensorFlow backend module: models Related to doctr.models topic: character classification Related to the task of character classification type: new feature New feature
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants