google-coral / tflite

Examples using TensorFlow Lite API to run inference on Coral devices
https://coral.withgoogle.com
Apache License 2.0
182 stars 68 forks source link

RuntimeError: Internal: Unsupported data type in custom op handler: 0Node number 3 (EdgeTpuDelegateForCustomOp) failed to prepare. #37

Closed AlesRuda closed 3 years ago

AlesRuda commented 3 years ago

Hello,

I followed setup steps described in https://coral.ai/docs/accelerator/get-started/#requirements. Clasification exsample works OK.

python classify_image.py --model models/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite --labels models/inat_bird_labels.txt --input images/parrot.jpg
----INFERENCE TIME----
Note: The first inference on Edge TPU is slow because it includes loading the model into Edge TPU memory.
24.6ms
9.9ms
10.3ms
6.2ms
5.9ms
-------RESULTS--------
Ara macao (Scarlet Macaw): 0.77734

I wrote simple semantic clasification script. It can by simply switched between EdgeTPU and CPU infference.

import tensorflow as tf
import numpy as np
from PIL import Image

def main():
  print(tf.version.VERSION)
# EdgeTPU
  interpreter = tf.lite.Interpreter(model_path='./model23_int8_edgetpu.tflite', experimental_delegates=[tf.lite.experimental.load_delegate('edgetpu.dll', {}) ])
# CPU
#  interpreter = tf.lite.Interpreter(model_path='./model23_int8.tflite')
  interpreter.allocate_tensors()

  _, height, width, _ = interpreter.get_input_details()[0]['shape']
  size = (height, width)
  image = Image.open('./1.jpg').convert('RGB').resize(size, Image.ANTIALIAS)
  image = np.asarray(image)/255.0
  input_details = interpreter.get_input_details()[0]
  interpreter.tensor(input_details['index'])()[0][:, :, :]=image
  interpreter.invoke()
  output_details = interpreter.get_output_details()[0]
  output_data = interpreter.get_tensor(output_details['index'])
  print(output_data.shape)
  pred_mask = tf.argmax(output_data, axis=-1)
  pred_mask = pred_mask[..., tf.newaxis]
  img=tf.keras.preprocessing.image.array_to_img(pred_mask[0])
  img.save('./1_mask.jpeg', 'JPEG')

if __name__ == '__main__':
  main()

On the CPU the script runs succesfly, but it fails on the EdgeTPU with error:

Traceback (most recent call last):
  File "test.py", line 38, in <module>
    main()
  File "test.py", line 17, in main
    interpreter.allocate_tensors()
  File "C:\Users\Ales\anaconda3\envs\TPU\lib\site-packages\tensorflow_core\lite\python\interpreter.py", line 247, in allocate_tensors
    return self._interpreter.AllocateTensors()
  File "C:\Users\Ales\anaconda3\envs\TPU\lib\site-packages\tensorflow_core\lite\python\interpreter_wrapper\tensorflow_wrap_interpreter_wrapper.py", line 110, in AllocateTensors
    return _tensorflow_wrap_interpreter_wrapper.InterpreterWrapper_AllocateTensors(self)
RuntimeError: Internal: Unsupported data type in custom op handler: 0Node number 3 (EdgeTpuDelegateForCustomOp) failed to prepare.

My environment: Windows 10 TensorFlow version 2.1.0 (compiled from commit d855adfc5a0195788bf5f92c3c7352e638aa1109)

EdgeTPU runtime BuildLabel(COMPILER=MSVC 192628805,DATE=Jun 24 2020,TIME=11:33:33,CL_NUMBER=291256449), RuntimeVersion(13)

python -c "import edgetpu.basic.edgetpu_utils; print(edgetpu.basic.edgetpu_utils.GetRuntimeVersion())"

Edge TPU Compiler: version 14.1.317412892

docker run -it --rm -v C:\temp\EdgeTPU\EdgeTPU\:/home/edgetpu edgetpu_compiler edgetpu_compiler --version

Test files: test.zip

Any suggestions where the problem may be?

Namburger commented 3 years ago

Are you running this on a docker container?

AlesRuda commented 3 years ago

Edge compiler is running in docker because is not available for windows. All other components are running directly on Windows.

Namburger commented 3 years ago

@AlesRuda Oh sorry, please use the tflite_runtime package instead of the tf.lite.Interpreter as we used here: https://github.com/google-coral/tflite/blob/master/python/examples/classification/classify_image.py#L36 The problem with using the tf.lite is that the package was built on a different tensorflow commit from libedgetpu, that won't works.

AlesRuda commented 3 years ago

It is my mistake. Thank you.