Comments (6)
i find a tool: https://github.com/surserrr/yolo_to_coco ,but not use yet.
from nanodet.
you can change it by yourself
from nanodet.
I wrote a script to convert coco to fit my own VOC data set
-- coding=utf-8 --
import json
import os
import cv2
import xml.etree.ElementTree as ET
import shutil
class2id = {'anchundanbopi1': 0, 'anchundanbopi': 0,
'aogan1': 1, 'aogan': 1,
'baigua': 2, 'baigua1': 2,
'boluo': 3, 'boluo1': 3,
}
从xml文件中提取bounding box信息, 格式为[[x_min, y_min, x_max, y_max, name]]
def parse_xml(xml_path):
tree = ET.parse(xml_path)
root = tree.getroot()
objs = root.findall('object')
coords = list()
for ix, obj in enumerate(objs):
name = obj.find('name').text
box = obj.find('bndbox')
x_min = int(box[0].text)
y_min = int(box[1].text)
x_max = int(box[2].text)
y_max = int(box[3].text)
coords.append([x_min, y_min, x_max, y_max, name])
return coords
def convert(root_path, source_xml_root_path, target_xml_root_path, phase='train', split=80000):
'''
root_path:
根路径,里面包含JPEGImages(图片文件夹),classes.txt(类别标签),以及annotations文件夹(如果没有则会自动创建,用于保存最后的json)
source_xml_root_path:
VOC xml文件存放的根目录
target_xml_root_path:
coco xml存放的根目录
phase:
状态:'train'或者'test'
split:
train和test图片的分界点数目
'''
dataset = {'categories': [], 'images': [], 'annotations': []}
# 打开类别标签
with open(os.path.join(root_path, 'classes.txt')) as f:
classes = f.read().strip().split()
# 建立类别标签和数字id的对应关系
for i, cls in enumerate(classes, 1):
dataset['categories'].append({'id': i, 'name': cls, 'supercategory': 'beverage'}) # mark
print(dataset['categories'])
# 读取images文件夹的图片名称
pics = [f for f in os.listdir(os.path.join(root_path, 'images'))]
# 判断是建立训练集还是验证集
if phase == 'train':
pics = [line for i, line in enumerate(pics) if i <= split]
elif phase == 'val':
pics = [line for i, line in enumerate(pics) if i > split]
print('---------------- start convert ---------------')
bnd_id = 1 # 初始为1
for i, pic in enumerate(pics):
# print('pic '+str(i+1)+'/'+str(len(pics)))
xml_path = os.path.join(source_xml_root_path, pic[:-4] + '.xml')
pic_path = os.path.join(root_path, 'images/' + pic)
# 用opencv读取图片,得到图像的宽和高
print('1')
try:
im = cv2.imread(pic_path)
except IOError:
print(pic_path)
height, width, _ = im.shape
# 添加图像的信息到dataset中
dataset['images'].append({'file_name': pic,
'id': i,
'width': width,
'height': height})
try:
coords = parse_xml(xml_path)
except:
print(pic[:-4] + '.xml not exists~')
continue
for coord in coords:
# x_min
x1 = int(coord[0]) - 1
x1 = max(x1, 0)
# y_min
y1 = int(coord[1]) - 1
y1 = max(y1, 0)
# x_max
x2 = int(coord[2])
# y_max
y2 = int(coord[3])
assert x1 < x2
assert y1 < y2
# name
name = coord[4]
# cls_id = classes.index(name) + 1 # 从1开始
cls_id = class2id[name] + 1
width = max(0, x2 - x1)
height = max(0, y2 - y1)
dataset['annotations'].append({
'area': width * height,
'bbox': [x1, y1, width, height],
'category_id': int(cls_id),
'id': bnd_id,
'image_id': i,
'iscrowd': 0,
# mask, 矩形是从左上角点按顺时针的四个顶点
'segmentation': [[x1, y1, x2, y1, x2, y2, x1, y2]]
})
bnd_id += 1
# 保存结果的文件夹
folder = os.path.join(target_xml_root_path, 'annotations')
if os.path.exists(folder):
shutil.rmtree(folder)
os.makedirs(folder)
json_name = os.path.join(target_xml_root_path, 'annotations/instances_{}2014.json'.format(phase))
with open(json_name, 'w') as f:
json.dump(dataset, f)
if name == 'main':
convert(root_path='./fruit_dataset/coco_fruit/',
source_xml_root_path='.coco_fruit/Annotations',
target_xml_root_path='./coco_fruit/coco_data')
from nanodet.
https://github.com/yukkyo/voc2coco , this repository is good, and I also made a little adjustment to it in its issue
from nanodet.
labels: txt-->xml-->json
This project support to Nanodet project to make official labels.
https://github.com/eeric/yolo2voc2coco
successfully, as following:
[root][12-01 02:42:11]INFO:val|Epoch2/70|Iter123(4940/4952)| lr:1.40e-01| loss_qfl:0.6564| loss_bbox:0.9067| loss_dfl:0.3167|
[root][12-01 02:42:11]INFO:val|Epoch2/70|Iter123(4942/4952)| lr:1.40e-01| loss_qfl:0.3882| loss_bbox:0.7180| loss_dfl:0.2757|
[root][12-01 02:42:12]INFO:val|Epoch2/70|Iter123(4944/4952)| lr:1.40e-01| loss_qfl:0.2997| loss_bbox:0.7028| loss_dfl:0.2815|
[root][12-01 02:42:12]INFO:val|Epoch2/70|Iter123(4946/4952)| lr:1.40e-01| loss_qfl:0.4795| loss_bbox:0.5486| loss_dfl:0.2645|
[root][12-01 02:42:13]INFO:val|Epoch2/70|Iter123(4948/4952)| lr:1.40e-01| loss_qfl:0.4833| loss_bbox:0.8868| loss_dfl:0.2684|
[root][12-01 02:42:14]INFO:val|Epoch2/70|Iter123(4950/4952)| lr:1.40e-01| loss_qfl:0.5885| loss_bbox:0.7382| loss_dfl:0.3275|
Loading and preparing results...
DONE (t=3.01s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type bbox
Loading and preparing results...
DONE (t=2.72s)
creating index...
index created!
Running per image evaluation...
Evaluate annotation type bbox
DONE (t=46.62s).
Accumulating evaluation results...
DONE (t=13.37s).
Average Precision (AP) @[ IoU=0.50:0.95 | area= all | maxDets=100 ] = 0.036
Average Precision (AP) @[ IoU=0.50 | area= all | maxDets=100 ] = 0.074
Average Precision (AP) @[ IoU=0.75 | area= all | maxDets=100 ] = 0.031
Average Precision (AP) @[ IoU=0.50:0.95 | area= small | maxDets=100 ] = 0.002
Average Precision (AP) @[ IoU=0.50:0.95 | area=medium | maxDets=100 ] = 0.019
Average Precision (AP) @[ IoU=0.50:0.95 | area= large | maxDets=100 ] = 0.061
Average Recall (AR) @[ IoU=0.50:0.95 | area= all | maxDets= 1 ] = 0.097
from nanodet.
I also wrote a script to convert yolo(txt) to coco(json)
The only thing you need to do is that change the content of the line with #####
Hope to help the one who needs it.
https://github.com/jerryWTMH/yolo2coco
import os
import numpy as np
import json
import cv2
id_counter = 0 # To record the id
FILE_PATH = 'put your image folder path here!' #####
out = {'annotations': [],
'categories': [{"id": 1, "name": "cricoid_cartilage", "supercategory": ""}, {"id": 2, "name": "thyroid_cartilage", "supercategory": ""}], ##### change the categories to match your dataset!
'images': [],
'info': {"contributor": "", "year": "", "version": "", "url": "", "description": "", "date_created": ""},
'licenses': {"id": 0, "name": "", "url": ""}
}
def annotations_data(whole_path , image_id):
# id, bbox, iscrowd, image_id, category_id
global id_counter
txt = open(whole_path,'r')
for line in txt.readlines(): # if txt.readlines is null, this for loop would not run
data = line.strip()
data = data.split()
# convert the center into the top-left point!
data[1] = float(data[1])* 800 - 0.5 * float(data[3])* 800 ##### change the 800 to your raw image width
data[2] = float(data[2])* 600 - 0.5 * float(data[4])* 600 ##### change the 600 to your raw image height
data[3] = float(data[3])* 800 ##### change the 800 to your raw image width
data[4] = float(data[4])* 600 ##### change the 600 to your raw image height
bbox = [data[1],data[2],data[3],data[4]]
ann = {'id': id_counter,
'bbox': bbox,
'area': data[3] * data[4],
'iscrowd': 0,
'image_id': int(image_id),
'category_id': int(data[0]) + 1
}
out['annotations'].append(ann)
id_counter = id_counter + 1
def images_data(file_name):
#id, height, width, file_name
id = file_name.split('.')[0]
file_name = id + '.jpg' ##### change '.jpg' to other image formats if the format of your image is not .jpg
imgs = {'id': int(id),
'height': 600, ##### change the 600 to your raw image height
'width': 800, ##### change the 800 to your raw image width
'file_name': file_name,
"coco_url": "",
"flickr_url": "",
"date_captured": 0,
"license": 0
}
out['images'].append(imgs)
if __name__ == '__main__':
files = os.listdir(FILE_PATH)
files.sort()
for file in files:
whole_path = os.path.join(FILE_PATH,file)
annotations_data(whole_path, file.split('.')[0])
images_data(file)
with open('instances_merge_train.json', 'w') as outfile: ##### change the str to the json file name you want
json.dump(out, outfile, separators=(',', ':'))
from nanodet.
Related Issues (20)
- 关于参数量的问题 HOT 2
- Unable to replicate model zoo mAP
- x1.5 model consistently underperforming HOT 2
- How to remove the AGM module when converting the ckpt model
- Memory error when I start training HOT 7
- How can we visualize the results on a test set?
- Why the training phase is quite slow with V100 GPU? HOT 5
- namodet config file nanodet.plus-0.5x.yml pipeline value
- concerning the auxiliary training module
- How to train model with MobileNetV2?
- How to add MobileNetV3 backbone?
- 训练问题
- Maybe there is some wrong code comments?
- How to apply MobileViTV2 using Timm?
- 为什么显存占用这么高Why is the memory usage so high
- Traning code freezed when saving best check point
- Why did this error occur during verification?
- How to change the backbone to CSPDarkNet53?
- > 我在训练时也遇到了同样的问题,但是我的gpu并没有占用,您知道该怎么解决吗 TimeoutError: The client socket has timed out after 1800s while trying to connect to (127.0.0.1, 53580).
- input image size
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nanodet.