Keras model in Coral accelerator

Keras with Coral accelerator

How to use Kares

Platform: Colab

keras guides

James training model

Coral use keras in colab

How to use coral accelerator

Coral accelerator setup in your computer

coral introduction(youtube)

coral document for convert model to TFlite

Achievement

  • I can run the coral accelerater in the Rpi4
  • Convert model from h5 to TFlite.
1
2
3
4
5
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()

with open('mobilenet_v2_1.0_224.tflite', 'wb') as f:
f.write(tflite_model)

Above is covert to TFlite , not just uint8 type TFlite

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# A generator that provides a representative dataset
def representative_data_gen():
dataset_list = tf.data.Dataset.list_files('/content/fruits-360_dataset/fruits-360/*/*.jpg')
#Input the dataset files
for i in range(100):
image = next(iter(dataset_list))
image = tf.io.read_file(image)
image = tf.io.decode_jpeg(image, channels=3)
image = tf.image.resize(image, [IMAGE_SIZE, IMAGE_SIZE])
image = tf.cast(image / 255., tf.float32)
image = tf.expand_dims(image, 0)
yield [image]

converter = tf.lite.TFLiteConverter.from_keras_model(model)
# This enables quantization
converter.optimizations = [tf.lite.Optimize.DEFAULT]
# This sets the representative dataset for quantization
converter.representative_dataset = representative_data_gen
# This ensures that if any ops can't be quantized, the converter throws an error
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
# For full integer quantization, though supported types defaults to int8 only, we explicitly declare it for clarity.
converter.target_spec.supported_types = [tf.int8]
# These set the input and output tensors to uint8 (added in r2.3)
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
tflite_model = converter.convert()

with open('mobilenet_v2_1.0_224_quant.tflite', 'wb') as f:
f.write(tflite_model)

Compile Edge TUP

1
2
3
4
5
6
7
8
9
! curl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

! echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.list

! sudo apt-get update

! sudo apt-get install edgetpu-compiler

! edgetpu_compiler mobilenet_v2_1.0_224_quant.tflite

Then download the model and labels to your computer, use them in your coral accelerator

1
2
3
4
python3 classify_image.py \
--model mobilenet_v2_1.0_224_quant_edgetpu.tflite \
--labels fruit_labels.txt \
--input fruit.jpg
  • Only tensorflow relative model can use in the coral accelerator, Keras is most adaptor on the coral.

Because coral accelerator flame is base on tensorflow.

Problem

  • I can’t use the coral in my pc where mac or windows or linux .

The one way would be remain me on there, the wheel is not suitable on your platform, even I try to install all of the coral wheels.

  • When I converter James model to TFlite , I can’t directly to converter it , still need to convert it to uint8 type TFlite.

How to convert uint8 type TFlite.

  • Compare value

I dont think the coral accelerator can use to training the model, its fuction is only to get up the accelerate for the detection

  • How do I setup my PC or Rpi if the coral can use to train the model.

Task

  • Compare model training speed in PC and Rpi with Coral
  • Compare Validation speed as use the test_dataset to train model once again in PC adn Rpi with coral.

Coral accelerator is not use in traning model, is only can use it accelerated ML inferencing to existing systems, if it can use to training how do I setup my computer or RPi.

  • Detection test Speed in PC with coral

I can’t use coral on the PC

  • Detection test Speed in Rpi with coral

  • Compare Rpi with coral or not on inference Speed

Compare the speed in same model and dataset

  • 30/11/22

  • Compare PC(Colab) inference speed in same dataset and models, diferent CPU or GPU or TPU.

  • Compare different traning speed in TPU ,GPU and CPU

  • Compare accuracy in different platform.

  • Compare diferent epoch number of the accuracy on the model.

  • Copyrights © 2022-2024 Jessy Huang
  • Visitors: | Views:

请我喝杯咖啡吧~