PaddleDetection icon indicating copy to clipboard operation
PaddleDetection copied to clipboard

使用贵方的sniper训练VisDrone数据集

Open dengxinlong opened this issue 5 years ago • 6 comments

描述问题/Describe the bug

在贵方的sniper中,使用的数据集就是VisDrone数据集,所以这里我也采用这个数据集进行训练,在训练之前需要将VisDrone数据集的格式转换为coco格式,官方的yml文件中采用的也是coco格式(后面给出转换代码)。在开始训练的时候报错,应该是数据集的类别数不对。

复现/Reproduction

  1. 您使用的命令是?/What command or script did you run?
python tools/train.py -c configs/sniper/faster_rcnn_r50_fpn_1x_visdrone.yml  --eval

  1. 您是否更改过代码或配置文件?您是否理解您所更改的内容?还请您提供所更改的部分代码。/Did you make any modifications on 配置文件可以认为没有改变,只是将配置文件中的dataset_dir从原来的dataset/VisDrone2019修改为了dataset/VisDrone

  2. 您使用的数据集是?/What dataset did you use? VisDrone2019

  3. 请提供您出现的报错信息及相关log。/Please provide the error messages or relevant log information.

python tools/train.py -c configs/sniper/faster_rcnn_r50_fpn_1x_visdrone.yml  --eval
Warning: import ppdet from source directory without installing, run 'python setup.py install' to install ppdet firstly
loading annotations into memory...
Done (t=1.69s)
creating index...
index created!
Traceback (most recent call last):
  File "tools/train.py", line 171, in <module>
    main()
  File "tools/train.py", line 167, in main
    run(FLAGS, cfg)
  File "tools/train.py", line 118, in run
    trainer = Trainer(cfg, mode='train')
  File "/home/xuzhi/xinlongdeng/PaddleDetectionNew/ppdet/engine/trainer.py", line 76, in __init__
    self.dataset, cfg.worker_num)
  File "/home/xuzhi/xinlongdeng/PaddleDetectionNew/ppdet/data/reader.py", line 163, in __call__
    self.dataset.parse_dataset()
  File "/home/xuzhi/xinlongdeng/PaddleDetectionNew/ppdet/data/source/coco.py", line 184, in parse_dataset
    gt_class[i][0] = self.catid2clsid[catid]
KeyError: 0

环境/Environment

  1. 请提供您使用的Paddle和PaddleDetection的版本号/Please provide the version of Paddle and PaddleDetection you use: Paddle: 2.2.0 PaddleDetection: release2.3

  2. 如您在使用PaddleDetection的同时还在使用其他产品,如PaddleServing、PaddleInference等,请您提供其版本号/ Please provide the version of any other related tools/products used, such as the version of PaddleServing and etc: none

  3. 请提供您使用的操作系统信息,如Linux/Windows/MacOS /Please provide the OS information, e.g., Linux: Ubuntu18.01

  4. 请问您使用的Python版本是?/ Please provide the version of Python you used. Python:3.7.10

  5. 请问您使用的CUDA/cuDNN的版本号是?/ Please provide the version of CUDA/cuDNN you used. CUDA:10.1

另:给出VisDrone转换为coco格式的代码

这里采用的转换代码也是之前在PaddleDetection release2.0版本时,我使用VisDrone来训练SSD时,贵方给出的。

import os
import cv2
from tqdm import tqdm
import json


def convert_to_cocodetection(dir, output_dir):
    train_dir = os.path.join(dir, "VisDrone2019-DET-train")
    val_dir = os.path.join(dir, "VisDrone2019-DET-val")
    train_annotations = os.path.join(train_dir, "annotations")
    val_annotations = os.path.join(val_dir, "annotations")
    train_images = os.path.join(train_dir, "images")
    val_images = os.path.join(val_dir, "images")
    id_num = 0

    categories = [{"id": 1, "name": "people"},
                  {"id": 2, "name": "bicycle"},
                  {"id": 3, "name": "car"},
                  {"id": 4, "name": "van"},
                  {"id": 5, "name": "truck"},
                  {"id": 6, "name": "tricycle"},
                  {"id": 7, "name": "awning-tricycle"},
                  {"id": 8, "name": "bus"},
                  {"id": 9, "name": "motor"}
                  ]
    for mode in ["train", "val"]:
        images = []
        annotations = []
        print(f"start loading {mode} data...")
        if mode == "train":
            set = os.listdir(train_annotations)
            #             print(set)
            annotations_path = train_annotations
            images_path = train_images
        else:
            id_num = 0
            set = os.listdir(val_annotations)
            annotations_path = val_annotations
            images_path = val_images
        for i in tqdm(set):
            if 'txt' in i:

                #             print(f'{i}++++++++++++')
                f = open(annotations_path + "/" + i, "r")
                name = i.replace(".txt", "")
                name1 = name.replace('_', '')
                name2 = name1.replace('d', '')
                image = {}
                height, width = cv2.imread(images_path + "/" + name + ".jpg").shape[:2]
                file_name = name + ".jpg"
                image["file_name"] = file_name
                image["height"] = height
                image["width"] = width
                image["id"] = name2
                images.append(image)
                for line in f.readlines():
                    annotation = {}
                    line = line.replace("\n", "")
                    if line.endswith(","):  # filter data
                        line = line.rstrip(",")
                    line_list = [int(i) for i in line.split(",")]
                    # import pdb; pdb.set_trace()
                    bbox_xywh = [line_list[0], line_list[1], line_list[2], line_list[3]]
                    annotation["image_id"] = name2
                    # annotation["score"] = line_list[4]
                    annotation["bbox"] = bbox_xywh
                    annotation["category_id"] = int(line_list[5])
                    annotation["id"] = id_num
                    annotation["iscrowd"] = 0
                    annotation["segmentation"] = []
                    annotation["area"] = bbox_xywh[2] * bbox_xywh[3]
                    id_num += 1
                    annotations.append(annotation)
        dataset_dict = {}
        dataset_dict["images"] = images
        dataset_dict["annotations"] = annotations
        dataset_dict["categories"] = categories
        json_str = json.dumps(dataset_dict)
        with open(f'{output_dir}/VisDrone2019-DET_{mode}_coco.json', 'w') as json_file:
            json_file.write(json_str)
    print("json file write done...")


def get_test_namelist(dir, out_dir):
    full_path = out_dir + "/" + "test.txt"
    file = open(full_path, 'w')
    for name in tqdm(os.listdir(dir)):
        name = name.replace(".txt", "")
        file.write(name + "\n")
    file.close()
    return None


def centerxywh_to_xyxy(boxes):
    """
    args:
        boxes:list of center_x,center_y,width,height,
    return:
        boxes:list of x,y,x,y,cooresponding to top left and bottom right
    """
    x_top_left = boxes[0] - boxes[2] / 2
    y_top_left = boxes[1] - boxes[3] / 2
    x_bottom_right = boxes[0] + boxes[2] / 2
    y_bottom_right = boxes[1] + boxes[3] / 2
    return [x_top_left, y_top_left, x_bottom_right, y_bottom_right]


def centerxywh_to_topleftxywh(boxes):
    """
    args:
        boxes:list of center_x,center_y,width,height,
    return:
        boxes:list of x,y,x,y,cooresponding to top left and bottom right
    """
    x_top_left = boxes[0] - boxes[2] / 2
    y_top_left = boxes[1] - boxes[3] / 2
    width = boxes[2]
    height = boxes[3]
    return [x_top_left, y_top_left, width, height]


def clamp(coord, width, height):
    if coord[0] < 0:
        coord[0] = 0
    if coord[1] < 0:
        coord[1] = 0
    if coord[2] > width:
        coord[2] = width
    if coord[3] > height:
        coord[3] = height
    return coord


if __name__ == '__main__':
    convert_to_cocodetection("./VisDrone2019", "./VisDrone2019_coco")

dengxinlong avatar Nov 30 '21 14:11 dengxinlong

cat id 你从0开始设置试一下

lyuwenyu avatar Dec 01 '21 07:12 lyuwenyu

cat id 你从0开始设置试一下

一样的错误??

loading annotations into memory...
Done (t=1.75s)
creating index...
index created!
Traceback (most recent call last):
  File "tools/train.py", line 171, in <module>
    main()
  File "tools/train.py", line 167, in main
    run(FLAGS, cfg)
  File "tools/train.py", line 118, in run
    trainer = Trainer(cfg, mode='train')
  File "/home/xuzhi/xinlongdeng/PaddleDetectionNew/ppdet/engine/trainer.py", line 76, in __init__
    self.dataset, cfg.worker_num)
  File "/home/xuzhi/xinlongdeng/PaddleDetectionNew/ppdet/data/reader.py", line 163, in __call__
    self.dataset.parse_dataset()
  File "/home/xuzhi/xinlongdeng/PaddleDetectionNew/ppdet/data/source/coco.py", line 184, in parse_dataset
    gt_class[i][0] = self.catid2clsid[catid]
KeyError: 10

dengxinlong avatar Dec 01 '21 07:12 dengxinlong

KeyError: 10 你数据里有10嘛 打开转换完的文件看看

lyuwenyu avatar Dec 03 '21 03:12 lyuwenyu

Since this issue has not been updated for more than three months, it will be closed, if it is not solved or there is a follow-up one, please reopen it at any time and we will continue to follow up. It is recommended to pull and try the latest code first. 由于该问题超过三个月未更新,将会被关闭,若问题未解决或有后续问题,请随时重新打开(建议先拉取最新代码进行尝试),我们会继续跟进。

paddle-bot-old avatar Mar 16 '22 06:03 paddle-bot-old

cat id 你从0开始设置试一下

一样的错误??

loading annotations into memory...
Done (t=1.75s)
creating index...
index created!
Traceback (most recent call last):
  File "tools/train.py", line 171, in <module>
    main()
  File "tools/train.py", line 167, in main
    run(FLAGS, cfg)
  File "tools/train.py", line 118, in run
    trainer = Trainer(cfg, mode='train')
  File "/home/xuzhi/xinlongdeng/PaddleDetectionNew/ppdet/engine/trainer.py", line 76, in __init__
    self.dataset, cfg.worker_num)
  File "/home/xuzhi/xinlongdeng/PaddleDetectionNew/ppdet/data/reader.py", line 163, in __call__
    self.dataset.parse_dataset()
  File "/home/xuzhi/xinlongdeng/PaddleDetectionNew/ppdet/data/source/coco.py", line 184, in parse_dataset
    gt_class[i][0] = self.catid2clsid[catid]
KeyError: 10

请问这个问题怎么解决的呢?

wenzonghui avatar May 29 '22 11:05 wenzonghui

cat id 你从0开始设置试一下

一样的错误??

loading annotations into memory...
Done (t=1.75s)
creating index...
index created!
Traceback (most recent call last):
  File "tools/train.py", line 171, in <module>
    main()
  File "tools/train.py", line 167, in main
    run(FLAGS, cfg)
  File "tools/train.py", line 118, in run
    trainer = Trainer(cfg, mode='train')
  File "/home/xuzhi/xinlongdeng/PaddleDetectionNew/ppdet/engine/trainer.py", line 76, in __init__
    self.dataset, cfg.worker_num)
  File "/home/xuzhi/xinlongdeng/PaddleDetectionNew/ppdet/data/reader.py", line 163, in __call__
    self.dataset.parse_dataset()
  File "/home/xuzhi/xinlongdeng/PaddleDetectionNew/ppdet/data/source/coco.py", line 184, in parse_dataset
    gt_class[i][0] = self.catid2clsid[catid]
KeyError: 10

请问这个问题怎么解决的呢?

你查看一下你的catid是不是str类型

LingZhang0215 avatar Sep 08 '22 08:09 LingZhang0215