emedvedev / attention-ocr

A Tensorflow model for text recognition (CNN + seq2seq with visual attention) available as a Python package and compatible with Google Cloud ML Engine.
MIT License
1.08k stars 256 forks source link

How to use "predict" to perform prediction on a single image? #145

Closed AhmadShaik closed 4 years ago

AhmadShaik commented 5 years ago

I am trying to perform the prediction on a single image using the following script. import argparse import tensorflow as tf import numpy as np

def load_graph(frozen_graph_filename):
    # We load the protobuf file from the disk and parse it to retrieve the
    # unserialized graph_def
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

    # Then, we can use again a convenient built-in function to import a graph_def into the
    # current default Graph
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(
            graph_def,
            input_map=None,
            return_elements=None,
            name="prefix",
            op_dict=None,
            producer_op_list=None
        )
    return graph

def getImage(path):
    with open(path, 'rb') as img_file:
        img = img_file.read()
    print(img)
    return img

frozen_model_filename = "exported-model/frozen_graph.pb"
graph = load_graph(frozen_model_filename)

def ocrImage(image):
    x = graph.get_tensor_by_name('prefix/input_image_as_bytes:0')
    y = graph.get_tensor_by_name('prefix/prediction:0')
    allProbs = graph.get_tensor_by_name('prefix/probability:0')

    img = getImage(image)

    with tf.Session(graph=graph) as sess:
        (y_out, probs_output) = sess.run([y,allProbs], feed_dict={
            x: [img]
        })
        # print(y_out)
        # print(allProbsToScore(probs_output))

        return {
            "predictions": [{
                "ocr": str(y_out),
                "confidence": probs_output
            }]
        };

if __name__ == '__main__':
    # Let's allow the user to pass the filename as an argument
    parser = argparse.ArgumentParser()
    # parser.add_argument("--frozen_model_filename", default="checkpoints_pruned/frozen_model.pb", type=str, help="Frozen model file to import")
    parser.add_argument("--image", default="0_15.png", type=str, help="Path to image")
    args = parser.parse_args()
    predictions = ocrImage(args.image)
    print(str(predictions))

When I run the script I am getting below error:

(/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env) ahmad@irs-aeye-ll-014:/data/work/tvs-part-rec/loc-aocr/aocr_50k$ python predict.py 
WARNING:tensorflow:From predict.py:21: calling import_graph_def (from tensorflow.python.framework.importer) with op_dict is deprecated and will be removed in a future version.
Instructions for updating:
Please file an issue at https://github.com/tensorflow/tensorflow/issues if you depend on this feature.
2019-08-06 14:47:13.028800: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-08-06 14:47:13.090489: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:964] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-08-06 14:47:13.090737: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties: 
name: GeForce GTX 1060 major: 6 minor: 1 memoryClockRate(GHz): 1.6705
pciBusID: 0000:01:00.0
totalMemory: 5.94GiB freeMemory: 5.72GiB
2019-08-06 14:47:13.090754: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
2019-08-06 14:47:13.408287: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-08-06 14:47:13.408323: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988]      0 
2019-08-06 14:47:13.408330: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0:   N 
2019-08-06 14:47:13.408416: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 5494 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1060, pci bus id: 0000:01:00.0, compute capability: 6.1)
Traceback (most recent call last):
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1334, in _do_call
    return fn(*args)
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1317, in _run_fn
    self._extend_graph()
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1352, in _extend_graph
    tf_session.ExtendSession(self._session)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation prefix/Rank: Could not satisfy explicit device specification '/device:GPU:0' because no supported kernel for GPU devices is available.
Registered kernels:
  device='XLA_CPU_JIT'; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT8, DT_COMPLEX64, DT_INT64, DT_BOOL, DT_QINT8, DT_QUINT8, DT_QINT32, DT_HALF, DT_UINT32, DT_UINT64]
  device='XLA_GPU_JIT'; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT8, ..., DT_QINT32, DT_BFLOAT16, DT_HALF, DT_UINT32, DT_UINT64]
  device='XLA_CPU'; T in [DT_UINT8, DT_QUINT8, DT_INT8, DT_QINT8, DT_INT32, DT_QINT32, DT_INT64, DT_HALF, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64, DT_BOOL]
  device='XLA_GPU'; T in [DT_UINT8, DT_QUINT8, DT_INT8, DT_QINT8, DT_INT32, DT_QINT32, DT_INT64, DT_HALF, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64, DT_BOOL, DT_BFLOAT16]
  device='CPU'
  device='GPU'; T in [DT_BOOL]
  device='GPU'; T in [DT_INT32]
  device='GPU'; T in [DT_VARIANT]
  device='GPU'; T in [DT_COMPLEX128]
  device='GPU'; T in [DT_COMPLEX64]
  device='GPU'; T in [DT_INT8]
  device='GPU'; T in [DT_UINT8]
  device='GPU'; T in [DT_INT16]
  device='GPU'; T in [DT_UINT16]
  device='GPU'; T in [DT_INT64]
  device='GPU'; T in [DT_DOUBLE]
  device='GPU'; T in [DT_FLOAT]
  device='GPU'; T in [DT_BFLOAT16]
  device='GPU'; T in [DT_HALF]

     [[{{node prefix/Rank}} = Rank[T=DT_STRING, _device="/device:GPU:0"](prefix/input_image_as_bytes)]]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "predict.py", line 61, in <module>
    predictions = ocrImage(args.image)
  File "predict.py", line 43, in ocrImage
    x: [img]
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 929, in run
    run_metadata_ptr)
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1152, in _run
    feed_dict_tensor, options, run_metadata)
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1328, in _do_run
    run_metadata)
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1348, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation prefix/Rank: Could not satisfy explicit device specification '/device:GPU:0' because no supported kernel for GPU devices is available.
Registered kernels:
  device='XLA_CPU_JIT'; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT8, DT_COMPLEX64, DT_INT64, DT_BOOL, DT_QINT8, DT_QUINT8, DT_QINT32, DT_HALF, DT_UINT32, DT_UINT64]
  device='XLA_GPU_JIT'; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT8, ..., DT_QINT32, DT_BFLOAT16, DT_HALF, DT_UINT32, DT_UINT64]
  device='XLA_CPU'; T in [DT_UINT8, DT_QUINT8, DT_INT8, DT_QINT8, DT_INT32, DT_QINT32, DT_INT64, DT_HALF, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64, DT_BOOL]
  device='XLA_GPU'; T in [DT_UINT8, DT_QUINT8, DT_INT8, DT_QINT8, DT_INT32, DT_QINT32, DT_INT64, DT_HALF, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64, DT_BOOL, DT_BFLOAT16]
  device='CPU'
  device='GPU'; T in [DT_BOOL]
  device='GPU'; T in [DT_INT32]
  device='GPU'; T in [DT_VARIANT]
  device='GPU'; T in [DT_COMPLEX128]
  device='GPU'; T in [DT_COMPLEX64]
  device='GPU'; T in [DT_INT8]
  device='GPU'; T in [DT_UINT8]
  device='GPU'; T in [DT_INT16]
  device='GPU'; T in [DT_UINT16]
  device='GPU'; T in [DT_INT64]
  device='GPU'; T in [DT_DOUBLE]
  device='GPU'; T in [DT_FLOAT]
  device='GPU'; T in [DT_BFLOAT16]
  device='GPU'; T in [DT_HALF]

     [[node prefix/Rank (defined at predict.py:21)  = Rank[T=DT_STRING, _device="/device:GPU:0"](prefix/input_image_as_bytes)]]

Caused by op 'prefix/Rank', defined at:
  File "predict.py", line 32, in <module>
    graph = load_graph(frozen_model_filename)
  File "predict.py", line 21, in load_graph
    producer_op_list=None
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/util/deprecation.py", line 488, in new_func
    return func(*args, **kwargs)
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/framework/importer.py", line 442, in import_graph_def
    _ProcessNewOps(graph)
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/framework/importer.py", line 234, in _ProcessNewOps
    for new_op in graph._add_new_tf_operations(compute_devices=False):  # pylint: disable=protected-access
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3440, in _add_new_tf_operations
    for c_op in c_api_util.new_tf_operations(self)
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3440, in <listcomp>
    for c_op in c_api_util.new_tf_operations(self)
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3299, in _create_op_from_tf_operation
    ret = Operation(c_op, self)
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1770, in __init__
    self._traceback = tf_stack.extract_stack()

InvalidArgumentError (see above for traceback): Cannot assign a device for operation prefix/Rank: Could not satisfy explicit device specification '/device:GPU:0' because no supported kernel for GPU devices is available.
Registered kernels:
  device='XLA_CPU_JIT'; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT8, DT_COMPLEX64, DT_INT64, DT_BOOL, DT_QINT8, DT_QUINT8, DT_QINT32, DT_HALF, DT_UINT32, DT_UINT64]
  device='XLA_GPU_JIT'; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT8, ..., DT_QINT32, DT_BFLOAT16, DT_HALF, DT_UINT32, DT_UINT64]
  device='XLA_CPU'; T in [DT_UINT8, DT_QUINT8, DT_INT8, DT_QINT8, DT_INT32, DT_QINT32, DT_INT64, DT_HALF, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64, DT_BOOL]
  device='XLA_GPU'; T in [DT_UINT8, DT_QUINT8, DT_INT8, DT_QINT8, DT_INT32, DT_QINT32, DT_INT64, DT_HALF, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64, DT_BOOL, DT_BFLOAT16]
  device='CPU'
  device='GPU'; T in [DT_BOOL]
  device='GPU'; T in [DT_INT32]
  device='GPU'; T in [DT_VARIANT]
  device='GPU'; T in [DT_COMPLEX128]
  device='GPU'; T in [DT_COMPLEX64]
  device='GPU'; T in [DT_INT8]
  device='GPU'; T in [DT_UINT8]
  device='GPU'; T in [DT_INT16]
  device='GPU'; T in [DT_UINT16]
  device='GPU'; T in [DT_INT64]
  device='GPU'; T in [DT_DOUBLE]
  device='GPU'; T in [DT_FLOAT]
  device='GPU'; T in [DT_BFLOAT16]
  device='GPU'; T in [DT_HALF]

     [[node prefix/Rank (defined at predict.py:21)  = Rank[T=DT_STRING, _device="/device:GPU:0"](prefix/input_image_as_bytes)]]

Can someone explain the process to perform recognition on a single image.

sushanth-d commented 5 years ago

I am trying to do the same, but no luck. Any help here would be much appreciated. Thank you.

githubpiyush commented 5 years ago

Screenshot from 2019-08-21 13-26-35

Modify line 263 in main.py file. Hope it helps.

Execute with this command:

python main.py predict (Two under score before main and two underscore after main)

sushanth-d commented 5 years ago

Thanks a lot, Piyush. Big help. The code could not have been more elegant.

AhmadShaik commented 5 years ago

@githubpiyush @githubpiyush I have updated the code in main.py. From where do I need to run main.py file. Could anyone of you can explain in more detail.

githubpiyush commented 5 years ago

@AhmadShaik You just need to go inside aocr folder open terminal over there and execute under score main.py file with predict as argument (Enter the directory path in your code where i mentioned enter directory absolute path in the code snippet )

kalai2033 commented 5 years ago

image @githubpiyush i have modified the code in the main.py and ran the command python main.py predict i didnt get any result.

sushanth-d commented 5 years ago

Try :python main.py --predict

Note:there is two underscores before and after main of course. It is not appearing for some reason.

kalai2033 commented 5 years ago

@sushanth-d Thanks man. That works. But im getting a new error. Do i need do any installation after cloning the repository. I just cloned the repo and modified the main.py and ran the above code. Now im getting this error. Can you let me know if there is any installation steps. I did pip installation aocr before. Is there anything else i should do before i run the code.

Traceback (most recent call last): File "main.py", line 14, in from .model.model import Model ModuleNotFoundError: No module named 'main.model'; 'main' is not a package

githubpiyush commented 5 years ago

It is importing some module and it is not present there which is causing this error.

If you use this repo in ubuntu then you just need to use this code in your main.py file: import sys sys.path.insert(0, "/home/Desktop/aocr ")

Note: Give your aocr folder directory path in above code. You need to add this code above the module imports. Please find the windows equivalent of this. Plus you need to remove the proceeding '.' From all the imports. For example: from .model.model import Model Becomes this from model.model import Model

kalai2033 commented 5 years ago

Thanks @githubpiyush. I changed that to from model.model import Model. Now im facing this error. Traceback (most recent call last): File "main.py", line 24, in from model.model import model File "C:\Users\Kalai\attention-ocr\aocr\model\model.py", line 19, in from ..util.data_gen import DataGen ValueError: attempted relative import beyond top-level package

githubpiyush commented 5 years ago

Remove those '..' too from model.py file or where is that error is coming from. from ..util.data_gen import DataGen become this: from util.data_gen import DataGen If you find any preceding '.' in import please remove them.

githubpiyush commented 5 years ago

Any success ?

kalai2033 commented 5 years ago

@githubpiyush I have cleared the import error.Thanks

kalai2033 commented 5 years ago

th1_1.jpg LLLLLZZZZZ 1.3522936940656732e-16 th1_10.jpg L66ZZZZZZZ 1.3564668442314004e-16 th1_100.jpg L66ZZZZZZZ 1.3502966319449094e-16 th1_101.jpg LLL6ZZZZZZ 1.3328914537112895e-16

i got the above output after passing the cropped text areas of my image. None of my predictions was right. As of now im using tesseract to extract the text which gives decent output after some preprocessing. I have no idea how to use aocr. Should i train my own model to extract the text. can someone please share their knowledge on how to extract text using aocr?

githubpiyush commented 5 years ago

What checkpoints you are using for this prediction?

You can train your own model on your dataset for best results. Training process is already mention in readme file of this repository.

kalai2033 commented 5 years ago

@githubpiyush Thats what im confused. Is there any pre trained models on which i can test my images. As of now i just clone the repositry and ran the test for my images. I dont see any checkpoints in the aocr module.

githubpiyush commented 5 years ago

You have to train it on your data and around 500-1000 epochs.

billxljin commented 4 years ago

I am trying to perform the prediction on a single image using the following script. import argparse import tensorflow as tf import numpy as np

def load_graph(frozen_graph_filename):
    # We load the protobuf file from the disk and parse it to retrieve the
    # unserialized graph_def
    with tf.gfile.GFile(frozen_graph_filename, "rb") as f:
        graph_def = tf.GraphDef()
        graph_def.ParseFromString(f.read())

    # Then, we can use again a convenient built-in function to import a graph_def into the
    # current default Graph
    with tf.Graph().as_default() as graph:
        tf.import_graph_def(
            graph_def,
            input_map=None,
            return_elements=None,
            name="prefix",
            op_dict=None,
            producer_op_list=None
        )
    return graph

def getImage(path):
    with open(path, 'rb') as img_file:
        img = img_file.read()
    print(img)
    return img

frozen_model_filename = "exported-model/frozen_graph.pb"
graph = load_graph(frozen_model_filename)

def ocrImage(image):
    x = graph.get_tensor_by_name('prefix/input_image_as_bytes:0')
    y = graph.get_tensor_by_name('prefix/prediction:0')
    allProbs = graph.get_tensor_by_name('prefix/probability:0')

    img = getImage(image)

    with tf.Session(graph=graph) as sess:
        (y_out, probs_output) = sess.run([y,allProbs], feed_dict={
            x: [img]
        })
        # print(y_out)
        # print(allProbsToScore(probs_output))

        return {
            "predictions": [{
                "ocr": str(y_out),
                "confidence": probs_output
            }]
        };

if __name__ == '__main__':
    # Let's allow the user to pass the filename as an argument
    parser = argparse.ArgumentParser()
    # parser.add_argument("--frozen_model_filename", default="checkpoints_pruned/frozen_model.pb", type=str, help="Frozen model file to import")
    parser.add_argument("--image", default="0_15.png", type=str, help="Path to image")
    args = parser.parse_args()
    predictions = ocrImage(args.image)
    print(str(predictions))

When I run the script I am getting below error:


(/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env) ahmad@irs-aeye-ll-014:/data/work/tvs-part-rec/loc-aocr/aocr_50k$ python predict.py 
WARNING:tensorflow:From predict.py:21: calling import_graph_def (from tensorflow.python.framework.importer) with op_dict is deprecated and will be removed in a future version.
Instructions for updating:
Please file an issue at https://github.com/tensorflow/tensorflow/issues if you depend on this feature.
2019-08-06 14:47:13.028800: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-08-06 14:47:13.090489: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:964] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-08-06 14:47:13.090737: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties: 
name: GeForce GTX 1060 major: 6 minor: 1 memoryClockRate(GHz): 1.6705
pciBusID: 0000:01:00.0
totalMemory: 5.94GiB freeMemory: 5.72GiB
2019-08-06 14:47:13.090754: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
2019-08-06 14:47:13.408287: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-08-06 14:47:13.408323: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988]      0 
2019-08-06 14:47:13.408330: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0:   N 
2019-08-06 14:47:13.408416: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 5494 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1060, pci bus id: 0000:01:00.0, compute capability: 6.1)
Traceback (most recent call last):
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1334, in _do_call
    return fn(*args)
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1317, in _run_fn
    self._extend_graph()
  File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1352, in _extend_graph
    tf_session.ExtendSession(self._session)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation prefix/Rank: Could not satisfy explicit device specification '/device:GPU:0' because no supported kernel for GPU devices is available.
Registered kernels:
  device='XLA_CPU_JIT'; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT8, DT_COMPLEX64, DT_INT64, DT_BOOL, DT_QINT8, DT_QUINT8, DT_QINT32, DT_HALF, DT_UINT32, DT_UINT64]
  device='XLA_GPU_JIT'; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT8, ..., DT_QINT32, DT_BFLOAT16, DT_HALF, DT_UINT32, DT_UINT64]
  device='XLA_CPU'; T in [DT_UINT8, DT_QUINT8, DT_INT8, DT_QINT8, DT_INT32, DT_QINT32, DT_INT64, DT_HALF, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64, DT_BOOL]
  device='XLA_GPU'; T in [DT_UINT8, DT_QUINT8, DT_INT8, DT_QINT8, DT_INT32, DT_QINT32, DT_INT64, DT_HALF, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64, DT_BOOL, DT_BFLOAT16]
  device='CPU'
  device='GPU'; T in [DT_BOOL]
  device='GPU'; T in [DT_INT32]
  device='GPU'; T in [DT_VARIANT]
  device='GPU'; T in [DT_COMPLEX128]
  device='GPU'; T in [DT_COMPLEX64]
  device='GPU'; T in [DT_INT8]
  device='GPU'; T in [DT_UINT8]
  device='GPU'; T in [DT_INT16]
  device='GPU'; T in [DT_UINT16]
  device='GPU'; T in [DT_INT64]
  device='GPU'; T in [DT_DOUBLE]
  device='GPU'; T in [DT_FLOAT]
  device='GPU'; T in [DT_BFLOAT16]
  device='GPU'; T in [DT_HALF]

   [[{{node prefix/Rank}} = Rank[T=DT_STRING, _device="/device:GPU:0"](prefix/input_image_as_bytes)]]

I got the same error when I try to run your code. Here is a working version I modified base on your code.

import sys import argparse import os import tensorflow as tf import numpy as np def load_graph(frozen_graph_filename): detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(frozen_graph_filename, "rb") as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def,name='') return detection_graph

def getImage(path): with open(path, 'rb') as img_file: img = img_file.read()

print(img)

return img

frozen_model_filename = "exported-model/frozen_graph.pb" detection_graph = load_graph(frozen_model_filename)

def ocrImage(image): img = getImage(image) tf_config = tf.ConfigProto() tf_config.gpu_options.allow_growth = True tf_config.allow_soft_placement=True with detection_graph.as_default(): with tf.Session(graph=detection_graph,config=tf_config) as sess: image_tensor = detection_graph.get_tensor_by_name('input_image_as_bytes:0') prediction_result = detection_graph.get_tensor_by_name('prediction:0') allProbs = detection_graph.get_tensor_by_name('probability:0')

        (y_out, probs_output) = sess.run([prediction_result,allProbs], 
        feed_dict={image_tensor: [img]})
        return {
            "predictions": [{
                "ocr": str(y_out),
                "confidence": probs_output
            }]
        }

if name == 'main':

Let's allow the user to pass the filename as an argument

parser = argparse.ArgumentParser()
parser.add_argument("--image", default="0_15.png", type=str, help="Path to image")
args = parser.parse_args()
predictions = ocrImage(args.image)
print(str(predictions))
githubpiyush commented 4 years ago

There is some problem with your GPU config or kernel is not registered in tensorflow for some operation for example data types in static hash tables.