mirror of
https://github.com/modelscope/modelscope.git
synced 2025-12-25 12:39:25 +01:00
Refactor the task_datasets module: 1. Add new module modelscope.msdatasets.dataset_cls.custom_datasets. 2. Add new function: modelscope.msdatasets.ms_dataset.MsDataset.to_custom_dataset(). 2. Add calling to_custom_dataset() func in MsDataset.load() to adapt new custom_datasets module. 3. Refactor the pipeline for loading custom dataset: 1) Only use MsDataset.load() function to load the custom datasets. 2) Combine MsDataset.load() with class EpochBasedTrainer. 4. Add new entry func for building datasets in EpochBasedTrainer: see modelscope.trainers.trainer.EpochBasedTrainer.build_dataset() 5. Add new func to build the custom dataset from model configuration, see: modelscope.trainers.trainer.EpochBasedTrainer.build_dataset_from_cfg() 6. Add new registry function for building custom datasets, see: modelscope.msdatasets.dataset_cls.custom_datasets.builder.build_custom_dataset() 7. Refine the class SiameseUIETrainer to adapt the new custom_datasets module. 8. Add class TorchCustomDataset as a superclass for custom datasets classes. 9. To move modules/classes/functions: 1) Move module msdatasets.audio to custom_datasets 2) Move module msdatasets.cv to custom_datasets 3) Move module bad_image_detecting to custom_datasets 4) Move module damoyolo to custom_datasets 5) Move module face_2d_keypoints to custom_datasets 6) Move module hand_2d_keypoints to custom_datasets 7) Move module human_wholebody_keypoint to custom_datasets 8) Move module image_classification to custom_datasets 9) Move module image_inpainting to custom_datasets 10) Move module image_portrait_enhancement to custom_datasets 11) Move module image_quality_assessment_degradation to custom_datasets 12) Move module image_quality_assmessment_mos to custom_datasets 13) Move class LanguageGuidedVideoSummarizationDataset to custom_datasets 14) Move class MGeoRankingDataset to custom_datasets 15) Move module movie_scene_segmentation custom_datasets 16) Move module object_detection to custom_datasets 17) Move module referring_video_object_segmentation to custom_datasets 18) Move module sidd_image_denoising to custom_datasets 19) Move module video_frame_interpolation to custom_datasets 20) Move module video_stabilization to custom_datasets 21) Move module video_super_resolution to custom_datasets 22) Move class GoproImageDeblurringDataset to custom_datasets 23) Move class EasyCVBaseDataset to custom_datasets 24) Move class ImageInstanceSegmentationCocoDataset to custom_datasets 25) Move class RedsImageDeblurringDataset to custom_datasets 26) Move class TextRankingDataset to custom_datasets 27) Move class VecoDataset to custom_datasets 28) Move class VideoSummarizationDataset to custom_datasets 10. To delete modules/functions/classes: 1) Del module task_datasets 2) Del to_task_dataset() in EpochBasedTrainer 3) Del build_dataset() in EpochBasedTrainer and renew a same name function. 11. Rename class Datasets to CustomDatasets in metainfo.py Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/11872747
123 lines
4.5 KiB
Python
123 lines
4.5 KiB
Python
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
import os
|
|
import shutil
|
|
import tempfile
|
|
import unittest
|
|
import zipfile
|
|
from functools import partial
|
|
|
|
from modelscope.hub.snapshot_download import snapshot_download
|
|
from modelscope.metainfo import Trainers
|
|
from modelscope.models.cv.image_instance_segmentation import \
|
|
CascadeMaskRCNNSwinModel
|
|
from modelscope.msdatasets import MsDataset
|
|
from modelscope.trainers import build_trainer
|
|
from modelscope.utils.config import Config, ConfigDict
|
|
from modelscope.utils.constant import DownloadMode, ModelFile
|
|
from modelscope.utils.test_utils import test_level
|
|
|
|
|
|
class TestImageInstanceSegmentationTrainer(unittest.TestCase):
|
|
|
|
model_id = 'damo/cv_swin-b_image-instance-segmentation_coco'
|
|
|
|
def setUp(self):
|
|
print(('Testing %s.%s' % (type(self).__name__, self._testMethodName)))
|
|
|
|
cache_path = snapshot_download(self.model_id)
|
|
config_path = os.path.join(cache_path, ModelFile.CONFIGURATION)
|
|
cfg = Config.from_file(config_path)
|
|
|
|
max_epochs = cfg.train.max_epochs
|
|
samples_per_gpu = cfg.train.dataloader.batch_size_per_gpu
|
|
try:
|
|
train_data_cfg = cfg.dataset.train
|
|
val_data_cfg = cfg.dataset.val
|
|
except Exception:
|
|
train_data_cfg = None
|
|
val_data_cfg = None
|
|
if train_data_cfg is None:
|
|
# use default toy data
|
|
train_data_cfg = ConfigDict(
|
|
name='pets_small', split='train', test_mode=False)
|
|
if val_data_cfg is None:
|
|
val_data_cfg = ConfigDict(
|
|
name='pets_small', split='validation', test_mode=True)
|
|
|
|
self.train_dataset = MsDataset.load(
|
|
dataset_name=train_data_cfg.name,
|
|
split=train_data_cfg.split,
|
|
test_mode=train_data_cfg.test_mode,
|
|
download_mode=DownloadMode.FORCE_REDOWNLOAD)
|
|
assert self.train_dataset.config_kwargs['classes']
|
|
assert next(
|
|
iter(self.train_dataset.config_kwargs['split_config'].values()))
|
|
|
|
self.eval_dataset = MsDataset.load(
|
|
dataset_name=val_data_cfg.name,
|
|
split=val_data_cfg.split,
|
|
test_mode=val_data_cfg.test_mode,
|
|
download_mode=DownloadMode.FORCE_REDOWNLOAD)
|
|
assert self.eval_dataset.config_kwargs['classes']
|
|
assert next(
|
|
iter(self.eval_dataset.config_kwargs['split_config'].values()))
|
|
|
|
from mmcv.parallel import collate
|
|
|
|
self.collate_fn = partial(collate, samples_per_gpu=samples_per_gpu)
|
|
|
|
self.max_epochs = max_epochs
|
|
|
|
self.tmp_dir = tempfile.TemporaryDirectory().name
|
|
if not os.path.exists(self.tmp_dir):
|
|
os.makedirs(self.tmp_dir)
|
|
|
|
def tearDown(self):
|
|
shutil.rmtree(self.tmp_dir)
|
|
super().tearDown()
|
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
|
def test_trainer(self):
|
|
kwargs = dict(
|
|
model=self.model_id,
|
|
data_collator=self.collate_fn,
|
|
train_dataset=self.train_dataset,
|
|
eval_dataset=self.eval_dataset,
|
|
work_dir=self.tmp_dir)
|
|
|
|
trainer = build_trainer(
|
|
name=Trainers.image_instance_segmentation, default_args=kwargs)
|
|
trainer.train()
|
|
results_files = os.listdir(self.tmp_dir)
|
|
self.assertIn(f'{trainer.timestamp}.log.json', results_files)
|
|
for i in range(self.max_epochs):
|
|
self.assertIn(f'epoch_{i+1}.pth', results_files)
|
|
|
|
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
|
|
def test_trainer_with_model_and_args(self):
|
|
tmp_dir = tempfile.TemporaryDirectory().name
|
|
if not os.path.exists(tmp_dir):
|
|
os.makedirs(tmp_dir)
|
|
|
|
cache_path = snapshot_download(self.model_id)
|
|
model = CascadeMaskRCNNSwinModel.from_pretrained(cache_path)
|
|
kwargs = dict(
|
|
cfg_file=os.path.join(cache_path, ModelFile.CONFIGURATION),
|
|
model=model,
|
|
data_collator=self.collate_fn,
|
|
train_dataset=self.train_dataset,
|
|
eval_dataset=self.eval_dataset,
|
|
work_dir=self.tmp_dir)
|
|
|
|
trainer = build_trainer(
|
|
name=Trainers.image_instance_segmentation, default_args=kwargs)
|
|
trainer.train()
|
|
results_files = os.listdir(self.tmp_dir)
|
|
self.assertIn(f'{trainer.timestamp}.log.json', results_files)
|
|
for i in range(self.max_epochs):
|
|
self.assertIn(f'epoch_{i+1}.pth', results_files)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|