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_style_transfer.py 2.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import os.path as osp
  3. import tempfile
  4. import unittest
  5. import cv2
  6. from modelscope.fileio import File
  7. from modelscope.hub.snapshot_download import snapshot_download
  8. from modelscope.outputs import OutputKeys
  9. from modelscope.pipelines import pipeline
  10. from modelscope.pipelines.base import Pipeline
  11. from modelscope.utils.constant import ModelFile, Tasks
  12. from modelscope.utils.test_utils import test_level
  13. class StyleTransferTest(unittest.TestCase):
  14. def setUp(self) -> None:
  15. self.model_id = 'damo/cv_aams_style-transfer_damo'
  16. @unittest.skip('deprecated, download model from model hub instead')
  17. def test_run_by_direct_model_download(self):
  18. snapshot_path = snapshot_download(self.model_id)
  19. print('snapshot_path: {}'.format(snapshot_path))
  20. style_transfer = pipeline(Tasks.style_transfer, model=snapshot_path)
  21. result = style_transfer(
  22. '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() >= 1, 'skip test in current test level')
  26. def test_run_modelhub(self):
  27. style_transfer = pipeline(Tasks.style_transfer, model=self.model_id)
  28. result = style_transfer(
  29. 'data/test/images/style_transfer_content.jpg',
  30. style='data/test/images/style_transfer_style.jpg')
  31. cv2.imwrite('result_styletransfer2.png', result[OutputKeys.OUTPUT_IMG])
  32. print('style_transfer.test_run_modelhub done')
  33. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  34. def test_run_modelhub_default_model(self):
  35. style_transfer = pipeline(Tasks.style_transfer)
  36. result = style_transfer(
  37. 'data/test/images/style_transfer_content.jpg',
  38. style='data/test/images/style_transfer_style.jpg')
  39. cv2.imwrite('result_styletransfer3.png', result[OutputKeys.OUTPUT_IMG])
  40. print('style_transfer.test_run_modelhub_default_model done')
  41. if __name__ == '__main__':
  42. unittest.main()