Convert SSD MobileNetV2 custom model to edgetpu model?

886 Views Asked by At

I'm trying to convert the mobilenetv2(ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8 ) model to EdgeTPU model for object detection. And ı'm using TF2.

I tried many solution but still have "cpu operations" when i convert tflite model to edge tpu. how can i convert whole mobilenet model?

Here my steps;

  1. Download pretrained model(mobilenetv2), prepate dataset(spesific class from coco) and train your model.
  2. Export checkpoints ./exporter_main_v2.py and now i have "saved_model.pb , assest, checkpoints" under saved_models files.
  3. Turn step2, install tf-nightly 2.5.0.dev20210218 and "export_tflite_graph_tf2.py" and export model now.
  4. This step, trying to quantize model for edgetpu(8bits) and converting to .tflite files. Scripts like these ;
  def representative_dataset_gen():
    for data in raw_test_data.take(10):
      image = data['image'].numpy()
      image = tf.image.resize(image, (320, 320))
      image = image[np.newaxis,:,:,:]
      image = image - 127.5
      image = image * 0.007843
      yield [image]
  
  raw_test_data, info = tfds.load(name="coco/2017", with_info=True, split="test", data_dir="/TFDS", download=False)

  converter =  tf.lite.TFLiteConverter.from_saved_model('~/saved_model')
  converter.optimizations = [tf.lite.Optimize.DEFAULT]
  converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8, tf.lite.OpsSet.SELECT_TF_OPS]
  converter.inference_input_type = tf.int8
  converter.inference_output_type = tf.int8
  converter.allow_custom_ops = True
  converter.representative_dataset = representative_dataset_gen
  tflite_model = converter.convert()
  
  with open('{}/model_full_integer_quant.tflite'.format('/saved_model'), 'wb') as w:

  w.write(tflite_model)
  1. After quantization visualize from netron and model properties like these ; enter image description here

  2. And i convert .tflite model to edgetpu model with "edgetpu_compiler" and my final models ; have 7 CPU operations enter image description here

0

There are 0 best solutions below