Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add finetune single_class_dataset #351

Merged
merged 1 commit into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ See [INSTALLATION](docs/en/installation.md) for details.

See [GETTING STARTED](GETTING_STARTED.md) for details.

## Custom dataset examples

See [examples](examples)

## Notes

⚠️ The current version is based on the [static shape of GRAPH](https://mindspore.cn/docs/en/r2.0/note/static_graph_syntax_support.html).
Expand Down
10 changes: 5 additions & 5 deletions docs/zh/how_to_guides/data_preparation.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@
```
└─ coco2017_yolo
├─ annotations
└─ instances_val2017.json
└─ instances_val2017.json
├─ images
├─ train2017 # coco2017 原始图片
└─ val2017 # coco2017 原始图片
├─ train2017 # coco2017 原始图片
└─ val2017 # coco2017 原始图片
├─ labels
├─ train2017
└─ val2017
├─ train2017
└─ val2017
├─ train2017.txt
├─ val2017.txt
└─ test-dev2017.txt
Expand Down
1 change: 0 additions & 1 deletion examples/finetune_car_detection/crejson.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,3 @@ def init_coco_format():
# 为每个分区保存JSON文件
with open(f"./annotations/instances_val2017.json", "w") as json_file:
json.dump(coco_format, json_file, indent=4)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

空行

5 changes: 2 additions & 3 deletions examples/finetune_car_detection/rename.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
start_num = 1

# 获取当前目录下所有的.txt文件
files = [f for f in os.listdir('images/train') if f.endswith(extension)]
files = [f for f in os.listdir('images/val') if f.endswith(extension)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为啥要换这个

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

只需要对验证集做改名和生成json文件,这里怕误导用户


for i, file in enumerate(files, start=start_num):
# 构造新的文件名,使用zfill填充前导零
new_filename = f"{i:06d}{extension}"

# 使用os.rename重命名文件
# 需根据自己的路径修改
os.chdir('D:/Desktop/mindspore/yolo_car_detection/bddua/bdd100k/images/train')
os.chdir('images/val')
os.rename(file, new_filename)

#print(f"Renamed '{file}' to '{new_filename}'")

print("All files have been renamed.")

9 changes: 3 additions & 6 deletions examples/finetune_car_detection/voc2yolo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import xml.etree.ElementTree as ET
import sys
import os.path
import cv2


class XmlParse:
Expand Down Expand Up @@ -43,7 +42,7 @@ def xml2txt(xml, labels, name_list, img_path):
fil_name = root.find('filename').text[:-4]
if not os.path.exists(labels): # 如果路径不存在则创建
os.mkdir(labels)
out = open(labels + './' + fil_name + '.txt', 'w+')
out = open(labels + '/' + fil_name + '.txt', 'w+')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以换成 os.path.join(path1, path2)的方式

for obj in root.iter('object'):

x_min = float(obj.find('bndbox').find('xmin').text)
Expand Down Expand Up @@ -74,8 +73,7 @@ def xml2txt(xml, labels, name_list, img_path):
# print(w)
# print(h)

#class_dict = dict(zip(name_list, range(0, len(name_list))))
class_dict={'rider':0, 'pedestrian':1, 'trailer':2, 'train':3,'bus':4,'car':5,'truck':6,'traffic sign':7,'traffic light':8,'other person':9,'motorcycle':10,'bicycle':11}
class_dict = {name: i for i, name in enumerate(name_list)}
class_name = obj.find('name').text
if class_name not in name_list:
pass
Expand All @@ -100,8 +98,7 @@ def folder_Path():
img_path = './images/val'
xml_path = './labels_xml/val' # xml路径
labels = './val' # 转txt路径
#name_list = ['rider', 'pedestrian', 'trailer', 'train','bus','car','truck','traffic sign','traffic light','other person','motorcycle','bicycle'] # 类别名
name_list = ['rider', 'pedestrian', 'trailer', 'train','bus','car','truck','traffic sign','traffic light','other person','motorcycle','bicycle',"van"] # 类别名
name_list = ['rider', 'pedestrian', 'trailer', 'train', 'bus', 'car', 'truck', 'traffic sign', 'traffic light', 'other person', 'motorcycle', 'bicycle', "van"] # 类别名

xml2txt(xml_path, labels, name_list, img_path)

Expand Down
63 changes: 63 additions & 0 deletions examples/finetune_single_class_dataset/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
### 单类别数据集训练流程

本文以自制巧克力花生豆数据集为例,介绍单类别数据集使用MindYOLO进行训练的主要流程。

#### 数据集格式转换

巧克力花生豆数据集采用voc格式的数据标注,其文件目录如下所示:
```
ROOT_DIR
├── Annotations
│ ├── 000000.xml
│ └── 000002.xml
├── Images
│ ├── 000000.jpg
│ └── 000002.jpg
└── Test_Images
├── 000004.jpg
└── 000006.jpg
```
数据集格式转换分为以下步骤:

1. 训练集与验证集转换为yolo格式。可参考[voc2yolo.py](../finetune_car_detection/voc2yolo.py),使用时需修改图片文件夹路径、标签文件夹路径与生成的txt标签文件夹路径,且对训练集和验证集依次完成该过程。
2. 验证集转换为coco格式。首先完成图片重命名,可参考[rename.py
](../finetune_car_detection/rename.py),使用时需确保当前目录为数据集的根目录。然后生成json文件,可参考[crejson.py](../finetune_car_detection/crejson.py),使用时需修改验证集图片文件夹路径,验证集txt标注文件路径以及生成的json文件路径。

#### 编写yaml配置文件
配置文件继承[yolov8n.yaml](../../configs/yolov8/yolov8n.yaml),并且列出需要修改的参数,通常包括数据集相关参数以及学习率等超参,如下所示:
```
__BASE__: [
'../../configs/yolov8/yolov8n.yaml',
]

data:
dataset_name: seed
train_set: ./seed/train.txt
val_set: ./seed/val.txt
nc: 1
# class names
names: [ 'seed' ]

optimizer:
lr_init: 0.001 # initial learning rate
warmup_bias_lr: 0.01 # warmup initial bias lr
min_warmup_step: 10 # minmum warmup step
```
#### 模型训练
选用yolov8n模型进行训练。
* 在多卡NPU/GPU上进行分布式模型训练,以8卡为例:

```shell
mpirun --allow-run-as-root -n 8 python train.py --config ./examples/finetune_single_class_dataset/yolov8n_single_class_dataset.yaml --is_parallel True
```

#### 可视化推理
使用/demo/predict.py即可用训练好的模型进行可视化推理,运行方式如下:

```shell
python demo/predict.py --config ./examples/finetune_single_class_dataset/yolov8n_single_class_dataset.yaml --weight=/path_to_ckpt/WEIGHT.ckpt --image_path /path_to_image/IMAGE.jpg
```
推理效果如下:
<div align=center>
<img width='600' src="https://github.com/yuedongli1/images/raw/master/16.jpg"/>
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
__BASE__: [
'../../configs/yolov8/yolov8n.yaml',
]

data:
dataset_name: seed
train_set: ./seed/train.txt
val_set: ./seed/val.txt
nc: 1
# class names
names: [ 'seed' ]

optimizer:
lr_init: 0.001 # initial learning rate
warmup_bias_lr: 0.01 # warmup initial bias lr
min_warmup_step: 10 # minmum warmup step
2 changes: 1 addition & 1 deletion requirements/cpu_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ numpy < 2
pyyaml >= 5.3
tqdm
pillow == 9.5.0
mindspore
mindspore <= 2.3.0
pylint
pytest
opencv-python
Expand Down
Loading