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 12 kB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import os
  3. import unittest
  4. from os import path as osp
  5. import cv2
  6. from PIL import Image
  7. from modelscope.models import Model
  8. from modelscope.outputs import OutputKeys
  9. from modelscope.pipelines import pipeline
  10. from modelscope.utils.constant import Tasks
  11. from modelscope.utils.cv.image_utils import created_boxed_image
  12. from modelscope.utils.demo_utils import DemoCompatibilityCheck
  13. from modelscope.utils.test_utils import test_level
  14. class OfaTasksTest(unittest.TestCase, DemoCompatibilityCheck):
  15. def setUp(self) -> None:
  16. self.output_dir = 'unittest_output'
  17. os.makedirs(self.output_dir, exist_ok=True)
  18. def save_img(self, image_in, box, image_out):
  19. cv2.imwrite(
  20. osp.join(self.output_dir, image_out),
  21. created_boxed_image(image_in, box))
  22. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  23. def test_run_with_image_captioning_with_model(self):
  24. model = Model.from_pretrained('damo/ofa_image-caption_coco_large_en')
  25. img_captioning = pipeline(
  26. task=Tasks.image_captioning,
  27. model=model,
  28. )
  29. image = 'data/test/images/image_captioning.png'
  30. result = img_captioning(image)
  31. print(result[OutputKeys.CAPTION])
  32. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  33. def test_run_with_image_captioning_with_name(self):
  34. img_captioning = pipeline(
  35. Tasks.image_captioning,
  36. model='damo/ofa_image-caption_coco_large_en')
  37. result = img_captioning('data/test/images/image_captioning.png')
  38. print(result[OutputKeys.CAPTION])
  39. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  40. def test_run_with_ocr_recognize_with_name(self):
  41. ocr_recognize = pipeline(
  42. Tasks.ocr_recognition,
  43. model='damo/ofa_ocr-recognition_scene_base_zh')
  44. result = ocr_recognize('data/test/images/image_ocr_recognition.jpg')
  45. print(result[OutputKeys.TEXT])
  46. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  47. def test_run_with_image_classification_with_model(self):
  48. model = Model.from_pretrained(
  49. 'damo/ofa_image-classification_imagenet_large_en')
  50. ofa_pipe = pipeline(Tasks.image_classification, model=model)
  51. image = 'data/test/images/image_classification.png'
  52. result = ofa_pipe(image)
  53. print(result)
  54. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  55. def test_run_with_image_classification_with_name(self):
  56. ofa_pipe = pipeline(
  57. Tasks.image_classification,
  58. model='damo/ofa_image-classification_imagenet_large_en')
  59. image = 'data/test/images/image_classification.png'
  60. result = ofa_pipe(image)
  61. print(result)
  62. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  63. def test_run_with_summarization_with_model(self):
  64. model = Model.from_pretrained(
  65. 'damo/ofa_summarization_gigaword_large_en')
  66. ofa_pipe = pipeline(Tasks.text_summarization, model=model)
  67. text = 'five-time world champion michelle kwan withdrew' + \
  68. 'from the #### us figure skating championships on wednesday ,' + \
  69. ' but will petition us skating officials for the chance to ' + \
  70. 'compete at the #### turin olympics .'
  71. input = {'text': text}
  72. result = ofa_pipe(input)
  73. print(result)
  74. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  75. def test_run_with_summarization_with_name(self):
  76. ofa_pipe = pipeline(
  77. Tasks.text_summarization,
  78. model='damo/ofa_summarization_gigaword_large_en')
  79. text = 'five-time world champion michelle kwan withdrew' + \
  80. 'from the #### us figure skating championships on wednesday ,' + \
  81. ' but will petition us skating officials for the chance to ' +\
  82. 'compete at the #### turin olympics .'
  83. input = {'text': text}
  84. result = ofa_pipe(input)
  85. print(result)
  86. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  87. def test_run_with_text_classification_with_model(self):
  88. model = Model.from_pretrained(
  89. 'damo/ofa_text-classification_mnli_large_en')
  90. ofa_pipe = pipeline(Tasks.text_classification, model=model)
  91. text = 'One of our number will carry out your instructions minutely.'
  92. text2 = 'A member of my team will execute your orders with immense precision.'
  93. result = ofa_pipe((text, text2))
  94. result = ofa_pipe({'text': text, 'text2': text2})
  95. print(result)
  96. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  97. def test_run_with_text_classification_with_name(self):
  98. ofa_pipe = pipeline(
  99. Tasks.text_classification,
  100. model='damo/ofa_text-classification_mnli_large_en')
  101. text = 'One of our number will carry out your instructions minutely.'
  102. text2 = 'A member of my team will execute your orders with immense precision.'
  103. result = ofa_pipe((text, text2))
  104. print(result)
  105. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  106. def test_run_with_visual_entailment_with_model(self):
  107. model = Model.from_pretrained(
  108. 'damo/ofa_visual-entailment_snli-ve_large_en')
  109. ofa_pipe = pipeline(Tasks.visual_entailment, model=model)
  110. image = 'data/test/images/dogs.jpg'
  111. text = 'there are two birds.'
  112. input = {'image': image, 'text': text}
  113. result = ofa_pipe(input)
  114. print(result)
  115. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  116. def test_run_with_visual_entailment_with_name(self):
  117. ofa_pipe = pipeline(
  118. Tasks.visual_entailment,
  119. model='damo/ofa_visual-entailment_snli-ve_large_en')
  120. image = 'data/test/images/dogs.jpg'
  121. text = 'there are two birds.'
  122. input = {'image': image, 'text': text}
  123. result = ofa_pipe(input)
  124. print(result)
  125. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  126. def test_run_with_visual_grounding_with_model(self):
  127. model = Model.from_pretrained(
  128. 'damo/ofa_visual-grounding_refcoco_large_en')
  129. ofa_pipe = pipeline(Tasks.visual_grounding, model=model)
  130. image = 'data/test/images/visual_grounding.png'
  131. text = 'a blue turtle-like pokemon with round head'
  132. input = {'image': image, 'text': text}
  133. result = ofa_pipe(input)
  134. print(result)
  135. image_name = image.split('/')[-2]
  136. self.save_img(
  137. image,
  138. result[OutputKeys.BOXES][0], # just one box
  139. osp.join('large_en_model_' + image_name + '.png'))
  140. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  141. def test_run_with_visual_grounding_with_name(self):
  142. ofa_pipe = pipeline(
  143. Tasks.visual_grounding,
  144. model='damo/ofa_visual-grounding_refcoco_large_en')
  145. image = 'data/test/images/visual_grounding.png'
  146. text = 'a blue turtle-like pokemon with round head'
  147. input = {'image': image, 'text': text}
  148. result = ofa_pipe(input)
  149. print(result)
  150. image_name = image.split('/')[-2]
  151. self.save_img(image, result[OutputKeys.BOXES][0],
  152. osp.join('large_en_name_' + image_name + '.png'))
  153. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  154. def test_run_with_visual_grounding_zh_with_name(self):
  155. model = 'damo/ofa_visual-grounding_refcoco_large_zh'
  156. ofa_pipe = pipeline(Tasks.visual_grounding, model=model)
  157. image = 'data/test/images/visual_grounding.png'
  158. text = '一个圆头的蓝色宝可梦'
  159. input = {'image': image, 'text': text}
  160. result = ofa_pipe(input)
  161. print(result)
  162. image_name = image.split('/')[-1]
  163. self.save_img(image, result[OutputKeys.BOXES][0],
  164. osp.join('large_zh_name_' + image_name))
  165. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  166. def test_run_with_visual_question_answering_with_model(self):
  167. model = Model.from_pretrained(
  168. 'damo/ofa_visual-question-answering_pretrain_large_en')
  169. ofa_pipe = pipeline(Tasks.visual_question_answering, model=model)
  170. image = 'data/test/images/visual_question_answering.png'
  171. text = 'what is grown on the plant?'
  172. input = {'image': image, 'text': text}
  173. result = ofa_pipe(input)
  174. print(result)
  175. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  176. def test_run_with_visual_question_answering_with_name(self):
  177. model = 'damo/ofa_visual-question-answering_pretrain_large_en'
  178. ofa_pipe = pipeline(Tasks.visual_question_answering, model=model)
  179. image = 'data/test/images/visual_question_answering.png'
  180. text = 'what is grown on the plant?'
  181. input = {'image': image, 'text': text}
  182. result = ofa_pipe(input)
  183. print(result)
  184. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  185. def test_run_with_image_captioning_distilled_with_model(self):
  186. model = Model.from_pretrained(
  187. 'damo/ofa_image-caption_coco_distilled_en')
  188. img_captioning = pipeline(
  189. task=Tasks.image_captioning,
  190. model=model,
  191. )
  192. image_path = 'data/test/images/image_captioning.png'
  193. image = Image.open(image_path)
  194. result = img_captioning(image)
  195. print(result[OutputKeys.CAPTION])
  196. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  197. def test_run_with_visual_entailment_distilled_model_with_name(self):
  198. ofa_pipe = pipeline(
  199. Tasks.visual_entailment,
  200. model='damo/ofa_visual-entailment_snli-ve_distilled_v2_en')
  201. image = 'data/test/images/dogs.jpg'
  202. text = 'there are two birds.'
  203. input = {'image': image, 'text': text}
  204. result = ofa_pipe(input)
  205. print(result)
  206. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  207. def test_run_with_visual_grounding_distilled_model_with_model(self):
  208. model = Model.from_pretrained(
  209. 'damo/ofa_visual-grounding_refcoco_distilled_en')
  210. ofa_pipe = pipeline(Tasks.visual_grounding, model=model)
  211. image = 'data/test/images/visual_grounding.png'
  212. text = 'a blue turtle-like pokemon with round head'
  213. input = {'image': image, 'text': text}
  214. result = ofa_pipe(input)
  215. print(result)
  216. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  217. def test_run_with_text_to_image_synthesis_with_name(self):
  218. model = 'damo/ofa_text-to-image-synthesis_coco_large_en'
  219. ofa_pipe = pipeline(Tasks.text_to_image_synthesis, model=model)
  220. ofa_pipe.model.generator.beam_size = 2
  221. example = {'text': 'a bear in the water.'}
  222. result = ofa_pipe(example)
  223. result[OutputKeys.OUTPUT_IMG].save('result.png')
  224. print(f'Output written to {osp.abspath("result.png")}')
  225. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  226. def test_run_with_text_to_image_synthesis_with_model(self):
  227. model = Model.from_pretrained(
  228. 'damo/ofa_text-to-image-synthesis_coco_large_en')
  229. ofa_pipe = pipeline(Tasks.text_to_image_synthesis, model=model)
  230. ofa_pipe.model.generator.beam_size = 2
  231. example = {'text': 'a bear in the water.'}
  232. result = ofa_pipe(example)
  233. result[OutputKeys.OUTPUT_IMG].save('result.png')
  234. print(f'Output written to {osp.abspath("result.png")}')
  235. @unittest.skip('demo compatibility test is only enabled on a needed-basis')
  236. def test_demo_compatibility(self):
  237. self.compatibility_check()
  238. if __name__ == '__main__':
  239. unittest.main()