2 Commits

Author SHA1 Message Date
  liuxy2318 ac8b96ca7a car 1 year ago
  liuxy2318 91eb8cf7c3 car 1 year ago
79 changed files with 1533 additions and 4739635 deletions
Split View
  1. +16
    -0
      .ipynb_checkpoints/requirements-checkpoint.txt
  2. +24
    -0
      configs/.ipynb_checkpoints/coco-checkpoint.yaml
  3. +10
    -0
      configs/yolov8/.ipynb_checkpoints/yolov8l-checkpoint.yaml
  4. BIN
      examples/car_detection.zip
  5. +10
    -0
      examples/car_detection/.ipynb_checkpoints/bdd100k-checkpoint.yaml
  6. +223
    -0
      examples/car_detection/.ipynb_checkpoints/bdd100k2voc-checkpoint.py
  7. +30
    -0
      examples/car_detection/.ipynb_checkpoints/bdd100k_train_val-checkpoint.py
  8. +21
    -0
      examples/car_detection/.ipynb_checkpoints/caryolov7l-checkpoint.yaml
  9. +29
    -0
      examples/car_detection/.ipynb_checkpoints/caryolov7l_a4000-checkpoint.yaml
  10. +21
    -0
      examples/car_detection/.ipynb_checkpoints/caryolov8l-checkpoint.yaml
  11. +13
    -0
      examples/car_detection/.ipynb_checkpoints/yolov8-l-car-checkpoint.yaml
  12. +33
    -0
      examples/car_detection/Reclass_voc.py
  13. +29
    -0
      examples/car_detection/Reclass_yolo.py
  14. +82
    -0
      examples/car_detection/alltxt2json.py
  15. +171
    -0
      examples/car_detection/anchor_kmeans++.py
  16. +170
    -44
      examples/car_detection/anchor_kmeans.py
  17. +10
    -0
      examples/car_detection/bdd100k.yaml
  18. +2
    -2
      examples/car_detection/bdd100k_train_val.py
  19. +22
    -0
      examples/car_detection/caryolov5m_a4000.yaml
  20. +22
    -0
      examples/car_detection/caryolov5m_a4000_test.yaml
  21. +0
    -21
      examples/car_detection/caryolov5s.yaml
  22. +0
    -21
      examples/car_detection/caryolov5s_a4000.yaml
  23. +21
    -0
      examples/car_detection/caryolov7l.yaml
  24. +29
    -0
      examples/car_detection/caryolov7l_a4000.yaml
  25. +14
    -7
      examples/car_detection/caryolov7s_a4000.yaml
  26. +21
    -0
      examples/car_detection/caryolov8l.yaml
  27. +21
    -0
      examples/car_detection/caryolov8s_a4000.yaml
  28. +35
    -0
      examples/car_detection/class_number_voc.py
  29. +19
    -0
      examples/car_detection/class_number_yolo.py
  30. +0
    -4739523
      examples/car_detection/instances_val2017.json
  31. BIN
      examples/car_detection/predict/007056.jpg
  32. BIN
      examples/car_detection/predict/data/cabc30fc-e7726578.jpg
  33. BIN
      examples/car_detection/predict/data/cabc30fc-eb673c5a.jpg
  34. BIN
      examples/car_detection/predict/data/cabc30fc-fd79926f.jpg
  35. BIN
      examples/car_detection/predict/data/cabc9045-1b8282ba.jpg
  36. BIN
      examples/car_detection/predict/data/cabc9045-581f64de.jpg
  37. BIN
      examples/car_detection/predict/data/cabc9045-5a50690f.jpg
  38. BIN
      examples/car_detection/predict/data/cabc9045-b3349548.jpg
  39. BIN
      examples/car_detection/predict/data/cabc9045-c6dc9529.jpg
  40. BIN
      examples/car_detection/predict/data/cabc9045-cd422b81.jpg
  41. BIN
      examples/car_detection/predict/data/cabc9045-d91ecb66.jpg
  42. +4
    -4
      examples/car_detection/rename_trainval.py
  43. +75
    -0
      examples/car_detection/txt2xml.py
  44. +13
    -13
      examples/car_detection/txt_gen.py
  45. BIN
      examples/car_detection/val2/000001.jpg
  46. BIN
      examples/car_detection/val2/000002.jpg
  47. BIN
      examples/car_detection/val2/000003.jpg
  48. BIN
      examples/car_detection/val2/000004.jpg
  49. BIN
      examples/car_detection/val2/000005.jpg
  50. BIN
      examples/car_detection/val2/000006.jpg
  51. BIN
      examples/car_detection/visual/val/000065.jpg
  52. BIN
      examples/car_detection/visual/val/000604.jpg
  53. BIN
      examples/car_detection/visual/val/000742.jpg
  54. BIN
      examples/car_detection/visual/val/001005.jpg
  55. BIN
      examples/car_detection/visual/val/001343.jpg
  56. BIN
      examples/car_detection/visual/val/002028.jpg
  57. BIN
      examples/car_detection/visual/val/002452.jpg
  58. BIN
      examples/car_detection/visual/val/003424.jpg
  59. BIN
      examples/car_detection/visual/val/003780.jpg
  60. BIN
      examples/car_detection/visual/val/003984.jpg
  61. BIN
      examples/car_detection/visual/val/004436.jpg
  62. BIN
      examples/car_detection/visual/val/005006.jpg
  63. BIN
      examples/car_detection/visual/val/005382.jpg
  64. BIN
      examples/car_detection/visual/val/005758.jpg
  65. BIN
      examples/car_detection/visual/val/006101.jpg
  66. BIN
      examples/car_detection/visual/val/006291.jpg
  67. BIN
      examples/car_detection/visual/val/007056.jpg
  68. BIN
      examples/car_detection/visual/val/007365.jpg
  69. BIN
      examples/car_detection/visual/val/008440.jpg
  70. BIN
      examples/car_detection/visual/val/008584.jpg
  71. BIN
      examples/car_detection/visual/val/008748.jpg
  72. BIN
      examples/car_detection/visual/val/008980.jpg
  73. BIN
      examples/car_detection/visual/val/009529.jpg
  74. BIN
      examples/car_detection/visual/val/009566.jpg
  75. BIN
      examples/car_detection/visual/val/009813.jpg
  76. +67
    -0
      examples/car_detection/visual_json.py
  77. +68
    -0
      examples/car_detection/visual_json2.py
  78. +195
    -0
      examples/car_detection/voc2yolo.py
  79. +13
    -0
      examples/car_detection/yolov8-l-car.yaml

+ 16
- 0
.ipynb_checkpoints/requirements-checkpoint.txt View File

@@ -0,0 +1,16 @@
# MindYOLO requirements
# Usage: pip install -r requirements.txt

# Setup
pybind11>=2.10.4

# Base
numpy>=1.17.0
PyYAML>=5.3
tqdm
opencv-python>=4.7.0.68
opencv-python-headless>=4.7.0.68

# Extras
albumentations>=1.0.3
pycocotools>=2.0.2

+ 24
- 0
configs/.ipynb_checkpoints/coco-checkpoint.yaml View File

@@ -0,0 +1,24 @@
# COCO 2017 dataset http://cocodataset.org

data:
dataset_name: coco

train_set: ./coco/train2017.txt # 118287 images
val_set: ./coco/val2017.txt # 5000 images
test_set: ./coco/test-dev2017.txt # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794

nc: 80

# class names
names: [ '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' ]

train_transforms: []
test_transforms: []

+ 10
- 0
configs/yolov8/.ipynb_checkpoints/yolov8l-checkpoint.yaml View File

@@ -0,0 +1,10 @@
__BASE__: [
'../coco.yaml',
'./hyp.scratch.high.yaml',
'./yolov8-base.yaml'
]

network:
depth_multiple: 1.00 # scales module repeats
width_multiple: 1.00 # scales convolution channels
max_channels: 512

BIN
examples/car_detection.zip View File


+ 10
- 0
examples/car_detection/.ipynb_checkpoints/bdd100k-checkpoint.yaml View File

@@ -0,0 +1,10 @@
data:
dataset_name: bdd100k
train_set: /home/ma-user/work/dataset/bdd100k_Vehicle/train.txt
val_set: /home/ma-user/work/dataset/bdd100k_Vehicle/val.txt
test_set: /home/ma-user/work/dataset/bdd100k_Vehicle/test.txt
nc: 2
# class names
names: [ 'Motor vehicle', 'Non-motor vehicle']
train_transforms: []
test_transforms: []

+ 223
- 0
examples/car_detection/.ipynb_checkpoints/bdd100k2voc-checkpoint.py View File

@@ -0,0 +1,223 @@
import os
import json
import sys
from xml.etree import ElementTree
from xml.etree.ElementTree import Element, SubElement
from lxml import etree
from xml.dom.minidom import parseString
#种类有'car', 'bus', 'person', 'bike', 'truck', 'motor', 'train', 'rider', 'traffic sign', 'traffic light',可以自己定义序号
categorys = ['car', 'bus', 'person', 'bike', 'truck', 'motor', 'train', 'rider', 'traffic sign', 'traffic light']
def parseJson(jsonFile):
'''
params:
jsonFile -- BDD00K数据集的一个json标签文件
return:
返回一个列表的列表,存储了一个json文件里面的方框坐标及其所属的类,
形如:[[325, 342, 376, 384, 'car'], [245, 333, 336, 389, 'car']]
'''
objs = []
obj = []
f = open(jsonFile)
info = json.load(f)
objects = info['frames'][0]['objects']
for i in objects:
if (i['category'] in categorys):
obj.append(int(i['box2d']['x1']))
obj.append(int(i['box2d']['y1']))
obj.append(int(i['box2d']['x2']))
obj.append(int(i['box2d']['y2']))
obj.append(i['category'])
objs.append(obj)
obj = []
return objs
class PascalVocWriter:
def __init__(self, foldername, filename, imgSize, databaseSrc='Unknown', localImgPath=None):
'''
params:
foldername -- 要存储的xml文件的父目录
filename -- xml文件的文件名
imgSize -- 图片的尺寸
databaseSrc -- 数据库名,这里不需要,默认为Unknown
localImaPath -- xml文件里面的<path></path>标签的内容
'''
self.foldername = foldername
self.filename = filename
self.databaseSrc = databaseSrc
self.imgSize = imgSize
self.boxlist = []
self.localImgPath = localImgPath
def prettify(self, elem):
"""
params:
elem -- xml的根标签,以<annotation>开始
return:
返回一个美观输出的xml(用到minidom),本质是一个str
"""
xml = ElementTree.tostring(elem)
dom = parseString(xml)
prettifyResult = dom.toprettyxml(' ')
return prettifyResult
def genXML(self):
"""
return:
生成一个VOC格式的xml,返回一个xml的根标签,以<annotation>开始
"""
if self.filename is None or \
self.foldername is None or \
self.imgSize is None or \
len(self.boxlist) <= 0:
return None
top = Element('annotation')
folder = SubElement(top, 'folder')
folder.text = self.foldername
filename = SubElement(top, 'filename')
filename.text = self.filename
localImgPath = SubElement(top, 'path')
localImgPath.text = self.localImgPath
source = SubElement(top, 'source')
database = SubElement(source, 'database')
database.text = self.databaseSrc
size_part = SubElement(top, 'size')
width = SubElement(size_part, 'width')
height = SubElement(size_part, 'height')
depth = SubElement(size_part, 'depth')
width.text = str(self.imgSize[1])
height.text = str(self.imgSize[0])
if len(self.imgSize) == 3:
depth.text = str(self.imgSize[2])
else:
depth.text = '1'
segmented = SubElement(top, 'segmented')
segmented.text = '0'
return top
def addBndBox(self, xmin, ymin, xmax, ymax, name):
'''
将检测对象框坐标及其对象类别作为一个字典加入到self.boxlist中
params:
xmin -- 检测框的左上角的x坐标
ymin -- 检测框的左上角的y坐标
xmax -- 检测框的右下角的x坐标
ymax -- 检测框的右下角的y坐标
name -- 检测框内的对象类别名
'''
bndbox = {'xmin': xmin, 'ymin': ymin, 'xmax': xmax, 'ymax': ymax}
bndbox['name'] = name
self.boxlist.append(bndbox)
def appendObjects(self, top):
'''
在xml文件中加入检测框的坐标及其对象类别名
params:
top -- xml的根标签,以<annotation>开始
'''
for each_object in self.boxlist:
object_item = SubElement(top, 'object')
name = SubElement(object_item, 'name')
name.text = str(each_object['name'])
pose = SubElement(object_item, 'pose')
pose.text = "Unspecified"
truncated = SubElement(object_item, 'truncated')
truncated.text = "0"
difficult = SubElement(object_item, 'Difficult')
difficult.text = "0"
bndbox = SubElement(object_item, 'bndbox')
xmin = SubElement(bndbox, 'xmin')
xmin.text = str(each_object['xmin'])
ymin = SubElement(bndbox, 'ymin')
ymin.text = str(each_object['ymin'])
xmax = SubElement(bndbox, 'xmax')
xmax.text = str(each_object['xmax'])
ymax = SubElement(bndbox, 'ymax')
ymax.text = str(each_object['ymax'])
def save(self, targetFile=None):
'''
以美观输出的xml格式来保存xml文件
params:
targetFile -- 存储的xml文件名,不包括.xml部分
'''
root = self.genXML()
self.appendObjects(root)
out_file = None
subdir = self.foldername.split('/')[-1]
if not os.path.isdir(subdir):
os.mkdir(subdir)
if targetFile is None:
with open(self.foldername + '/' + self.filename + '.xml', 'w') as out_file:
prettifyResult = self.prettify(root)
out_file.write(prettifyResult)
out_file.close()
else:
with open(targetFile, 'w') as out_file:
prettifyResult = self.prettify(root)
out_file.write(prettifyResult)
out_file.close()
class PascalVocReader:
def __init__(self, filepath):
self.shapes = []
self.filepath = filepath
self.parseXML()
def getShapes(self):
return self.shapes
def addShape(self, label, bndbox):
xmin = int(bndbox.find('xmin').text)
ymin = int(bndbox.find('ymin').text)
xmax = int(bndbox.find('xmax').text)
ymax = int(bndbox.find('ymax').text)
points = [(xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax)]
self.shapes.append((label, points, None, None))
def parseXML(self):
assert self.filepath.endswith('.xml'), "Unsupport file format"
parser = etree.XMLParser(encoding='utf-8')
xmltree = ElementTree.parse(self.filepath, parser=parser).getroot()
filename = xmltree.find('filename').text
for object_iter in xmltree.findall('object'):
bndbox = object_iter.find("bndbox")
label = object_iter.find('name').text
self.addShape(label, bndbox)
return True
def main(srcDir, dstDir):
i = 1
for dirpath, dirnames, filenames in os.walk(srcDir):
for filepath in filenames:
fileName = os.path.join(dirpath, filepath)
print(fileName)
print("processing: {}, {}".format(i, fileName))
i = i + 1
xmlFileName = filepath[:-5]
objs = parseJson(str(fileName))
if len(objs):
tmp = PascalVocWriter(dstDir, xmlFileName, (720, 1280, 3), fileName)
for obj in objs:
tmp.addBndBox(obj[0], obj[1], obj[2], obj[3], obj[4])
tmp.save()
else:
print(fileName)
if __name__ == '__main__':
srcDir = 'D:\\1深度学习\\数据集\\BDD100K\\bdd100k_labels\\bdd100k\\labels\\100k\\train'
dstDir = 'D:\\1深度学习\\数据集\\BDD100K\\bdd100k_labels\\bdd100k\\labels\\100k\\train_xml'
main(srcDir, dstDir)

+ 30
- 0
examples/car_detection/.ipynb_checkpoints/bdd100k_train_val-checkpoint.py View File

@@ -0,0 +1,30 @@
#生成train.txt和val.txt和test.txt文件(三者包含的是训练集、验证集和测试集的所有图片的路径)

import os
import glob

def generate_train_and_val(image_path, txt_file):
with open(txt_file, 'w') as tf:
image_files = glob.glob(image_path + '*.jpg')
if len(image_files) == 0:
print("No image files found in the specified path.")
return
for jpg_file in image_files:
print(jpg_file)
tf.write(jpg_file + '\n')




def main(path, dstpath):
generate_train_and_val(path, dstpath)

if __name__ == '__main__':
#srcpath = 'bdd100k/images/val/'
#dstpath = 'bdd100k/images/val.txt'
srcpath = '/home/ma-user/work/dataset/bdd100k_Vehicle/images/val/' # 训练集图片所在的路径(自己设置)
dstpath = '/home/ma-user/work/dataset/bdd100k_Vehicle/val.txt' # 新生成的txt所在路径(自己设置)
main(srcpath, dstpath)


+ 21
- 0
examples/car_detection/.ipynb_checkpoints/caryolov7l-checkpoint.yaml View File

@@ -0,0 +1,21 @@
__BASE__: [
'../../configs/yolov7/yolov7.yaml',
]

per_batch_size: 8 # 单卡batchsize,总的batchsize=per_batch_size * device_num
img_size: 640 # image sizes
weight: /home/ma-user/work/pretrainmodel/yolov7l.ckpt
strict_load: False # 是否按严格加载ckpt内参数,默认True,若设成False,当分类数不一致,丢掉最后一层分类器的weight
log_interval: 10 # 每log_interval次迭代打印一次loss结果

data:
dataset_name: bdd100k_Vehicle
train_set: /home/ma-user/work/dataset/bdd100k_Vehicle/train.txt # 实际训练数据路径
val_set: /home/ma-user/work/dataset/bdd100k_Vehicle/val.txt
test_set: /home/ma-user/work/dataset/bdd100k_Vehicle/test.txt
nc: 2 # 分类数
# class names
names: ['Motor vehicle', 'Non-motor vehicle'] # 每一类的名字

optimizer:
lr_init: 0.001 # initial learning rate

+ 29
- 0
examples/car_detection/.ipynb_checkpoints/caryolov7l_a4000-checkpoint.yaml View File

@@ -0,0 +1,29 @@
__BASE__: [
'../../configs/yolov7/yolov7.yaml',
]

per_batch_size: 8 # 单卡batchsize,总的batchsize=per_batch_size * device_num
img_size: 640 # image sizes
weight: /home/lxy/ubuntu/lxy/project/pretrainmodel/yolov7l.ckpt
strict_load: False # 是否按严格加载ckpt内参数,默认True,若设成False,当分类数不一致,丢掉最后一层分类器的weight
log_interval: 10 # 每log_interval次迭代打印一次loss结果

network:
# anchors
anchors:
- [12,16, 19,36, 40,28] # P3/8
- [36,75, 76,55, 72,146] # P4/16
- [142,110, 192,243, 459,401] # P5/32


data:
dataset_name: bdd100k_Vehicle
train_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/train.txt # 实际训练数据路径
val_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/val.txt
test_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/test.txt
nc: 2 # 分类数
# class names
names: ['Motor vehicle', 'Non-motor vehicle'] # 每一类的名字

optimizer:
lr_init: 0.01 # initial learning rate

+ 21
- 0
examples/car_detection/.ipynb_checkpoints/caryolov8l-checkpoint.yaml View File

@@ -0,0 +1,21 @@
__BASE__: [
'../../configs/yolov8/yolov8l.yaml',
]

per_batch_size: 8 # 单卡batchsize,总的batchsize=per_batch_size * device_num
img_size: 640 # image sizes
weight: /home/ma-user/work/pretrainmodel/yolov8l.ckpt
strict_load: False # 是否按严格加载ckpt内参数,默认True,若设成False,当分类数不一致,丢掉最后一层分类器的weight
log_interval: 10 # 每log_interval次迭代打印一次loss结果

data:
dataset_name: bdd100k
train_set: /home/ma-user/work/dataset/bdd100k_Vehicle/train.txt # 实际训练数据路径
val_set: /home/ma-user/work/dataset/bdd100k_Vehicle/val.txt
test_set: /home/ma-user/work/dataset/bdd100k_Vehicle/test.txt
nc: 2 # 分类数
# class names
names: ['Motor vehicle', 'Non-motor vehicle'] # 每一类的名字

optimizer:
lr_init: 0.001 # initial learning rate

+ 13
- 0
examples/car_detection/.ipynb_checkpoints/yolov8-l-car-checkpoint.yaml View File

@@ -0,0 +1,13 @@
__BASE__: [
'../bdd100k.yaml',
'./hyp.scratch.high.yaml',
'./yolov8-base.yaml'
]

network:
depth_multiple: 1.00 # scales module repeats
width_multiple: 1.00 # scales convolution channels
max_channels: 512

img_size: 640 # 2720 during validation
per_batch_size: 2

+ 33
- 0
examples/car_detection/Reclass_voc.py View File

@@ -0,0 +1,33 @@
import os
import xml.etree.ElementTree as ET
#程序功能:批量修改VOC数据集中xml标签文件的标签名称
def changelabelname(inputpath):
listdir = os.listdir(inputpath)
for file in listdir:
if file.endswith('xml'):
file = os.path.join(inputpath,file)
tree = ET.parse(file)
root = tree.getroot()
for object1 in root.findall('object'):
for sku in object1.findall('name'): #查找需要修改的名称
if (sku.text == 'car' or sku.text == 'bus' or sku.text == 'suv' or sku.text == 'policecar' or sku.text == 'ambulance'
or sku.text == 'truck' or sku.text == 'van' or sku.text == 'minibus' or sku.text == 'army vehicle' or sku.text == 'taxi'
or sku.text == 'garbagevan' or sku.text == 'pickup' or sku.text == 'minivan'): #‘preName’为修改前的名称
sku.text = 'Motor vehicle' #‘TESTNAME’为修改后的名称
tree.write(file,encoding='utf-8') #写进原始的xml文件并避免原始xml中文字符乱码
elif (sku.text == 'bicycle' or sku.text == 'auto rickshaw' or sku.text == 'three wheelers (CNG)' or sku.text == 'scooter'
or sku.text == 'rickshaw' or sku.text == 'motorbike' or sku.text == 'wheelbarrow'): #‘preName’为修改前的名称
sku.text = 'Non-motor vehicle' #‘TESTNAME’为修改后的名称
tree.write(file,encoding='utf-8') #写进原始的xml文件并避免原始xml中文字符乱码
elif (sku.text == 'Person'): #‘preName’为修改前的名称
sku.text = 'Non-motor vehicle' #‘TESTNAME’为修改后的名称
tree.write(file,encoding='utf-8') #写进原始的xml文件并避免原始xml中文字符乱码
else:
pass
else:
pass
if __name__ == '__main__':
inputpath = '/home/lxy/ubuntu/lxy/project/dataset/Vehicle-Dataset/Annotations' #此处替换为自己的路径
changelabelname(inputpath)

+ 29
- 0
examples/car_detection/Reclass_yolo.py View File

@@ -0,0 +1,29 @@

import os

# 定义旧类别编号和新类别编号
old_class_id = 7
new_class_id = 1
delete_class_id = 2

# 定义YOLO标签文件夹路径
label_folder_path = '/home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/labels/train'

# 获取YOLO标签文件夹中所有标签文件的路径
label_files = os.listdir(label_folder_path)
label_files = [os.path.join(label_folder_path, f) for f in label_files if f.endswith('.txt')]

# 循环读取每个标签文件,替换类别编号,并保存回原路径
for file_path in label_files:
with open(file_path, 'r') as f:
lines = f.readlines()

with open(file_path, 'w') as f:
for line in lines:
line = line.strip().split()
# if not (int(line[0]) == delete_class_id):
# f.write(' '.join(line) + '\n')
if int(line[0]) == old_class_id:
line[0] = str(new_class_id)
f.write(' '.join(line) + '\n')

+ 82
- 0
examples/car_detection/alltxt2json.py View File

@@ -0,0 +1,82 @@
import os
import json
import cv2
import random
import time
from PIL import Image
# 根据yolo格式的所有txt文件生成coco格式的一个标注json文件
coco_format_save_path='/home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/annotations/' #要生成的标准coco格式标签所在文件夹
yolo_format_classes_path='/home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/classes.txt' #类别文件,一行一个类
yolo_format_annotation_path='/home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/labels/val/' #yolo格式标签所在文件夹
img_pathDir='/home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/images/val/' #图片所在文件夹

with open(yolo_format_classes_path,'r') as fr: #打开并读取类别文件
lines1=fr.readlines()
# print(lines1)
categories=[] #存储类别的列表
for j,label in enumerate(lines1):
label=label.strip()
categories.append({'id':j,'name':label,'supercategory':'None'}) #将类别信息添加到categories中
print(categories)

write_json_context=dict() #写入.78json文件的大字典
write_json_context['info']= {'description': '', 'url': '', 'version': '', 'year': 2023, 'contributor': 'lxy', 'date_created': '2023-11-24'}
write_json_context['licenses']=[{'id':1,'name':None,'url':None}]
write_json_context['categories']=categories
write_json_context['images']=[]
write_json_context['annotations']=[]

#接下来的代码主要添加'images'和'annotations'的key值
imageFileList=os.listdir(img_pathDir) #遍历该文件夹下的所有文件,并将所有文件名添加到列表中
for i,imageFile in enumerate(imageFileList):
imagePath = os.path.join(img_pathDir,imageFile) #获取图片的绝对路径
image = Image.open(imagePath) #读取图片,然后获取图片的宽和高
W, H = image.size

img_context={} #使用一个字典存储该图片信息
#img_name=os.path.basename(imagePath) #返回path最后的文件名。如果path以/或\结尾,那么就会返回空值
img_context['file_name']=imageFile
img_context['height']=H
img_context['width']=W
img_context['date_captured']='2023-11-24'
img_context['id']=int(imageFile.split('.')[0]) #该图片的id
img_context['license']=1
img_context['color_url']=''
img_context['flickr_url']=''
write_json_context['images'].append(img_context) #将该图片信息添加到'image'列表中


txtFile=imageFile.split('.')[0]+'.txt' #获取该图片获取的txt文件
print(txtFile)
with open(os.path.join(yolo_format_annotation_path,txtFile),'r') as fr:
lines=fr.readlines() #读取txt文件的每一行数据,lines2是一个列表,包含了一个图片的所有标注信息
for j,line in enumerate(lines):

bbox_dict = {} #将每一个bounding box信息存储在该字典中
# line = line.strip().split()
# print(line.strip().split(' '))

class_id,x,y,w,h=line.strip().split(' ') #获取每一个标注框的详细信息
class_id,x, y, w, h = int(class_id), float(x), float(y), float(w), float(h) #将字符串类型转为可计算的int和float类型

xmin=(x-w/2)*W #坐标转换
ymin=(y-h/2)*H
xmax=(x+w/2)*W
ymax=(y+h/2)*H
w=w*W
h=h*H

bbox_dict['id']=i*10000+j #bounding box的坐标信息
bbox_dict['image_id']=int(imageFile.split('.')[0])
bbox_dict['category_id']=class_id #目标类别从0开始
bbox_dict['iscrowd']=0
height,width=abs(ymax-ymin),abs(xmax-xmin)
bbox_dict['area']=height*width
bbox_dict['bbox']=[xmin,ymin,w,h]
bbox_dict['segmentation']=[[xmin,ymin,xmax,ymin,xmax,ymax,xmin,ymax]]
write_json_context['annotations'].append(bbox_dict) #将每一个由字典存储的bounding box信息添加到'annotations'列表中

name = os.path.join(coco_format_save_path,"instances_val2017"+ '.json')
with open(name,'w') as fw: #将字典信息写入.json文件中
json.dump(write_json_context,fw,indent=2)


+ 171
- 0
examples/car_detection/anchor_kmeans++.py View File

@@ -0,0 +1,171 @@
from __future__ import division, print_function

import numpy as np
import random
import math


def iou(box, clusters):
x = np.minimum(clusters[:, 0], box[0])
y = np.minimum(clusters[:, 1], box[1])
if np.count_nonzero(x == 0) > 0 or np.count_nonzero(y == 0) > 0:
raise ValueError("Box has no area")

intersection = x * y
box_area = box[0] * box[1]
cluster_area = clusters[:, 0] * clusters[:, 1]

iou_ = np.true_divide(intersection, box_area + cluster_area - intersection + 1e-10)
# iou_ = intersection / (box_area + cluster_area - intersection + 1e-10)

return iou_

def iou_kpp(box, clusters):
x = np.minimum(clusters[0], box[0])
y = np.minimum(clusters[1], box[1])
if np.count_nonzero(x == 0) > 0 or np.count_nonzero(y == 0) > 0:
raise ValueError("Box has no area")

intersection = x * y
box_area = box[0] * box[1]
cluster_area = clusters[0] * clusters[1]

iou_ = np.true_divide(intersection, box_area + cluster_area - intersection + 1e-10)
# iou_ = intersection / (box_area + cluster_area - intersection + 1e-10)

return iou_


def avg_iou(boxes, clusters):
return np.mean([np.max(iou(boxes[i], clusters)) for i in range(boxes.shape[0])])


def get_closest_dist(point, centroids):
min_dist = math.inf # 初始设为无穷大
print(centroids)
for i, centroid in enumerate(centroids):
print(centroids)
dist = 1 - iou_kpp(point, centroid) # 点和当前每个中心点进行计算距离
if dist < min_dist:
min_dist = dist # 注意我K-means++博客中的这句“指该点离中心点这一数组中所有中心点距离中的最短距离”
return min_dist


def kpp_centers(data_set: list, k: int) -> list:
"""
从数据集中返回 k 个对象可作为质心
"""
cluster_centers = []
cluster_centers.append(random.choice(data_set))
d = [0 for _ in range(len(data_set))]
#print(d)
for _ in range(1, k):
total = 0.0
for i, point in enumerate(data_set):
d[i] = get_closest_dist(point, cluster_centers) # 与最近一个聚类中心的距离
total += d[i]
total *= random.random()
for i, di in enumerate(d): # 轮盘法选出下一个聚类中心;
total -= di
if total > 0:
continue
cluster_centers.append(data_set[i])
break
return cluster_centers



def kmeans(boxes, k, dist=np.median):
rows = boxes.shape[0]

distances = np.empty((rows, k))
last_clusters = np.zeros((rows,))

np.random.seed()

# the Forgy method will fail if the whole array contains the same rows
clusters = kpp_centers(boxes, k)
clusters = np.array(clusters)
#clusters = boxes[np.random.choice(rows, k, replace=False)] 这是K-means的,两个切换注释下就行了

while True:
for row in range(rows):
distances[row] = 1 - iou(boxes[row], clusters) # iou很大则距离很小
# 对每个标注框选择与其距离最接近的集群中心的标号作为所属类别的编号。
nearest_clusters = np.argmin(distances, axis=1) # axis=1表示沿着列的方向水平延伸

if (last_clusters == nearest_clusters).all():
break

for cluster in range(k):
clusters[cluster] = dist(boxes[nearest_clusters == cluster], axis=0) # 给每类算均值新中心点

last_clusters = nearest_clusters
print(last_clusters)

return clusters


def parse_anno(annotation_path, target_size=None):
anno = open(annotation_path, 'r')
result = []
# 对每一个标记图片
for line in anno:
s = line.strip().split(' ')
img_w = float(s[2])
img_h = float(s[3])
s = s[4:]
box_cnt = len(s) // 5
# 分别处理每一个标记框的信息,并提取标记框的高度和宽度,存入result 列表
for i in range(box_cnt):
x_min, y_min, x_max, y_max = float(s[i*5+1]), float(s[i*5+2]), float(s[i*5+3]), float(s[i*5+4])
width = x_max - x_min
height = y_max - y_min
# assert width > 0
# assert height > 0
# use letterbox resize, i.e. keep the original aspect ratio
# get k-means anchors on the resized target image size
if target_size is not None:
resize_ratio = min(target_size[0] / img_w, target_size[1] / img_h)
width *= resize_ratio
height *= resize_ratio
result.append([width, height])
# get k-means anchors on the original image size
else:
result.append([width, height])
result = np.asarray(result)
return result


def get_kmeans(anno, cluster_num=9):
# 使用kmeans算法计算需要的anchors
anchors = kmeans(anno, cluster_num)

ave_iou = avg_iou(anno, anchors)
# 格式化为int类型
anchors = anchors.astype('int').tolist()
# 按照面积大小排序,
anchors = sorted(anchors, key=lambda x: x[0] * x[1])

return anchors, ave_iou


if __name__ == '__main__':

target_size = [640, 640]
annotation_path = "/home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/labels/train/000001.txt"
anno_result = parse_anno(annotation_path, target_size=target_size)
anchors, ave_iou = get_kmeans(anno_result, 9)

# 格式化输出anchors数据
anchor_string = ''
for anchor in anchors:
anchor_string += '{},{}, '.format(anchor[0], anchor[1])
anchor_string = anchor_string[:-2]

print('anchors are:')
print(anchor_string)
print('the average iou is:')
print(ave_iou)



+ 170
- 44
examples/car_detection/anchor_kmeans.py View File

@@ -1,44 +1,170 @@
import glob
import xml.etree.ElementTree as ET

import numpy as np

from kmeans import kmeans, avg_iou

ANNOTATIONS_PATH = r"C:\Users\Aministration\Desktop\annotations_test"
CLUSTERS = 9

def load_dataset(path):
dataset = []
for xml_file in glob.glob("{}/*xml".format(path)):
# print(xml_file)
tree = ET.parse(xml_file)

height = int(tree.findtext("./size/height"))
width = int(tree.findtext("./size/width"))

for obj in tree.iter("object"):
xmin = int(float(obj.findtext("bndbox/xmin"))) / width
ymin = int(float(obj.findtext("bndbox/ymin"))) / height
xmax = int(float(obj.findtext("bndbox/xmax"))) / width
ymax = int(float(obj.findtext("bndbox/ymax"))) / height

dataset.append([xmax - xmin, ymax - ymin])

return np.array(dataset)


data = load_dataset(ANNOTATIONS_PATH)
print('data shape is {}'.format(data.shape))
out = kmeans(data, k=CLUSTERS)

yolov3clusters = [[10,13],[16,30],[33,23],[30,61],[62,45],[59,119],[116,90],[156,198],[373,326]]
yolov3out= np.array(yolov3clusters)/416.0

print("self data Accuracy: {:.2f}%".format(avg_iou(data, out) * 100))
print("yolov3 Accuracy: {:.2f}%".format(avg_iou(data, yolov3out) * 100))
print("Boxes:\n {}-{}".format(out[:, 0]*416, out[:, 1]*416))
# print("Boxes:\n {}".format(out))

ratios = np.around(out[:, 0] / out[:, 1], decimals=2).tolist()
print("Ratios:\n {}".format(sorted(ratios)))
import glob
import xml.etree.ElementTree as ET
import numpy as np
import random
import math
# from kmeans import kmeans, avg_iou
# from sklearn.cluster import KMeans
ANNOTATIONS_PATH = "/home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/train_xml"
CLUSTERS = 7
def load_dataset(path):
dataset = []
for xml_file in glob.glob("{}/*xml".format(path)):
# print(xml_file)
tree = ET.parse(xml_file)
height = int(tree.findtext("./size/height"))
width = int(tree.findtext("./size/width"))
for obj in tree.iter("object"):
xmin = int(float(obj.findtext("bndbox/xmin"))) / width
ymin = int(float(obj.findtext("bndbox/ymin"))) / height
xmax = int(float(obj.findtext("bndbox/xmax"))) / width
ymax = int(float(obj.findtext("bndbox/ymax"))) / height
dataset.append([xmax - xmin, ymax - ymin])
return np.array(dataset)
import numpy as np
def wh_iou(wh1, wh2):
# Returns the nxm IoU matrix. wh1 is nx2, wh2 is mx2
wh1 = wh1[:, None] # [N,1,2]
wh2 = wh2[None] # [1,M,2]
inter = np.minimum(wh1, wh2).prod(2) # [N,M]
return inter / (wh1.prod(2) + wh2.prod(2) - inter) # iou = inter / (area1 + area2 - inter)
def k_means(boxes, k, dist=np.median):
"""
yolo k-means methods
refer: https://github.com/qqwweee/keras-yolo3/blob/master/kmeans.py
Args:
boxes: 需要聚类的bboxes
k: 簇数(聚成几类)
dist: 更新簇坐标的方法(默认使用中位数,比均值效果略好)
"""
box_number = boxes.shape[0]
last_nearest = np.zeros((box_number,))
# 在所有的bboxes中随机挑选k个作为簇的中心。
clusters = boxes[np.random.choice(box_number, k, replace=False)]
while True:
# 计算每个bboxes离每个簇的距离 1-IOU(bboxes, anchors)
distances = 1 - wh_iou(boxes, clusters)
# 计算每个bboxes距离最近的簇中心
current_nearest = np.argmin(distances, axis=1)
# 每个簇中元素不在发生变化说明以及聚类完毕
if (last_nearest == current_nearest).all():
break # clusters won't change
for cluster in range(k):
# 根据每个簇中的bboxes重新计算簇中心
clusters[cluster] = dist(boxes[current_nearest == cluster], axis=0)
last_nearest = current_nearest
return clusters
def iou(box, clusters):
"""
Calculates the Intersection over Union (IoU) between a box and k clusters.
param:
box: tuple or array, shifted to the origin (i. e. width and height)
clusters: numpy array of shape (k, 2) where k is the number of clusters
return:
numpy array of shape (k, 0) where k is the number of clusters
"""
x = np.minimum(clusters[:, 0], box[0])
y = np.minimum(clusters[:, 1], box[1])
if np.count_nonzero(x == 0) > 0 or np.count_nonzero(y == 0) > 0:
raise ValueError("Box has no area")
intersection = x * y # 相交面积
box_area = box[0] * box[1]
cluster_area = clusters[:, 0] * clusters[:, 1]
iou_ = np.true_divide(intersection, box_area + cluster_area - intersection + 1e-10) # 交并比 = 相交面积 / 两个框面积相加并减去相交面积
# iou_ = intersection / (box_area + cluster_area - intersection + 1e-10)
return iou_
def avg_iou(boxes, clusters):
"""
Calculates the average Intersection over Union (IoU) between a numpy array of boxes and k clusters.
param:
boxes: numpy array of shape (r, 2), where r is the number of rows
clusters: numpy array of shape (k, 2) where k is the number of clusters
return:
average IoU as a single float
"""
return np.mean([np.max(iou(boxes[i], clusters)) for i in range(boxes.shape[0])]) # 查看训练集框和anchor box的交并比
def kmeans(boxes, k, dist=np.mean):
"""
Calculates k-means clustering with the Intersection over Union (IoU) metric.
param:
boxes: numpy array of shape (r, 2), where r is the number of rows
k: number of clusters
dist: distance function
return:
numpy array of shape (k, 2)
"""
rows = boxes.shape[0] # boxes就是result,看看有多少个框
distances = np.empty((rows, k)) # 存放每个点与中心点的距离
last_clusters = np.zeros((rows,)) # 存放上一次的距离,用于结束循环
np.random.seed()
# the Forgy method will fail if the whole array contains the same rows
clusters = boxes[np.random.choice(rows, k, replace=False)] # 随机选取k个中心点,默认是9个
while True:
for row in range(rows):
distances[row] = 1 - iou(boxes[row], clusters) # 上述的距离公式,用1减去每个框和中心点的交并比,得到每个框到中心点的距离
nearest_clusters = np.argmin(distances, axis=1) # 得到每个框距离哪个中心点距离最小
if (last_clusters == nearest_clusters).all(): # 如果上一次距离和这次距离一样,跳出循环,结束(距离一样,聚类结果肯定一样)
break
for cluster in range(k):
clusters[cluster] = dist(boxes[nearest_clusters == cluster], axis=0) # 更新中心点
'''
利用与第几个点距离最小的框求均值得到聚类结果,如现在求第一个anchor box,
那么就取出nearest_clusters == 0的box,因为这些box是与第一个中心点距离最近的(nearest_clusters)=0
然后利用均值,求出新的中心点
'''
last_clusters = nearest_clusters
return clusters
data = load_dataset(ANNOTATIONS_PATH)
print('data shape is {}'.format(data.shape))
out = k_means(data, k=CLUSTERS)
yolov5clusters = [[10,13],[16,30],[33,23],[30,61],[62,45],[59,119],[116,90],[156,198],[373,326]]
yolov5out= np.array(yolov5clusters)/640.0
# print("self data Accuracy: {:.2f}%".format(avg_iou(data, out) * 100))
# print("yolov5 Accuracy: {:.2f}%".format(avg_iou(data, yolov5out) * 100))
print("Boxes:\n {}-{}".format(out[:, 0]*640, out[:, 1]*640))
# print("Boxes:\n {}".format(out))
ratios = np.around(out[:, 0] / out[:, 1], decimals=2).tolist()
print("Ratios:\n {}".format(sorted(ratios)))
# [41,54],[10,17],[16,22],[9,10],[6,8],[148,209],[6,13],[23,35],[73,95]
# [6,8],[6,13],[9,10],[10,17],[16,22],[23,35],[41,54],[73,95],[148,209]
[6,10],[10,14],[14,23],[23,33],[40,53],[72,93],[147,207]

+ 10
- 0
examples/car_detection/bdd100k.yaml View File

@@ -0,0 +1,10 @@
data:
dataset_name: bdd100k
train_set: /home/ma-user/work/dataset/bdd100k_Vehicle/train.txt
val_set: /home/ma-user/work/dataset/bdd100k_Vehicle/val.txt
test_set: /home/ma-user/work/dataset/bdd100k_Vehicle/test.txt
nc: 2
# class names
names: [ 'Motor vehicle', 'Non-motor vehicle']
train_transforms: []
test_transforms: []

+ 2
- 2
examples/car_detection/bdd100k_train_val.py View File

@@ -24,7 +24,7 @@ def main(path, dstpath):
if __name__ == '__main__':
#srcpath = 'bdd100k/images/val/'
#dstpath = 'bdd100k/images/val.txt'
srcpath = '/home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/images/test/' # 训练集图片所在的路径(自己设置)
dstpath = '/home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/test.txt' # 新生成的txt所在路径(自己设置)
srcpath = '/home/ma-user/work/dataset/bdd100k_Vehicle/images/val/' # 训练集图片所在的路径(自己设置)
dstpath = '/home/ma-user/work/dataset/bdd100k_Vehicle/val.txt' # 新生成的txt所在路径(自己设置)
main(srcpath, dstpath)


+ 22
- 0
examples/car_detection/caryolov5m_a4000.yaml View File

@@ -0,0 +1,22 @@
__BASE__: [
'../../configs/yolov5/yolov5m.yaml',
]

per_batch_size: 16 # 单卡batchsize,总的batchsize=per_batch_size * device_num
img_size: 640 # image sizes
weight: /home/lxy/ubuntu/lxy/project/pretrainmodel/yolov5m.ckpt
strict_load: False # 是否按严格加载ckpt内参数,默认True,若设成False,当分类数不一致,丢掉最后一层分类器的weight
log_interval: 10 # 每log_interval次迭代打印一次loss结果
ms_amp_level: O0

data:
dataset_name: bdd100k_Vehicle
train_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/train.txt # 实际训练数据路径
val_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/val.txt
test_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/test.txt
nc: 2 # 分类数
# class names
names: ['Motor vehicle', 'Non-motor vehicle'] # 每一类的名字

optimizer:
lr_init: 0.01 # initial learning rate

+ 22
- 0
examples/car_detection/caryolov5m_a4000_test.yaml View File

@@ -0,0 +1,22 @@
__BASE__: [
'../../configs/yolov5/yolov5m.yaml',
]

per_batch_size: 16 # 单卡batchsize,总的batchsize=per_batch_size * device_num
img_size: 640 # image sizes
weight: /home/lxy/ubuntu/lxy/project/pretrainmodel/yolov5m.ckpt
strict_load: False # 是否按严格加载ckpt内参数,默认True,若设成False,当分类数不一致,丢掉最后一层分类器的weight
log_interval: 10 # 每log_interval次迭代打印一次loss结果
ms_amp_level: O2

data:
dataset_name: bdd100k_Vehicle
train_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/train.txt # 实际训练数据路径
val_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/val.txt
test_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/test.txt
nc: 2 # 分类数
# class names
names: ['Motor vehicle', 'Non-motor vehicle'] # 每一类的名字

optimizer:
lr_init: 0.01 # initial learning rate

+ 0
- 21
examples/car_detection/caryolov5s.yaml View File

@@ -1,21 +0,0 @@
__BASE__: [
'../../configs/yolov5/yolov5s.yaml',
]

per_batch_size: 16 # 单卡batchsize,总的batchsize=per_batch_size * device_num
img_size: 640 # image sizes
weight: /pretrainmodel/yolov5s_300e_mAP376-860bcf3b.ckpt
strict_load: False # 是否按严格加载ckpt内参数,默认True,若设成False,当分类数不一致,丢掉最后一层分类器的weight
log_interval: 10 # 每log_interval次迭代打印一次loss结果

data:
dataset_name: bdd100k
train_set: /multi_data_url /images_labels/train.txt # 实际训练数据路径
val_set: /multi_data_url /images_labels/val.txt
test_set: /multi_data_url /images_labels/test.txt
nc: 10 # 分类数
# class names
names: ['car', 'bus', 'person', 'bike', 'truck', 'motor', 'train', 'rider', 'traffic sign', 'traffic light'] # 每一类的名字

optimizer:
lr_init: 0.001 # initial learning rate

+ 0
- 21
examples/car_detection/caryolov5s_a4000.yaml View File

@@ -1,21 +0,0 @@
__BASE__: [
'../../configs/yolov5/yolov5s.yaml',
]

per_batch_size: 16 # 单卡batchsize,总的batchsize=per_batch_size * device_num
img_size: 640 # image sizes
weight: /home/lxy/ubuntu/lxy/project/pretrainmodel/yolov5s_300e_mAP376-860bcf3b.ckpt
strict_load: False # 是否按严格加载ckpt内参数,默认True,若设成False,当分类数不一致,丢掉最后一层分类器的weight
log_interval: 10 # 每log_interval次迭代打印一次loss结果

data:
dataset_name: bdd100k
train_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/train.txt # 实际训练数据路径
val_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/val.txt
test_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/test.txt
nc: 10 # 分类数
# class names
names: ['car', 'bus', 'person', 'bike', 'truck', 'motor', 'train', 'rider', 'traffic sign', 'traffic light'] # 每一类的名字

optimizer:
lr_init: 0.0001 # initial learning rate

+ 21
- 0
examples/car_detection/caryolov7l.yaml View File

@@ -0,0 +1,21 @@
__BASE__: [
'../../configs/yolov7/yolov7.yaml',
]

per_batch_size: 8 # 单卡batchsize,总的batchsize=per_batch_size * device_num
img_size: 640 # image sizes
weight: /home/ma-user/work/pretrainmodel/yolov7l.ckpt
strict_load: False # 是否按严格加载ckpt内参数,默认True,若设成False,当分类数不一致,丢掉最后一层分类器的weight
log_interval: 10 # 每log_interval次迭代打印一次loss结果

data:
dataset_name: bdd100k_Vehicle
train_set: /home/ma-user/work/dataset/bdd100k_Vehicle/train.txt # 实际训练数据路径
val_set: /home/ma-user/work/dataset/bdd100k_Vehicle/val.txt
test_set: /home/ma-user/work/dataset/bdd100k_Vehicle/test.txt
nc: 2 # 分类数
# class names
names: ['Motor vehicle', 'Non-motor vehicle'] # 每一类的名字

optimizer:
lr_init: 0.001 # initial learning rate

+ 29
- 0
examples/car_detection/caryolov7l_a4000.yaml View File

@@ -0,0 +1,29 @@
__BASE__: [
'../../configs/yolov7/yolov7.yaml',
]

per_batch_size: 8 # 单卡batchsize,总的batchsize=per_batch_size * device_num
img_size: 640 # image sizes
weight: /home/lxy/ubuntu/lxy/project/pretrainmodel/yolov7l.ckpt
strict_load: False # 是否按严格加载ckpt内参数,默认True,若设成False,当分类数不一致,丢掉最后一层分类器的weight
log_interval: 10 # 每log_interval次迭代打印一次loss结果

network:
# anchors
anchors:
- [12,16, 19,36, 40,28] # P3/8
- [36,75, 76,55, 72,146] # P4/16
- [142,110, 192,243, 459,401] # P5/32


data:
dataset_name: bdd100k_Vehicle
train_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/train.txt # 实际训练数据路径
val_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/val.txt
test_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/test.txt
nc: 2 # 分类数
# class names
names: ['Motor vehicle', 'Non-motor vehicle'] # 每一类的名字

optimizer:
lr_init: 0.01 # initial learning rate

+ 14
- 7
examples/car_detection/caryolov7s_a4000.yaml View File

@@ -4,18 +4,25 @@ __BASE__: [

per_batch_size: 16 # 单卡batchsize,总的batchsize=per_batch_size * device_num
img_size: 640 # image sizes
weight: /home/lxy/ubuntu/lxy/project/pretrainmodel/EMA_caryolov7s_a4000-55_4375
weight: /home/lxy/ubuntu/lxy/project/pretrainmodel/yolov7-tiny_pretrain.ckpt
strict_load: False # 是否按严格加载ckpt内参数,默认True,若设成False,当分类数不一致,丢掉最后一层分类器的weight
log_interval: 10 # 每log_interval次迭代打印一次loss结果

# network:
# # anchors
# anchors:
# - [6,10, 10,14, 14,23] # P3/8
# - [23,33, 40,53, 72,93] # P4/16
# - [147,207, 156,198, 373,326] # P5/32

data:
dataset_name: bdd100k
train_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/train.txt # 实际训练数据路径
val_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/val.txt
test_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/test.txt
nc: 10 # 分类数
dataset_name: bdd100k_Vehicle
train_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/train.txt # 实际训练数据路径
val_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/val.txt
test_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/test.txt
nc: 2 # 分类数
# class names
names: ['car', 'bus', 'person', 'bike', 'truck', 'motor', 'train', 'rider', 'traffic sign', 'traffic light'] # 每一类的名字
names: ['Motor vehicle', 'Non-motor vehicle'] # 每一类的名字

optimizer:
lr_init: 0.001 # initial learning rate

+ 21
- 0
examples/car_detection/caryolov8l.yaml View File

@@ -0,0 +1,21 @@
__BASE__: [
'../../configs/yolov8/yolov8l.yaml',
]

per_batch_size: 8 # 单卡batchsize,总的batchsize=per_batch_size * device_num
img_size: 640 # image sizes
weight: /home/ma-user/work/pretrainmodel/yolov8l.ckpt
strict_load: False # 是否按严格加载ckpt内参数,默认True,若设成False,当分类数不一致,丢掉最后一层分类器的weight
log_interval: 10 # 每log_interval次迭代打印一次loss结果

data:
dataset_name: bdd100k
train_set: /home/ma-user/work/dataset/bdd100k_Vehicle/train.txt # 实际训练数据路径
val_set: /home/ma-user/work/dataset/bdd100k_Vehicle/val.txt
test_set: /home/ma-user/work/dataset/bdd100k_Vehicle/test.txt
nc: 2 # 分类数
# class names
names: ['Motor vehicle', 'Non-motor vehicle'] # 每一类的名字

optimizer:
lr_init: 0.001 # initial learning rate

+ 21
- 0
examples/car_detection/caryolov8s_a4000.yaml View File

@@ -0,0 +1,21 @@
__BASE__: [
'../../configs/yolov8/yolov8s.yaml',
]

per_batch_size: 16 # 单卡batchsize,总的batchsize=per_batch_size * device_num
img_size: 640 # image sizes
weight: /home/lxy/ubuntu/lxy/project/pretrainmodel/yolov8s_pretrain.ckpt
strict_load: False # 是否按严格加载ckpt内参数,默认True,若设成False,当分类数不一致,丢掉最后一层分类器的weight
log_interval: 10 # 每log_interval次迭代打印一次loss结果

data:
dataset_name: bdd100k
train_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/train.txt # 实际训练数据路径
val_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/val.txt
test_set: /home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/test.txt
nc: 2 # 分类数
# class names
names: ['Motor vehicle', 'Non-motor vehicle'] # 每一类的名字

optimizer:
lr_init: 0.001 # initial learning rate

+ 35
- 0
examples/car_detection/class_number_voc.py View File

@@ -0,0 +1,35 @@
import os
import random
import xml.etree.ElementTree as ET
# 统计voc格式的标注中各个类别的数量
clses = []
cls_num = {}
xml_path = r'/home/lxy/ubuntu/lxy/project/dataset/Vehicle-Dataset/Annotations'
temp_xml = os.listdir(xml_path)
total_xml = []
for xml in temp_xml:
if xml.endswith(".xml"):
total_xml.append(xml)
for name in total_xml:
try:
in_file = open(os.path.join(xml_path, name), encoding='utf-8')
tree = ET.parse(in_file)
root = tree.getroot()
except:
in_file = open(os.path.join(xml_path, name))
tree = ET.parse(in_file)
root = tree.getroot()
for obj in root.iter('object'):
cls = obj.find('name').text
clses.append(cls)
if cls not in cls_num.keys():
cls_num[cls] = 1
else:
cls_num[cls] += 1
result = list(set(clses))
print(cls_num)

+ 19
- 0
examples/car_detection/class_number_yolo.py View File

@@ -0,0 +1,19 @@
import os
txt_path = r'/home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/labels/train' # txt文件所在路径
class_num = 2 # 样本类别数
class_list = [i for i in range(class_num)]
class_num_list = [0 for i in range(class_num)]
labels_list = os.listdir(txt_path)
for i in labels_list:
file_path = os.path.join(txt_path, i)
file = open(file_path, 'r') # 打开文件
file_data = file.readlines() # 读取所有行
for every_row in file_data:
class_val = every_row.split(' ')[0]
class_ind = class_list.index(int(class_val))
class_num_list[class_ind] += 1
file.close()
# 输出每一类的数量以及总数
print(class_num_list)
print('total:', sum(class_num_list))

+ 0
- 4739523
examples/car_detection/instances_val2017.json
File diff suppressed because it is too large
View File


BIN
examples/car_detection/predict/007056.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 247 KiB

BIN
examples/car_detection/predict/data/cabc30fc-e7726578.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 50 KiB

BIN
examples/car_detection/predict/data/cabc30fc-eb673c5a.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 55 KiB

BIN
examples/car_detection/predict/data/cabc30fc-fd79926f.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 53 KiB

BIN
examples/car_detection/predict/data/cabc9045-1b8282ba.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 38 KiB

BIN
examples/car_detection/predict/data/cabc9045-581f64de.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 37 KiB

BIN
examples/car_detection/predict/data/cabc9045-5a50690f.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 41 KiB

BIN
examples/car_detection/predict/data/cabc9045-b3349548.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 36 KiB

BIN
examples/car_detection/predict/data/cabc9045-c6dc9529.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 48 KiB

BIN
examples/car_detection/predict/data/cabc9045-cd422b81.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 35 KiB

BIN
examples/car_detection/predict/data/cabc9045-d91ecb66.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 44 KiB

+ 4
- 4
examples/car_detection/rename_trainval.py View File

@@ -1,8 +1,8 @@
import os

# 定义两个文件夹路径
images_folder = "D:\\1深度学习\\数据集\\BDD100K\\bdd100k_10class_yolo\\bdd100k\\images_lables\\images\\train\\"
labels_folder = "D:\\1深度学习\\数据集\\BDD100K\\bdd100k_10class_yolo\\bdd100k\\images_lables\\labels\\train\\"
images_folder = "/home/lxy/ubuntu/lxy/project/dataset/Vehicle-Dataset/JPEGImages"
labels_folder = "/home/lxy/ubuntu/lxy/project/dataset/Vehicle-Dataset/Annotations"

# 获取文件夹中的文件列表
image_files = sorted(os.listdir(images_folder))
@@ -13,11 +13,11 @@ if len(image_files) != len(label_files):
else:
for idx, (image_file, label_file) in enumerate(zip(image_files, label_files)):
# 构建新文件名
new_filename = f"{idx + 1:06}" # 创建新的文件名,从 '000001' 开始编号
new_filename = f"{idx + 70001:06}" # 创建新的文件名,从 '070001' 开始编号

# 构建新的文件路径
new_image_path = os.path.join(images_folder, new_filename + ".jpg")
new_label_path = os.path.join(labels_folder, new_filename + ".txt")
new_label_path = os.path.join(labels_folder, new_filename + ".xml")

# 原始文件路径
old_image_path = os.path.join(images_folder, image_file)


+ 75
- 0
examples/car_detection/txt2xml.py View File

@@ -0,0 +1,75 @@
# -*- coding: utf-8 -*-
import os,shutil
import cv2
from lxml.etree import Element, SubElement, tostring
def txt_xml(img_path,img_name,txt_path,img_txt,xml_path,img_xml):
#读取txt的信息
clas=[] #存放每一行txt数据
img=cv2.imread(os.path.join(img_path,img_name))
imh, imw = img.shape[0:2] #图片的高和宽
txt_img=os.path.join(txt_path,img_txt)
with open(txt_img,"r") as f:
for line in f.readlines():
line = line.strip('\n')
list = line.split(" ")
# print(list)
clas.append(list)
node_root = Element('annotation')
node_folder = SubElement(node_root, 'folder')
node_folder.text = '1'
node_filename = SubElement(node_root, 'filename')
#图像名称
node_filename.text = img_name
node_size = SubElement(node_root, 'size')
node_width = SubElement(node_size, 'width')
node_width.text = str(imw)
node_height = SubElement(node_size, 'height')
node_height.text = str(imh)
node_depth = SubElement(node_size, 'depth')
node_depth.text = '3'
for i in range(len(clas)):
node_object = SubElement(node_root, 'object')
node_name = SubElement(node_object, 'name')
node_name.text = str(clas[i][0])
node_pose=SubElement(node_object, 'pose')
node_pose.text="Unspecified"
node_truncated=SubElement(node_object, 'truncated')
node_truncated.text="truncated"
node_difficult = SubElement(node_object, 'difficult')
node_difficult.text = '0'
node_bndbox = SubElement(node_object, 'bndbox')
node_xmin = SubElement(node_bndbox, 'xmin')
node_xmin.text = str(int((2*float(clas[i][1])*imw - float(clas[i][3])*imw)/2))
node_ymin = SubElement(node_bndbox, 'ymin')
node_ymin.text = str(int((2*float(clas[i][2])*imh - float(clas[i][4])*imh)/2))
node_xmax = SubElement(node_bndbox, 'xmax')
node_xmax.text = str(int((2*float(clas[i][1])*imw + float(clas[i][3])*imw)/2))
node_ymax = SubElement(node_bndbox, 'ymax')
node_ymax.text = str(int((2*float(clas[i][2])*imh + float(clas[i][4])*imh)/2))
xml = tostring(node_root, pretty_print=True) # 格式化显示,该换行的换行
img_newxml = os.path.join(xml_path, img_xml)
file_object = open(img_newxml, 'wb')
file_object.write(xml)
file_object.close()

if __name__ == "__main__":
#图像文件夹所在位置
img_path = "/home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/images/train"
#标注文件夹所在位置
txt_path="/home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/labels/train"
#txt转化成xml格式后存放的文件夹
xml_path="/home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/train_xml"
i=0
for img_name in os.listdir(img_path):
i=i+1
print(str(i)+ "-" + img_name)
img_xml=img_name.split(".")[0]+".xml"
img_txt=img_name.split(".")[0]+".txt"
txt_xml(img_path, img_name, txt_path, img_txt,xml_path, img_xml)




+ 13
- 13
examples/car_detection/txt_gen.py View File

@@ -1,13 +1,13 @@
import os
# 程序入口
if __name__ == "__main__":
filePath = '/home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/images/val'
dstpath = '/home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/images/val.txt' # 新生成的txt所在路径(自己设置)
fileList = os.listdir(filePath) # os.listdir方法返回一个列表对象
file = open(dstpath, 'w') # 创建文件,权限为写入
for i in fileList:
rowInfo = i + '\n'
print(rowInfo)
file.write(rowInfo)
#argon2:$argon2id$v=19$m=10240,t=10,p=8$SX/VXus0tjF3qoZT5WGA3A$TfU1T9Dhqy6napO9t/dwmxh2jTDVp7BpvdujIg18cQw
import os
# 程序入口
if __name__ == "__main__":
folder_path = '/home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/images/test'
dstpath = '/home/lxy/ubuntu/lxy/project/dataset/bdd100k_Vehicle/test.txt' # 新生成的txt所在路径(自己设置)
fileList = os.listdir(folder_path) # os.listdir方法返回一个列表对象
file = open(dstpath, 'w') # 创建文件,权限为写入
for file_name in fileList:
file_path = os.path.join(folder_path, file_name) + '\n'
print(file_path)
file.write(file_path)
#argon2:$argon2id$v=19$m=10240,t=10,p=8$SX/VXus0tjF3qoZT5WGA3A$TfU1T9Dhqy6napO9t/dwmxh2jTDVp7BpvdujIg18cQw

BIN
examples/car_detection/val2/000001.jpg View File

Before After
Width: 800  |  Height: 1000  |  Size: 39 KiB

BIN
examples/car_detection/val2/000002.jpg View File

Before After
Width: 800  |  Height: 1000  |  Size: 25 KiB

BIN
examples/car_detection/val2/000003.jpg View File

Before After
Width: 800  |  Height: 1000  |  Size: 26 KiB

BIN
examples/car_detection/val2/000004.jpg View File

Before After
Width: 800  |  Height: 1000  |  Size: 42 KiB

BIN
examples/car_detection/val2/000005.jpg View File

Before After
Width: 800  |  Height: 1000  |  Size: 40 KiB

BIN
examples/car_detection/val2/000006.jpg View File

Before After
Width: 800  |  Height: 1000  |  Size: 26 KiB

BIN
examples/car_detection/visual/val/000065.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 128 KiB

BIN
examples/car_detection/visual/val/000604.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 147 KiB

BIN
examples/car_detection/visual/val/000742.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 122 KiB

BIN
examples/car_detection/visual/val/001005.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 124 KiB

BIN
examples/car_detection/visual/val/001343.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 168 KiB

BIN
examples/car_detection/visual/val/002028.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 72 KiB

BIN
examples/car_detection/visual/val/002452.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 198 KiB

BIN
examples/car_detection/visual/val/003424.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 185 KiB

BIN
examples/car_detection/visual/val/003780.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 138 KiB

BIN
examples/car_detection/visual/val/003984.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 194 KiB

BIN
examples/car_detection/visual/val/004436.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 99 KiB

BIN
examples/car_detection/visual/val/005006.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 103 KiB

BIN
examples/car_detection/visual/val/005382.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 62 KiB

BIN
examples/car_detection/visual/val/005758.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 99 KiB

BIN
examples/car_detection/visual/val/006101.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 92 KiB

BIN
examples/car_detection/visual/val/006291.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 103 KiB

BIN
examples/car_detection/visual/val/007056.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 241 KiB

BIN
examples/car_detection/visual/val/007365.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 200 KiB

BIN
examples/car_detection/visual/val/008440.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 92 KiB

BIN
examples/car_detection/visual/val/008584.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 244 KiB

BIN
examples/car_detection/visual/val/008748.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 167 KiB

BIN
examples/car_detection/visual/val/008980.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 94 KiB

BIN
examples/car_detection/visual/val/009529.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 186 KiB

BIN
examples/car_detection/visual/val/009566.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 112 KiB

BIN
examples/car_detection/visual/val/009813.jpg View File

Before After
Width: 1280  |  Height: 720  |  Size: 87 KiB

+ 67
- 0
examples/car_detection/visual_json.py View File

@@ -0,0 +1,67 @@
import json
import os, cv2

# train_json = r'C:\Users\mage\Desktop\detectron2-maskrcnn\datasets\coco\annotations\instances_test2014.json'
# train_path = r'C:\Users\mage\Desktop\detectron2-maskrcnn\datasets\coco\test2014'
# visual_output = r'C:\Users\mage\Desktop\SolarCOCO\visual\test'





# 可视化coco格式json标注中的box到图片上
import json
import shutil
import cv2

def select(json_path, outpath, image_path, names):
json_file = open(json_path)
infos = json.load(json_file)
images = infos["images"]
annos = infos["annotations"]
assert len(images) == len(images)
for i in range(len(images)):
im_id = images[i]["id"]
im_path = image_path + "/" + images[i]["file_name"]
img = cv2.imread(im_path)
for j in range(len(annos)):
if annos[j]["image_id"] == im_id:
x, y, w, h = annos[j]["bbox"]
x, y, w, h = int(x), int(y), int(w), int(h)
x2, y2 = x + w, y + h
category_id = annos[j]["category_id"]
object_name = names[category_id]
img = cv2.rectangle(img, (x, y), (x2, y2), (255, 0, 0), thickness=2)
font=cv2.FONT_HERSHEY_SIMPLEX # 定义字体
# 图片 添加的文字 位置 字体 字体大小 字体颜色 字体粗细
print(object_name)
imgzi = cv2.putText(img, '{}'.format(object_name), (x, y-5), font, 0.5, (0, 255, 255), 1)

img_name = outpath + "/" + images[i]["file_name"]
cv2.imwrite(img_name, img)
# continue
print(i)

if __name__ == "__main__":

# train_json = r'D:\PY_SCI\Drawing board\data\COCO\annotations\train.json'
# train_path = r'D:\PY_SCI\Drawing board\data\COCO\train'
# visual_output = r'D:\PY_SCI\Drawing board\data\COCO\visual\train'

# train_json = r'D:\PY_SCI\Drawing board\data\COCO\annotations\test.json'
# train_path = r'D:\PY_SCI\Drawing board\data\COCO\test'
# visual_output = r'D:\PY_SCI\Drawing board\data\COCO\visual\test'
#
names= ['car', 'bus', 'person', 'bike', 'truck', 'motor', 'train', 'rider', 'traffic sign', 'traffic light'] # 每一类的名字
train_json = r'/home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/annotations/instances_val2017.json'
train_path = r'/home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/images/val'
visual_output = r'/home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/visual/val'

# json_path = "/Users/wanghao/Desktop/Tianchi_bottle/train1/fix_anno.json"
# out_path = "/Users/wanghao/Desktop/Tianchi_bottle/train1/vis"
# image_path = "/Users/wanghao/Desktop/Tianchi_bottle/train1/images"


select(train_json, visual_output, train_path, names)


+ 68
- 0
examples/car_detection/visual_json2.py View File

@@ -0,0 +1,68 @@
# -*- coding:utf-8 -*-
import os
from pycocotools.coco import COCO
import skimage.io as io
import matplotlib.pyplot as plt
import pylab
pylab.rcParams['figure.figsize'] = (8.0, 10.0)
annFile_path = '/home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/annotations/instances_val2017.json' # Your annotation file path
img_base_path = '/home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/images/val/' # Your image path
save_path = '/home/lxy/ubuntu/lxy/project/dataset/bdd100k/images_labels/visual/val2/'
coco = COCO(annFile_path) # COCO的初始化,读入相应的ann文件

# display COCO categories and supercategories
# coco.getCatIds()返回的是ann文件中list of所有的["categories"]["id"]
# coco.loadCats()返回的是list of对应所有id的categories(即又回到了ann文件中的字典形式)
cats = coco.loadCats(coco.getCatIds())
nms = [cat['name'] for cat in cats] # 提取各个字典中的name
print('COCO categories: {}'.format(' '.join(nms)))
nms = set([cat['supercategory'] for cat in cats]) # 提取各个字典中的supercategory
print('COCO supercategories: {}\n'.format(' '.join(nms)))
# COCO categories:
# person
# COCO supercategories:
# person

n = len(coco.imgs)

for ind in range(1, n+1):
plt.close()
# loadImgs()返回值是单元素列表,用[0]调用,img其元素为字典,key为"license", "file_name", "coco_url", "height", "width", "date_captured", "id"
# 加载指定id照片,两种途径:
# 1)本地图片,img["file_name"]
# 2)远程图片,img["coco_url"] --> I = io.imread(img["coco_url"])
imgIds = coco.getImgIds(imgIds=[ind]) # 给定参数img_id和cat_id,返回list of对应的所有img_id和包含了指定cat_id的所有img_id
img = coco.loadImgs(imgIds)[0] # loadImgs()返回list of给出id的image,为ann文件中的字典形式,这里用[0]调取列表中第一张img信息
img_path = os.path.join(img_base_path, img["file_name"])
I = io.imread(img_path)
# I = io.imread(img["coco_url"])
plt.axis('off');plt.imshow(I)
#plt.show()


# 加载指定index的照片和该照片内包含的所有类别的物体并且可视化标注
catIds = []
for ann in coco.dataset['annotations']:
if ann['image_id'] == imgIds[0]:
catIds.append(ann['category_id']) # 把该照片包含的所有的物体的cat_id都提取出来
plt.imshow(I); plt.axis('off')
annIds = coco.getAnnIds(imgIds=img['id'], catIds=catIds, iscrowd=None)
anns = coco.loadAnns(annIds)
coco.showAnns(anns)
# print(img['id']==imgIds[0]) --> true
#plt.show()


# 加载并打印该照片包含的语义分析数据
coco_caps = COCO(annFile_path)
annIds = coco_caps.getAnnIds(imgIds=img['id'])
anns = coco_caps.loadAnns(annIds)
coco_caps.showAnns(anns)
plt.imshow(I)
plt.axis('off')
plt.savefig(save_path + img["file_name"])
#plt.show()

print('***{}/{}***'.format(ind, n))

+ 195
- 0
examples/car_detection/voc2yolo.py View File

@@ -0,0 +1,195 @@
import xml.etree.ElementTree as ET
import os
import random
from shutil import copy
from tqdm import tqdm
import shutil
import cv2
classes = ['Motor vehicle','Non-motor vehicle']
# 训练集 验证集 测试集比例
train_k = 0.9
val_k = 0.9
# VOC格式数据集地址
dirs = r'/home/lxy/ubuntu/lxy/project/dataset/Vehicle-Dataset'
random.seed(0)
def clear_hidden_files(path):
dir_list = os.listdir(path)
for i in dir_list:
abspath = os.path.join(os.path.abspath(path), i)
if os.path.isfile(abspath):
if i.startswith("._"):
os.remove(abspath)
else:
clear_hidden_files(abspath)
def convert(size, box):
dw = 1. / size[0]
dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)
def convert_annotation(label_path, xml_path):
try:
in_file = open(xml_path, 'r', encoding='utf-8')
out_file = open(label_path, 'w')
# print(xml_path)
out_tmp = set()
tree = ET.parse(in_file)
root = tree.getroot()
except:
in_file = open(xml_path, 'r')
out_file = open(label_path, 'w')
# print(xml_path)
out_tmp = set()
tree = ET.parse(in_file)
root = tree.getroot()
size = root.find('size')
w = int(size.find('width').text)
h = int(size.find('height').text)
if w <= 0 or h <= 0:
tmp_img_path = xml_path.split('.')[0].split('/')[-1]
tmp_img = cv2.imread(os.path.join(dirs, 'JPEGImages', tmp_img_path + '.jpg'))
h, w, c = tmp_img.shape
for obj in root.iter('object'):
difficult = obj.find('difficult').text
cls = obj.find('name').text
if cls not in classes or int(difficult) == 1:
continue
cls_id = classes.index(cls)
xmlbox = obj.find('bndbox')
b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text),
float(xmlbox.find('ymax').text))
bb = convert((w, h), b)
out_tmp.add(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
# out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
for i in out_tmp:
out_file.write(i)
in_file.close()
out_file.close()
if os.path.isdir(os.path.join(dirs, "labels")):
shutil.rmtree(os.path.join(dirs, "labels"), True)
if not os.path.isdir(os.path.join(dirs, "labels")):
os.mkdir(os.path.join(dirs, "labels"))
dir_label = os.path.join(dirs, "labels")
if not os.path.isdir(os.path.join(dir_label, "train")):
os.mkdir(os.path.join(dir_label, "train"))
if not os.path.isdir(os.path.join(dir_label, "val")):
os.mkdir(os.path.join(dir_label, "val"))
if not os.path.isdir(os.path.join(dir_label, "test")):
os.mkdir(os.path.join(dir_label, "test"))
jpg_dirs = os.path.join(dirs, 'images')
if os.path.isdir(os.path.join(dirs, "images")):
shutil.rmtree(os.path.join(dirs, "images"), True)
if not os.path.isdir(jpg_dirs):
os.mkdir(jpg_dirs)
jpg_train_dir = os.path.join(jpg_dirs, 'train')
if not os.path.isdir(jpg_train_dir):
os.mkdir(jpg_train_dir)
jpg_val_dir = os.path.join(jpg_dirs, 'val')
if not os.path.isdir(jpg_val_dir):
os.mkdir(jpg_val_dir)
jpg_test_dir = os.path.join(jpg_dirs, 'test')
if not os.path.isdir(jpg_test_dir):
os.mkdir(jpg_test_dir)
label_dir = os.path.join(dirs, "labels")
clear_hidden_files(label_dir)
train_label_dir = os.path.join(label_dir, "train")
val_label_dir = os.path.join(label_dir, "val")
test_label_dir = os.path.join(label_dir, "test")
train_anno_dir = r''
# train_file = open(os.path.join(dirs, "train.txt"), 'w')
# val_file = open(os.path.join(dirs, "val.txt"), 'w')
# test_file = open(os.path.join(dirs, "test.txt"), 'w')
# train_file.close()
# val_file.close()
# test_file.close()
train_file = open(os.path.join(dirs, "train.txt"), 'a')
val_file = open(os.path.join(dirs, "val.txt"), 'a')
test_file = open(os.path.join(dirs, "test.txt"), 'a')
jpg_dir = os.path.join(dirs, 'JPEGImages')
clear_hidden_files(jpg_dir)
jpg_dir_total = os.listdir(jpg_dir)
random.shuffle(jpg_dir_total)
list_train = jpg_dir_total[:int(len(jpg_dir_total) * train_k * val_k)]
list_val = jpg_dir_total[int(len(jpg_dir_total) * train_k * val_k): int(len(jpg_dir_total) * train_k)]
list_test = jpg_dir_total[int(len(jpg_dir_total) * train_k):]
for i in tqdm(range(0, len(list_train))):
image_path = os.path.join(jpg_dir, list_train[i])
voc_path = list_train[i]
(nameWithoutExtention, extention) = os.path.splitext(os.path.basename(image_path))
(voc_nameWithoutExtention, voc_extention) = os.path.splitext(os.path.basename(voc_path))
annotation_name = nameWithoutExtention + '.xml'
label_name = nameWithoutExtention + '.txt'
label_path = os.path.join(train_label_dir, label_name)
xml_path = os.path.join(dirs, 'Annotations', annotation_name)
convert_annotation(label_path, xml_path) # convert label
new_path = os.path.join(jpg_train_dir, list_train[i])
copy(image_path, new_path)
train_file.write(new_path + '\n')
# copy(xml_path, os.path.join(label_dir, 'train', annotation_name))
for i in tqdm(range(0, len(list_val))):
image_path = os.path.join(jpg_dir, list_val[i])
voc_path = list_val[i]
(nameWithoutExtention, extention) = os.path.splitext(os.path.basename(image_path))
(voc_nameWithoutExtention, voc_extention) = os.path.splitext(os.path.basename(voc_path))
annotation_name = nameWithoutExtention + '.xml'
label_name = nameWithoutExtention + '.txt'
label_path = os.path.join(val_label_dir, label_name)
xml_path = os.path.join(dirs, 'Annotations', annotation_name)
convert_annotation(label_path, xml_path) # convert label
new_path = os.path.join(jpg_val_dir, list_val[i])
copy(image_path, new_path)
val_file.write(new_path + '\n')
# copy(xml_path, os.path.join(label_dir, 'val', annotation_name))
for i in tqdm(range(0, len(list_test))):
image_path = os.path.join(jpg_dir, list_test[i])
voc_path = list_test[i]
(nameWithoutExtention, extention) = os.path.splitext(os.path.basename(image_path))
(voc_nameWithoutExtention, voc_extention) = os.path.splitext(os.path.basename(voc_path))
annotation_name = nameWithoutExtention + '.xml'
label_name = nameWithoutExtention + '.txt'
label_path = os.path.join(test_label_dir, label_name)
xml_path = os.path.join(dirs, 'Annotations', annotation_name)
# convert_annotation(label_path, xml_path) # convert label
new_path = os.path.join(jpg_test_dir, list_test[i])
copy(image_path, new_path)
test_file.write(new_path + '\n')
# copy(xml_path, os.path.join(label_dir, 'test', annotation_name))
train_file.close()
val_file.close()
test_file.close()

+ 13
- 0
examples/car_detection/yolov8-l-car.yaml View File

@@ -0,0 +1,13 @@
__BASE__: [
'../bdd100k.yaml',
'./hyp.scratch.high.yaml',
'./yolov8-base.yaml'
]

network:
depth_multiple: 1.00 # scales module repeats
width_multiple: 1.00 # scales convolution channels
max_channels: 512

img_size: 640 # 2720 during validation
per_batch_size: 2

Loading…
Cancel
Save
Baidu
map