Files
modelscope/tests/trainers/test_ocr_recognition_trainer.py
mulin.lyh cba4e40bc1 fix numpy pandas compatible issue
明确受影响的模型(damo):  
ONE-PEACE-4B	ModuleNotFoundError: MyCustomPipeline: MyCustomModel: No module named 'one_peace',缺少依赖。
cv_resnet50_face-reconstruction	 不兼容tf2  
nlp_automatic_post_editing_for_translation_en2de	tf2.0兼容性问题,tf1.x需要  
cv_resnet18_ocr-detection-word-level_damo	tf2.x兼容性问题  
cv_resnet18_ocr-detection-line-level_damo	tf兼容性问题  
cv_resnet101_detection_fewshot-defrcn	模型限制必须detection0.3+torch1.11.0"  
speech_dfsmn_ans_psm_48k_causal	"librosa, numpy兼容性问题  
cv_mdm_motion-generation	"依赖numpy版本兼容性问题:   File ""/opt/conda/lib/python3.8/site-packages/smplx/body_models.py"",  
cv_resnet50_ocr-detection-vlpt	numpy兼容性问题  
cv_clip-it_video-summarization_language-guided_en	tf兼容性问题

Link: https://code.alibaba-inc.com/Ali-MaaS/MaaS-lib/codereview/13744636
* numpy and pandas no version

* modify compatible issue

* fix numpy compatible issue

* modify ci

* fix lint issue

* replace Image.ANTIALIAS to Image.Resampling.LANCZOS pillow compatible

* skip uncompatible cases

* fix numpy compatible issue, skip cases that can not compatbile numpy or tensorflow2.x

* skip compatible cases

* fix clip model issue

* fix body 3d keypoints compatible issue
2023-08-22 23:04:31 +08:00

99 lines
3.4 KiB
Python

# Copyright (c) Alibaba, Inc. and its affiliates.
import os
import shutil
import tempfile
import unittest
from modelscope.hub.snapshot_download import snapshot_download
from modelscope.metainfo import Trainers
from modelscope.models.cv.ocr_recognition import OCRRecognition
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
@unittest.skip(
"For FileNotFoundError: [Errno 2] No such file or directory: './work_dir/output/pytorch_model.pt' issue"
)
class TestOCRRecognitionTrainer(unittest.TestCase):
model_id = 'damo/cv_crnn_ocr-recognition-general_damo'
def setUp(self):
print(('Testing %s.%s' % (type(self).__name__, self._testMethodName)))
cache_path = snapshot_download(self.model_id, revision='v2.2.2')
config_path = os.path.join(cache_path, ModelFile.CONFIGURATION)
cfg = Config.from_file(config_path)
max_epochs = cfg.train.max_epochs
train_data_cfg = ConfigDict(
name='ICDAR13_HCTR_Dataset', split='test', namespace='damo')
test_data_cfg = ConfigDict(
name='ICDAR13_HCTR_Dataset', split='test', namespace='damo')
self.train_dataset = MsDataset.load(
dataset_name=train_data_cfg.name,
split=train_data_cfg.split,
namespace=train_data_cfg.namespace,
download_mode=DownloadMode.REUSE_DATASET_IF_EXISTS)
assert next(
iter(self.train_dataset.config_kwargs['split_config'].values()))
self.test_dataset = MsDataset.load(
dataset_name=test_data_cfg.name,
split=test_data_cfg.split,
namespace=train_data_cfg.namespace,
download_mode=DownloadMode.REUSE_DATASET_IF_EXISTS)
assert next(
iter(self.test_dataset.config_kwargs['split_config'].values()))
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,
train_dataset=self.train_dataset,
eval_dataset=self.test_dataset,
work_dir=self.tmp_dir)
trainer = build_trainer(
name=Trainers.ocr_recognition, default_args=kwargs)
trainer.train()
@unittest.skipUnless(test_level() >= 0, '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, revision='v2.2.2')
model = OCRRecognition.from_pretrained(cache_path)
kwargs = dict(
cfg_file=os.path.join(cache_path, ModelFile.CONFIGURATION),
model=model,
train_dataset=self.train_dataset,
eval_dataset=self.test_dataset,
work_dir=tmp_dir)
trainer = build_trainer(
name=Trainers.ocr_recognition, default_args=kwargs)
trainer.train()
if __name__ == '__main__':
unittest.main()