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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 ImageStyleTransferTest(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. image_style_transfer = pipeline(
  21. Tasks.image_style_transfer, model=snapshot_path)
  22. result = image_style_transfer(
  23. 'data/test/images/style_transfer_content.jpg',
  24. style='data/test/images/style_transfer_style.jpg')
  25. cv2.imwrite('result_styletransfer1.png', result[OutputKeys.OUTPUT_IMG])
  26. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  27. def test_run_modelhub(self):
  28. image_style_transfer = pipeline(
  29. Tasks.image_style_transfer, model=self.model_id)
  30. result = image_style_transfer(
  31. '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. 'data/test/images/style_transfer_content.jpg',
  40. style='data/test/images/style_transfer_style.jpg')
  41. cv2.imwrite('result_styletransfer3.png', result[OutputKeys.OUTPUT_IMG])
  42. print('style_transfer.test_run_modelhub_default_model done')
  43. if __name__ == '__main__':
  44. unittest.main()