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_ofa_tasks.py 7.9 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import unittest
  3. from modelscope.models import Model
  4. from modelscope.outputs import OutputKeys
  5. from modelscope.pipelines import pipeline
  6. from modelscope.utils.constant import Tasks
  7. from modelscope.utils.test_utils import test_level
  8. class OfaTasksTest(unittest.TestCase):
  9. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  10. def test_run_with_image_captioning_with_model(self):
  11. model = Model.from_pretrained(
  12. 'damo/ofa_image-caption_coco_distilled_en')
  13. img_captioning = pipeline(
  14. task=Tasks.image_captioning,
  15. model=model,
  16. )
  17. result = img_captioning(
  18. {'image': 'data/test/images/image_captioning.png'})
  19. print(result[OutputKeys.CAPTION])
  20. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  21. def test_run_with_image_captioning_with_name(self):
  22. img_captioning = pipeline(
  23. Tasks.image_captioning,
  24. model='damo/ofa_image-caption_coco_distilled_en')
  25. result = img_captioning(
  26. {'image': 'data/test/images/image_captioning.png'})
  27. print(result[OutputKeys.CAPTION])
  28. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  29. def test_run_with_image_classification_with_model(self):
  30. model = Model.from_pretrained(
  31. 'damo/ofa_image-classification_imagenet_large_en')
  32. ofa_pipe = pipeline(Tasks.image_classification, model=model)
  33. image = 'data/test/images/image_classification.png'
  34. input = {'image': image}
  35. result = ofa_pipe(input)
  36. print(result)
  37. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  38. def test_run_with_image_classification_with_name(self):
  39. ofa_pipe = pipeline(
  40. Tasks.image_classification,
  41. model='damo/ofa_image-classification_imagenet_large_en')
  42. image = 'data/test/images/image_classification.png'
  43. input = {'image': image}
  44. result = ofa_pipe(input)
  45. print(result)
  46. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  47. def test_run_with_summarization_with_model(self):
  48. model = Model.from_pretrained(
  49. 'damo/ofa_summarization_gigaword_large_en')
  50. ofa_pipe = pipeline(Tasks.summarization, model=model)
  51. text = 'five-time world champion michelle kwan withdrew' + \
  52. 'from the #### us figure skating championships on wednesday ,' + \
  53. ' but will petition us skating officials for the chance to ' + \
  54. 'compete at the #### turin olympics .'
  55. input = {'text': text}
  56. result = ofa_pipe(input)
  57. print(result)
  58. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  59. def test_run_with_summarization_with_name(self):
  60. ofa_pipe = pipeline(
  61. Tasks.summarization,
  62. model='damo/ofa_summarization_gigaword_large_en')
  63. text = 'five-time world champion michelle kwan withdrew' + \
  64. 'from the #### us figure skating championships on wednesday ,' + \
  65. ' but will petition us skating officials for the chance to ' +\
  66. 'compete at the #### turin olympics .'
  67. input = {'text': text}
  68. result = ofa_pipe(input)
  69. print(result)
  70. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  71. def test_run_with_text_classification_with_model(self):
  72. model = Model.from_pretrained(
  73. 'damo/ofa_text-classification_mnli_large_en')
  74. ofa_pipe = pipeline(Tasks.text_classification, model=model)
  75. text = 'One of our number will carry out your instructions minutely.'
  76. text2 = 'A member of my team will execute your orders with immense precision.'
  77. input = {'text': text, 'text2': text2}
  78. result = ofa_pipe(input)
  79. print(result)
  80. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  81. def test_run_with_text_classification_with_name(self):
  82. ofa_pipe = pipeline(
  83. Tasks.text_classification,
  84. model='damo/ofa_text-classification_mnli_large_en')
  85. text = 'One of our number will carry out your instructions minutely.'
  86. text2 = 'A member of my team will execute your orders with immense precision.'
  87. input = {'text': text, 'text2': text2}
  88. result = ofa_pipe(input)
  89. print(result)
  90. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  91. def test_run_with_visual_entailment_with_model(self):
  92. model = Model.from_pretrained(
  93. 'damo/ofa_visual-entailment_snli-ve_large_en')
  94. ofa_pipe = pipeline(Tasks.visual_entailment, model=model)
  95. image = 'data/test/images/dogs.jpg'
  96. text = 'there are two birds.'
  97. input = {'image': image, 'text': text}
  98. result = ofa_pipe(input)
  99. print(result)
  100. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  101. def test_run_with_visual_entailment_with_name(self):
  102. ofa_pipe = pipeline(
  103. Tasks.visual_entailment,
  104. model='damo/ofa_visual-entailment_snli-ve_large_en')
  105. image = 'data/test/images/dogs.jpg'
  106. text = 'there are two birds.'
  107. input = {'image': image, 'text': text}
  108. result = ofa_pipe(input)
  109. print(result)
  110. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  111. def test_run_with_visual_grounding_with_model(self):
  112. model = Model.from_pretrained(
  113. 'damo/ofa_visual-grounding_refcoco_large_en')
  114. ofa_pipe = pipeline(Tasks.visual_grounding, model=model)
  115. image = 'data/test/images/visual_grounding.png'
  116. text = 'a blue turtle-like pokemon with round head'
  117. input = {'image': image, 'text': text}
  118. result = ofa_pipe(input)
  119. print(result)
  120. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  121. def test_run_with_visual_grounding_with_name(self):
  122. ofa_pipe = pipeline(
  123. Tasks.visual_grounding,
  124. model='damo/ofa_visual-grounding_refcoco_large_en')
  125. image = 'data/test/images/visual_grounding.png'
  126. text = 'a blue turtle-like pokemon with round head'
  127. input = {'image': image, 'text': text}
  128. result = ofa_pipe(input)
  129. print(result)
  130. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  131. def test_run_with_visual_question_answering_with_model(self):
  132. from modelscope.preprocessors.multi_modal import OfaPreprocessor
  133. model = Model.from_pretrained(
  134. 'damo/ofa_visual-question-answering_pretrain_large_en')
  135. preprocessor = OfaPreprocessor(model_dir=model.model_dir)
  136. ofa_pipe = pipeline(
  137. Tasks.visual_question_answering,
  138. model=model,
  139. preprocessor=preprocessor)
  140. image = 'data/test/images/visual_question_answering.png'
  141. text = 'what is grown on the plant?'
  142. input = {'image': image, 'text': text}
  143. result = ofa_pipe(input)
  144. print(result)
  145. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  146. def test_run_with_visual_question_answering_with_name(self):
  147. from modelscope.preprocessors.multi_modal import OfaPreprocessor
  148. model = 'damo/ofa_visual-question-answering_pretrain_large_en'
  149. preprocessor = OfaPreprocessor(model_dir=model)
  150. ofa_pipe = pipeline(
  151. Tasks.visual_question_answering,
  152. model=model,
  153. preprocessor=preprocessor)
  154. image = 'data/test/images/visual_question_answering.png'
  155. text = 'what is grown on the plant?'
  156. input = {'image': image, 'text': text}
  157. result = ofa_pipe(input)
  158. print(result)
  159. if __name__ == '__main__':
  160. unittest.main()