计算机系统应用教程网站

网站首页 > 技术文章 正文

78.人工智能——基于ImageAI的模型训练和对象检测

btikc 2024-09-30 12:58:47 技术文章 12 ℃ 0 评论

前面讲过,ImageAI是一个python库,能使简单的几行代码就可以实现图像预测,自定义图像预测,物体检测,视频检测,视频对象跟踪和图像预测训练。

图像预测可以参看:76.人工智能——基于ImageAI:实现图像识别

一、使用自带的模型文件进行对象检测

resnet50_coco模型下载:链接:https://github.com/OlafenwaMoses/ImageAI/releases/download/1.0/resnet50_coco_best_v2.0.1.h5

from imageai.Detection import ObjectDetection

detector = ObjectDetection()
detector.setModelTypeAsRetinaNet()
detector.setModelPath("model/resnet50_coco_best_v2.0.1.h5")
detector.loadModel()
detections = detector.detectObjectsFromImage(input_image="img/street.jpg", output_image_path="output/street_detected.jpg")

for eachObject in detections:
    print(eachObject["name"] + " : " + str(eachObject["percentage_probability"]))
    print("--------------------------------")

运行结果:

person : 63.008952140808105
--------------------------------
person : 80.95442056655884
--------------------------------
person : 69.07119750976562
--------------------------------
person : 58.298319578170776
--------------------------------
car : 73.18016290664673
--------------------------------
handbag : 54.369056224823
--------------------------------
person : 83.78970623016357
--------------------------------
car : 59.569525718688965
--------------------------------
car : 86.99244260787964
--------------------------------
person : 95.52204012870789
--------------------------------
person : 79.09015417098999
--------------------------------
person : 94.94342803955078
--------------------------------
bicycle : 99.3865966796875

二、基于yolo3模型训练

数据集是一个浣熊(raccoon),来源于网络。

训练时遇一小坑,这台电脑没有GPU配置,在训练的时候报错如下,后来把tensorflow版本降到1.13.1。问题解决。

AttributeError: ‘_TfDeviceCaptureOp’ object has no attribute ‘_set_device_from_string’
#训练代码
from imageai.Detection.Custom import DetectionModelTrainer
import tensorflow as tf

#设置训练数据目录
data_path = 'raccoon'
#创建训练器
trainer=DetectionModelTrainer()
trainer.setModelTypeAsYOLOv3()
trainer.setDataDirectory(data_directory=data_path)
#设置训练参数
trainer.setTrainConfig(object_names_array=["raccoon"],
                       batch_size=2, num_experiments=1,
                       train_from_pretrained_model="model/yolo.h5")
trainer.trainModel()
#训练过程数据
Epoch 1/1
  1/800 [..............................] - ETA: 11:32:27 - loss: 117.3804 - yolo_layer_1_loss: 15.3062 - yolo_layer_2_loss: 33.9420 - yolo_
  2/800 [..............................] - ETA: 7:29:24 - loss: 116.7755 - yolo_layer_1_loss: 15.0781 - yolo_layer_2_loss: 33.9727 - yolo_l

CPU环境下,训练过程是漫长的……………………………………………………

三、模型预测

from imageai.Detection.Custom import CustomObjectDetection

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

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

Tags:

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表