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

support train motchallenge and crowdhuman #265

Merged
merged 3 commits into from
Feb 13, 2023
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
42 changes: 42 additions & 0 deletions configs/detection/fcos/fcos_r50_torch_1x_mot20_crowdhuman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
_base_ = './fcos_r50_torch_1x_coco.py'

data_root0 = 'data/tracking/crowdhuman/'
data_root1 = 'data/tracking/MOT20/'
CLASSES = ('pedestrian', )
train_dataset = dict(
data_source=dict(
ann_file=[
data_root1 + 'annotations/train_cocoformat.json', data_root0 +
'/annotations/crowdhuman_train.json', data_root0 +
'/annotations/crowdhuman_val.json'
],
img_prefix=[
data_root1 + 'train', data_root0 + 'train', data_root0 + 'val'
],
classes=CLASSES))

val_dataset = dict(
data_source=dict(
ann_file=data_root0 + '/annotations/crowdhuman_val.json',
img_prefix=data_root0 + 'val',
classes=CLASSES))

data = dict(
imgs_per_gpu=2, workers_per_gpu=2, train=train_dataset, val=val_dataset)

model = dict(head=dict(num_classes=1))

optimizer = dict(lr=0.001)

eval_pipelines = [
dict(
mode='test',
evaluators=[
dict(type='CocoDetectionEvaluator', classes=CLASSES),
],
)
]

checkpoint_config = dict(interval=1)

load_from = 'https://pai-vision-data-hz.oss-cn-zhangjiakou.aliyuncs.com/EasyCV/modelzoo/detection/fcos/fcos_epoch_12.pth'
18 changes: 10 additions & 8 deletions easycv/datasets/builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@


def _concat_dataset(cfg, default_args=None):
ann_files = cfg['ann_file']
img_prefixes = cfg.get('img_prefix', None)
seg_prefixes = cfg.get('seg_prefix', None)
proposal_files = cfg.get('proposal_file', None)
ann_files = cfg['data_source']['ann_file']
img_prefixes = cfg['data_source'].get('img_prefix', None)
seg_prefixes = cfg['data_source'].get('seg_prefix', None)
proposal_files = cfg['data_source'].get('proposal_file', None)

datasets = []
num_dset = len(ann_files)
for i in range(num_dset):
data_cfg = copy.deepcopy(cfg)
data_cfg['ann_file'] = ann_files[i]
data_cfg['data_source']['ann_file'] = ann_files[i]
if isinstance(img_prefixes, (list, tuple)):
data_cfg['img_prefix'] = img_prefixes[i]
data_cfg['data_source']['img_prefix'] = img_prefixes[i]
if isinstance(seg_prefixes, (list, tuple)):
data_cfg['seg_prefix'] = seg_prefixes[i]
data_cfg['data_source']['seg_prefix'] = seg_prefixes[i]
if isinstance(proposal_files, (list, tuple)):
data_cfg['proposal_file'] = proposal_files[i]
data_cfg['data_source']['proposal_file'] = proposal_files[i]
datasets.append(build_dataset(data_cfg, default_args))

return ConcatDataset(datasets)
Expand All @@ -35,6 +35,8 @@ def build_dataset(cfg, default_args=None):
elif cfg['type'] == 'RepeatDataset':
dataset = RepeatDataset(
build_dataset(cfg['dataset'], default_args), cfg['times'])
elif isinstance(cfg['data_source'].get('ann_file'), (list, tuple)):
dataset = _concat_dataset(cfg, default_args)
else:
dataset = build_from_cfg(cfg, DATASETS, default_args)

Expand Down
102 changes: 102 additions & 0 deletions tools/prepare_data/crowdhuman2coco.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# Copyright (c) OpenMMLab. All rights reserved.
import argparse
import json
import os
import os.path as osp
from collections import defaultdict

import mmcv
from PIL import Image
from tqdm import tqdm

# python tools/convert_datasets/crowdhuman2coco.py -i /apsarapangu/disk4/easycv_nfs/data/tracking/crowdhuman -o /apsarapangu/disk4/easycv_nfs/data/tracking/crowdhuman/annotations


def parse_args():
parser = argparse.ArgumentParser(
description='CrowdHuman to COCO Video format')
parser.add_argument(
'-i',
'--input',
help='root directory of CrowdHuman annotations',
)
parser.add_argument(
'-o',
'--output',
help='directory to save coco formatted label file',
)
return parser.parse_args()


def load_odgt(filename):
with open(filename, 'r') as f:
lines = f.readlines()
data_infos = [json.loads(line.strip('\n')) for line in lines]
return data_infos


def convert_crowdhuman(ann_dir, save_dir, mode='train'):
"""Convert CrowdHuman dataset in COCO style.

Args:
ann_dir (str): The path of CrowdHuman dataset.
save_dir (str): The path to save annotation files.
mode (str): Convert train dataset or validation dataset. Options are
'train', 'val'. Default: 'train'.
"""
assert mode in ['train', 'val']

records = dict(img_id=1, ann_id=1)
outputs = defaultdict(list)
outputs['categories'] = [dict(id=1, name='pedestrian')]

data_infos = load_odgt(osp.join(ann_dir, f'annotation_{mode}.odgt'))
for data_info in tqdm(data_infos):
img_name = osp.join('Images', f"{data_info['ID']}.jpg")
img = Image.open(osp.join(ann_dir, mode, img_name))
width, height = img.size[:2]
image = dict(
file_name=img_name,
height=height,
width=width,
id=records['img_id'])
outputs['images'].append(image)

if mode != 'test':
for ann_info in data_info['gtboxes']:
bbox = ann_info['fbox']
if 'extra' in ann_info and 'ignore' in ann_info[
'extra'] and ann_info['extra']['ignore'] == 1:
iscrowd = True
else:
iscrowd = False
ann = dict(
id=records['ann_id'],
image_id=records['img_id'],
category_id=outputs['categories'][0]['id'],
vis_bbox=ann_info['vbox'],
bbox=bbox,
area=bbox[2] * bbox[3],
iscrowd=iscrowd)
outputs['annotations'].append(ann)
records['ann_id'] += 1
records['img_id'] += 1

if not osp.isdir(save_dir):
os.makedirs(save_dir)
mmcv.dump(outputs, osp.join(save_dir, f'crowdhuman_{mode}.json'))
print(f'-----CrowdHuman {mode} set------')
print(f'total {records["img_id"] - 1} images')
if mode != 'test':
print(f'{records["ann_id"] - 1} pedestrians are annotated.')
print('-----------------------')


def main():
args = parse_args()
convert_crowdhuman(args.input, args.output, mode='train')
convert_crowdhuman(args.input, args.output, mode='val')


if __name__ == '__main__':
main()
Loading