一、山东第三届数据应用创新创业大赛未知工作服属性识别简介

山东省第三届数据应用创新创业大赛主赛场以“协同创新 强省惠民”为主题,加快推动公共数据资源开发利用,充分释放公共数据资源的经济价值和社会价值,集聚数据应用相关企业、团队、爱好者,打造一批具有创新性的数据产品、数据服务,促进发挥数据“优政、惠民、兴业”作用。地址如下:http://data.sd.gov.cn/cmpt/sd/home.html

此次比赛为其中的一个,安全方面的工服检测, 竞赛地址:http://data.sd.gov.cn/cmpt/cmptDetail.html?id=64

图片名称

1.竞赛背景

随着科技的发展,监控视频已被广泛应用于日常生活中。但是在实际的使用中,标注的训练数据往往需要花费大量的人力物力才可以获得,严重阻碍了视觉算法的应用和落地。在工作服属性检测中,不同的场地工作服样式基本相同,但是颜色却千差万别。所以要通过对已有数据的训练,对未知颜色和样式的工作服进行检测。

2.任务

工作服属性检测会提供白色厨师服训练数据和少量的工厂工作服训练数据。训练数据包括是否穿着工作服已经工作服的区域坐标。要求识别出是否穿着工作服。比赛分为初赛和复赛,初赛和复赛分别为不同的任务。

初赛任务:初赛将提供白色厨师服标注数据,需要选手建立迁移学习模型,去识别出测试集中不同颜色的厨师服属性。

3.数据

提供带有标注的白色厨师服训练数据900多张,其中标注文件为voc格式的xml文件。测试数据集白色厨师图片若干张,其他颜色厨师图片若干张。
*训练集标注准确率约97%。

初赛提供的训练数据集标注字段如下:

在这里插入图片描述

二、环境准备

此次比赛使用PaddleDetection端到端目标检测套件进行。

PaddleDetection提供了目标检测、实例分割、多目标跟踪、关键点检测等多种能力,具有以下特性

  • 模型丰富: 包含目标检测、实例分割、人脸检测等100+个预训练模型,涵盖多种全球竞赛冠军方案
  • 使用简洁:模块化设计,解耦各个网络组件,开发者轻松搭建、试用各种检测模型及优化策略,快速得到高性能、定制化的算法。
  • 端到端打通: 从数据增强、组网、训练、压缩、部署端到端打通,并完备支持云端/边缘端多架构、多设备部署。
  • 高性能: 基于飞桨的高性能内核,模型训练速度及显存占用优势明显。支持FP16训练, 支持多机训练。
# 下载PaddleDetection,当前版本为realse/2.3版本
!git clone https://gitee.com/paddlepaddle/PaddleDetection.git --depth=1
# 安装
%cd ~/PaddleDetection
# 安装依赖
!pip install -r requirements.txt
# 本地安装
!pip install -e ./
# 查看版本
!pip list|grep paddledet
paddledet              2.3.0           /home/aistudio/PaddleDetection
# 切换至尊版GPU环境32Gb显卡运行
!nvidia-smi
Mon Jan  3 12:20:32 2022       
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 418.67       Driver Version: 418.67       CUDA Version: 10.1     |
|-------------------------------+----------------------+----------------------+
| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |
| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |
|===============================+======================+======================|
|   0  Tesla V100-SXM2...  Off  | 00000000:05:00.0 Off |                    0 |
| N/A   34C    P0    39W / 300W |      0MiB / 32480MiB |      0%      Default |
+-------------------------------+----------------------+----------------------+
                                                                               
+-----------------------------------------------------------------------------+
| Processes:                                                       GPU Memory |
|  GPU       PID   Type   Process name                             Usage      |
|=============================================================================|
|  No running processes found                                                 |
+-----------------------------------------------------------------------------+

三、数据处理

1.数据基本情况

  • 训练集:提供带有标注的白色厨师服训练数据900多张,其中标注文件为voc格式的xml文件
  • 测试集:测试数据集白色厨师图片35张,其他颜色厨师图片49张

2.数据处理任务

  • 解压缩数据并重命名目录
  • 利用PaddleX套件把原始训练集按 8:2 分隔为 train 以及 val 数据集
# 解压缩数据集到data目录
# 此处建议解压到data目录,关闭时不保存,启动时速度较快,强烈推荐
%cd ~
!unzip -qoa data/data124244/省厅-未知工作服属性识别-算法赛-A榜数据.zip -d data
# 重命名数据集
!mv data/省厅-工服识别-算法赛-A榜数据 data/data_a
# 安装PaddleX
!pip install paddlex
# 按照PaddleX要求规范目录命名
!mv data/data_a/train/images data/data_a/train/JPEGImages 
# 利用PaddleX对数据集进行分隔
!paddlex --split_dataset --format VOC --dataset_dir data/data_a/train/ --val_value 0.2
2022-01-03 12:27:08 [INFO]	Dataset split starts...
2022-01-03 12:27:08 [INFO]	Dataset split done.
2022-01-03 12:27:08 [INFO]	Train samples: 736
2022-01-03 12:27:08 [INFO]	Eval samples: 183
2022-01-03 12:27:08 [INFO]	Test samples: 0
2022-01-03 12:27:08 [INFO]	Split files saved in data/data_a/train/

四、模型训练

此次计划选择 ** YOLOv3**

configs/yolov3/yolov3_darknet53_270e_voc.yml 配置为基础进行配置

1.总体配置

configs/yolov3/yolov3_darknet53_270e_voc.yml,该配置文件由5部分组成,可以极大方便切换其他网络。

_BASE_: [
  '../datasets/voc.yml',
  '../runtime.yml',
  '_base_/optimizer_270e.yml',
  '_base_/yolov3_darknet53.yml',
  '_base_/yolov3_reader.yml',
]

snapshot_epoch: 5
weights: output/yolov3_darknet53_270e_voc/model_final

# set collate_batch to false because ground-truth info is needed
# on voc dataset and should not collate data in batch when batch size
# is larger than 1.
EvalReader:
  collate_batch: false

2.数据集配置

重点配置数据集类型的 num_classes、数据路径等PaddleDetection/configs/datasets/voc.yml

metric: VOC
map_type: 11point
num_classes: 2

# 训练集
TrainDataset:
  !VOCDataSet
    dataset_dir: /home/aistudio/data/data_a/train/
    anno_path: /home/aistudio/data/data_a/train/train_list.txt
    label_list: /home/aistudio/data/data_a/train/labels.txt
    data_fields: ['image', 'gt_bbox', 'gt_class', 'difficult']

# 测试集
EvalDataset:
  !VOCDataSet
    dataset_dir: /home/aistudio/data/data_a/train/
    anno_path: /home/aistudio/data/data_a/train/val_list.txt
    label_list: /home/aistudio/data/data_a/train/labels.txt
    data_fields: ['image', 'gt_bbox', 'gt_class', 'difficult']

# 预测集
TestDataset:
  !ImageFolder
  	 # 预测标签配置
    anno_path: /home/aistudio/data/data_a/train/labels.txt

3.运行配置

运行配置PaddleDetection/configs/runtime.yml

#GPU模式
use_gpu: true
# 日志打印间隔
log_iter: 20
# 保存目录
save_dir: output
snapshot_epoch: 500
print_flops: false

4.学习率等超参配置

重点配置epoch、 LR等

PaddleDetection/configs/yolov3/base/optimizer_270e.yml

# 训练轮次配置
epoch: 270

# 学习率配置
LearningRate:
  base_lr: 0.001
  schedulers:
  - !PiecewiseDecay
    gamma: 0.1
    milestones:
    - 216
    - 243
  - !LinearWarmup
    start_factor: 0.
    steps: 4000

OptimizerBuilder:
  optimizer:
    momentum: 0.9
    type: Momentum
  regularizer:
    factor: 0.0005
    type: L2

5.网络配置

网络配置,此次配置骨干网为DarkNet
PaddleDetection/configs/yolov3/_base_/yolov3_darknet53.yml

architecture: YOLOv3
pretrain_weights: https://paddledet.bj.bcebos.com/models/pretrained/DarkNet53_pretrained.pdparams
norm_type: sync_bn

YOLOv3:
  backbone: DarkNet
  neck: YOLOv3FPN
  yolo_head: YOLOv3Head
  post_process: BBoxPostProcess

DarkNet:
  depth: 53
  return_idx: [2, 3, 4]

# use default config
# YOLOv3FPN:

YOLOv3Head:
  anchors: [[10, 13], [16, 30], [33, 23],
            [30, 61], [62, 45], [59, 119],
            [116, 90], [156, 198], [373, 326]]
  anchor_masks: [[6, 7, 8], [3, 4, 5], [0, 1, 2]]
  loss: YOLOv3Loss

YOLOv3Loss:
  ignore_thresh: 0.7
  downsample: [32, 16, 8]
  label_smooth: false

BBoxPostProcess:
  decode:
    name: YOLOBox
    conf_thresh: 0.005
    downsample_ratio: 32
    clip_bbox: true
  nms:
    name: MultiClassNMS
    keep_top_k: 100
    score_threshold: 0.01
    nms_threshold: 0.45
    nms_top_k: 1000

6.Reader配置

重点关注 batch_size、image_shape等
PaddleDetection/configs/yolov3/_base_/yolov3_reader.yml

worker_num: 2
TrainReader:
  inputs_def:
    num_max_boxes: 50
  sample_transforms:
    - Decode: {}
    - Mixup: {alpha: 1.5, beta: 1.5}
    - RandomDistort: {}
    - RandomExpand: {fill_value: [123.675, 116.28, 103.53]}
    - RandomCrop: {}
    - RandomFlip: {}
  batch_transforms:
    - BatchRandomResize: {target_size: [320, 352, 384, 416, 448, 480, 512, 544, 576, 608], random_size: True, random_interp: True, keep_ratio: False}
    - RandomDistort: {}
    - NormalizeBox: {}
    - PadBox: {num_max_boxes: 50}
    - BboxXYXY2XYWH: {}
    - NormalizeImage: {mean: [0.485, 0.456, 0.406], std: [0.229, 0.224, 0.225], is_scale: True}
    - Permute: {}
    - Gt2YoloTarget: {anchor_masks: [[6, 7, 8], [3, 4, 5], [0, 1, 2]], anchors: [[10, 13], [16, 30], [33, 23], [30, 61], [62, 45], [59, 119], [116, 90], [156, 198], [373, 326]], downsample_ratios: [32, 16, 8]}
  batch_size: 20
  shuffle: true
  drop_last: true
  mixup_epoch: 250
  use_shared_memory: true

EvalReader:
  inputs_def:
    num_max_boxes: 50
  sample_transforms:
    - Decode: {}
    - Resize: {target_size: [608, 608], keep_ratio: False, interp: 2}
    - NormalizeImage: {mean: [0.485, 0.456, 0.406], std: [0.229, 0.224, 0.225], is_scale: True}
    - Permute: {}
  batch_size: 20

TestReader:
  inputs_def:
    image_shape: [3, 608, 608]
  sample_transforms:
    - Decode: {}
    - Resize: {target_size: [608, 608], keep_ratio: False, interp: 2}
    - NormalizeImage: {mean: [0.485, 0.456, 0.406], std: [0.229, 0.224, 0.225], is_scale: True}
    - Permute: {}
  batch_size: 1

7.开始训练

# 数据集配置,其他可采取默认
!cp -f  ~/voc.yml ~/PaddleDetection/configs/datasets/voc.yml
# 训练

%cd ~/PaddleDetection/

!python tools/train.py -c ./configs/yolov3/yolov3_darknet53_270e_voc.yml \
     --use_vdl=true \
    --vdl_log_dir=vdl_dir/scalar \
    --eval -o use_gpu=true

8.输出日志

训练日志

[01/03 23:08:05] ppdet.engine INFO: Epoch: [10] [ 0/36] learning_rate: 0.000090 loss_xy: 5.516527 loss_wh: 3.053876 loss_obj: 12.486687 loss_cls: 1.317490 loss: 22.319881 eta: 8:48:30 batch_cost: 3.1413 data_cost: 2.6554 ips: 6.3668 images/s
[01/03 23:09:07] ppdet.engine INFO: Epoch: [10] [20/36] learning_rate: 0.000095 loss_xy: 6.114716 loss_wh: 3.206046 loss_obj: 13.324018 loss_cls: 1.355693 loss: 24.122631 eta: 8:44:56 batch_cost: 3.0901 data_cost: 2.5885 ips: 6.4723 images/s
[01/03 23:10:03] ppdet.engine INFO: Epoch: [11] [ 0/36] learning_rate: 0.000099 loss_xy: 5.409023 loss_wh: 2.845924 loss_obj: 12.471905 loss_cls: 1.237947 loss: 21.803427 eta: 8:44:45 batch_cost: 3.4201 data_cost: 2.8799 ips: 5.8477 images/s
[01/03 23:11:06] ppdet.engine INFO: Epoch: [11] [20/36] learning_rate: 0.000104 loss_xy: 5.595372 loss_wh: 2.837581 loss_obj: 12.333159 loss_cls: 1.263098 loss: 22.268236 eta: 8:41:59 batch_cost: 3.1570 data_cost: 2.5889 ips: 6.3351 images/s
[01/03 23:12:03] ppdet.engine INFO: Epoch: [12] [ 0/36] learning_rate: 0.000108 loss_xy: 5.279551 loss_wh: 2.735991 loss_obj: 11.844409 loss_cls: 1.155111 loss: 21.146755 eta: 8:41:51 batch_cost: 3.4755 data_cost: 3.0134 ips: 5.7545 images/s
[01/03 23:13:10] ppdet.engine INFO: Epoch: [12] [20/36] learning_rate: 0.000113 loss_xy: 5.620305 loss_wh: 2.650487 loss_obj: 11.757578 loss_cls: 1.057634 loss: 21.237453 eta: 8:40:36 batch_cost: 3.3526 data_cost: 2.9166 ips: 5.9655 images/s
[01/03 23:14:04] ppdet.engine INFO: Epoch: [13] [ 0/36] learning_rate: 0.000117 loss_xy: 5.811560 loss_wh: 2.833800 loss_obj: 12.568821 loss_cls: 1.326491 loss: 21.987259 eta: 8:39:42 batch_cost: 3.3750 data_cost: 2.8388 ips: 5.9260 images/s
[01/03 23:15:10] ppdet.engine INFO: Epoch: [13] [20/36] learning_rate: 0.000122 loss_xy: 5.573336 loss_wh: 2.614874 loss_obj: 11.848093 loss_cls: 1.017949 loss: 20.871216 eta: 8:37:56 batch_cost: 3.2704 data_cost: 2.7479 ips: 6.1155 images/s
[01/03 23:16:04] ppdet.engine INFO: Epoch: [14] [ 0/36] learning_rate: 0.000126 loss_xy: 5.268225 loss_wh: 2.517870 loss_obj: 10.861967 loss_cls: 1.058530 loss: 19.757854 eta: 8:37:08 batch_cost: 3.3716 data_cost: 2.8755 ips: 5.9318 images/s
[01/03 23:17:06] ppdet.engine INFO: Epoch: [14] [20/36] learning_rate: 0.000131 loss_xy: 5.842254 loss_wh: 2.545744 loss_obj: 12.144636 loss_cls: 1.203430 loss: 21.758163 eta: 8:34:32 batch_cost: 3.1135 data_cost: 2.5847 ips: 6.4237 images/s
[01/03 23:17:52] ppdet.utils.checkpoint INFO: Save checkpoint: output/yolov3_darknet53_270e_voc
[01/03 23:17:53] ppdet.engine INFO: Eval iter: 0
[01/03 23:17:57] ppdet.metrics.metrics INFO: Accumulating evaluatation results...
[01/03 23:17:57] ppdet.metrics.metrics INFO: mAP(0.50, 11point) = 18.91%
[01/03 23:17:57] ppdet.engine INFO: Total sample number: 183, averge FPS: 37.56547376000298
[01/03 23:17:57] ppdet.engine INFO: Best test bbox ap is 0.189.
[01/03 23:18:01] ppdet.utils.checkpoint INFO: Save checkpoint: output/yolov3_darknet53_270e_voc

visualdl可视化

五、模型预测

1.单个预测

  • -c 参数表示指定使用哪个配置文件
  • -o 参数表示指定配置文件中的全局变量(覆盖配置文件中的设置)
  • –infer_img 参数指定预测图像路径
  • 预测结束后会在output文件夹中生成一张画有预测结果的同名图像
# 单个预测
%cd ~/PaddleDetection/

!python tools/infer.py -c ./configs/yolov3/yolov3_darknet53_270e_voc.yml   \
    -o use_gpu=true --infer_img=../data/data_a/train/JPEGImages/chef910-0000.jpg
/home/aistudio/PaddleDetection
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py:130: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. 
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  if data.dtype == np.object:
W0104 08:04:54.116544 22620 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W0104 08:04:54.121711 22620 device_context.cc:465] device: 0, cuDNN Version: 7.6.
[01/04 08:04:57] ppdet.utils.checkpoint INFO: Finish loading model weights: output/yolov3_darknet53_270e_voc/model_final.pdparams
[01/04 08:04:57] ppdet.engine INFO: Detection bbox results save in output/chef910-0000.jpg
# 打印预测结果
from PIL import Image

img=Image.open('output/chef910-0000.jpg')
img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-8iwLDZ1T-1642075237750)(output_21_0.png)]

2.批量预测

重点关注参数有:

  • –save_txt=True
  • –draw_threshold=0.5

通过 --save_txt=True 保存预测结果为文本

%cd ~/PaddleDetection/

# 其他颜色工作服预测
!python tools/infer.py -c./configs/yolov3/yolov3_darknet53_270e_voc.yml    \
    -o use_gpu=true --infer_dir=../data/data_a/test-A/othercolor/images/ \
    --save_txt=True \
    --draw_threshold=0.5

# 白色工作服预测
!python tools/infer.py -c./configs/yolov3/yolov3_darknet53_270e_voc.yml    \
    -o use_gpu=true --infer_dir=../data/data_a/test-A/whitecolor/images/ \
    --save_txt=True \
    --draw_threshold=0.5
/home/aistudio/PaddleDetection
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py:130: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. 
Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations
  if data.dtype == np.object:
W0104 08:10:57.304265 23501 device_context.cc:447] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1
W0104 08:10:57.309389 23501 device_context.cc:465] device: 0, cuDNN Version: 7.6.
[01/04 08:11:00] ppdet.utils.checkpoint INFO: Finish loading model weights: output/yolov3_darknet53_270e_voc/model_final.pdparams
[01/04 08:11:00] train INFO: Found 35 inference images in total.
[01/04 08:11:02] ppdet.engine INFO: Detection bbox results save in output/chef910-0598.jpg
[01/04 08:11:02] ppdet.engine INFO: Detection bbox results save in output/chef911--1351.jpg
[01/04 08:11:02] ppdet.engine INFO: Detection bbox results save in output/chef911--1349.jpg
[01/04 08:11:02] ppdet.engine INFO: Detection bbox results save in output/chef911--1352.jpg
[01/04 08:11:02] ppdet.engine INFO: Detection bbox results save in output/chef911--1632.jpg
[01/04 08:11:02] ppdet.engine INFO: Detection bbox results save in output/chef911--1625.jpg
[01/04 08:11:02] ppdet.engine INFO: Detection bbox results save in output/chef910-0595.jpg
[01/04 08:11:02] ppdet.engine INFO: Detection bbox results save in output/chef911--0452.jpg
[01/04 08:11:02] ppdet.engine INFO: Detection bbox results save in output/chef910-0298.jpg
[01/04 08:11:02] ppdet.engine INFO: Detection bbox results save in output/chef911--0453.jpg
[01/04 08:11:02] ppdet.engine INFO: Detection bbox results save in output/chef910-0596.jpg
[01/04 08:11:02] ppdet.engine INFO: Detection bbox results save in output/chef911--1622.jpg
[01/04 08:11:02] ppdet.engine INFO: Detection bbox results save in output/chef911--1348.jpg
[01/04 08:11:02] ppdet.engine INFO: Detection bbox results save in output/chef911--0455.jpg
[01/04 08:11:02] ppdet.engine INFO: Detection bbox results save in output/chef911--1628.jpg
[01/04 08:11:02] ppdet.engine INFO: Detection bbox results save in output/chef911--1627.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef910-0594.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef911--2049.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef911--0454.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef911--1353.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef911--2048.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef911--0451.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef910-0597.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef911--1629.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef911--1626.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef910-0592.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef911--0456.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef911--1630.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef911--1635.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef911--0457.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef911--1633.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef910-0069.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef911--1634.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef911--1631.jpg
[01/04 08:11:03] ppdet.engine INFO: Detection bbox results save in output/chef910-0593.jpg

3.后处理及提交

主要步骤如下:

  • 拷贝预测结果 txt文件到指定目录
  • 把预测txt文件处理为比赛需要的格式
# 可视化预测结果
from PIL import Image

img=Image.open('output/chef910-0298.jpg')
img

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-cboKOkmn-1642075237751)(output_25_0.png)]

%cd ~
# 新建result_dir
!mkdir result_dir
# 拷贝txt预测结果文件到result_dir
!cp -r PaddleDetection/output/*.txt result_dir
#!/usr/bin/python
# -*- coding: UTF-8 -*-
"""
@author:livingbody
@file:test.py
@time:2022/01/04
"""
import pandas as pd
import os

# 修改标签
# chefclothes为1
# otherclothes为0
def fix_clothes(data):
    print(data)
    if data == 'chefclothes':
        return 1
    else:
        return 0


# 坐标格式化数据,保留小数点后1位
def fix_pint(data):
    data1 = float('%.1f' % data)
    return data1

# 概率格式化,保留小数点后2位
def fix_pint2(data):
    data1 = float('%.2f' % data)
    return data1

# 获取预测结果
def get_data(file_name):
    data = pd.read_csv(file_name, index_col=None, header=None, sep=' ')
    data[0] = data[0].apply(fix_clothes)
    data[1] = data[1].apply(fix_pint2)
    for i in range(2, len(data.columns)):
        data[i] = data[i].apply(fix_pint)
    return data

# 获取单个预测文件数据
def get_single_data(file_name):
    # 获取txt文件名
    temp = file_name.split('\\')[-1]
    # 获取图片文件名
    temp = temp.split('.')[0] + '.jpg'
    mylist = []
    data = None
    try:
        data = get_data(file_name)
    except:
        # 如文件为空,返回None
        return None    
    for index, row in data.iterrows():
        row = row.tolist()
        # 第2列概率
        # 第3列左上顶点x值
        # 第4列左上顶点y值
        
        # 第1列为标签,int
        row[0] = int(row[0])
        # 第5列右下顶点x值
        # 第6列右下顶点y值
        # PaddleDetection预测结果为:左上x 左上y  宽度  高度,此处需要转换
        row[4] = row[2] + row[4]
        row[4] = float('%.1f' % row[4])
        row[5] = row[3] + row[5]
        row[5] = float('%.1f' % row[5])
        for i in range(len(row)):
            row[i] = str(row[i])
        row = temp + ' ' + ' '.join(row)
        # print(row)  # 输出每行的索引值
        mylist.append(row)
    return mylist

# 输出结果到提交文件
def get_all_data():
    mylist = []
    for dirpath, dirname, filenames in os.walk('./result_dir'):
        for filename in filenames:
            if '.txt' in filename:
                print(filename)
                tmp = get_single_data(os.path.join(dirpath, filename))
                if tmp == None:
                    continue
                mylist.extend(tmp)
    return mylist


if __name__ == '__main__':
    all_data = get_all_data()
    with open('myresult.txt', 'w') as f:
        for item in all_data:
            f.write(item + '\n')
!head myresult.txt
    for item in all_data:
        f.write(item + '\n')


```python
!head myresult.txt

如上所示,提交即可获得成绩,如下图:

如需提分,可在模型选择,超参设置等方面下功夫。

Logo

学大模型,用大模型上飞桨星河社区!每天8点V100G算力免费领!免费领取ERNIE 4.0 100w Token >>>

更多推荐