crowdhuman 数据集 darknet yolov7训练

这篇具有很好参考价值的文章主要介绍了crowdhuman 数据集 darknet yolov7训练。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.下载crowdhuman数据集,下载链接如下:CrowdHuman Dataset

2.labels文件odgt格式json文件转换成coco数据格式,代码如下:

# corwdhuman->coco
import os
import json
from PIL import Image
def load_file(fpath):#fpath是具体的文件 ,作用:#str to list
    assert os.path.exists(fpath)  #assert() raise-if-not
    with open(fpath,'r') as fid:
        lines = fid.readlines()
    records = [json.loads(line.strip('\n')) for line in lines] #str to list
    return records

def crowdhuman2coco(odgt_path,json_path):#一个输入文件路径,一个输出文件路径
    records = load_file(odgt_path)   #提取odgt文件数据
    #预处理
    json_dict = {"images":[], "annotations": [], "categories": []}#定义一个字典,coco数据集标注格式
    START_B_BOX_ID = 1 #设定框的起始ID
    image_id = 1  #每个image的ID唯一,自己设定start,每次++
    bbox_id = START_B_BOX_ID
    image = {} #定义一个字典,记录image
    annotation = {} #记录annotation
    categories = {}  #进行类别记录
    record_list = len(records)  #获得record的长度,循环遍历所有数据。
    print(record_list)
    #一行一行的处理。
    for i in range(record_list):
        file_name = records[i]['ID']+'.jpg'  #这里是字符串格式  eg.273278,600e5000db6370fb
        #image_id = int(records[i]['ID'].split(",")[0]) 这样会导致id唯一,要自己设定
        im = Image.open("/media/ubuntu/work_space/data-human/crowdhuman/Images/"+file_name)
        #根据文件名,获取图片,这样可以获取到图片的宽高等信息。因为再odgt数据集里,没有宽高的字段信息。
        image = {'file_name': file_name, 'height': im.size[1], 'width': im.size[0],'id':image_id} #im.size[0],im.size[1]分别是宽高
        json_dict['images'].append(image) #这一步完成一行数据到字典images的转换。

        gt_box = records[i]['gtboxes']  
        gt_box_len = len(gt_box) #每一个字典gtboxes里,也有好几个记录,分别提取记录。
        for j in range(gt_box_len):
            category = gt_box[j]['tag']
            if category not in categories:  #该类型不在categories,就添加上去
                new_id = len(categories) + 1 #ID递增
                categories[category] = new_id
            category_id = categories[category]  #重新获取它的类别ID
            fbox = gt_box[j]['fbox']  #获得全身框
            #对ignore进行处理,ignore有时在key:extra里,有时在key:head_attr里。属于互斥的。
            ignore = 0 #下面key中都没有ignore时,就设为0,据观察,都存在,只是存在哪个字典里,需要判断一下
            if "ignore" in gt_box[j]['head_attr']:
                ignore = gt_box[j]['head_attr']['ignore']
            if "ignore" in gt_box[j]['extra']:
                ignore = gt_box[j]['extra']['ignore']
            #对字典annotation进行设值。
            annotation = {'area': fbox[2]*fbox[3], 'iscrowd': ignore, 'image_id':  #添加hbox、vbox字段。
                        image_id, 'bbox':fbox, 'hbox':gt_box[j]['hbox'],'vbox':gt_box[j]['vbox'],
                        'category_id': category_id,'id': bbox_id,'ignore': ignore,'segmentation': []}  
            #area的值,暂且就是fbox的宽高相乘了,观察里面的数据,发现fbox[2]小、fbox[3]很大,刚好一个全身框的宽很小,高就很大。(猜测),不是的话,再自行修改
            #segmentation怎么处理?博主自己也不知道,找不到对应的数据,这里就暂且不处理。
            #hbox、vbox、ignore是添加上去的,以防有需要。
            json_dict['annotations'].append(annotation)
            
            bbox_id += 1 #框ID ++
        image_id += 1 #这个image_id的递增操作,注意位置,博主一开始,放上面执行了,后面出了bug,自己可以理一下。
        #annotations的转化结束。
    #下面这一步,对所有数据,只需执行一次,也就是对categories里的类别进行统计。
    for cate, cid in categories.items():
            #dict.items()返回列表list的所有列表项,形如这样的二元组list:[(key,value),(key,value),...]
            cat = {'supercategory': 'none', 'id': cid, 'name': cate}
            json_dict['categories'].append(cat)
    #到此,json_dict的转化全部完成,对于其他的key,
    #因为没有用到(不访问),就不需要给他们空间,也不需要去处理,字典是按key访问的,如果自己需要就自己添加上去就行
    json_fp = open(json_path, 'w')
    json_str = json.dumps(json_dict) #写json文件。
    json_fp.write(json_str)
    json_fp.close()
if __name__ == '__main__':
    json_dict = '/media/ubuntu/work_space/data-human/crowdhuman/train.json'
    odge_path = '/media/ubuntu/work_space/data-human/crowdhuman/annotation_train.odgt'
    crowdhuman2coco(odge_path,json_dict)

3.coco数据格式转换yolo数据

"""
COCO 格式的数据集转化为 YOLO 格式的数据集
--json_path 输入的json文件路径
--save_path 保存的文件夹名字,默认为当前目录下的labels。
"""

import os 
import json
from tqdm import tqdm
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--json_path', default='/media/ubuntu/work_space/data-human/crowdhuman/val.json',type=str, help="input: coco format(json)")
parser.add_argument('--save_path', default='/media/ubuntu/work_space/data-human/crowdhuman/labels/', type=str, help="specify where to save the output dir of labels")
arg = parser.parse_args()

#中心点坐标,长和宽(小数点形式)
def convert(size, box):
    dw = 1. / (size[0])
    dh = 1. / (size[1])
    x = box[0] + box[2] / 2.0
    y = box[1] + box[3] / 2.0
    w = box[2]
    h = box[3]

    x = x * dw
    w = w * dw
    y = y * dh
    h = h * dh
    return (x, y, w, h)

if __name__ == '__main__':
    json_file = arg.json_path # COCO Object Instance 类型的标注
    ana_txt_save_path = arg.save_path  # 保存的路径

    data = json.load(open(json_file, 'r'))
    if not os.path.exists(ana_txt_save_path):
        os.makedirs(ana_txt_save_path)

    id_map = {} # coco数据集的id不连续!根据自己需求顺序自定义输出每个类别的id
    with open(os.path.join(ana_txt_save_path, 'classes.txt'), 'w') as f:
        # 写入classes.txt
        for i, category in enumerate(data['categories']): 
            f.write(f"{category['name']}\n") 
            id_map[category['id']] = category['id']-1
            print("name={}, id={}".format(category['name'], category['id']))
        print("id_map={}".format(id_map))

    i = 0
    for img in tqdm(data['images']):
        filename = img["file_name"]
        img_width = img["width"]
        img_height = img["height"]
        img_id = img["id"]
        head, tail = os.path.splitext(filename)
        ana_txt_name = head + ".txt"  # 对应的txt名字,与jpg一致
        f_txt = open(os.path.join(ana_txt_save_path, ana_txt_name), 'w')
        
        for ann in data['annotations']:
            # print("ann_type={}".format(type(ann)))
            if ann == None:
                # print("filename={}".format(filename))
                continue
            else:
                if ann['image_id'] == img_id:
                    # print("i={}".format(i))
                    print("img_id is:",img_id)
                    box = convert((img_width, img_height), ann["bbox"])
                    print('****',id_map[ann["category_id"]])
                    f_txt.write("%s %s %s %s %s\n" % (id_map[ann["category_id"]], box[0], box[1], box[2], box[3]))
        f_txt.close()   
        i = i + 1 

4.通过步骤3,crowdhuman标签转换成了coco数据集,但是在标签文档中有个classes的txt文档,内容有两个标签:person和mask   (person:0,mask:1)

For example txt containing:

0  0.4609375  0.2791666666   0.028125    0.0638888

0  0.5910156  0.6375000000   0.04140625  0.0916666

0  0.9058593  0.4722222222   0.04765625  0.0916666

5.清理yolo的txt标签文件,去掉mask,类别为1的标签去掉,代码如下:


import os
import shutil
import json
from tqdm import tqdm

txt_path = '/media/ubuntu/work_space/data-human/crowdhuman/train_labels/'

txt_list = os.listdir(txt_path)

for i in tqdm(txt_list):
    txt_name = txt_path + i
    lines = [l for l in open(txt_name,"r") if l.find("1",0,1) !=0]
    fd = open(txt_name,"a")
    fd.truncate(0)
    j = 0
    while True:
        if j < len(lines):
            fd.write(lines[j])
            j += 1
        else:
            break
    fd.close()

6.darknet environment preparation

(1) Install darknet environment

git clone GitHub - AlexeyAB/darknet: YOLOv4 / Scaled-YOLOv4 / YOLO - Neural Networks for Object Detection (Windows and Linux version of Darknet )

(2) modify Makefile

GPU=1

CUDNN=1

OPENCV=1

(3) compile darknet

cd darknet

Make

7.data preparation
   (1)generate train.txt file and test.txt

Run the following command in the terminal to generate train.txt

find /media/deepnorth/14b6945d-9936/head-doc/JPEGImages/  -name "*.jpg"  > train.txt

train.txt contains the path of each images in the JPEGImages file

text.txt is the same method ,contains the path of each images in the JPEGImages file, as shown below:

 

   crowdhuman 数据集 darknet yolov7训练 

8.train file preparation

Create a new folder named yolov7 under the darknet folder, including the following files

 crowdhuman 数据集 darknet yolov7训练

 

(1) Create file obj.data containing (where classes = number of objects), backup is the path where the model is saved :

classes= 1

train  = yolov7/train.list

valid  = yolov7/test.list

names =  yolov7/obj.names

backup = backup/

(2)Create file obj.names with objects names - each in new line

here is:person

(3)Calculating the anchors of a dataset

execute the following command in the darknet directory

./darknet detector calc_anchors yolov7/yolov7.data -num_of_clusters 9 -width 640 -height 640

:可以省略

(4)modify yolov7.cfg

[yolo]
mask = 3,4,5
anchors = 12,16, 19,36, 40,28, 36,75, 76,55, 72,146, 142,110, 192,243, 459,401
classes=1
num=9

change line classes=1 to your number of objects in each of 3 [yolo]-layers

change [filters=255] to filters=(classes + 5)x3 in the 3 [convolutional] before each [yolo] layer

anchors are calculated from step (3)

9.Start training

./darknet detector train yolov7/obj.data yolov7/yolov7.cfg -dont_show -gpus 0,1,2,3

(Optional) Resume training after interruption

./darknet detector train yolov7/obj.data yolov7/yolov7.cfg /backup/yolov3-tiny_1000.weights -gpus 0,1,2,3

(Optional) drawing of chart of average-Loss and accuracy-mAP (-map flag) during training

./darknet detector train yolov7/obj.data yolov7/yolov7.cfg -dont_show -mjpeg_port 8090 -map

When the training is completed, we will see that many models are saved under the path corresponding to backup, as shown in the following figure.

crowdhuman 数据集 darknet yolov7训练

 10.Test

(1)Test image

./darknet detector test yolov7/obj.data yolov7/yolov7.cfg  yolov7.weight imgtest.jpg

(2) Test video

./darknet detector demo yolov7/obj.data yolov7/yolov7.cfg  yolov7.weight <video file>

(3) Check accuracy mAP@IoU=0.5:

./darknet detector map yolov7/obj.data yolov7/yolov7.cfg  yolov7.weight -iou_thresh 0.5

11.Model selection

Method 1: When -map flag is added to the training command line, you will get the following chart. Select the model with the highest map value as the final model.

crowdhuman 数据集 darknet yolov7训练

Method 2: Calculate the map through step5, and then select the model with the highest map as the final model.文章来源地址https://www.toymoban.com/news/detail-451647.html

到了这里,关于crowdhuman 数据集 darknet yolov7训练的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包赞助服务器费用

相关文章

  • yolov7训练自己的数据集-gpu版

    yolov7训练自己的数据集-gpu版

    三级目录 yolov7官网: https://github.com/WongKinYiu/yolov7 miniconda清华源: https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/ 安装最新款的 在开始中找到Anaconda并打开 1.创建 2.激活 打开yolov7源码找到最下面的requirements.txt打开并将torch那一行注释 打开终端安装依赖 1.直接跳转看详细版 详细版

    2024年02月07日
    浏览(8)
  • YOLOv7训练自己的数据集(txt文件,笔记)

    YOLOv7训练自己的数据集(txt文件,笔记)

    目录 1.代码下载 2.数据集准备(.xml转.txt) (1)修改图像文件名 (2)图片和标签文件数量不对应,解决办法 (3).xml转.txt (4).txt文件随机划分出对应的训练集、测试集、验证集 3.训练数据集 (1)修改.yaml文件  (2)修改网络参数  (3)训练中断 论文地址: https://arxiv.

    2024年02月02日
    浏览(12)
  • YOLOV7训练自己的数据集以及训练结果分析(手把手教你)

    YOLOV7训练自己的数据集以及训练结果分析(手把手教你)

    YOLOV7训练自己的数据集整个过程主要包括:环境安装----制作数据集----参数修改----模型测试----模型推理 labelme标注的数据格式是VOC,而YOLOv7能够直接使用的是YOLO格式的数据,因此下面将介绍如何将自己的数据集转换成可以直接让YOLOv7进行使用。 1. 创建数据集 在data目录下新建

    2023年04月20日
    浏览(12)
  • YOLOV7训练TT100K交通标识符数据集

    YOLOV7训练TT100K交通标识符数据集

                                                             《临江仙》                                                         作者:缠中说禅                 浊水倾波三万里,愀然独坐孤峰。龙潜狮睡候飙风。无情皆竖子,有泪亦

    2024年02月06日
    浏览(13)
  • 模型实战(3)之YOLOv7实例分割、模型训练自己数据集

    下载yolov7实例分割模型: 安装环境

    2023年04月08日
    浏览(13)
  • win下YOLOv7训练自己的数据集(交通标志TT100K识别)

    win下YOLOv7训练自己的数据集(交通标志TT100K识别)

    预测结果: 数据集的准备包括数据集适配YOLO格式的重新分配以及相应配置文件的书写,此处可查看博主的TT100K2yolo的重新分配博文,该文章包括数据集划分,配置文件书写,以及最终的数据集层级目录组织,可以直接提供给下一步进行训练。 需要注意的是数据集的yaml文件有

    2024年02月06日
    浏览(10)
  • 必看新手教程!一篇就够!pycharm链接云服务器--yolov5 yolov7训练自己的数据集(矩池云)

    必看新手教程!一篇就够!pycharm链接云服务器--yolov5 yolov7训练自己的数据集(矩池云)

    趁着寒假期间稍微尝试跑了一下yolov5和yolov7的代码,由于自己用的笔记本没有独显,台式机虽有独显但用起来并不顺利,所以选择了租云服务器的方式,选择的平台是矩池云(价格合理,操作便捷) 需要特别指出的是,如果需要用pycharm链接云服务器训练,必须要使用pycharm的

    2024年02月03日
    浏览(8)
  • 【YOLOv7训练】——预训练重使用

    【YOLOv7训练】——预训练重使用

    YOLOv7论文链接:YOLOv7: Trainable bag-of-freebies sets new state-of-the-art for real-time object detectors 官方github代码链接:https://github.com/WongKinYiu/yolov7 YOLOv7于2022.07发布,已被CVPR2023接收! 此贴记录自己使用YOLOv7训练自己数据集时权重使用问题 最后个人建议,能不用YOLOv7就别用!!!别用!!

    2023年04月08日
    浏览(5)
  • YOLOV7训练模型分析

    YOLOV7训练模型分析

    训练后在runs/train文件下生成了包含这些文件或文件夹: 训练后会得到一个权重文件(weights),weights文件是YOLOv7模型的核心,它保存了模型的训练结果,也就是训练好的模型,是进行目标检测的必要文件。该文件内包括best.pt和last.pt,一般使用best.pt去进行推理。这个文件包含

    2024年02月06日
    浏览(7)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包