2022-09-14 16:37:45 +00:00
|
|
|
import pytest
|
|
|
|
|
2022-09-16 06:06:59 +00:00
|
|
|
from imaginairy import LazyLoadingImage
|
2022-09-13 07:27:53 +00:00
|
|
|
from imaginairy.api import imagine, imagine_image_files
|
2022-09-11 06:27:22 +00:00
|
|
|
from imaginairy.schema import ImaginePrompt
|
2022-09-14 16:37:45 +00:00
|
|
|
from imaginairy.utils import get_device
|
2022-09-11 20:58:14 +00:00
|
|
|
|
2022-09-10 05:14:04 +00:00
|
|
|
from . import TESTS_FOLDER
|
|
|
|
|
2022-09-15 02:40:50 +00:00
|
|
|
device_sampler_type_test_cases = {
|
|
|
|
"mps": {
|
|
|
|
("plms", "b4b434ed45919f3505ac2be162791c71"),
|
|
|
|
("ddim", "b369032a025915c0a7ccced165a609b3"),
|
|
|
|
("k_lms", "b87325c189799d646ccd07b331564eb6"),
|
|
|
|
("k_dpm_2", "cb37ca934938466bdbc1dd995da037de"),
|
|
|
|
("k_dpm_2_a", "ef155995ca1638f0ae7db9f573b83767"),
|
|
|
|
("k_euler", "d126da5ca8b08099cde8b5037464e788"),
|
|
|
|
("k_euler_a", "cac5ca2e26c31a544b76a9442eb2ea37"),
|
|
|
|
("k_heun", "0382ef71d9967fefd15676410289ebab"),
|
|
|
|
},
|
|
|
|
"cuda": {
|
|
|
|
("plms", "62e78287e7848e48d45a1b207fb84102"),
|
|
|
|
("ddim", "164c2a008b100e5fa07d3db2018605bd"),
|
|
|
|
("k_lms", "450fea507ccfb44b677d30fae9f40a52"),
|
|
|
|
("k_dpm_2", "901daad7a9e359404d8e3d3f4236c4ce"),
|
|
|
|
("k_dpm_2_a", "855e80286dfdc89752f6bdd3fdeb1a62"),
|
|
|
|
("k_euler", "06df9c19d472bfa6530db98be4ea10e8"),
|
|
|
|
("k_euler_a", "79552628ff77914c8b6870703fe116b5"),
|
|
|
|
("k_heun", "8ced3578ae25d34da9f4e4b1a20bf416"),
|
|
|
|
},
|
2022-09-14 16:37:45 +00:00
|
|
|
}
|
2022-09-15 02:40:50 +00:00
|
|
|
sampler_type_test_cases = device_sampler_type_test_cases[get_device()]
|
2022-09-14 16:37:45 +00:00
|
|
|
|
2022-09-10 05:14:04 +00:00
|
|
|
|
2022-09-15 02:40:50 +00:00
|
|
|
@pytest.mark.parametrize("sampler_type,expected_md5", sampler_type_test_cases)
|
2022-09-14 16:37:45 +00:00
|
|
|
def test_imagine(sampler_type, expected_md5):
|
|
|
|
prompt_text = "a scenic landscape"
|
2022-09-10 07:32:31 +00:00
|
|
|
prompt = ImaginePrompt(
|
2022-09-15 02:40:50 +00:00
|
|
|
prompt_text, width=512, height=256, steps=5, seed=1, sampler_type=sampler_type
|
2022-09-10 07:32:31 +00:00
|
|
|
)
|
2022-09-13 07:27:53 +00:00
|
|
|
result = next(imagine(prompt))
|
2022-09-14 16:37:45 +00:00
|
|
|
result.img.save(
|
|
|
|
f"{TESTS_FOLDER}/test_output/sampler_type_{sampler_type.upper()}.jpg"
|
|
|
|
)
|
|
|
|
assert result.md5() == expected_md5
|
2022-09-10 05:14:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_img_to_img():
|
|
|
|
prompt = ImaginePrompt(
|
|
|
|
"a photo of a beach",
|
|
|
|
init_image=f"{TESTS_FOLDER}/data/beach_at_sainte_adresse.jpg",
|
2022-09-14 16:37:45 +00:00
|
|
|
init_image_strength=0.8,
|
2022-09-10 05:14:04 +00:00
|
|
|
width=512,
|
|
|
|
height=512,
|
2022-09-15 02:40:50 +00:00
|
|
|
steps=5,
|
2022-09-10 05:14:04 +00:00
|
|
|
seed=1,
|
|
|
|
sampler_type="DDIM",
|
|
|
|
)
|
|
|
|
out_folder = f"{TESTS_FOLDER}/test_output"
|
|
|
|
imagine_image_files(prompt, outdir=out_folder)
|
|
|
|
|
|
|
|
|
2022-09-16 06:06:59 +00:00
|
|
|
def test_img_to_img_from_url():
|
|
|
|
prompt = ImaginePrompt(
|
|
|
|
"dogs lying on a hot pink couch",
|
|
|
|
init_image=LazyLoadingImage(
|
|
|
|
url="http://images.cocodataset.org/val2017/000000039769.jpg"
|
|
|
|
),
|
|
|
|
init_image_strength=0.5,
|
|
|
|
width=512,
|
|
|
|
height=512,
|
2022-09-17 05:21:20 +00:00
|
|
|
steps=5,
|
2022-09-16 06:06:59 +00:00
|
|
|
seed=1,
|
|
|
|
sampler_type="DDIM",
|
|
|
|
)
|
|
|
|
out_folder = f"{TESTS_FOLDER}/test_output"
|
|
|
|
imagine_image_files(prompt, outdir=out_folder)
|
|
|
|
|
|
|
|
|
2022-09-10 05:14:04 +00:00
|
|
|
def test_img_to_file():
|
|
|
|
prompt = ImaginePrompt(
|
2022-09-11 06:27:22 +00:00
|
|
|
"an old growth forest, diffuse light poking through the canopy. high-resolution, nature photography, nat geo photo",
|
2022-09-10 07:32:31 +00:00
|
|
|
width=512 + 64,
|
|
|
|
height=512 - 64,
|
2022-09-15 02:40:50 +00:00
|
|
|
steps=5,
|
2022-09-11 06:27:22 +00:00
|
|
|
seed=2,
|
2022-09-10 05:14:04 +00:00
|
|
|
sampler_type="PLMS",
|
2022-09-10 07:32:31 +00:00
|
|
|
upscale=True,
|
2022-09-10 05:14:04 +00:00
|
|
|
)
|
|
|
|
out_folder = f"{TESTS_FOLDER}/test_output"
|
|
|
|
imagine_image_files(prompt, outdir=out_folder)
|