Search code examples
tensorflowquantizationtpugoogle-coral

Edge TPU Compiler: ERROR: quantized_dimension must be in range [0, 1). Was 3


I'm trying to get a Mobilenetv2 model (retrained last layers to my data) to run on the Google edge TPU Coral.

I've followed this tuturial https://www.tensorflow.org/lite/performance/post_training_quantization?hl=en to do the post-training quantization. The relevant code is:

...
train = tf.convert_to_tensor(np.array(train, dtype='float32'))
my_ds = tf.data.Dataset.from_tensor_slices(train).batch(1)


# POST TRAINING QUANTIZATION
def representative_dataset_gen():
    for input_value in my_ds.take(30):
        yield [input_value]

converter = tf.lite.TFLiteConverter.from_keras_model_file(saved_model_dir)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset_gen
converter.target_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
tflite_quant_model = converter.convert()

I've successfully generated the tflite quantized model but when I run the edgetpu_compiler (followed this page https://coral.withgoogle.com/docs/edgetpu/compiler/#usage) I get this output:

edgetpu_compiler  Notebooks/MobileNetv2_3class_visit_split_best-val- 
acc.h5.quant.tflite

Edge TPU Compiler version 2.0.258810407
INFO: Initialized TensorFlow Lite runtime.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
ERROR: quantized_dimension must be in range [0, 1). Was 3.
Invalid model: Notebooks/MobileNetv2_3class_visit_split_best-val-        
acc.h5.quant.tflite
Model could not be parsed

The input shape of the model is a 3 channel RGB image. Is possible to do full integer quantization on 3 channel images? I couldn't find anything saying that you can't either on TensorFlow and Google Coral documentation.


Solution

  • I had similar errors, doing the post training full integer quantization with tf-nightly build 1.15 and the use that .tflite file, compile with edge TPU compiler it should work. my error was solved with this approach.

    Same issue was raised in github, you can see it - here