You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_py_dataset.py 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import unittest
  2. import datasets as hfdata
  3. from modelscope.models import Model
  4. from modelscope.preprocessors import SequenceClassificationPreprocessor
  5. from modelscope.preprocessors.base import Preprocessor
  6. from modelscope.pydatasets import PyDataset
  7. from modelscope.utils.constant import Hubs
  8. from modelscope.utils.test_utils import require_tf, require_torch, test_level
  9. class ImgPreprocessor(Preprocessor):
  10. def __init__(self, *args, **kwargs):
  11. super().__init__(*args, **kwargs)
  12. self.path_field = kwargs.pop('image_path', 'image_path')
  13. self.width = kwargs.pop('width', 'width')
  14. self.height = kwargs.pop('height', 'width')
  15. def __call__(self, data):
  16. import cv2
  17. image_path = data.get(self.path_field)
  18. if not image_path:
  19. return None
  20. img = cv2.imread(image_path)
  21. return {
  22. 'image':
  23. cv2.resize(img,
  24. (data.get(self.height, 128), data.get(self.width, 128)))
  25. }
  26. class PyDatasetTest(unittest.TestCase):
  27. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  28. def test_ds_basic(self):
  29. ms_ds_full = PyDataset.load('squad')
  30. ms_ds_full_hf = hfdata.load_dataset('squad')
  31. ms_ds_train = PyDataset.load('squad', split='train')
  32. ms_ds_train_hf = hfdata.load_dataset('squad', split='train')
  33. ms_image_train = PyDataset.from_hf_dataset(
  34. hfdata.load_dataset('beans', split='train'))
  35. self.assertEqual(ms_ds_full['train'][0], ms_ds_full_hf['train'][0])
  36. self.assertEqual(ms_ds_full['validation'][0],
  37. ms_ds_full_hf['validation'][0])
  38. self.assertEqual(ms_ds_train[0], ms_ds_train_hf[0])
  39. print(next(iter(ms_ds_full['train'])))
  40. print(next(iter(ms_ds_train)))
  41. print(next(iter(ms_image_train)))
  42. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  43. @require_torch
  44. def test_to_torch_dataset_text(self):
  45. model_id = 'damo/bert-base-sst2'
  46. nlp_model = Model.from_pretrained(model_id)
  47. preprocessor = SequenceClassificationPreprocessor(
  48. nlp_model.model_dir,
  49. first_sequence='context',
  50. second_sequence=None)
  51. ms_ds_train = PyDataset.load('squad', split='train')
  52. pt_dataset = ms_ds_train.to_torch_dataset(preprocessors=preprocessor)
  53. import torch
  54. dataloader = torch.utils.data.DataLoader(pt_dataset, batch_size=5)
  55. print(next(iter(dataloader)))
  56. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  57. @require_tf
  58. def test_to_tf_dataset_text(self):
  59. import tensorflow as tf
  60. tf.compat.v1.enable_eager_execution()
  61. model_id = 'damo/bert-base-sst2'
  62. nlp_model = Model.from_pretrained(model_id)
  63. preprocessor = SequenceClassificationPreprocessor(
  64. nlp_model.model_dir,
  65. first_sequence='context',
  66. second_sequence=None)
  67. ms_ds_train = PyDataset.load('squad', split='train')
  68. tf_dataset = ms_ds_train.to_tf_dataset(
  69. batch_size=5,
  70. shuffle=True,
  71. preprocessors=preprocessor,
  72. drop_remainder=True)
  73. print(next(iter(tf_dataset)))
  74. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  75. @require_torch
  76. def test_to_torch_dataset_img(self):
  77. ms_image_train = PyDataset.from_hf_dataset(
  78. hfdata.load_dataset('beans', split='train'))
  79. pt_dataset = ms_image_train.to_torch_dataset(
  80. preprocessors=ImgPreprocessor(
  81. image_path='image_file_path', label='labels'))
  82. import torch
  83. dataloader = torch.utils.data.DataLoader(pt_dataset, batch_size=5)
  84. print(next(iter(dataloader)))
  85. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  86. @require_tf
  87. def test_to_tf_dataset_img(self):
  88. import tensorflow as tf
  89. tf.compat.v1.enable_eager_execution()
  90. ms_image_train = PyDataset.load('beans', split='train')
  91. tf_dataset = ms_image_train.to_tf_dataset(
  92. batch_size=5,
  93. shuffle=True,
  94. preprocessors=ImgPreprocessor(image_path='image_file_path'),
  95. drop_remainder=True,
  96. label_cols='labels')
  97. print(next(iter(tf_dataset)))
  98. if __name__ == '__main__':
  99. unittest.main()