-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
133 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# Copyright (c) 2021, Zhiqiang Wang. All Rights Reserved. | ||
from pathlib import Path | ||
import unittest | ||
|
||
import torch | ||
from torch.utils import data | ||
|
||
from yolort.datasets.coco import CocoDetection | ||
from yolort.datasets.transforms import collate_fn, default_train_transforms | ||
from yolort.utils import prepare_coco128 | ||
|
||
from typing import Dict | ||
|
||
|
||
class DataPipelineTester(unittest.TestCase): | ||
|
||
def test_prepare_coco128(self): | ||
data_path = Path('data-bin') | ||
coco128_dirname = 'coco128' | ||
prepare_coco128(data_path, dirname=coco128_dirname) | ||
annotation_file = data_path / coco128_dirname / 'annotations' / 'instances_train2017.json' | ||
self.assertTrue(annotation_file.is_file()) | ||
|
||
def test_vanilla_dataloader(self): | ||
# Acquire the images and labels from the coco128 dataset | ||
data_path = Path('data-bin') | ||
coco128_dirname = 'coco128' | ||
coco128_path = data_path / coco128_dirname | ||
image_root = coco128_path / 'images' / 'train2017' | ||
annotation_file = coco128_path / 'annotations' / 'instances_train2017.json' | ||
|
||
if not annotation_file.is_file(): | ||
prepare_coco128(data_path, dirname=coco128_dirname) | ||
|
||
dataset = CocoDetection(image_root, annotation_file, default_train_transforms()) | ||
# Test the datasets | ||
image, target = next(iter(dataset)) | ||
self.assertIsInstance(image, torch.Tensor) | ||
self.assertIsInstance(target, Dict) | ||
|
||
batch_size = 4 | ||
sampler = data.RandomSampler(dataset) | ||
batch_sampler = data.BatchSampler(sampler, batch_size, drop_last=True) | ||
loader = data.DataLoader(dataset, batch_sampler=batch_sampler, collate_fn=collate_fn, num_workers=0) | ||
# Test the dataloader | ||
images, targets = next(iter(loader)) | ||
|
||
self.assertEqual(len(images), batch_size) | ||
self.assertEqual(len(targets), batch_size) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .flash_utils import get_callable_dict | ||
from .image_utils import cv2_imshow, get_image_from_url, read_image_to_tensor | ||
from .update_module_state import update_module_state_from_ultralytics | ||
from .file_utils import prepare_coco128 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright (c) 2021, Zhiqiang Wang. All Rights Reserved. | ||
from pathlib import PosixPath | ||
from zipfile import ZipFile | ||
|
||
import torch | ||
|
||
|
||
def prepare_coco128( | ||
data_path: PosixPath, | ||
dirname: str = 'coco128', | ||
) -> None: | ||
""" | ||
Prepare coco128 dataset to test. | ||
Args: | ||
data_path (PosixPath): root path of coco128 dataset. | ||
dirname (str): the directory name of coco128 dataset. Default: 'coco128'. | ||
""" | ||
if not data_path.is_dir(): | ||
print(f'Create a new directory: {data_path}') | ||
data_path.mkdir(parents=True, exist_ok=True) | ||
|
||
zip_path = data_path / 'coco128.zip' | ||
coco128_url = 'https://github.com/zhiqwang/yolov5-rt-stack/releases/download/v0.3.0/coco128.zip' | ||
if not zip_path.is_file(): | ||
print(f'Downloading coco128 datasets form {coco128_url}') | ||
torch.hub.download_url_to_file(coco128_url, zip_path, hash_prefix='a67d2887') | ||
|
||
coco128_path = data_path / dirname | ||
if not coco128_path.is_dir(): | ||
print(f'Unzipping dataset to {coco128_path}') | ||
with ZipFile(zip_path, 'r') as zip_obj: | ||
zip_obj.extractall(data_path) |