2022-07-20 11:40:54 +08:00
|
|
|
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
import cv2
|
|
|
|
|
|
|
|
|
|
from modelscope.hub.snapshot_download import snapshot_download
|
|
|
|
|
from modelscope.outputs import OutputKeys
|
|
|
|
|
from modelscope.pipelines import pipeline
|
2022-08-06 12:22:17 +08:00
|
|
|
from modelscope.utils.constant import Tasks
|
2022-07-20 11:40:54 +08:00
|
|
|
from modelscope.utils.test_utils import test_level
|
|
|
|
|
|
|
|
|
|
|
2023-05-22 10:53:18 +08:00
|
|
|
class ImageStyleTransferTest(unittest.TestCase):
|
2022-07-20 11:40:54 +08:00
|
|
|
|
|
|
|
|
def setUp(self) -> None:
|
2022-09-08 14:08:51 +08:00
|
|
|
self.task = Tasks.image_style_transfer
|
2022-07-20 11:40:54 +08:00
|
|
|
self.model_id = 'damo/cv_aams_style-transfer_damo'
|
|
|
|
|
|
2022-08-23 14:27:19 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
|
2022-07-20 11:40:54 +08:00
|
|
|
def test_run_by_direct_model_download(self):
|
|
|
|
|
snapshot_path = snapshot_download(self.model_id)
|
|
|
|
|
print('snapshot_path: {}'.format(snapshot_path))
|
2022-08-02 11:01:09 +08:00
|
|
|
image_style_transfer = pipeline(
|
|
|
|
|
Tasks.image_style_transfer, model=snapshot_path)
|
2022-07-20 11:40:54 +08:00
|
|
|
|
2022-08-02 11:01:09 +08:00
|
|
|
result = image_style_transfer(
|
2022-10-17 10:40:08 +08:00
|
|
|
dict(
|
|
|
|
|
content='data/test/images/style_transfer_content.jpg',
|
|
|
|
|
style='data/test/images/style_transfer_style.jpg'))
|
2022-07-20 11:40:54 +08:00
|
|
|
cv2.imwrite('result_styletransfer1.png', result[OutputKeys.OUTPUT_IMG])
|
|
|
|
|
|
2022-08-06 12:22:17 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
2022-07-20 11:40:54 +08:00
|
|
|
def test_run_modelhub(self):
|
2022-08-02 11:01:09 +08:00
|
|
|
image_style_transfer = pipeline(
|
|
|
|
|
Tasks.image_style_transfer, model=self.model_id)
|
2022-07-20 11:40:54 +08:00
|
|
|
|
2022-08-02 11:01:09 +08:00
|
|
|
result = image_style_transfer(
|
2022-10-17 10:40:08 +08:00
|
|
|
dict(
|
|
|
|
|
content='data/test/images/style_transfer_content.jpg',
|
|
|
|
|
style='data/test/images/style_transfer_style.jpg'))
|
2022-07-20 11:40:54 +08:00
|
|
|
cv2.imwrite('result_styletransfer2.png', result[OutputKeys.OUTPUT_IMG])
|
|
|
|
|
print('style_transfer.test_run_modelhub done')
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
|
|
|
|
|
def test_run_modelhub_default_model(self):
|
2022-08-02 11:01:09 +08:00
|
|
|
image_style_transfer = pipeline(Tasks.image_style_transfer)
|
2022-07-20 11:40:54 +08:00
|
|
|
|
2022-08-02 11:01:09 +08:00
|
|
|
result = image_style_transfer(
|
2022-10-17 10:40:08 +08:00
|
|
|
dict(
|
|
|
|
|
content='data/test/images/style_transfer_content.jpg',
|
|
|
|
|
style='data/test/images/style_transfer_style.jpg'))
|
2022-07-20 11:40:54 +08:00
|
|
|
cv2.imwrite('result_styletransfer3.png', result[OutputKeys.OUTPUT_IMG])
|
|
|
|
|
print('style_transfer.test_run_modelhub_default_model done')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|