solo分割训练1-数据准备

1. labeme数据标注

2.labelme-json转cocoDatatset脚本

import os
from tqdm import tqdm
import json
import time

localtime = time.localtime(time.time())
path = './'
file_out= "../coco/annotation/train.json"
classes = {"road_tooth":1,
           "road":2,
           "wall":3,
           "car":4}
for root, dirs, files in os.walk(path):
    dict = {"info": {}, "images": [], "annotations": [], "licenses": [], "categories": [
        {
            "supercategory": "road_tooth",
            "id": 1,
            "name": "road_tooth"
        },
        {
            "supercategory": "road",
            "id": 2,
            "name": "road"
        },
        {
            "supercategory": "wall",
            "id": 3,
            "name": "wall"
        },
        {
            "supercategory": "car",
            "id": 4,
            "name": "car"
        }
    ]}
    info = {"year": localtime[0],
            "version": "1.0",
            "description": "labelme",
            "contributor": "xiexinyan",
            "data_created": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}
    dict["info"] = info
    cnt = 0
    cnt_seg =1
    for file in tqdm(files):
        if file.endswith('.json'):
            with open(file, 'r', encoding='utf8') as file_in:
                json_data = json.load(file_in)
                tmp_img={"id":cnt,
                         "width":json_data["imageWidth"],
                         "height":json_data["imageHeight"],
                         "filename":json_data["imagePath"],
                         "license": 0,
                         "date_captured":""}
                dict["images"].append(tmp_img)
                for i in range(len(json_data["shapes"])):
                    tmp_ann = {"segmentation": [[]],
                               "area":0,
                               "bbox":[],
                               "iscrowd":0,
                               "id": 0,
                               "image_id": cnt,
                               "category_id": 0}
                    min_x =1920
                    min_y=1080
                    max_x=0
                    max_y=0
                    tmp_ann["category_id"] = classes[json_data["shapes"][i]["label"]]
                    for p in json_data["shapes"][i]["points"]:
                        tmp_ann["segmentation"][0].append(int(p[0]))
                        tmp_ann["segmentation"][0].append(int(p[1]))
                        min_x = int(p[0]) if(min_x>p[0]) else min_x
                        max_x = int(p[0]) if(max_x<p[0]) else max_x
                        min_y = int(p[1]) if (min_y > p[1]) else min_y
                        max_y = int(p[1]) if (max_y < p[1]) else max_y
                    # print(min_x,max_x, min_y,max_y)
                    assert (min_x>=0)
                    assert (min_y>=0)
                    assert (max_x<1920)
                    assert (max_y<1080)
                    height = max_y-min_y
                    width = max_x-min_x
                    tmp_ann["area"] = height*width
                    tmp_ann["bbox"].append(min_x)
                    tmp_ann["bbox"].append(min_y)
                    tmp_ann["bbox"].append(width)
                    tmp_ann["bbox"].append(height)
                    tmp_ann["id"] = cnt_seg
                    cnt_seg = cnt_seg +1
                    dict["annotations"].append(tmp_ann)
                cnt = cnt + 1
                file_in.close()

    with open(file_out, "w", newline='\n', encoding='utf8') as f:
        f.write(json.dumps(dict, indent=1))
        f.close()
  • json文件处理-label重命名脚本
import os
from tqdm import tqdm
import json
path = './'
for root, dirs, files in os.walk(path):
    for file in tqdm(files):
        if file.endswith('.json'):
            file_o = "data/"+file
            dict = {}
            with open(file, 'r', encoding='utf8') as file_in:
                json_data = json.load(file_in)
                for i in range(len(json_data["shapes"])):
                    if json_data["shapes"][i]["label"] == "road tooth":
                        json_data["shapes"][i]["label"] = "road_tooth"
                json_data["imageData"] = {}
                dict = json_data

            file_in.close()
            with open(file_o, "w", encoding='utf8') as file_out:
                file_out.write(json.dumps(dict))
            file_out.close()

3. solov2网络训练注意:

  • dataset中label的id从1开始,对应coco.py中
 self.cat2label = {
            cat_id: i+1
            for i, cat_id in enumerate(self.cat_ids)
        }
        print(self.cat2label)
  • dataset中
"categories": [
        {
            "supercategory": "road_tooth",
            "id": 1,
            "name": "road_tooth"
        },
        {
            "supercategory": "road",
            "id": 2,
            "name": "road"
        },
        {
            "supercategory": "wall",
            "id": 3,
            "name": "wall"
        },
        {
            "supercategory": "car",
            "id": 4,
            "name": "car"
        }
    ]

是pycocotools中self.cat_ids = self.coco.getCatIds()的数据集

4. 训练结果验证时,需要将mmdet/core/evaluation/class_names.py中相关的label进行更改


def coco_classes():
    return [
        # 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train',
        # 'truck', 'boat', 'traffic_light', 'fire_hydrant', 'stop_sign',
        # 'parking_meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep',
        # 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella',
        # 'handbag', 'tie', 'suitcase', 'frisbee', 'skis', 'snowboard',
        # 'sports_ball', 'kite', 'baseball_bat', 'baseball_glove', 'skateboard',
        # 'surfboard', 'tennis_racket', 'bottle', 'wine_glass', 'cup', 'fork',
        # 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich', 'orange',
        # 'broccoli', 'carrot', 'hot_dog', 'pizza', 'donut', 'cake', 'chair',
        # 'couch', 'potted_plant', 'bed', 'dining_table', 'toilet', 'tv',
        # 'laptop', 'mouse', 'remote', 'keyboard', 'cell_phone', 'microwave',
        # 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase',
        # 'scissors', 'teddy_bear', 'hair_drier', 'toothbrush'
        'road_tooth', 'road', 'wall', 'car'
    ]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值