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.1 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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.test_utils import test_level
  9. class ImageStyleTransferTest(unittest.TestCase):
  10. def setUp(self) -> None:
  11. self.model_id = 'damo/cv_aams_style-transfer_damo'
  12. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  13. def test_run_by_direct_model_download(self):
  14. snapshot_path = snapshot_download(self.model_id)
  15. print('snapshot_path: {}'.format(snapshot_path))
  16. image_style_transfer = pipeline(
  17. Tasks.image_style_transfer, model=snapshot_path)
  18. result = image_style_transfer(
  19. 'data/test/images/style_transfer_content.jpg',
  20. style='data/test/images/style_transfer_style.jpg')
  21. cv2.imwrite('result_styletransfer1.png', result[OutputKeys.OUTPUT_IMG])
  22. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  23. def test_run_modelhub(self):
  24. image_style_transfer = pipeline(
  25. Tasks.image_style_transfer, model=self.model_id)
  26. result = image_style_transfer(
  27. 'data/test/images/style_transfer_content.jpg',
  28. style='data/test/images/style_transfer_style.jpg')
  29. cv2.imwrite('result_styletransfer2.png', result[OutputKeys.OUTPUT_IMG])
  30. print('style_transfer.test_run_modelhub done')
  31. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  32. def test_run_modelhub_default_model(self):
  33. image_style_transfer = pipeline(Tasks.image_style_transfer)
  34. result = image_style_transfer(
  35. 'data/test/images/style_transfer_content.jpg',
  36. style='data/test/images/style_transfer_style.jpg')
  37. cv2.imwrite('result_styletransfer3.png', result[OutputKeys.OUTPUT_IMG])
  38. print('style_transfer.test_run_modelhub_default_model done')
  39. if __name__ == '__main__':
  40. unittest.main()