700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Colab下Imageai自定义模型训练和目标检测

Colab下Imageai自定义模型训练和目标检测

时间:2023-04-07 09:51:19

相关推荐

Colab下Imageai自定义模型训练和目标检测

本文运行再colab环境下

目录

本文运行再colab环境下

1.准备

a.库的安装

b.colab环境的准备

2.模型的训练

a.准备数据集

b.训练

3.通过模型进行目标检测

4.注意:

1.准备

a.库的安装

依赖项:opencv、1.15>=tensorflow>=1.4、keras,

pip install imagea

b.colab环境的准备

修改——笔记本设置——选择GPU

然后运行以下命令,更换tf版本

!sudo pip install tensorflow-gpu==1.13.1

否则导致报错

最后,代码执行程序——重新启动代码执行程序。准备完成。

2.模型的训练

a.准备数据集

每个是识别的图片最好200张以上

之后通过lableimage标记图片,标记图片时候路径不要含有中文,可以新建一个Images文件保存图片

图片和xml名字例如:image(1).png,image(1).xml

建立如下目录

└───dataset├───train│ ├───annotations│ └───images└───validation├───annotations└───images

将所有图片和对应xml的百分之七八十分别放在train内的Images和annotations内

将剩余图片和对应xml放在validation内的annotations和images内

数据集至此准备完毕

b.训练

运行一下代码开始训练

from imageai.Detection.Custom import DetectionModelTrainertrainer = DetectionModelTrainer()# 训练的是YOLOV3模型trainer.setModelTypeAsYOLOv3()# 数据集地址trainer.setDataDirectory(data_directory="dataset")# 图片类别,batch_size,训练次数,train_from_pretrained_model="pretrained-yolov3.h5"可以决定是否采用转移训练trainer.setTrainConfig(object_names_array=["1", "2", "3","4", "5", "6","7", "8"],batch_size=2,num_experiments=10,)# In the above,when training for detecting multiple objects,#set object_names_array=["object1", "object2", "object3",..."objectz"]trainer.trainModel()

后续训练需要modle内的h5文件和json内的detection_config.json文件

3.通过模型进行目标检测

需要用到的文件:上面训练得到的h5和json文件,识别的图片

运行下列代码

from imageai.Detection.Custom import CustomObjectDetectiondetector = CustomObjectDetection()detector.setModelTypeAsYOLOv3()# h5文件地址detector.setModelPath("detection_model-ex-013--loss-0027.176.h5")# json文件地址detector.setJsonPath("detection_config.json")detector.loadModel()# 输入输出图片地址detections = detector.detectObjectsFromImage(input_image="metro1.png", output_image_path="metro1_new.png")for detection in detections:print(detection["name"], " : ", detection["percentage_probability"], " : ", detection["box_points"])

完成检测

4.注意:

卡在cpoch1/xxx不动,可能是电脑性能问题

数据集修改后,一定要删除除了train和annotations外的其他文件,再进行训练

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。