天池热身赛-布匹瑕疵目标检测

7 篇文章 0 订阅
5 篇文章 2 订阅

1、检测代码

代码源自datawhale官方提供baseline: https://github.com/datawhalechina/team-learning-cv/tree/master/DefectDetection

baseline使用的是yolov5,我的显卡只有一个1080Ti,所以先选择yolov5s进行训练,设置训练50个epoch, 图片大小设置为512x512。

这部分内容主要参考了https://blog.csdn.net/qq_26751117/article/details/113853150

  • 数据处理:主要是将比赛方提供的数据格式转化为yolo需要的格式,先使用convertTrainLabel.py转化,然后在运行process_data_yolo.py,就得到了数据,存放位置为process_data文件夹;注意需要修改process_data_yolo中val字段,全部改为train字段,运行两次,分别得到验证和训练的数据文件。
  • 预训练权重:尝试了一下不加载预训练权重,效果不是很好,可能是因为本来数据就比较少,还是需要进行迁移学习的。所以想办法下载了yolov5s.pt文件,进行了加载。由于模型比较小,可以设置较大的batch size, 这里是16。这里借上边那个文章的图,需要简单修改一下加载权重的部分。

  • 运行,简单修改了一下一些报错的点,然后就可以运行了yolov5s模型了。

2、docker提交

我的dockerfile文件:

# Base Images
FROM registry.cn-shanghai.aliyuncs.com/tcc-public/pytorch:1.4-cuda10.1-py3

ADD . /workspace

WORKDIR /workspace

RUN pip install -r requirements.txt

CMD ["sh", "run.sh"]

开始构建:

(torch16) pdluser@pdluser-System-Product-Name:~/project/tianchi_demo$ sudo docker build -t registry.cn-shenzhen.aliyuncs.com/nine_percent/tianchi_submit:1.0 .
[sudo] pdluser 的密码:
Sending build context to Docker daemon  6.778GB
Step 1/5 : FROM registry.cn-shanghai.aliyuncs.com/tcc-public/pytorch:1.4-cuda10.1-py3
 ---> 76c152fbfd03
Step 2/5 : ADD . /workspace
 ---> 10ca596f6d20
Step 3/5 : WORKDIR /workspace
 ---> Running in 37a88d04d2a9
Removing intermediate container 37a88d04d2a9
 ---> 7f7982fbfaba
Step 4/5 : RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple --ignore-installed PyYAML
 ---> Running in 877004f83473
Looking in indexes: https://mirrors.aliyun.com/pypi/simple 
  Downloading https://mirrors.aliyun.com/pypi/packages/ec/d6/a82d191ec058314b2b7cbee5635150f754ba1c6ffc05387bc9a57efe48b8/cryptacular-1.5.5.tar.gz
  Installing build dependencies: started
  Installing build dependencies: finished with status 'done'
  Getting requirements to build wheel: started
  Getting requirements to build wheel: finished with status 'done'
    Preparing wheel metadata: started
    Preparing wheel metadata: finished with status 'done'
Collecting zope.sqlalchemy
  Downloading https://mirrors.aliyun.com/pypi/packages/fa/83/459decec1dd2c14d60f9a360fff989c128abe545a1554a1da64b054a55d4/zope.sqlalchemy-1.3-py2.py3-none-any.whl
Collecting velruse>=1.0.3
  Downloading https://mirrors.aliyun.com/pypi/packages/8f/0b/d47ea894587f3155f8c4520aa74d57c856189d0bbe27e831881d655a3386/PasteDeploy-2.1.1-py2.py3-none-any.whl
Building wheels for collected packages: cryptacular
  Building wheel for cryptacular (PEP 517): started
  Building wheel for cryptacular (PEP 517): finished with status 'done'
  Created wheel for cryptacular: filename=cryptacular-1.5.5-cp37-abi3-manylinux2010_x86_64.whl size=52452 sha256=93037b68313c3d86df4c8cab9d0cc0866d1579cb7399410c7903b56eb2ff0067
  Stored in directory: /root/.cache/pip/wheels/dd/c7/11/721f100da8477396b1f8fcfa2d23c801d5bac07d0e2d82dc0d
Successfully built cryptacular
Building wheels for collected packages: apex, velruse, pbkdf2, anykeystore
  Building wheel for apex (setup.py): started
  Building wheel for apex (setup.py): finished with status 'done'
  Created wheel for apex: filename=apex-0.9.10.dev0-cp37-none-any.whl size=46468 sha256=c68745de219dd6169195cfec426e528cd5f5f932bd3cb7ddbc22817a9827cfea
  Stored in directory: /root/.cache/pip/wheels/b8/f0/7a/2fc4cf8a70bfc0981f7009a2146685d06ee220398c0b780acf
  Building wheel for velruse (setup.py): started
  Building wheel for velruse (setup.py): finished with status 'done'
  Created wheel for velruse: filename=velruse-1.1.1-cp37-none-any.whl size=50923 sha256=c300b70b745467b6b075bec09d6b2a11ab3524f6de31605431a62308613648e3
  Stored in directory: 
Successfully built apex velruse pbkdf2 anykeystore
Installing collected packages: PyYAML, Cython, numpy, opencv-python, typing-extensions, torch, pyparsing, kiwisolver, six, cycler, pillow
Removing intermediate container 877004f83473
 ---> 5c40d92c4bc1
Step 5/5 : CMD ["sh", "run.sh"]
 ---> Running in 41c2daf77fbc
Removing intermediate container 41c2daf77fbc
 ---> 603e3fe4452c
Successfully built 603e3fe4452c
Successfully tagged registry.cn-shenzhen.aliyuncs.com/nine_percent/tianchi_submit:1.0

在构建完镜像以后,进入镜像:

先查看一下对应的ID:

pdluser@pdluser-System-Product-Name:~$ sudo docker images
[sudo] pdluser 的密码:
REPOSITORY                                                      TAG                 IMAGE ID            CREATED             SIZE
registry.cn-shenzhen.aliyuncs.com/nine_percent/tianchi_submit   1.0                 b773b4e52e7a        4 minutes ago       11.2GB
<none>                                                          <none>              f99df53cc33c        23 hours ago        7.92GB
registry.cn-shanghai.aliyuncs.com/tcc-public/pytorch            1.4-cuda10.1-py3    76c152fbfd03        13 months ago       7.56GB
registry.cn-shanghai.aliyuncs.com/tcc-public/python             3                   a4cc999cf2aa        21 months ago       929MB

进入第一个镜像,b7:

(torch16) pdluser@pdluser-System-Product-Name:~/project/tianchi_demo$ sudo docker run -it b7 /bin/bash
root@2a128d20af63:/workspace#

在这里运行run.sh,测试成功就可以提交了。

下一步将镜像推送到Registry:

$ sudo docker login --username=用户名 registry.cn-shenzhen.aliyuncs.com
$ sudo docker tag [ImageId] registry.cn-shenzhen.aliyuncs.com/nine_percent/tianchi_submit:[镜像版本号]
$ sudo docker push registry.cn-shenzhen.aliyuncs.com/nine_percent/tianchi_submit:[镜像版本号]

3、遇到的问题

在进行build的时候,发现以下问题,ERROR: Double requirement given: PyYAML>=5.3 (from -r requirements.txt (line 10)) (already in PyYAML, name=‘PyYAML’)

通过把yaml的等级要求去掉,就不会报错了。

用到opencv的时候也出现了报错:

Python 3.7.4 (default, Aug 13 2019, 20:35:49)
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/conda/lib/python3.7/site-packages/cv2/__init__.py", line 5, in <module>
    from .cv2 import *
ImportError: libGL.so.1: cannot open shared object file: No such file or directory

解决方案是在dockerfile中添加以下内容:

RUN apt update
RUN apt install libgl1-mesa-glx
RUN apt-get install -y libglib2.0-0

但是会遇到以下问题:

这样改动dockfile,避免交互:

RUN DEBIAN_FRONTEND=noninteractive apt update -y
RUN DEBIAN_FRONTEND=noninteractive apt install libgl1-mesa-glx -y
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y libglib2.0-0

最终版本dockerfile:

# Base Images
FROM registry.cn-shanghai.aliyuncs.com/tcc-public/pytorch:1.4-cuda10.1-py3

#registry.cn-shanghai.aliyuncs.com/tcc-public/pytorch:1.4-cuda10.1-py3
ADD . /

WORKDIR /

RUN pip install -r requirements.txt -i https://mirrors.aliyun.com/pypi/simple

RUN DEBIAN_FRONTEND=noninteractive apt update -y
RUN DEBIAN_FRONTEND=noninteractive apt install libgl1-mesa-glx -y
RUN DEBIAN_FRONTEND=noninteractive apt-get install -y libglib2.0-0 -y

CMD ["sh", "run.sh"]

第一次提交出错:

啊,连续两次错误了。

之后调节了一下文件存放位置,和对应的命令,终于提交成功了,可喜可贺可喜可贺。

这个docker虽然很不错,但是入门还是有一定门槛的,我总结了一下使用过程中经常用到的知识点:https://blog.csdn.net/DD_PP_JJ/article/details/113902874 可以参考一下。

  • 5
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论
天池布匹瑕疵目标检测使用的是YOLO(You Only Look Once)算法。YOLO是一种实时目标检测算法,它能够在一张图像中同时检测出多个目标,并给出它们的位置和类别。 在天池布匹瑕疵目标检测比赛中,使用的是YOLOv3算法。YOLOv3是YOLO系列中的最新版本,相比于之前的版本,它在检测精度和速度上都有所提升。 要使用YOLOv3进行布匹瑕疵目标检测,首先需要准备好训练数据集和标注文件。然后,可以使用YOLOv3的预训练模型进行目标检测,或者自己训练一个模型。 以下是使用YOLOv3进行布匹瑕疵目标检测的步骤: 1. 准备数据集和标注文件:将布匹瑕疵的图像和对应的标注文件放在同一个文件夹中,标注文件应该包含每个目标的类别和边界框的位置信息。 2. 下载YOLOv3的预训练模型:可以从互联网上下载YOLOv3的预训练模型,例如Darknet官方网站提供的模型。 3. 安装YOLOv3的依赖库:需要安装OpenCV、NumPy和其他一些依赖库。 4. 加载模型和配置文件:使用OpenCV加载YOLOv3的模型和配置文件。 5. 运行目标检测:将布匹瑕疵的图像输入到YOLOv3模型中,模型会输出检测到的目标的类别和位置信息。 6. 可视化结果:可以将检测结果绘制在图像上,以便查看检测效果。 下面是一个使用YOLOv3进行布匹瑕疵目标检测的示例代码: ```python import cv2 import numpy as np # 加载模型和配置文件 net = cv2.dnn.readNetFromDarknet('yolov3.cfg', 'yolov3.weights') layer_names = net.getLayerNames() output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()] # 加载图像 image = cv2.imread('image.jpg') height, width, channels = image.shape # 对图像进行预处理 blob = cv2.dnn.blobFromImage(image, 0.00392, (416, 416), (0, 0, 0), True, crop=False) net.setInput(blob) # 运行目标检测 outs = net.forward(output_layers) # 解析检测结果 class_ids = [] confidences = [] boxes = [] for out in outs: for detection in out: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.5: # 计算边界框的位置 center_x = int(detection[0] * width) center_y = int(detection[1] * height) w = int(detection[2] * width) h = int(detection[3] * height) x = int(center_x - w / 2) y = int(center_y - h / 2) # 保存类别、置信度和边界框的位置信息 class_ids.append(class_id) confidences.append(float(confidence)) boxes.append([x, y, w, h]) # 非极大值抑制,去除重叠的边界框 indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) # 绘制检测结果 font = cv2.FONT_HERSHEY_SIMPLEX for i in range(len(boxes)): if i in indexes: x, y, w, h = boxes[i] label = str(class_ids[i]) confidence = confidences[i] color = (0, 255, 0) cv2.rectangle(image, (x, y), (x + w, y + h), color, 2) cv2.putText(image, label, (x, y - 10), font, 0.5, color, 2) # 显示结果图像 cv2.imshow("Image", image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 这是一个简单的示例代码,实际使用时可能需要根据具体情况进行调整和优化。
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

*pprp*

如果有帮助可以打赏一杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值