[yolov3] 检查 yolov3 数据标注是否正确

24 篇文章 2 订阅
11 篇文章 0 订阅

使用方法:

1.安装依赖包

2.修改:

*2.1 labels,有几类,写几类,按0-n增大顺序

*2.2 img_dir,修改输入图片路径

*2.3 yolo_txt_dir,修改为yolo.txt所在文件路径

× 2.4 scale_percent,最终显示的图片的缩放比

× 2.5 check_rate,检查的图片的比例

× 2.6 color_list,每一类的物体的标记的颜色(RGB)

3.使用脚本

requirements.txt:

cycler==0.10.0
kiwisolver==1.1.0
matplotlib==3.1.2
numpy==1.18.1
opencv-python==4.1.2.30
Pillow==7.0.0
pyparsing==2.4.6
python-dateutil==2.8.1
six==1.13.0

Scripts:

# -*- coding: utf-8 -*-

import os
import random
import cv2 as cv
import matplotlib.pyplot as plt




labels = ["ArmorBlue", "ArmorRed", "Base", "Watcher"]
color_list = [(0, 0, 255), (255, 0, 0), (0, 255, 0), (0, 255, 255)]
img_dir = "/home/youyheng/DJIdata/robomaster_Final_Tournament/image"
yolo_txt_dir = "/home/youyheng/DJIdata/robomaster_Final_Tournament/processedTXT"
# result_dst_dir = "/home/youyheng/DJIdata/robomaster_Final_Tournament/check_label_result"
scale_percent = 80
# rates that represent the imgs of all datasets
# 1 for all imgs, 0.5 for half of the imgs
check_rate = 1
random_check = False

def cv_imread(file_path):
    img = plt.imread(file_path)
    img_rgb = cv.cvtColor(img, cv.COLOR_BGR2RGB)
    return img_rgb


def my_line(img, start, end):
    thickness = 2
    line_type = 8
    cv.line(img,
             start,
             end,
             (0, 0, 0),
             thickness,
             line_type)


# draw rectangle with the data caught in the data file
# And set the name of the label to it
def draw_label_rec(img, label_index, label_info_list, img_name):
    global labels

    img_height = img.shape[0]
    img_width = img.shape[1]

    x = float(label_info_list[0])
    y = float(label_info_list[1])
    w = float(label_info_list[2])
    h = float(label_info_list[3])

    x_center = x * img_width
    y_center = y * img_height

    xmax = int(x_center + w * img_width / 2)
    xmin = int(x_center - w * img_width / 2)
    ymax = int(y_center + w * img_height / 2)
    ymin = int(y_center - w * img_height / 2)

    # Set font
    font = cv.FONT_HERSHEY_SIMPLEX
    global color_list
    
    # draw_rectangle
    cv.rectangle(img,  # img to paint on
             (xmin, ymin),  # bottom top
             (xmax, ymax),  # bottom right
             color_list[int(label_index)],  # bgr color
             2)  # line thickness

    ###########need perfection
    cv.putText(img, str(img_name), (5, 50), cv.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)


def main():
    global img_dir, yolo_txt_dir, labels, random_check

    origin_window = "Origin Window"

    # Load all imgs with label info
    img_name_list = os.listdir(img_dir)
    if random_check is True:
        random.shuffle(img_name_list)

    check_max_times = int(check_rate * len(img_name_list))
    for index, img_name in enumerate(img_name_list):
        if not img_name.endswith('jpg'):
            continue

        # Checked for max_times and quit
        if index >= check_max_times:
            return
        print("**check img : {0} **".format(os.path.join(img_dir, img_name)))
        # Open IMG
        src_image = cv_imread(os.path.join(img_dir, img_name))

        # Open yolo label txt
        if os.path.exists(os.path.join(yolo_txt_dir, img_name.rpartition(".")[0]+".txt")):
            file_reader = open(os.path.join(yolo_txt_dir, img_name.rpartition(".")[0]+".txt"), "r")
        else:
            continue

        ## Dada loaded ##
        if src_image is None:
            print("Open image Error")
            return

        if file_reader is None:
            print("Open txt error")
            return

        # Pre-handling for Img
        src_height = src_image.shape[0]
        src_width = src_image.shape[1]

        # percent of original size
        global scale_percent
        width = int(src_width * scale_percent / 100)
        height = int(src_height * scale_percent / 100)
        dim = (width, height)

        # Decode the data
        while True:
            line = file_reader.readline()
            if not line:
                break
            label_info_list = line.split()
            # Get 5 nums in labeled_obj_info_list:
            # labels[label_info_list[0]] obj type : 0 ArmorBlue, 1 ArmorRed, 2 Base, 3 Watcher
            # label_info_list[1] x
            # label_info_list[2] y
            # label_info_list[3] w
            # label_info_list[4] h
            label_index = int(label_info_list[0])
            x = label_info_list[1]
            y = label_info_list[2]
            w = label_info_list[3]
            h = label_info_list[4]

            ########################
            # need perfection
            draw_label_rec(src_image, label_index, [x, y, w, h], img_name)

        resized_src = cv.resize(src_image, dim, interpolation=cv.INTER_CUBIC)

        # show the result
        cv.imshow(origin_window, resized_src)
        cv.waitKey(0)

        # Debug
        # print("src_height = {0}".format(src_height))
        # print("src_width = {0}".format(src_width))
        cv.destroyAllWindows()

        file_reader.close()
        print("**check over**")


if __name__ == "__main__":
    main()

效果预览:(按任意键跳转到下一张图片,由于opencv不能读取中文路径,所以路径中有中文将会停止检查)

2020/01/08 解决中文路径问题

效果预览:

有中文路径的情况:

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
YOLOv3 Darknet模型训练后不出框框可能是由于以下几个原因造成的。 1. 数据集问题:数据集可能存在标注错误或缺失标注信息的情况。因为YOLOv3是基于边界框的目标检测算法,如果数据集中目标未正确标注或者标注位置不准确,模型就无法准确地检测目标并生成边界框。 2. 参数设置问题:训练过程中使用的参数设置可能不正确,比如学习率设置过高或过低,训练批次数不足等。这些参数设置不合理可能导致模型无法充分学习目标检测的特征。 3. 模型架构问题:YOLOv3的网络结构比较复杂,包含多个层级的特征提取和预测,如果模型的层级结构不正确或者网络层数不够深,就可能导致模型无法准确地检测目标并生成边界框。 解决这个问题可以按照以下方法进行尝试: 1. 仔细检查数据是否存在标注错误或缺失标注信息的情况,确保数据集中的目标都正确标注。 2. 调整训练参数,根据实际情况适当调整学习率、训练批次数等参数,可以尝试在较小学习率下进行长时间训练,观察是否能够得到更好的结果。 3. 检查模型的网络结构是否正确是否包含了YOLOv3所需的多个层级的特征提取和预测部分。可以参考YOLOv3模型的官方实现或其他已经经过验证的实现进行对比。 4. 通过查看训练过程的日志输出和模型的评估结果,进一步分析问题所在,可能有助于定位和解决问题。 最后,要注意的是YOLOv3 Darknet模型训练是一个复杂的过程,需要耐心和实践来优化模型并获得准确的目标检测结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值