|
| 1 | +"""Tests require GPU, so they will not be running on CI (unless someone |
| 2 | +wants to figure that out for me). |
| 3 | +
|
| 4 | +We'll run these locally before pushing to the repo, or at the very least |
| 5 | +before making a release. |
| 6 | +""" |
| 7 | + |
| 8 | +from stable_diffusion_videos import NoCheck, StableDiffusionWalkPipeline |
| 9 | +import torch |
| 10 | +from pathlib import Path |
| 11 | +from shutil import rmtree |
| 12 | + |
| 13 | +import pytest |
| 14 | + |
| 15 | + |
| 16 | +TEST_OUTPUT_ROOT = "test_outputs" |
| 17 | +SAMPLES_DIR = Path(__file__).parent / "samples" |
| 18 | + |
| 19 | +@pytest.fixture |
| 20 | +def pipeline(scope="session"): |
| 21 | + pipe = StableDiffusionWalkPipeline.from_pretrained( |
| 22 | + "CompVis/stable-diffusion-v1-4", |
| 23 | + use_auth_token=True, |
| 24 | + torch_dtype=torch.float16, |
| 25 | + revision="fp16", |
| 26 | + ).to('cuda') |
| 27 | + pipe.safety_checker = NoCheck().cuda() |
| 28 | + return pipe |
| 29 | + |
| 30 | + |
| 31 | +@pytest.fixture |
| 32 | +def run_name(request): |
| 33 | + fn_name = request.node.name.lstrip('test_') |
| 34 | + output_path = Path(TEST_OUTPUT_ROOT) / fn_name |
| 35 | + if output_path.exists(): |
| 36 | + rmtree(output_path) |
| 37 | + # We could instead yield here and rm the dir after its written. |
| 38 | + # However, I like being able to view the files locally to see if they look right. |
| 39 | + return fn_name |
| 40 | + |
| 41 | + |
| 42 | +def test_walk_basic(pipeline, run_name): |
| 43 | + video_path = pipeline.walk( |
| 44 | + ['a cat', 'a dog', 'a horse'], |
| 45 | + seeds=[42, 1337, 2022], |
| 46 | + num_interpolation_steps=[3, 3], |
| 47 | + output_dir=TEST_OUTPUT_ROOT, |
| 48 | + name=run_name, |
| 49 | + fps=3, |
| 50 | + ) |
| 51 | + assert Path(video_path).exists(), "Video file was not created" |
| 52 | + |
| 53 | + |
| 54 | +def test_walk_with_audio(pipeline, run_name): |
| 55 | + fps = 6 |
| 56 | + audio_offsets = [2, 4, 5, 8] |
| 57 | + num_interpolation_steps = [(b - a) * fps for a, b in zip(audio_offsets, audio_offsets[1:])] |
| 58 | + video_path = pipeline.walk( |
| 59 | + ['a cat', 'a dog', 'a horse', 'a cow'], |
| 60 | + seeds=[42, 1337, 4321, 1234], |
| 61 | + num_interpolation_steps=num_interpolation_steps, |
| 62 | + output_dir=TEST_OUTPUT_ROOT, |
| 63 | + name=run_name, |
| 64 | + fps=fps, |
| 65 | + audio_filepath=str(Path(SAMPLES_DIR) / 'choice.wav'), |
| 66 | + audio_start_sec=audio_offsets[0], |
| 67 | + batch_size=16, |
| 68 | + ) |
| 69 | + assert Path(video_path).exists(), "Video file was not created" |
| 70 | + |
| 71 | + |
| 72 | +def test_walk_with_upsampler(pipeline, run_name): |
| 73 | + video_path = pipeline.walk( |
| 74 | + ['a cat', 'a dog', 'a horse'], |
| 75 | + seeds=[42, 1337, 2022], |
| 76 | + num_interpolation_steps=[3, 3], |
| 77 | + output_dir=TEST_OUTPUT_ROOT, |
| 78 | + name=run_name, |
| 79 | + fps=3, |
| 80 | + upsample=True, |
| 81 | + ) |
| 82 | + assert Path(video_path).exists(), "Video file was not created" |
0 commit comments