Movidius VPU移植ssd_mobilenet问题记录

本文记录了在尝试将ssd_mobilenet模型移植到Movidius VPU过程中遇到的问题,包括input node找不到、不支持多个输出节点以及张量类型不支持等挑战,同时探讨了可能的解决思路。

ssd_mobilenet移植到Movidius到目前为止我还没移植成功,这里记录一下遇到的一些问题,以及后续的研究方向。

首先,我们在 detection_models_zoo中把ssdlite_mobilenet_v2_coco算法模型下载下来,下载后的tar包中有如下__图二__中的几个文件:

这里写图片描述
checkpoint:这个文件是检查点文件,该文件是在网络训练过程中通过tf.train.Saver保存的当前批次训练结束后的检查点文件。
frozen_inference_graph.pb:这个文件是生成的一个可以用来导入到tflite中的计算图,我们在tensorflow中调用该模型进行目标检测时正是通过解析该文件来获取计算图和参数集。
model.ckpt.*:这三个文件和前面的__checkpoint__文件是对应出现的,model.ckpt.data*是所有weights和bias的值,model.ckpt.meta是计算图,model.ckpt.index是索引文件。
pipeline.config:是保存一些模型入参的配置文件,是用来我们训练自己的模型时进行调参用。

所以按照ncsdk/docs/tf_compile_guidance.html描述,再结合上面这几个文件作用的分析,我使用的编译命令如下:

mvNCCompile frozen_inference_graph.pb -s 12 -in input -on output -o ssd_mobilenet.graph
mvNCCompile:Ncsdk的编译工具,用来生成计算图。
-s:  使用SHAVE的个数。
-in: 网络模型中输入层node的名字。
-on: 网络模型中输出层node的名字。
-o:  编译后生成的文件名。

运行之后,问题来了:
问题一:input node找不到

$ mvNCCompile frozen_inference_graph.pb -s 12 -in input -on output -o ssd_mobilenet.graph
mvNCCompile v02.00, Copyright @ Intel Corporation 2017

/usr/local/lib/python3.5/dist-packages/tensorflow/python/util/tf_inspect.py:45: DeprecationWarning: inspect.getargspec() is deprecated, use inspect.signature() instead
Traceback (most recent call last):
  File "/usr/local/bin/mvNCCompile", line 169, in <module>
    create_graph(args.network, args.image, args.inputnode, args.outputnode, args.outfile, args.nshav
在树莓派小车上部署和使用 MobileNetSSD 模型进行目标检测是一项常见的嵌入式 AI 任务。以下是一个详细的教程,涵盖从环境搭建到模型部署的全过程。 ### ### 环境准备 1. **操作系统安装** 使用官方推荐的 Raspberry Pi OS(如 Raspbian Bullseye)作为系统基础。可以通过 [Raspberry Pi Imager](https://www.raspberrypi.com/software/) 工具烧录系统镜像。 2. **更新系统与安装依赖库** 安装必要的开发工具和 OpenCV 库: ```bash sudo apt update && sudo apt upgrade -y sudo apt install -y build-essential cmake git libopencv-dev python3-opencv python3-pip ``` 3. **Python 虚拟环境配置(可选)** 创建虚拟环境以避免依赖冲突: ```bash python3 -m venv mobilenetssd_env source mobilenetssd_env/bin/activate pip install opencv-python numpy ``` ### ### 下载 MobileNetSSD 模型文件 根据引用信息,可以下载所需的模型文件[^1]: - `MobileNetSSD_deploy.prototxt`:网络结构定义文件。 - `MobileNetSSD_deploy.caffemodel`:预训练权重文件。 可通过以下命令下载模型文件(假设已获取有效链接): ```bash wget https://example.com/path/to/MobileNetSSD_deploy.prototxt wget https://example.com/path/to/MobileNetSSD_deploy.caffemodel ``` 若模型托管在 GitHub 上,也可以直接克隆相关仓库[^2]: ```bash git clone https://github.com/PINTO0309/MobileNet-SSD-RealSense.git cd MobileNet-SSD-RealSense/caffemodel/MobileNetSSD/ ``` ### ### 编写目标检测脚本 使用 OpenCV 的 `dnn` 模块加载 Caffe 模型并执行推理: ```python import cv2 import numpy as np # 加载模型 net = cv2.dnn.readNetFromCaffe("MobileNetSSD_deploy.prototxt", "MobileNetSSD_deploy.caffemodel") # 类别标签(常见对象) CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat", "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person", "pottedplant", "sheep", "sofa", "train", "tvmonitor"] # 颜色映射 COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3)) # 打开摄像头 cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() if not ret: break (h, w) = frame.shape[:2] blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 0.007843, (300, 300), 127.5) net.setInput(blob) detections = net.forward() # 遍历检测结果 for i in range(detections.shape[2]): confidence = detections[0, 0, i, 2] if confidence > 0.2: # 设置置信度阈值 idx = int(detections[0, 0, i, 1]) box = detections[0, 0, i, 3:7] * np.array([w, h, w, h]) (startX, startY, endX, endY) = box.astype("int") label = f"{CLASSES[idx]}: {confidence:.2f}" cv2.rectangle(frame, (startX, startY), (endX, endY), COLORS[idx], 2) y = startY - 15 if startY - 15 > 15 else startY + 15 cv2.putText(frame, label, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2) cv2.imshow("Frame", frame) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() ``` ### ### 性能优化建议 由于树莓派资源有限,建议采取以下措施提升性能: - **降低输入分辨率**:将输入图像尺寸调整为更小(如 224x224),但需修改 prototxt 文件中的输入层设置。 - **使用 FP16 推理模式**:如果模型支持,可以在加载时启用半精度浮点数计算。 - **关闭不必要的服务**:释放 CPU 和内存资源,例如关闭桌面环境运行 headless 模式。 - **硬件加速**:尝试使用 Coral Edge TPU 或 VPU 设备进行推理加速。 ### ### 注意事项 - 确保模型文件路径正确,否则会报错“无法读取模型”。 - 若使用 USB 摄像头,请确认设备权限已设置(如 `sudo chmod 666 /dev/video0`)。 - 树莓派上运行深度学习模型时,注意散热和供电稳定性。 ---
评论 17
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值