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

ValueError: Layer 'conv2d_9' expected 1 variables, but received 0 variables during loading. Expected: ['conv2d_9/kernel:0'] #1792

Open
MuhammadShifa opened this issue Nov 21, 2024 · 10 comments
Labels
framework: tensorflow Related to TensorFlow backend os: windows Involving Windows users type: bug Something isn't working

Comments

@MuhammadShifa
Copy link

MuhammadShifa commented Nov 21, 2024

Bug description

Hello thanks for the awesome package. I am going to process a pdf document with OCR but I am facing the issue in Window Machine. I have installed the doctr, tf and pt from source via the following line.

git clone https://github.com/mindee/doctr.git
pip install -e doctr/.
# for TensorFlow
pip install -e doctr/.[tf]
# for PyTorch
pip install -e doctr/.[torch]

Code snippet to reproduce the bug

from doctr.io import DocumentFile
from doctr.models import kie_predictor

# Model
model = kie_predictor(det_arch='db_resnet50', reco_arch='crnn_vgg16_bn', pretrained=True)
# PDF
doc = DocumentFile.from_pdf("./testing_doctr_ocr.pdf")
# Analyze
result = model(doc)

predictions = result.pages[0].predictions
for class_name in predictions.keys():
    list_predictions = predictions[class_name]
    for prediction in list_predictions:
        print(f"Prediction for {class_name}: {prediction}")```


### Error traceback

Traceback (most recent call last):
File "E:\OCR\ml_mo\doctr_mo\testing_doct.py", line 5, in
model = kie_predictor(det_arch='db_resnet50', reco_arch='crnn_vgg16_bn', pretrained=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\OCR\ml_mo\doctr\doctr\models\zoo.py", line 228, in kie_predictor
return _kie_predictor(
^^^^^^^^^^^^^^^
File "E:\OCR\ml_mo\doctr\doctr\models\zoo.py", line 146, in _kie_predictor
det_predictor = detection_predictor(
^^^^^^^^^^^^^^^^^^^^
File "E:\OCR\ml_mo\doctr\doctr\models\detection\zoo.py", line 110, in detection_predictor
return _predictor(
^^^^^^^^^^^
File "E:\OCR\ml_mo\doctr\doctr\models\detection\zoo.py", line 50, in _predictor
_model = detection.dict[arch](
^^^^^^^^^^^^^^^^^^^^^^^^^
File "E:\OCR\ml_mo\doctr\doctr\models\detection\differentiable_binarization\tensorflow.py", line 390, in db_resnet50
return _db_resnet(
^^^^^^^^^^^
File "E:\OCR\ml_mo\doctr\doctr\models\detection\differentiable_binarization\tensorflow.py", line 318, in _db_resnet
load_pretrained_params(
File "E:\OCR\ml_mo\doctr\doctr\models\utils\tensorflow.py", line 73, in load_pretrained_params
model.load_weights(archive_path, skip_mismatch=skip_mismatch)
File "C:\Users\admin\anaconda3\envs\doctr_ocr\Lib\site-packages\tf_keras\src\utils\traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\admin\anaconda3\envs\doctr_ocr\Lib\site-packages\tf_keras\src\engine\base_layer.py", line 3518, in load_own_variables
raise ValueError(
ValueError: Layer 'conv2d_9' expected 1 variables, but received 0 variables during loading. Expected: ['conv2d_9/kernel:0']


### Environment

WARNING:tensorflow:From C:\Users\admin\anaconda3\envs\doctr_ocr\Lib\site-packages\tf_keras\src\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.

WARNING:tensorflow:From C:\Users\admin\anaconda3\envs\doctr_ocr\Lib\site-packages\tf2onnx\tf_loader.py:68: The name tf.reset_default_graph is deprecated. Please use tf.compat.v1.reset_default_graph instead.

WARNING:tensorflow:From C:\Users\admin\anaconda3\envs\doctr_ocr\Lib\site-packages\tf2onnx\tf_loader.py:72: The name tf.train.import_meta_graph is deprecated. Please use tf.compat.v1.train.import_meta_graph instead.

WARNING:tensorflow:From C:\Users\admin\anaconda3\envs\doctr_ocr\Lib\site-packages\tf_keras\src\backend.py:873: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.

Collecting environment information...

DocTR version: 0.10.1a0
TensorFlow version: 2.18.0
PyTorch version: 2.5.1+cpu (torchvision 0.20.1+cpu)
OpenCV version: 4.10.0
OS: Microsoft Windows 10 Pro
Python version: 3.11.10
Is CUDA available (TensorFlow): No
Is CUDA available (PyTorch): No
CUDA runtime version: 11.8.89
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Could not collect
(doctr_ocr) PS E:\OCR\ml_mo\doctr\scripts>


### Deep Learning backend

is_tf_available: True
is_torch_available: True
@MuhammadShifa MuhammadShifa added the type: bug Something isn't working label Nov 21, 2024
@felixdittrich92 felixdittrich92 added os: windows Involving Windows users framework: tensorflow Related to TensorFlow backend labels Nov 21, 2024
@felixdittrich92
Copy link
Contributor

felixdittrich92 commented Nov 21, 2024

Hi @MuhammadShifa 👋,

Thanks for reporting this seems to be an issue with TensorFlow on Windows machines -> #1776

CC @odulcy-mindee Do we have a windows machine where we can try to reproduce the bug ? 😅

With linux everything works as expected -> CI also green

@MuhammadShifa
Copy link
Author

Hi @felixdittrich92 , @odulcy-mindee
Yes exactly, I am using the Window Machine.

@felixdittrich92
Copy link
Contributor

Quickly tested also with py3.11 in a docker container without issues.
So it's definitely related to Windows

@JulianAndhika
Copy link

Currently have the same problem whenever i try to run the ocr_predictor. Any current alternative fix?

@felixdittrich92
Copy link
Contributor

Currently have the same problem whenever i try to run the ocr_predictor. Any current alternative fix?

Hi @JulianAndhika 👋🏼 ,

I expect you use also windows ?
Doesn't looks like there is an quick fix unfortunately.

What you could do is switching to PyTorch as backend

btw. which docTR version do you use ?

@JulianAndhika
Copy link

Hi @felixdittrich92

Yeah im using windows. I'm currently trying to use OnnxTR as an alternative. I'm currently fine tuning the model using train_tensorflow.py, and was wondering if i could do the same thing on OnnxTR.

Also i was using docTR version 0.10.0

@felixT2K
Copy link
Contributor

felixT2K commented Dec 2, 2024

Hi @felixdittrich92

Yeah im using windows. I'm currently trying to use OnnxTR as an alternative. I'm currently fine tuning the model using train_tensorflow.py, and was wondering if i could do the same thing on OnnxTR.

Also i was using docTR version 0.10.0

OnnxTR is inference only so you can train / fine tune your model with docTR and export it to Onnx as described here:
https://mindee.github.io/doctr/using_doctr/using_model_export.html#export-to-onnx
Afterwards you can load it in OnnxTR :)

@felixdittrich92
Copy link
Contributor

Quick update:

I was able to reproduce the issue and as suspected it's a bug in Keras (v2) - happens only on Windows

@JulianAndhika
Copy link

Hi @felixdittrich92 @felixT2K. That's unfortunate, hopefully the bug could be fixed soon. I've resorted to fine tune the model on colab, since it doesn't seem to be running into the bug when fine tuning. Using pytorch is fine for inference, but when i try fine tuning, it produces an error of

RuntimeError: Expected tensor to have size at least 36 at dimension 1, but got size 32 for argument #2 'targets' (while checking arguments for ctc_loss_cpu)

Maybe a bug on pytorch part or something i don't know. Any ideas on what's wrong?

@felixT2K
Copy link
Contributor

felixT2K commented Dec 3, 2024

@MuhammadShifa The pytorch bug (it's not really a bug) is easy to fix your recognition dataset contains samples with more than 32 characters (models max_length) - so keep only samples with <= 30 characters (because EOS + SOS will be added on top so + 2) and everything should work as expected

Additional don't miss to pass the correct vocab for training / fine tuning (feel free to share the command you use to start training)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
framework: tensorflow Related to TensorFlow backend os: windows Involving Windows users type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants