Calamari-OCR / calamari

Line based ATR Engine based on OCRopy
Apache License 2.0
1.04k stars 209 forks source link

Cannot convert a symbolic Tensor - Cannot even initialize the Predictor object #314

Closed D3nii closed 2 years ago

D3nii commented 2 years ago

Hi there,

I have tried the version 1.0 and 2.0 models from https://github.com/Calamari-OCR/calamari_models/archive, and also tried the best.ckpt located at https://github.com/Calamari-OCR/calamari/tree/master/calamari_ocr/test/models.

But whatever I use, I run into this error

INFO     2022-04-03 08:14:12,538     tfaip.device.device_config: Setting up device config DeviceConfigParams(gpus=None, gpu_auto_tune=False, gpu_memory=None, soft_device_placement=True, dist_strategy=<DistributionStrategy.DEFAULT: 'default'>)
INFO     2022-04-03 08:14:12,546 calamari_ocr.ocr.savedmodel.sa: Checkpoint version 5 is up-to-date.
/usr/local/lib/python3.7/dist-packages/paiargparse/dataclass_json_overrides.py:78: RuntimeWarning: `NoneType` object value of non-optional type strides detected when decoding MaxPool2DLayerParams.
  warnings.warn(f"`NoneType` object {warning}.", RuntimeWarning)
INFO     2022-04-03 08:14:12,584     tfaip.device.device_config: Setting up device config DeviceConfigParams(gpus=None, gpu_auto_tune=False, gpu_memory=None, soft_device_placement=True, dist_strategy=<DistributionStrategy.DEFAULT: 'default'>)
---------------------------------------------------------------------------
NotImplementedError                       Traceback (most recent call last)
[<ipython-input-14-6903a23e2422>](https://0dn9cyaravqb-496ff2e9c6d22116-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20220331-060046-RC00_438531407#) in <module>()
      1 predictor = Predictor.from_checkpoint(
      2     params=PredictorParams(),
----> 3     checkpoint="./best.ckpt")

13 frames
[/usr/local/lib/python3.7/dist-packages/tensorflow/python/autograph/impl/api.py](https://0dn9cyaravqb-496ff2e9c6d22116-0-colab.googleusercontent.com/outputframe.html?vrz=colab-20220331-060046-RC00_438531407#) in wrapper(*args, **kwargs)
    668       except Exception as e:  # pylint:disable=broad-except
    669         if hasattr(e, 'ag_error_metadata'):
--> 670           raise e.ag_error_metadata.to_exception(e)
    671         else:
    672           raise

NotImplementedError: in user code:

    /usr/local/lib/python3.7/dist-packages/tfaip/model/graphbase.py:267 call  *
        return self._graph(inputs_targets)
    /usr/local/lib/python3.7/dist-packages/tfaip/model/graphbase.py:309 call  *
        outputs = self.build_prediction_graph(inputs)
    /usr/local/lib/python3.7/dist-packages/tfaip/model/graphbase.py:338 build_prediction_graph  *
        return self.build_train_graph(inputs)
    /usr/local/lib/python3.7/dist-packages/tfaip/model/graphbase.py:343 build_train_graph  *
        return self.build_graph(inputs, training)
    /usr/local/lib/python3.7/dist-packages/calamari_ocr/ocr/model/graph.py:83 build_graph  *
        last_layer_output = layer(last_layer_output)
    /usr/local/lib/python3.7/dist-packages/calamari_ocr/ocr/model/layers/layer.py:77 call  *
        return self._call(inputs, **kwargs)
    /usr/local/lib/python3.7/dist-packages/calamari_ocr/ocr/model/layers/bilstm.py:50 _call  *
        return self.lstm(inputs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/wrappers.py:539 __call__  **
        return super(Bidirectional, self).__call__(inputs, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py:1012 __call__
        outputs = call_fn(inputs, *args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/wrappers.py:653 call
        initial_state=forward_state, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/recurrent.py:660 __call__
        return super(RNN, self).__call__(inputs, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/base_layer.py:1012 __call__
        outputs = call_fn(inputs, *args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/recurrent_v2.py:1157 call
        inputs, initial_state, _ = self._process_inputs(inputs, initial_state, None)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/recurrent.py:859 _process_inputs
        initial_state = self.get_initial_state(inputs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/recurrent.py:643 get_initial_state
        inputs=None, batch_size=batch_size, dtype=dtype)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/recurrent.py:2507 get_initial_state
        self, inputs, batch_size, dtype))
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/recurrent.py:2987 _generate_zero_filled_state_for_cell
        return _generate_zero_filled_state(batch_size, cell.state_size, dtype)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/recurrent.py:3003 _generate_zero_filled_state
        return nest.map_structure(create_zeros, state_size)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/nest.py:659 map_structure
        structure[0], [func(*x) for x in entries],
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/nest.py:659 <listcomp>
        structure[0], [func(*x) for x in entries],
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/layers/recurrent.py:3000 create_zeros
        return array_ops.zeros(init_state_size, dtype=dtype)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper
        return target(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/array_ops.py:2819 wrapped
        tensor = fun(*args, **kwargs)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/array_ops.py:2868 zeros
        output = _constant_if_small(zero, shape, dtype, name)
    /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/array_ops.py:2804 _constant_if_small
        if np.prod(shape) < 1000:
    <__array_function__ internals>:6 prod

    /usr/local/lib/python3.7/dist-packages/numpy/core/fromnumeric.py:3052 prod
        >>> np.cumprod(a, dtype=float) # specify type of output
    /usr/local/lib/python3.7/dist-packages/numpy/core/fromnumeric.py:86 _wrapreduction

    /usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/ops.py:855 __array__
        " a NumPy call, which is not supported".format(self.name))

    NotImplementedError: Cannot convert a symbolic Tensor (root/CalamariGraph/lstm_0/bidirectional/forward_lstm/strided_slice:0) to a numpy array. This error may indicate that you're trying to pass a Tensor to a NumPy call, which is not supported

Any ideas what to do?

andbue commented 2 years ago

I have not seen this error before, it might be from some tensorflow version mismatch? It looks as if you're trying to run it on colab. I've just tested it myself:

!pip install opencv-python-headless==4.1.2.30 # some problems with other version, see https://stackoverflow.com/questions/70537488/cannot-import-name-registermattype-from-cv2-cv2
!pip install calamari-ocr
!wget https://github.com/Calamari-OCR/calamari_models/raw/master/uw3-modern-english/0.ckpt.h5
!wget https://github.com/Calamari-OCR/calamari_models/raw/master/uw3-modern-english/0.ckpt.json
predictor = Predictor.from_checkpoint(
    params=PredictorParams(),
    checkpoint='0.ckpt')

So far no errors. Had to restart runtime after installing other numpy version.

D3nii commented 2 years ago

@andbue I was taking that into account before too, but magically, it works now!

Thanks a lot!