【魔改版】全国大学生智能车竞赛线上资格赛-车道线检测Baseline

【魔改版】全国大学生智能车竞赛线上资格赛-车道线检测Baseline

    • 解压数据集
    • 数据集统计
    • 均衡化数据集
    • 划分数据集:
    • 执行训练
    • 中值滤波
    • 提交结果
    • 关注我的公众号:

项目链接: https://aistudio.baidu.com/aistudio/projectdetail/1513513

解压数据集

默认解压到/data目录下,该目录会在每次进入环境时重置,但可以节省项目启动时间。

!unzip -oq /home/aistudio/data/data68698/智能车数据集.zip -d /home/aistudio/data

数据集统计

import os
import cv2
import numpy as np
from tqdm import tqdm

mask_pt = '/home/aistudio/data/mask_4000'

count =  [0 for _ in range(16)]

for m in tqdm(os.listdir(mask_pt)):
    pt = os.path.join(mask_pt, m)
    mask = cv2.imread(pt, 0)
    size = mask.shape[0] * mask.shape[1]
    for i in range(16):
        a = mask == i
        ratio_i = np.sum(a.astype(np.float)) / size
        count[i] += ratio_i

sum_ = np.sum(count[1:])
ratios = [v/sum_ for v in count[1:]]
for i in range(0, len(ratios)):
    print('-[INFO] Label {}: {:.4f}'.format(i+1, ratios[i]))
-[INFO] Label 1: 0.0170
-[INFO] Label 2: 0.4482
-[INFO] Label 3: 0.0843
-[INFO] Label 4: 0.0767
-[INFO] Label 5: 0.0334
-[INFO] Label 6: 0.2513
-[INFO] Label 7: 0.0070
-[INFO] Label 8: 0.0025
-[INFO] Label 9: 0.0158
-[INFO] Label 10: 0.0152
-[INFO] Label 11: 0.0292
-[INFO] Label 12: 0.0087
-[INFO] Label 13: 0.0061
-[INFO] Label 14: 0.0046
-[INFO] Label 15: 0.0000
import matplotlib.pyplot as plt

%matplotlib inline

plt.figure(figsize=(6,9)) #调节图形大小
labels = [str(i) for i in range(1, 16)] #定义标签
patches,text1,text2 = plt.pie(ratios,
                      labels=labels,
                      autopct = '%3.2f%%', #数值保留固定小数位
                      shadow = False, #无阴影设置
                      startangle =90, #逆时针起始角度设置
                      pctdistance = 0.6) #数值距圆心半径倍数距离
#patches饼图的返回值,texts1饼图外label的文本,texts2饼图内部的文本
# x,y轴刻度设置一致,保证饼图为圆形
plt.axis('equal')
plt.show()

【魔改版】全国大学生智能车竞赛线上资格赛-车道线检测Baseline_第1张图片

均衡化数据集

import os

os.makedirs('aug_data')
os.makedirs('aug_data/image_4000')
os.makedirs('aug_data/mask_4000')
from shutil import copyfile
import os
from tqdm import tqdm
import cv2
import numpy as np

mask_pt = '/home/aistudio/data/mask_4000'
color_path = '/home/aistudio/data/image_4000'

count = 0
for m in tqdm(os.listdir(mask_pt)):
    scr_mask = os.path.join(mask_pt, m)
    mask = cv2.imread(scr_mask, 0)
    a = mask == 2
    b = mask == 6
    size = mask.shape[0] * mask.shape[1]
    ratio_a = np.sum(a.astype(np.float)) / size
    ratio_b = np.sum(a.astype(np.float)) / size
    if ratio_a + ratio_b < 0.003:
        count += 1
        src_color = os.path.join(color_path, m)
        dst_color = os.path.join('aug_data/image_4000', m)
        dst_mask = os.path.join('aug_data/mask_4000', m)
        copyfile(src_color, dst_color)
        copyfile(scr_mask, dst_mask)

print('-[TOTAL] Argued:', count)
100%|██████████| 4000/4000 [01:24<00:00, 47.19it/s]

-[TOTAL] Argued: 728
import os
import cv2
import numpy as np
from tqdm import tqdm

count =  [0 for _ in range(16)]

mask_pt = '/home/aistudio/data/mask_4000'
for m in tqdm(os.listdir(mask_pt)):
    pt = os.path.join(mask_pt, m)
    mask = cv2.imread(pt, 0)
    size = mask.shape[0] * mask.shape[1]
    for i in range(16):
        a = mask == i
        ratio_i = np.sum(a.astype(np.float)) / size
        count[i] += ratio_i

mask_pt = '/home/aistudio/aug_data/mask_4000'
for m in tqdm(os.listdir(mask_pt)):
    pt = os.path.join(mask_pt, m)
    mask = cv2.imread(pt, 0)
    size = mask.shape[0] * mask.shape[1]
    for i in range(16):
        a = mask == i
        ratio_i = np.sum(a.astype(np.float)) / size
        count[i] += ratio_i

sum_ = np.sum(count[1:])
ratios = [v/sum_ for v in count[1:]]
100%|██████████| 4000/4000 [05:30<00:00, 12.09it/s]
100%|██████████| 728/728 [00:54<00:00, 13.27it/s]
for i in range(0, len(ratios)):
    print('-[INFO] Label {}: {:.4f}'.format(i+1, ratios[i]))
-[INFO] Label 1: 0.0193
-[INFO] Label 2: 0.4062
-[INFO] Label 3: 0.0897
-[INFO] Label 4: 0.0973
-[INFO] Label 5: 0.0409
-[INFO] Label 6: 0.2506
-[INFO] Label 7: 0.0094
-[INFO] Label 8: 0.0025
-[INFO] Label 9: 0.0160
-[INFO] Label 10: 0.0154
-[INFO] Label 11: 0.0308
-[INFO] Label 12: 0.0091
-[INFO] Label 13: 0.0070
-[INFO] Label 14: 0.0058
-[INFO] Label 15: 0.0000
import matplotlib.pyplot as plt

%matplotlib inline

plt.figure(figsize=(6,9)) #调节图形大小
labels = [str(i) for i in range(1, 16)] #定义标签
patches,text1,text2 = plt.pie(ratios,
                      labels=labels,
                      autopct = '%3.2f%%', #数值保留固定小数位
                      shadow = False, #无阴影设置
                      startangle =90, #逆时针起始角度设置
                      pctdistance = 0.6) #数值距圆心半径倍数距离
#patches饼图的返回值,texts1饼图外label的文本,texts2饼图内部的文本
# x,y轴刻度设置一致,保证饼图为圆形
plt.axis('equal')
plt.show()

【魔改版】全国大学生智能车竞赛线上资格赛-车道线检测Baseline_第2张图片

划分数据集:

import os
from imghdr import what
import numpy as np
import random


def get_all_date(dir_images, dir_masks):
    """生成训练、测试所需的txt文件"""
    res = []
    for file in os.listdir(dir_images):
        image_path = os.path.join(dir_images, file)
        mask_path = os.path.join(dir_masks, file)
        if os.path.exists(image_path) and os.path.exists(mask_path):  
            if what(image_path) and what(mask_path):
                res.append((os.path.join(dir_images, file), os.path.join(dir_masks, file)))
        else:
            print(image_path, mask_path)
    return res

if True:
    res1 = get_all_date('/home/aistudio/data/image_4000', '/home/aistudio/data/mask_4000')
    res2 = get_all_date('/home/aistudio/aug_data/image_4000', '/home/aistudio/aug_data/mask_4000')
    res = res1 + res2
    print(len(res))
    random.shuffle(res)
    random.shuffle(res)
    #4500-500
    with open('./train_list.txt', 'w') as f:
        for line in res[:-500]:
            f.writelines(line[0] + ' ' + line[1] + '\n')

    with open('./val_list.txt', 'w') as f:
        for line in res[-500:]:
            f.writelines(line[0] + ' ' + line[1] + '\n')
4728

执行训练

其中PaddleSeg/configs/deeplabv3p_resnet50_os8_cityscapes_1024x512_80k.yml文件是训练的配置文件,其引用了PaddleSeg/configs/_base_/cityscapes.yml,我们可以在训练时针对不同情况进行调参。当然选手也可以在’PaddleSeg/configs’中选择更多模型配置,但需要注意的是数据读取路径等细节也在配置文件中,如非默认则需要自行修改。

# 设置环境变量只使用第1块GPU并切换工作目录
# 注意!此操作会更改程序的工作目录!
%set_env CUDA_VISIBLE_DEVICES=0
%set_env PYTHONPATH='/home/aistudio/PaddleSeg'
%cd /home/aistudio
env: CUDA_VISIBLE_DEVICES=0
env: PYTHONPATH='/home/aistudio/PaddleSeg'
/home/aistudio
# 安装依赖包
!pip install paddleseg
# 训练命令 如需恢复训练则在下方命令尾部增加 --resume_model output/iter_8000(已存在的ppCheckpoint文件夹名)
!python PaddleSeg/train.py \
    --config PaddleSeg/configs/deeplabv3p/deeplabv3p_resnet50_os8_cityscapes_1024x512_80k.yml \
    --do_eval --use_vdl --resume_model output/iter_6000
2021-02-05 16:32:43 [INFO]	[TRAIN] epoch=3, iter=6010/80000, loss=0.0514, lr=0.002330, batch_cost=0.9115, reader_cost=0.0536 | ETA 18:44:02
2021-02-05 16:32:51 [INFO]	[TRAIN] epoch=3, iter=6020/80000, loss=0.0767, lr=0.002330, batch_cost=0.8471, reader_cost=0.0025 | ETA 17:24:25
# 进行推理,其中--model_path后跟自己训练好的模型文件路径,一般选择bast_model或iter后最大值的参数文件
!python PaddleSeg/predict.py \
    --config PaddleSeg/configs/deeplabv3p/deeplabv3p_resnet50_os8_cityscapes_1024x512_80k.yml \
    --model_path output/best_model/model.pdparams --image_path data/infer --save_dir output/result

中值滤波

import os
from tqdm import tqdm

base = 'output/result'

for im in tqdm(os.listdir(base)):
    pt = os.path.join(base, im)
    img = cv2.imread(pt, 0)
    img = cv2.medianBlur(img, 5)
= 'output/result'

for im in tqdm(os.listdir(base)):
    pt = os.path.join(base, im)
    img = cv2.imread(pt, 0)
    img = cv2.medianBlur(img, 5)
    cv2.imwrite(img, pt)

提交结果

压缩0-14Label的预测结果文件,在赛题页面->提交结果 上进行提交结果。

%cd /home/aistudio/output/result/result
!zip -r -o /home/aistudio/predict.zip ./
%cd /home/aistudio

关注我的公众号:

感兴趣的同学关注我的公众号——可达鸭的深度学习教程:
在这里插入图片描述

你可能感兴趣的:(深度学习-语义分割,python,深度学习,paddlepaddle,计算机视觉,图像分割)