全球人工智能技术创新大赛【热身赛一】提交baseline

全球人工智能技术创新大赛【热身赛一】提交baseline

一、下载数据集

下载其中的guangdong1_round2_train2_20191004_Annotationsguangdong1_round2_train2_20191004_images放入train_data文件夹下,用于训练数据。

二、数据的预处理

使用的baseline是yolov5,需要将官方提交的数据格式转换为yolov5的数据格式,代码详见convertTrainLabel.py 。

 

process_data_yolo将原始标注图片及数据进行裁剪,需要注意的是训练集和验证集都要处理。

 

三、模型训练

1.数据路径设置: 编辑一个数据路径文件夹yaml文件

如新建data/tianchi_det.yaml

# train and val datasets (image directory or *.txt file with image paths)
train: ./process_data/images/train/
val: ./process_data/images/val/
​
# number of classes
nc: 15
​
# class names
names: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12','13', '14', '15']

2.模型文件选择: yolov5x.yaml, yolov5m.yaml, yolov5l.yaml yolov5s.yaml文件

可以选择合适的模型文件,从左到右精度下降,但是速率增大

3 .模型训练:

$ python train.py --data coco.yaml --cfg yolov5s.yaml --weights '' --batch-size                                                                                                        
                                     yolov5m                                40
                                     yolov5l                                24
                                     yolov5x                                16

3.1训练时遇到的错误

(1)module 'yaml' has no attribute 'FullLoader'

解决方案:pip install --ignore-installed PyYAML

 

(2)torch.nn.modules.module.ModuleAttributeError: 'BatchNorm2d' object has no attribute '_non_persistent_buffers_set'

 

(3)KeyError: "weights/yolov5s1.pt is not compatible with models/yolov5s1.yaml. This may be due to model differences or weights/yolov5s1.pt may be out of date. Please delete or update weights/yolov5s1.pt and try again, or use --weights '' to train from scratch."

遇到(2)、(3)这两个问题时,没法太好的解决办法,从yolov5官方重新下载最新的baseline(git上自行下载),并作以下修改:

将utils/utils.py复制到最新yolov5的utils目录下,并将utils/datasets.py里LoadImagesTest类复制到yolov5的utils/datasets.py中。(此处参考https://blog.csdn.net/weixin_39800983/article/details/113883462

可以正常训练,训练300轮结果如下图所示

 

四、使用Docker提交

1. 最新的yolov5 需要pytorch1.7,首先在Dockerfile构建一个基础的python3镜像

# Base Images
## 从天池基础镜像构建
FROM registry.cn-shanghai.aliyuncs.com/tcc-public/python:3
​
## 把当前文件夹里的文件构建到镜像的根目录下
ADD . /
​
## 指定默认工作目录为根目录(需要把run.sh和生成的结果文件都放在该文件夹下,提交后才能运行)
WORKDIR /
​
## 镜像启动后统一执行 sh run.sh
CMD ["sh", "run.sh"]

使用docker build -t 仓库地址:版本号 进行构建

 

2. 进入容器配置环境

(1)首先 docker images查看镜像ID

(2)接着dockers run -it 镜像ID /bin/bash

(3)安装需要的包,也可以直接用

pip install -i https://pypi.douban.com/simple/ -r requirements.txt

进行安装但总感觉反馈不及时,知道哪里出错了,故使用下面的命令一步一步安装,推荐使用豆瓣源,安装速度较快

pip install torch==1.7.1+cu101 torchvision==0.8.2+cu101 torchaudio===0.7.2 -f https://download.pytorch.org/whl/torch_stable.html -i https://pypi.douban.com/simple/
​
pip install -i https://pypi.douban.com/simple/ matplotlib
​
pip install -i https://pypi.douban.com/simple/ numpy
​
pip install -i https://pypi.douban.com/simple/ opencv-python
​
pip install -i https://pypi.douban.com/simple/ Pillow

(4)import cv2 会出错

解决方法:

apt update

apt install libgl1-mesa-glx

apt-get install -y libglib2.0-0

 

3.保存镜像

注意不要关闭容器

 

4. 测试镜像

如果没有相关的文件夹,可能会出现如下错误,是正常现象,推送镜像即可

UnboundLocalError:local variable 'files' referenced before assignment

 

5. 推送镜像

docker push 仓库地址即可

最后可以总算是运行出结果,但不理想,还有很多优化的空间

注:docker 复制文件

拷贝本地文件到容器docker cp 本地路径 容器ID:容器路径

 

参考

https://github.com/datawhalechina/team-learning-cv/tree/master/DefectDetection

https://blog.csdn.net/weixin_39800983/article/details/113883462

https://blog.csdn.net/qq_26751117/article/details/113853150

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值