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

[Feature] Support training on a single channel image dataset. #460

Merged
merged 27 commits into from
Feb 23, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
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
35 changes: 35 additions & 0 deletions projects/single_channel/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# MMYOLO Model #409
hhaAndroid marked this conversation as resolved.
Show resolved Hide resolved

## Introduction

This project was developed for training on a single channel image dataset.

Now, the script only support `YOLOv5` .

## Usage

### Data set preparation

If you are using a custom grayscale image dataset, you can skip this step.

```shell
python tools/misc/download_dataset.py --dataset-name balloon --save-dir projects/single_channel/data --unzip
python projects/single_channel/balloon2coco_single_channel.py
python projects/single_channel/single_channel.py
```

### Training

In the configuration file, `_base_` and `data_root` need to be modified to the corresponding dataset paths.
hhaAndroid marked this conversation as resolved.
Show resolved Hide resolved

```shell
python tools/train.py projects/single_channel/configs/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon_single_channel.py
```

### Model Testing

```shell
python tools/test.py projects/single_channel/configs/yolov5/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon.py \
work_dirs/yolov5_s-v61_syncbn_fast_1xb4-300e_balloon/epoch_300.pth \
--show-dir show_results
```
64 changes: 64 additions & 0 deletions projects/single_channel/balloon2coco_single_channel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import os.path as osp

import mmcv
import mmengine


def convert_balloon_to_coco(ann_file, out_file, image_prefix):

data_infos = mmengine.load(ann_file)

annotations = []
images = []
obj_count = 0
for idx, v in enumerate(mmengine.track_iter_progress(data_infos.values())):
filename = v['filename']
img_path = osp.join(image_prefix, filename)
height, width = mmcv.imread(img_path).shape[:2]

images.append(
dict(id=idx, file_name=filename, height=height, width=width))

for _, obj in v['regions'].items():
assert not obj['region_attributes']
obj = obj['shape_attributes']
px = obj['all_points_x']
py = obj['all_points_y']
poly = [(x + 0.5, y + 0.5) for x, y in zip(px, py)]
poly = [p for x in poly for p in x]

x_min, y_min, x_max, y_max = (min(px), min(py), max(px), max(py))

data_anno = dict(
image_id=idx,
id=obj_count,
category_id=0,
bbox=[x_min, y_min, x_max - x_min, y_max - y_min],
area=(x_max - x_min) * (y_max - y_min),
segmentation=[poly],
iscrowd=0)
annotations.append(data_anno)
obj_count += 1

coco_format_json = dict(
images=images,
annotations=annotations,
categories=[{
'id': 0,
'name': 'balloon'
}])
mmengine.dump(coco_format_json, out_file)


if __name__ == '__main__':

convert_balloon_to_coco(
'projects/single_channel/data/balloon/train/'
'via_region_data.json',
'projects/single_channel/data/balloon/train.json',
'projects/single_channel/data/balloon/train/')
convert_balloon_to_coco(
'projects/single_channel/data/balloon/val/'
'via_region_data.json',
'projects/single_channel/data/balloon/val.json',
'projects/single_channel/data/balloon/val/')
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
_base_ = '../../../configs/yolov5/yolov5_s-v61_syncbn_fast_8xb16-300e_coco.py'

data_root = '../../../projects/single_channel/data/balloon/'

train_batch_size_per_gpu = 4
train_num_workers = 2

metainfo = {
'classes': ('balloon', ),
'palette': [
(220, 20, 60),
]
}

train_dataloader = dict(
batch_size=train_batch_size_per_gpu,
num_workers=train_num_workers,
dataset=dict(
data_root=data_root,
metainfo=metainfo,
data_prefix=dict(img='train/'),
ann_file='train.json'))

val_dataloader = dict(
dataset=dict(
data_root=data_root,
metainfo=metainfo,
data_prefix=dict(img='val/'),
ann_file='val.json'))

test_dataloader = val_dataloader

val_evaluator = dict(ann_file=data_root + 'val.json')

test_evaluator = val_evaluator

model = dict(bbox_head=dict(head_module=dict(num_classes=1)))

default_hooks = dict(logger=dict(interval=1))
29 changes: 29 additions & 0 deletions projects/single_channel/single_channel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import imghdr
import os
from typing import List

from PIL import Image

path = 'projects/single_channel/data/balloon/train'
save_path = 'projects/single_channel/data/balloon/train'
file_list: List[str] = os.listdir(path)

# Grayscale conversion of each image
for file in file_list:
if imghdr.what(path + '/' + file) != 'jpeg':
continue
o_img = Image.open(path + '/' + file)
L_img = o_img.convert('L')
L_img.save(save_path + '/' + file)

path = 'projects/single_channel/data/balloon/val'
save_path = 'projects/single_channel/data/balloon/val'
file_list = os.listdir(path)
hhaAndroid marked this conversation as resolved.
Show resolved Hide resolved

# Grayscale conversion of each image
for file in file_list:
hhaAndroid marked this conversation as resolved.
Show resolved Hide resolved
if imghdr.what(path + '/' + file) != 'jpeg':
continue
o_img = Image.open(path + '/' + file)
L_img = o_img.convert('L')
L_img.save(save_path + '/' + file)