ImageAI(2)——6行代码训练自己的yolov3模型(python)

1、准备自己的数据集

1、确定要检测的对象类型,并为每个对象收集大约200张(最低推荐值)或更多图片
2、收集图像后,需要注释图像中的对象。ImageAI使用 Pascal VOC格式进行图像注释。使用易于使用的LabelImg图像注释工具为图像生成此注释。
3、完成对图像的注释后,将为数据集中的每个图像生成XML文件。该XML文件描述了每一个或所有图像中的物体。
4、获得所有图像的标注文件后,为数据集创建一个文件夹(例如耳机),并在此父文件夹中创建子文件夹train 和validation。
5、在train 文件夹中,创建images 和annotations 子文件夹。将每个对象图像的数据集的大约70-80%放在images文件夹中,并将这些图像的相应XML文件放在annotations 文件夹中。
6、在validation文件夹中,创建images 和annotations 子文件夹。将其余的数据集图像放入images文件夹中,并将这些图像的相应XML文件放入annotations 文件夹中。
7、完成此操作后,图像数据集文件夹的结构应如下所示:
在这里插入图片描述
8、可以完全从零开始训练您的自定义检测模型,也可以从预先训练的YOLOv3模型中使用转移学习(为获得更好的准确性而推荐)

2、训练模型

我们创建了hololens文件夹来存放数据和一系列文件,在文件夹下进行训练。代码如下:

from imageai.Detection.Custom import DetectionModelTrainer

# 定义模型训练器
trainer = DetectionModelTrainer()
# 设置网络类型
trainer.setModelTypeAsYOLOv3()
# 设置要在其上训练网络的图像数据集的路径
trainer.setDataDirectory(data_directory="hololens")
trainer.setTrainConfig(object_names_array=["hololens"], batch_size=4, num_experiments=200, train_from_pretrained_model="pretrained-yolov3.h5")
# 当训练用于检测多个对象时,设置object_names_array=["object1", "object2", "object3",..."objectz"]
trainer.trainModel()

只需6行代码,您就可以在自定义数据集上训练对象检测模型

trainer.setTrainConfig(object_names_array=["hololens"], batch_size=4, num_experiments=200, train_from_pretrained_model="pretrained-yolov3.h5")

上面的行中,我们配置了检测模型训练器。我们在函数中声明的参数如下:

num_objects:这是一个包含我们数据集中的对象名称的数组 batch_size:这是说明训练的批量大小
num_experiments:这是用于说明网络将对所有训练图像进行训练的次数,也称为时期
train_from_pretrained_model(可选):这是使用来自预训练的YOLOv3模型的转移学习进行训练

对于实验后损失的每一次下降,都会将模型保存在hololens / models文件夹中。损失越小,模型越好。
培训的Tensorboard报告文件将保存在hololens / logs文件夹中
完成训练后后,models文件夹会保存检测模型和detection_config.json文件可以用来执行目标检测。

3、调用训练好的模型进行目标检测

from imageai.Detection.Custom import CustomObjectDetection

detector = CustomObjectDetection()
# 创建该类实例,并将模型类型设置为YOLOv3
detector.setModelTypeAsYOLOv3()
# 指定了模型文件的文件路径
detector.setModelPath("hololens-ex-60--loss-2.76.h5")
# 指定了detection_config.json文件的路径
detector.setJsonPath("detection_config.json")
# 并在第三行中加载了模型
detector.loadModel()
# 运行了detectObjectsFromImage()函数并解析了测试图像的路径以及该函数将保存的新图像的路径。然后,该函数返回一个字典数组,每个字典对应于图像中检测到的对象数
detections = detector.detectObjectsFromImage(input_image="holo2.jpg", output_image_path="holo2-detected.jpg")


for detection in detections:
# 每个字典都具有属性name(对象的名称), percentage_probability(检测的概率百分比)和box_points(对象的边界框的x1,y1,x2和y2坐标)。
    print(detection["name"], " : ", detection["percentage_probability"], " : ", detection["box_points"])

4、评估保存的检测模型的mAP

(1)单一模型评估:要评估单个模型,只需使用下面的示例代码,并提供数据集目录的路径,模型文件以及在培训期间保存的detection_config.json文件。

from imageai.Detection.Custom import DetectionModelTrainer

trainer = DetectionModelTrainer()
trainer.setModelTypeAsYOLOv3()
trainer.setDataDirectory(data_directory="hololens")
metrics = trainer.evaluateModel(model_path="detection_model-ex-60--loss-2.76.h5", json_path="detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5)

输出为:

Model File:  hololens_detection_model-ex-09--loss-4.01.h5 
Using IoU :  0.5
Using Object Threshold :  0.3
Using Non-Maximum Suppression :  0.5
hololens: 0.9613
mAP: 0.9613
===============================

(2)多模型评估:要评估所有保存的模型,只需在包含模型的文件夹的路径中将其解析为model_path

from imageai.Detection.Custom import DetectionModelTrainer

trainer = DetectionModelTrainer()
trainer.setModelTypeAsYOLOv3()
trainer.setDataDirectory(data_directory="hololens")
metrics = trainer.evaluateModel(model_path="hololens/models", json_path="hololens/json/detection_config.json", iou_threshold=0.5, object_threshold=0.3, nms_threshold=0.5)

输出为训练过程中所有的模型的评估结果:

Model File:  hololens/models/detection_model-ex-07--loss-4.42.h5 
Using IoU :  0.5
Using Object Threshold :  0.3
Using Non-Maximum Suppression :  0.5
hololens: 0.9231
mAP: 0.9231
===============================
Model File:  hololens/models/detection_model-ex-10--loss-3.95.h5 
Using IoU :  0.5
Using Object Threshold :  0.3
Using Non-Maximum Suppression :  0.5
hololens: 0.9725
mAP: 0.9725
===============================
Model File:  hololens/models/detection_model-ex-05--loss-5.26.h5 
Using IoU :  0.5
Using Object Threshold :  0.3
Using Non-Maximum Suppression :  0.5
hololens: 0.9204
mAP: 0.9204
===============================
Model File:  hololens/models/detection_model-ex-03--loss-6.44.h5 
Using IoU :  0.5
Using Object Threshold :  0.3
Using Non-Maximum Suppression :  0.5
hololens: 0.8120
mAP: 0.8120
===============================

总结

ImageAI支持YOLOV3、TinyYOLOV3和RetinaNet三种目标检测网络,所以可以用同样的方法训练另外两个模型。

  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 9
    评论
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值