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_image_style_transfer.py 2.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import unittest
  3. import cv2
  4. from modelscope.hub.snapshot_download import snapshot_download
  5. from modelscope.outputs import OutputKeys
  6. from modelscope.pipelines import pipeline
  7. from modelscope.utils.constant import Tasks
  8. from modelscope.utils.demo_utils import DemoCompatibilityCheck
  9. from modelscope.utils.test_utils import test_level
  10. class ImageStyleTransferTest(unittest.TestCase, DemoCompatibilityCheck):
  11. def setUp(self) -> None:
  12. self.task = Tasks.image_style_transfer
  13. self.model_id = 'damo/cv_aams_style-transfer_damo'
  14. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  15. def test_run_by_direct_model_download(self):
  16. snapshot_path = snapshot_download(self.model_id)
  17. print('snapshot_path: {}'.format(snapshot_path))
  18. image_style_transfer = pipeline(
  19. Tasks.image_style_transfer, model=snapshot_path)
  20. result = image_style_transfer(
  21. dict(
  22. content='data/test/images/style_transfer_content.jpg',
  23. style='data/test/images/style_transfer_style.jpg'))
  24. cv2.imwrite('result_styletransfer1.png', result[OutputKeys.OUTPUT_IMG])
  25. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  26. def test_run_modelhub(self):
  27. image_style_transfer = pipeline(
  28. Tasks.image_style_transfer, model=self.model_id)
  29. result = image_style_transfer(
  30. dict(
  31. content='data/test/images/style_transfer_content.jpg',
  32. style='data/test/images/style_transfer_style.jpg'))
  33. cv2.imwrite('result_styletransfer2.png', result[OutputKeys.OUTPUT_IMG])
  34. print('style_transfer.test_run_modelhub done')
  35. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  36. def test_run_modelhub_default_model(self):
  37. image_style_transfer = pipeline(Tasks.image_style_transfer)
  38. result = image_style_transfer(
  39. dict(
  40. content='data/test/images/style_transfer_content.jpg',
  41. style='data/test/images/style_transfer_style.jpg'))
  42. cv2.imwrite('result_styletransfer3.png', result[OutputKeys.OUTPUT_IMG])
  43. print('style_transfer.test_run_modelhub_default_model done')
  44. @unittest.skip('demo compatibility test is only enabled on a needed-basis')
  45. def test_demo_compatibility(self):
  46. self.compatibility_check()
  47. if __name__ == '__main__':
  48. unittest.main()