目录
1. MobileNet V3网络
2. 汽车颜色检测
2.1 数据集
2.2 训练参数
2.3 训练结果
2.4 可视化网页推理
3. 下载
1. MobileNet V3网络
MobileNet V3 相比MobileNet V2更加的准确,也同样是一个轻量级的网络,这里只做简单介绍,更为具体的可以参考:MobileNet V3 图像分类_mobilenetv3类的封装-CSDN博客
它的亮点如下:
-
更新了Block结构(bneck)
-
使用了NSA(Neural Architecture Search) 参数搜索
-
重新设计耗时层结构
MobileNet V3主要体现在以下几个方面:
-
h-swish 激活函数:
-
MobileNet V3引入了h-swish和h-sigmoid这两种硬激活函数。这些函数不仅提供了良好的非线性特性,而且计算成本更低,有助于提高模型的运行效率。
-
h-swish函数是对传统swish激活函数的简化,它通过乘以一个ReLU6激活的线性变换来实现。h-sigmoid则是对传统sigmoid函数的近似,使用ReLU6函数来实现。这些硬激活函数的优势在于它们可以轻松地在硬件上实现,并且计算量小,非常适合移动和嵌入式设备。
-
-
Squeeze-and-Excitation(SE)模块:
-
SE模块是一种注意力机制,它通过重新校准通道特征的重要性,增强了模型对有用特征的识别能力。
-
在MobileNet V3中,SE模块通过全局平均池化来压缩空间维度,然后通过两个全连接层来学习通道的重要性权重。这种方法允许网络更加关注于重要的特征,可以提高模型的性能,同时不会显著增加计算负担。
-
-
深度可分离卷积和线性瓶颈:
-
MobileNet V3继续采用深度可分离卷积,这种卷积方式将标准卷积分解为深度卷积和逐点卷积,有效减少了计算量。深度卷积能够捕捉到局部的特征,而逐点卷积则用于组合这些特征。
-
同时,线性瓶颈结构在保持模型表达能力的同时减少了特征维度,进一步降低了模型的复杂度。这种设计使得MobileNet V3在处理高分辨率图像时更加高效,同时保持了较小的模型大小。
-
-
神经架构搜索(NAS):
-
在MobileNet V3的设计过程中,谷歌利用神经架构搜索技术自动化地优化网络架构,选择最优的超参数和模块组合,以适应不同的计算环境。
-
NAS技术通过搜索大量的网络架构来找到最佳的配置,这种方法可以显著提高网络的性能,并且可以针对特定的硬件平台进行优化。
-
-
两个版本:Large和Small:
-
MobileNet V3推出了两个版本,Large和Small。Large版本适用于需要较高准确率但计算资源相对充足的环境,例如高端智能手机和平板电脑。而Small版本则更加轻量,适合资源受限的环境,如低端手机和物联网设备。
-
这两个版本的设计使得MobileNet V3能够灵活地应用于不同的设备和场景,为用户提供了更多的选择。
-
-
NetAdapt算法:
-
MobileNet V3使用了NetAdapt算法来确定卷积核和通道的最佳数量,以实现最佳的性能和效率。
-
NetAdapt算法通过模拟人类设计网络的过程,自动地调整网络的容量,以达到预定的性能目标。这种方法不仅减少了人为设计的复杂性,而且还能够确保网络在不同的设备上都能达到最优的性能。
-
2. 汽车颜色检测
MobileNet V3 实现的model部分代码如下面所示,这里如果采用官方预训练权重的话,会自动导入官方提供的最新版本的权重
2.1 数据集
数据集文件如下:
标签如下:
{"0": "black","1": "blue","2": "gold","3": "green","4": "grey","5": "pink","6": "purple","7": "red","8": "silver","9": "white"
}
其中,训练集的总数为3345,验证集的总数为1425
2.2 训练参数
训练的参数如下:
parser.add_argument("--model", default='small', type=str,help='small,large')parser.add_argument("--pretrained", default=True, type=bool) # 采用官方权重parser.add_argument("--freeze_layers", default=True, type=bool) # 冻结权重parser.add_argument("--batch-size", default=8, type=int)parser.add_argument("--epochs", default=20, type=int)parser.add_argument("--optim", default='AdamW', type=str,help='SGD,Adam,AdamW') # 优化器选择parser.add_argument('--lr', default=0.01, type=float)parser.add_argument('--lrf',default=0.01,type=float) # 最终学习率 = lr * lrfparser.add_argument('--save_ret', default='runs', type=str) # 保存结果parser.add_argument('--data_train',default='./data/train',type=str) # 训练集路径parser.add_argument('--data_val',default='./data/val',type=str) # 测试集路径
需要注意的是网络分类的个数不需要指定,摆放好数据集后,代码会根据数据集自动生成!
网络模型信息如下:
"train parameters": {"model version": "small","pretrained": true,"freeze_layers": true,"batch_size": 8,"epochs": 20,"optim": "AdamW","lr": 0.01,"lrf": 0.01,"save_folder": "runs"},"dataset": {"trainset number": 3345,"valset number": 1425,"number classes": 10},"model": {"total parameters": 1528106.0,"train parameters": 601098,"flops": 61464184.0},
2.3 训练结果
所有的结果都保存在 save_ret 目录下,这里是 runs :
weights 下有最好和最后的权重,在训练完成后控制台会打印最好的epoch
这里只展示部分结果:可以看到网络没有完全收敛,增大epoch会得到更好的效果
最后一轮结果:
"epoch:19": {"train info": {"accuracy": 0.8044843049303304,"black": {"Precision": 0.6638,"Recall": 0.8175,"Specificity": 0.9614,"F1 score": 0.7327},"blue": {"Precision": 0.8816,"Recall": 0.9019,"Specificity": 0.9777,"F1 score": 0.8916},"gold": {"Precision": 0.937,"Recall": 0.8095,"Specificity": 0.9975,"F1 score": 0.8686},"green": {"Precision": 0.9356,"Recall": 0.8456,"Specificity": 0.9922,"F1 score": 0.8883},"grey": {"Precision": 0.5277,"Recall": 0.54,"Specificity": 0.9524,"F1 score": 0.5338},"pink": {"Precision": 0.9338,"Recall": 0.7906,"Specificity": 0.9937,"F1 score": 0.8563},"purple": {"Precision": 0.9325,"Recall": 0.8085,"Specificity": 0.9926,"F1 score": 0.8661},"red": {"Precision": 0.8946,"Recall": 0.9709,"Specificity": 0.9824,"F1 score": 0.9312},"silver": {"Precision": 0.5336,"Recall": 0.5315,"Specificity": 0.9618,"F1 score": 0.5325},"white": {"Precision": 0.729,"Recall": 0.8269,"Specificity": 0.9716,"F1 score": 0.7749},"mean precision": 0.79692,"mean recall": 0.7842900000000002,"mean specificity": 0.97833,"mean f1 score": 0.7876},"valid info": {"accuracy": 0.7642105263104266,"black": {"Precision": 0.6024,"Recall": 0.8264,"Specificity": 0.9494,"F1 score": 0.6968},"blue": {"Precision": 0.8694,"Recall": 0.8694,"Specificity": 0.9759,"F1 score": 0.8694},"gold": {"Precision": 0.9057,"Recall": 0.7619,"Specificity": 0.9963,"F1 score": 0.8276},"green": {"Precision": 0.9448,"Recall": 0.8155,"Specificity": 0.9936,"F1 score": 0.8754},"grey": {"Precision": 0.5217,"Recall": 0.4687,"Specificity": 0.9576,"F1 score": 0.4938},"pink": {"Precision": 0.9217,"Recall": 0.7361,"Specificity": 0.993,"F1 score": 0.8185},"purple": {"Precision": 0.9138,"Recall": 0.6625,"Specificity": 0.9921,"F1 score": 0.7681},"red": {"Precision": 0.8651,"Recall": 0.9738,"Specificity": 0.9765,"F1 score": 0.9162},"silver": {"Precision": 0.4434,"Recall": 0.4352,"Specificity": 0.9552,"F1 score": 0.4393},"white": {"Precision": 0.6163,"Recall": 0.8833,"Specificity": 0.9494,"F1 score": 0.726},"mean precision": 0.7604299999999999,"mean recall": 0.7432799999999999,"mean specificity": 0.9739000000000001,"mean f1 score": 0.74311}
训练集和测试集的混淆矩阵:
ROC曲线和auc值:
2.4 可视化网页推理
推理是指没有标签,只有图片数据的情况下对数据的预测,这里使用了网页推理
值得注意的是,如果训练了自己的数据集,需要对infer脚本进行更改,如下:
MODEL = 'small'
LABELS = r'D:\project\MobileNetV3系列\runs\class_indices.json'
PTH = r'D:\project\MobileNetV3系列\runs\weights\best.pth'
IMAGE_PATH = r'D:\project\MobileNetV3系列\data\train\black\0a9a7e401c.jpg'
运行:
streamlit run D:\project\MobileNet V3\infer.py
3. 下载
关于本项目代码和数据集、训练结果的下载:
计算机视觉实战:基于MobileNetV3模型实现的图像识别项目:汽车颜色分类资源-CSDN文库
关于Ai 深度学习图像识别、医学图像分割改进系列:AI 改进系列_听风吹等浪起的博客-CSDN博客
神经网络改进完整实战项目:改进系列_听风吹等浪起的博客-CSDN博客