[RPI.CM4] Coral TPU Accelerator

2023-12-03 08:40
文章标签 tpu rpi accelerator cm4 coral

本文主要是介绍[RPI.CM4] Coral TPU Accelerator,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

官网链接:https://coral.ai/products/accelerator/

1. Edge TPU runtime

A. Add Debian package repository to system:

echo "deb https://packages.cloud.google.com/apt coral-edgetpu-stable main" | sudo tee /etc/apt/sources.list.d/coral-edgetpu.listcurl https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -sudo apt update

B. Install the Edge TPU runtime:

sudo apt install libedgetpu1-std

 

sudo apt remove  python3-apt
sudo apt install python3-apt

2. PyCoral Library

PyCoral是一个建立在TensorFlow Lite库之上的Python库,以加快你的开发速度,并为Edge TPU提供额外的功能。

A. Install Pycoral on Linux

sudo apt-get install python3-pycoral# or with pippython3 -m pip install --extra-index-url https://google-coral.github.io/py-repo/ pycoral~=2.0

3. 测试

A.下载案例代码

mkdir coral && cd coralgit clone https://github.com/google-coral/pycoral.gitcd pycoral

B. 下载模型,标签和图片

bash examples/install_requirements.sh classify_image.py

C. 运行图片分类器

python3 examples/classify_image.py \
--model test_data/mobilenet_v2_1.0_224_inat_bird_quant_edgetpu.tflite \
--labels test_data/inat_bird_labels.txt \
--input test_data/parrot.jpg

 

4. 加速自己的模型

A. 将tensorflow saved model 转成 .tflite model(整形量化)

train_dataset = reader.train_reader_tfrecord(data_path=config.trainpath,num_epochs=1,batch_Size=128)for batch_idx, data_batch in enumerate(train_dataset):global datadata = data_batch['data'].numpy().reshape(-1, 128, 54, 1)breakdef representative_data_gen():global datafor input_value in tf.data.Dataset.from_tensor_slices(data).batch(1).take(100):yield [input_value]converter = tf.lite.TFLiteConverter.from_saved_model("saved_model/")
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_data_gen
# Ensure that if any ops can't be quantized, the converter throws an error
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
# Set the input and output tensors to uint8 (APIs added in r2.3)
converter.inference_input_type = tf.uint8
converter.inference_output_type = tf.uint8
tflite_model_quant = converter.convert()# Save the model.
tflite_models_dir = pathlib.Path("tflite_models/")
tflite_models_dir.mkdir(exist_ok=True, parents=True)# Save the quantized model:
tflite_model_quant_file = tflite_models_dir/"model_quant.tflite"
tflite_model_quant_file.write_bytes(tflite_model_quant)
print("OK")

B. 将量化的.tflite model 转成TPU用的edgetpu.tflite model 

使用edgetpu-compiler软件,不过该软件已经不支持ARM,可以使用网络转换器

https://colab.research.google.com/github/google-coral/tutorials/blob/master/compile_for_edgetpu.ipynb#scrollTo=x47uW_lI1DoV

 

 

C. 加载并推理

有时候无法使用TPU,可能是因为Edge TPU runtime的版本不匹配(太新了),https://coral.ai/docs/edgetpu/compiler/#compiler-and-runtime-versions

apt list | grep libedgetpu1-std

 从官网下载其他版本的Edge TPU runtime并安装,Software | Coral

# Load the TFLite model and allocate tensors.
interpreter = tflite.Interpreter(model_path="../tflite_model/model_quant_edgetpu.tflite",experimental_delegates=[tflite.load_delegate('libedgetpu.so.1.0')])
interpreter.allocate_tensors()input_details = interpreter.get_input_details()output_details = interpreter.get_output_details()# 260msmerge_feature = tfLite.feature(audio_data, dis_data, mpu_x, mpu_y, mpu_z)# Test the model on random input data.# RPI4 USB3.0# unit8    45ms without tpu, 20ms with tpu# float32, 55ms without tpu# CM4 USB2.0# unit8 44ms without tpu, 77ms with tpu# float 48ms without tpuinterpreter.set_tensor(input_details[0]['index'], merge_feature.astype(dtype=np.uint8).reshape(1, 128, 54, 1))interpreter.invoke()# The function `get_tensor()` returns a copy of the tensor data.# Use `tensor()` in order to get a pointer to the tensor.output_data = interpreter.get_tensor(output_details[0]['index'])

 运行以上代码,如果USB TPU的灯闪烁,说明可以正常运行。

这篇关于[RPI.CM4] Coral TPU Accelerator的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/448813

相关文章

ArmSoM CM5 RK3576核心板推出,强势替代树莓派CM4

ArmSoM团队隆重推出全新的CM5 RK3576核心板,这款模块专为嵌入式开发者设计,凭借其强大的性能与丰富的扩展性,完美替代树莓派CM4,成为开发者们的理想选择。 CM5核心板采用了先进的RK3576 SoC,凭借卓越的计算能力和出色的能效表现,在各类应用中表现出色。相比树莓派CM4,CM5核心板在多个方面实现了显著的升级,不仅性能更强,还拥有更多的接口和扩展选项,适合多样化的应用需求。

树莓派(RPI/Raspberry Pi):一款基于ARM的微型电脑主板【为学习计算机编程教育而设计,只有信用卡大小的微型电脑,其系统基于Linux】

Raspberry Pi(中文名为“树莓派”,简写为RPi,(或者RasPi / RPI) 是为学习计算机编程教育而设计,只有信用卡大小的微型电脑,其系统基于Linux。 随着Windows 10 IoT的发布,我们也将可以用上运行Windows的树莓派。 自问世以来,受众多计算机发烧友和创客的追捧,曾经一“派”难求。别看其外表“娇小”,内“芯”却很强大,视频、音频等功能通通皆有,可谓是“麻雀虽

AI芯片:Edge TPU(谷歌出品)【在边缘(edge)设备上运行的“专用集成芯片”】【量化操作:Edge TPU使用8 位权重进行计算,而通常使用32位权重。所以我们应该将权重从32位转换为8位】

谷歌Edge TPU的价格不足1000人民币,远低于TPU。实际上,Edge TPU基本上就是机器学习的树莓派,它是一个用TPU在边缘进行推理的设备。 一、云vs边缘 1、边缘运行没有网络延迟 Edge TPU显然是在边缘(edge)运行的,但边缘是什么呢?为什么我们不选择在云上运行所有东西呢? 在云中运行代码意味着使用的CPU、GPU和TPU都是通过浏览器提供的。边缘与云相反,即在

2022年了,PyTorch、TensorFlow选哪个【模型可用性、部署便捷性、生态系统(嵌入式/物联网首选:TFLite + Coral)】

坊间传闻:「TensorFlow 适合业界,PyTorch 适合学界」。都 2022 年了,还是这样吗? 快到 2022 了,你是选 PyTorch 还是 TensorFlow?之前有一种说法:TensorFlow 适合业界,PyTorch 适合学界。这种说法到 2022 年还成立吗?在这篇文章中,作者从模型可用性、部署便捷度和生态系统三个方面对比了两个框架的优缺点,并针对不同身份的读者给

cm4 vl805-Q6 应用

执行命令"sudo -E rpi-eeprom-config --edit" 在引导程序中添加"VL805=1". 通过执行"sudo rpi-eeprom-config"查看是否更改成功 最后执行"sudo reboot"重启。 pi@raspberrypi:/ $ sudo rpi-eeprom-config [all]BOOT_UART=0WAKE_ON_GPIO=1POWE

算力巅峰对决,一文读懂CPU、GPU、GPGPU、FPGA、DPU、TPU

    通俗理解CPU、GPU、GPGPU、FPGA、DPU、TPU     每个处理器都有它的独特之处和擅长领域,它们共同构成了现代计算的多彩世界。 1. CPU - 中央处理单元 CPU,城市的市中心,精通从基础计算到复杂逻辑决策的各项任务。它高效执行操作指令,轻松应对日常任务如网页浏览和文档编辑。尽管多才多艺,面对超复杂或特定任务

TFLite: 编译(rpi)

看rpi的编译过程,分析TFLite的代码组成,参考tensorflow/lite/g3doc/rpi.md。 .sh相关目录修改 因为lite代码从contrib移动到上级目录,而rpi编译相关的sh并没有做对应的修改如: tensorflow/lite/tools/make/download_dependencies.sh -cd "$SCRIPT_DIR/../../../../..

树莓派安装wiringPi,BCM2835,以及python的RPI.GPIO

转载出处: http://www.waveshare.net/study/article-742-1.html     一、wiringPi WiringPi是应用于树莓派平台的GPIO控制库函数,WiringPi中的函数类似于Arduino的wiring系统。官网:http://wiringpi.com/ 1、wiringPi安装 (1)方案1-使用GIT工具 通过GIT获得wirin

TPU 和 GPU 的区别与相似点

TPU 和 GPU 的区别与相似点 TPU(Tensor Processing Unit) 概述: TPU 是谷歌专门为加速机器学习工作负载而设计的专用芯片。主要用于深度学习模型的训练和推理。 设计目标: 高效执行矩阵运算,特别是用于神经网络中的张量计算。优化了低精度计算(如 bfloat16),在不显著降低精度的情况下提高计算速度。 硬件架构: 采用了大量的矩阵乘法单元,专门用于加

FPGA第二篇,FPGA与CPU GPU APU DSP NPU TPU 之间的关系与区别

简介:首先,FPGA与CPU GPU APU NPU TPU DSP这些不同类型的处理器,可以被统称为"处理器"或者"加速器"。它们在计算机硬件系统中承担着核心的计算和处理任务,可以说是系统的"大脑"和"加速引擎"。这些处理器单元都是计算机系统中的关键组件,它们扮演着不同的角色,为计算机系统提供各种计算和处理能力。 FPGA ~ 第 2 篇   ——    FPGA、CPU、GPU、AP