2022-07-02 15:47:25 +08:00
|
|
|
# Copyright (c) Alibaba, Inc. and its affiliates.
|
|
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
from modelscope.models import Model
|
2022-07-14 16:25:55 +08:00
|
|
|
from modelscope.outputs import OutputKeys
|
2022-07-02 15:47:25 +08:00
|
|
|
from modelscope.pipelines import pipeline
|
|
|
|
|
from modelscope.utils.constant import Tasks
|
|
|
|
|
from modelscope.utils.test_utils import test_level
|
|
|
|
|
|
|
|
|
|
|
2023-05-22 10:53:18 +08:00
|
|
|
class TextToImageSynthesisTest(unittest.TestCase):
|
2022-09-08 14:08:51 +08:00
|
|
|
|
|
|
|
|
def setUp(self) -> None:
|
|
|
|
|
self.task = Tasks.text_to_image_synthesis
|
|
|
|
|
self.model_id = 'damo/cv_diffusion_text-to-image-synthesis_tiny'
|
|
|
|
|
|
2022-07-06 07:22:50 +08:00
|
|
|
test_text = {
|
|
|
|
|
'text': '宇航员',
|
|
|
|
|
'generator_ddim_timesteps': 2,
|
|
|
|
|
'upsampler_256_ddim_timesteps': 2,
|
|
|
|
|
'upsampler_1024_ddim_timesteps': 2,
|
|
|
|
|
'debug': True
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
|
2022-07-02 15:47:25 +08:00
|
|
|
def test_run_with_model_from_modelhub(self):
|
|
|
|
|
model = Model.from_pretrained(self.model_id)
|
|
|
|
|
pipe_line_text_to_image_synthesis = pipeline(
|
|
|
|
|
task=Tasks.text_to_image_synthesis, model=model)
|
|
|
|
|
img = pipe_line_text_to_image_synthesis(
|
2023-01-15 13:30:10 +00:00
|
|
|
self.test_text)[OutputKeys.OUTPUT_IMGS][0]
|
2022-07-02 15:47:25 +08:00
|
|
|
print(np.sum(np.abs(img)))
|
|
|
|
|
|
2022-08-02 22:15:45 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
|
2022-07-02 15:47:25 +08:00
|
|
|
def test_run_with_model_name(self):
|
|
|
|
|
pipe_line_text_to_image_synthesis = pipeline(
|
|
|
|
|
task=Tasks.text_to_image_synthesis, model=self.model_id)
|
|
|
|
|
img = pipe_line_text_to_image_synthesis(
|
2023-01-15 13:30:10 +00:00
|
|
|
self.test_text)[OutputKeys.OUTPUT_IMGS][0]
|
2022-07-02 15:47:25 +08:00
|
|
|
print(np.sum(np.abs(img)))
|
|
|
|
|
|
|
|
|
|
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
|
|
|
|
|
def test_run_with_default_model(self):
|
|
|
|
|
pipe_line_text_to_image_synthesis = pipeline(
|
|
|
|
|
task=Tasks.text_to_image_synthesis)
|
|
|
|
|
img = pipe_line_text_to_image_synthesis(
|
2023-01-15 13:30:10 +00:00
|
|
|
self.test_text)[OutputKeys.OUTPUT_IMGS][0]
|
2022-07-02 15:47:25 +08:00
|
|
|
print(np.sum(np.abs(img)))
|
|
|
|
|
|
2022-11-25 09:42:48 +08:00
|
|
|
@unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
|
|
|
|
|
def test_run_with_model_from_modelhub_dpm_solver(self):
|
2023-01-07 03:25:54 +08:00
|
|
|
self.test_text.update({'solver': 'dpm-solver'})
|
2022-11-25 09:42:48 +08:00
|
|
|
model = Model.from_pretrained(self.model_id)
|
|
|
|
|
pipe_line_text_to_image_synthesis = pipeline(
|
|
|
|
|
task=Tasks.text_to_image_synthesis, model=model)
|
|
|
|
|
img = pipe_line_text_to_image_synthesis(
|
2023-01-15 13:30:10 +00:00
|
|
|
self.test_text)[OutputKeys.OUTPUT_IMGS][0]
|
2022-11-25 09:42:48 +08:00
|
|
|
print(np.sum(np.abs(img)))
|
|
|
|
|
|
2022-07-02 15:47:25 +08:00
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
unittest.main()
|