通过数字识别,来学习一下nn模块。顺便说下我病好了(doge)
咸鱼出品,能用就行~
选用版本:v831-m2dock-maixpy3-0.5.0-20220601
在人工智能中,面对大量用户输入的数据/素材,如果要在杂乱无章的内容准确、容易地识别,输出我们期待输出的图像/语音,并不是那么容易的。因此算法就显得尤为重要了。算法就是我们所说的模型。
我们玩硬件的时候用大佬提供的模型文件就行了。当然也可以自己训练。先学会用再说吧。
首先去官网下载MaixHub模型文件
将文件放入root/app(U盘里,镜像文件版本0.5.0)
运行main.py,首次请在串口工具下运行,方便调试。
from time import time
class Number:
mdsc_path = "./Number.mud"
labels = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'] #标签
anchors = [1.0, 5.0, 1.35, 5.42, 0.49, 2.55, 0.86, 3.75, 0.65, 4.38] #描点数据匹配度列表
def __init__(self) -> None:
from maix import nn
self.model = nn.load(self.mdsc_path)
from maix.nn import decoder
self.decoder = decoder.Yolo2(len(self.labels) , self.anchors , net_in_size = (224, 224) ,net_out_size = (7,7))
def __del__(self):
del self.model
del self.decoder
def cal_fps(self ,start , end):
one_second = 1
one_flash = end - start
fps = one_second / one_flash
return fps
def draw_rectangle_with_title(self ,img, box, disp_str , fps ):
img.draw_rectangle(box[0], box[1], box[0] + box[2], box[1] + box[3],color=(255, 0, 0), thickness=2)
img.draw_string(box[0], box[1]+ box[3] ,disp_str, scale=2,color=(255, 0, 30), thickness=2)
img.draw_string(0, 0 ,'FPS :'+str(fps), scale=2 ,color=(0, 0, 255), thickness=2)
def process(self,input):
t = time()
out = self.model.forward(input, quantize=1, layout = "hwc")
boxes, probs = self.decoder.run(out, nms=0.5, threshold=0.3, img_size=(224,224))
for i, box in enumerate(boxes):
class_id = probs[i][0]
prob = probs[i][1][class_id]
# disp_str = "{}:{:.2f}%".format(self.labels[class_id], prob*100)
disp_str = "{}".format(self.labels[class_id])
fps = self.cal_fps(t, time())
self.draw_rectangle_with_title(input, box, disp_str, fps)
def main():
from maix import display, camera
app = Number()
camera.config((224,224))
while True:
img = camera.capture()
app.process(img)
display.show(img)
main()
效果如下,尽量只识别数字,碰到字母和数字混用的时候,不是很准。
整几个有用的就行~
from maix import nn
{
"param": "/root/models/sobel_int8.param",
"bin": "/root/models/sobel_int8.bin"
}
model = {
"param": "./number_awnn.param",
"bin": "./number_awnn.bin" #注意路径
}
# opt设置↓
options = {
"model_type": "awnn", #模型类别, 目前仅支持 awnn
"inputs": {
# 输入层, 字典形式, 关键字是层名称, 为字符串, 如果是加密模型, 需要使用整型
# 值是层形状, 为一个tuple类型:(h, w, c)
"input0": (224, 224, 3)
},
"outputs": { #输出层, 同理输入层. 支持多层输出
"output0": (7, 7, (1+4+len(labels))*5)
},
#如果在forward使参数standardize=True(默认为True), 则会使用这个参数对输入数据进行归一化, 计算方法为(x - mean) * norm;
"mean": [127.5, 127.5, 127.5],
"norm": [0.0078125, 0.0078125, 0.0078125], #定义参见上述mean描述
}
model_path = "./sobel_int8.mud" #注意路径
当model_path选择字符串格式时, 此项opt不用进行赋值,默认为None
返回 maix.nn.Model 对象,包含了一系列神经网络操作, maix.nn.load()
会返回其对象。
只能由具体的对象调用, 不能类直接调用
inputs: 输入, 可以是_maix_image的Image对象, 也可以是HWC排列的bytes对象, 也可以是HWC排列的numpy.ndarray对象(还未支持), 多层输入使用list(还未支持)
这个参数未来可能会进行优化
standardize: int 类型,默认为1, 当load()加载字典类型时, opt 的mean,norm参数对数据进行标准化;当load()加载字符串类型时,会根据mud文件自动进行标准化;置为0时不会对输入数据进行处理,则输入前需要将数据进行手动处理,处理方式跟模型训练时的数据处理一致
layout: "hwc" 或者 "chw", 默认设置为 "hwc"
debug: int 类型,默认为0,该值不为0时会打印debug信息和forward的推理耗时
maix.nn.decoder.Yolo2
YOLO V2 的后处理模块, 使用时需要创建一个对象,调用run方法对模型推理输出进行解码得到物体的坐标和类别.
decoder.run(out, nms=0.5, threshold=0.3, img_size=(224,224))
参数
- fmap: 网路输出的特征图, 一般是forward 函数的结果
- nms: 非极大值抑制(Non-MaximumSuppression), 用来去掉重复的框, IOU(两个框的重叠区域)大于这个值就只取概率大的一个, 取值范围:[0, 1], 默认值为 0.3
- threshold: 置信度阈值, 大于这个值才认为物体有效, 取值范围:[0, 1], 默认 0.5
- img_size: 源图像大小, tuple类型, 比如(320, 240), 这会使返回值的box 坐标自动计算为相对于源图的坐标, 并且类型为整型,默认None 则不会计算, box 返回相对值(百分比), 浮点类型, 范围:[0, 1]
返回值
[boxes, probs], list 类型, 可以参考上面的使用例子, 其中:
- boxes: list 类型, 每个元素也是一个list, 包含[x, y, w, h], 分别代表了框左上角坐标, 以及框的宽高
- probs: list 类型, 每个元素也是一个list, 包含[max_prob_idx, all_classes_probs]
- all_classes_probs: list 类型, 包含了该框所有类别的置信度
- max_prob_idx: 整型,代表了all_classes_probs中最大值的下标, 取值范围: [0, classes_num - 1]