简体   繁体   中英

TF Yamnet Transfer Learning and Quantization

TLDR:

  • Short term: Trying to quantize a specific portion of a TF model (recreated from a TFLite model). Skip to pictures below. \
  • Long term: Transfer Learn on Yamnet and compile for Edge TPU.

Source code to follow along is here

I've been trying to transfer learn on Yamnet and compile for a Coral Edge TPU for a few weeks now.

Started here , but quickly realized that model wouldn't quantize and compile for the Edge TPU because of the dynamic input and out of the box TFLite quantization doesn't work well with the preprocessing of audio before Yamnet's MobileNet.

After tinkering and learning for a few weeks, I found a Yamnet model compiled for the Edge TPU (sadly without source code) and figured my best shot would be to try to recreate it in TF, then quantize , then compile to TFLite, then compile for the edge TPU. I'll also have to figure out how to set the weights - not sure if I have to/can do that pre or post quantization. Anyway, I've effectively recreated the model, but am having a hard time quantizing without a bunch of wacky behavior.

The model currently looks like this:
重新创建 Yamnet 边缘模型输入 重新创建 Yamnet 边缘模型输出

I want it to look like this:
Yamnet 边缘模型输入 Yamnet 边缘模型输出

For quantizing, I tried:

If you know a better way to achieve the long term goal than what I proposed, please (please please please) share, Otherwise, help on specific quant ops would be great! Also, reach out for clarity

I've ran into your same issues trying to convert the Yamnet model by tensorflow into full integers in order to compile it for Coral edgetpu and I think I've found a workaround for that.

I've been trying to stick to the tutorials posted in the section tflite-model-maker and finding a solution within this API because, for experience, I found it to be a very powerful tool.

If your goal is to build a model which is fully compiled for the edgetpu (meaning all layers, including input and output ones, being converted to int8 type) I'm afraid this solution won't fit for you. But since you posted you're trying to obtain a custom model with the same structure of:

Yamnet model compiled for the Edge TPU

then I think this workaround would help you.

When you train your custom model following the basic tutorial it is possible to export the custom model both in.tflite format

model.export(models_path, tflite_filename='my_birds_model.tflite')

and full tensorflow model:

model.export(models_path, export_format=[mm.ExportFormat.SAVED_MODEL, mm.ExportFormat.LABEL])

Then it is possible to convert the full tensorflow saved model to tflite format by using the following script:

import tensorflow as tf
import numpy as np
import glob
from scipy.io import wavfile


dataset_path = '/path/to/DATASET/testing/*/*.wav'

representative_data = []
saved_model_path = './saved_model'
samples = glob.glob(dataset_path)
input_size = 15600 #Yamnet model's input size
def representative_data_gen():
  for input_value in samples:
      sample_rate, audio_data = wavfile.read(input_value, 'rb')
      audio_data = np.array(audio_data)
      splitted_audio_data = tf.signal.frame(audio_data, input_size, input_size, pad_end=True, pad_value=0) / tf.int16.max #normalization in [-1,+1] range
      yield [np.float32(splitted_audio_data[0])]

tf.compat.v1.enable_eager_execution()

converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_path)
converter.experimental_new_converter = True #if you're using tensorflow<=2.2
converter.optimizations = [tf.lite.Optimize.DEFAULT]
#converter.inference_input_type = tf.uint8  # or tf.uint8
#converter.inference_output_type = tf.uint8  # or tf.uint8
converter.representative_dataset = representative_data_gen
tflite_model = converter.convert()
open(saved_model_path + "converted_model.tflite", "wb").write(tflite_model)

As you can see, the lines which tell the converter to change input/output type are commented. This is because Yamnet model expects in input normalized values of audio sample in the range [-1,+1] and the numerical representation must be float32 type. In fact the compiled model of Yamnet you posted uses the same dtype for input and output layers (float32).

That being said you will end up with a tflite model converted from the full tensorflow model produced by tflite-model-maker. The script will end with the following line:

fully_quantize: 0, inference_type: 6, input_inference_type: 0, output_inference_type: 0

and the inference_type: 6 tells you the inference operations are suitable for being compiled to coral edgetpu.

The last step is to compile the model. If you compile the model with the standard edgetpu_compiler command line:

edgetpu_compiler -s converted_model.tflite 

the final model would have only 4 operations which run on the EdgeTPU:

Number of operations that will run on Edge TPU: 4
Number of operations that will run on CPU: 53

You have to add the optional flag -a which enables multiple subgraphs (it is in experimental stage though)

edgetpu_compiler -sa converted_model.tflite 

After this you will have:

Number of operations that will run on Edge TPU: 44
Number of operations that will run on CPU: 13

And most of the model operations will be mapped to edgetpu, namely:

Operator                       Count      Status

MUL                            1          Mapped to Edge TPU
DEQUANTIZE                     4          Operation is working on an unsupported data type
SOFTMAX                        1          Mapped to Edge TPU
GATHER                         2          Operation not supported
COMPLEX_ABS                    1          Operation is working on an unsupported data type
FULLY_CONNECTED                3          Mapped to Edge TPU
LOG                            1          Operation is working on an unsupported data type
CONV_2D                        14         Mapped to Edge TPU
RFFT2D                         1          Operation is working on an unsupported data type
LOGISTIC                       1          Mapped to Edge TPU
QUANTIZE                       3          Operation is otherwise supported, but not mapped due to some unspecified limitation
DEPTHWISE_CONV_2D              13         Mapped to Edge TPU
MEAN                           1          Mapped to Edge TPU
STRIDED_SLICE                  2          Mapped to Edge TPU
PAD                            2          Mapped to Edge TPU
RESHAPE                        1          Operation is working on an unsupported data type
RESHAPE                        6          Mapped to Edge TPU

 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM