旭日X3派具有最高5TOPS的等效算力
(X3M BPU主频1.2GHz、算力5TOS; X3E BPU 主频0.6GHZ,算力3TOPS),
因此在开发板上可以运行丰富的AI算法。
在系统/app/ai_inference目录下提供了基础算法示例:
01_basic_sample
系统给出的测试程序只能对给定的斑马图片(zebra_cls.jpg)进行检测,
对其进行少许改造、使其能够对给定的任意图片进行检测,
同时能够输出对象分类ID、置信度和对象名称。
修改后的测试程序目录结构如下:
修改要点:
1)将imagenet1000_clsidx_to_labels.tx文件转换成imagenet1000_clsidx_to_labels.json
2) 增加image目录
存储测试图片,本次测试使用以下4张图片
3)修改test_mobilenetv1.py
#!/usr/bin/env python3
from hobot_dnn import pyeasy_dnn as dnn
import numpy as np
import cv2
import sys
import json
def bgr2nv12_opencv(image):
height, width = image.shape[0], image.shape[1]
area = height * width
yuv420p = cv2.cvtColor(image, cv2.COLOR_BGR2YUV_I420).reshape((area * 3 // 2,))
y = yuv420p[:area]
uv_planar = yuv420p[area:].reshape((2, area // 4))
uv_packed = uv_planar.transpose((1, 0)).reshape((area // 2,))
nv12 = np.zeros_like(yuv420p)
nv12[:height * width] = y
nv12[height * width:] = uv_packed
return nv12
def print_properties(pro):
print("tensor type:", pro.tensor_type)
print("data type:", pro.dtype)
print("layout:", pro.layout)
print("shape:", pro.shape)
def get_hw(pro):
if pro.layout == "NCHW":
return pro.shape[2], pro.shape[3]
else:
return pro.shape[1], pro.shape[2]
if __name__ == '__main__':
#input a picture file
if len(sys.argv) == 1:
print("please input picture!!!\nusage: test_mobilenetv1.py filename\n")
exit(1)
filename = sys.argv[1]
#load object classify infomation
data_base = None
with open("./imagenet1000_clsidx_to_labels.json", "r") as f:
data_base = json.load(f)
#load inference modle file
models = dnn.load('../models/mobilenetv1_224x224_nv12.bin')
#load image file and process
img_file = cv2.imread(filename)
h, w = get_hw(models[0].inputs[0].properties)
des_dim = (w, h)
resized_data = cv2.resize(img_file, des_dim, interpolation=cv2.INTER_AREA)
nv12_data = bgr2nv12_opencv(resized_data)
#ai inference
outputs = models[0].forward(nv12_data)
cls_id = np.argmax(outputs[0].buffer)
confidence = outputs[0].buffer[0][np.argmax(outputs[0].buffer)]
#print result
print("#"*20, "Result", "#"*20)
print("->Object Class Id: %d" % (cls_id))
print("->Confidence: %f" % (confidence))
print("->Object Name: ", (data_base[str(cls_id)]))
print("#"*48)
测试用例:
1)斑马检测
2)老虎检测
3)大鹅检测
4)卷纸检测
通过以上4个测试用例的运行,可以体验到旭日X3派检测分类准确率和效率还是非常高的。
imagenet1000支持1000个物体类别,更多的物体检测和分类,
可以通过增加image中的图片进行测试体验。
文中所有文件,均已打包在附件中,见如下链接:
https://download.csdn.net/download/bingdund/89309722
参考资料:
3.1. 模型推理示例 — 旭日X3派用户手册 1.0.1 文档https://developer.horizon.ai/api/v1/fileData/documents_pi/Samples/AI_Inference.html