赞
踩
mkdir yolov5
cd yolov5
docker run -it --gpus all --name yolov5_dev -v $PWD:/home/ cuda_dev_image:v1.0 bash
apt update
apt install git -y
git clone https://github.com/ultralytics/yolov5
cd yolov5
bash data/scripts/get_coco.sh --train --val
cd ../datasets/coco/
wget http://images.cocodataset.org/annotations/annotations_trainval2017.zip
rm -rf annotations
unzip annotations_trainval2017.zip
path: /home/dataset/coco
train: train2017.txt
val: val2017.txt
names:
0: person
1: bicycle
2: car
3: motorcycle
4: airplane
5: bus
#create_sub_coco_dataset.py import json import yaml import sys import os import shutil import tqdm def MakeDirs(dir): if not os.path.exists(dir): os.makedirs(dir,True) def create_sub_coco_dataset(data_yaml="coco-6.yaml", src_root_dir="../datasets/coco", dst_root_dir="class6/coco", folder="val2017" ): MakeDirs(dst_root_dir+"/annotations/") MakeDirs(dst_root_dir+"/images/"+folder) MakeDirs(dst_root_dir+"/labels/"+folder) keep_names=[x+1 for x in yaml.safe_load(open(data_yaml).read())['names'].keys()] all_annotations=json.loads(open(src_root_dir+"/annotations/instances_{}.json".format(folder)).read()) keep_categories=[x for x in all_annotations["categories"] if x["id"] in keep_names] keep_annotations=[x for x in all_annotations['annotations'] if x['category_id'] in keep_names] all_annotations['annotations']=keep_annotations all_annotations["categories"]=keep_categories if not os.path.exists(dst_root_dir+"/annotations/instances_{}.json".format(folder)): with open(dst_root_dir+"/annotations/instances_{}.json".format(folder), "w") as f: json.dump(all_annotations, f) filelist=set() for i in tqdm.tqdm(keep_annotations): img_src_path="/images/{}/{:012d}.jpg".format(folder,i["image_id"]) label_src_path="/labels/{}/{:012d}.txt".format(folder,i["image_id"]) if not os.path.exists(dst_root_dir+img_src_path): shutil.copy(src_root_dir+img_src_path, dst_root_dir+img_src_path) if not os.path.exists(dst_root_dir+label_src_path): keep_records=[x for x in open(src_root_dir+label_src_path,"r").readlines() if (int(x.strip().split(" ")[0])+1) in keep_names] with open(dst_root_dir+label_src_path,"w") as f: for r in keep_records: f.write(r) filelist.add("./images/{}/{:012d}.jpg\n".format(folder,i["image_id"])) with open(dst_root_dir+"/{}.txt".format(folder),"w") as f: for r in filelist: f.write(r) new_data_yaml=yaml.safe_load(open(data_yaml).read()) new_data_yaml["path"]=dst_root_dir with open(dst_root_dir+"/coco.yaml", 'w') as f: f.write(yaml.dump(new_data_yaml, allow_unicode=True)) create_sub_coco_dataset(sys.argv[1],sys.argv[2],sys.argv[3],sys.argv[4])
cd /home/yolov5
rm -rf class6
python create_sub_coco_dataset.py coco-6.yaml ../datasets/coco class6/coco train2017
python create_sub_coco_dataset.py coco-6.yaml ../datasets/coco class6/coco val2017
python train.py --data class6/coco/coco.yaml \
--weights '' --cfg models/yolov5m.yaml \
--img 640 --workers 0 --device 0
python val.py --weights best.pt --data class6/coco/coco.yaml \
--img 640 --conf-thres 0.001 --iou-thres 0.6 \
--workers 0 --device 0 --half --batch-size 1
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。