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_ms_dataset.py 4.4 kB

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