test_repaint.py 2.19 KB
Newer Older
Revist's avatar
Revist committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# coding=utf-8
# Copyright 2022 HuggingFace Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import numpy as np
import torch

from diffusers import RePaintPipeline, RePaintScheduler, UNet2DModel
22
from diffusers.utils.testing_utils import load_image, load_numpy, require_torch_gpu, slow, torch_device
Revist's avatar
Revist committed
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38


torch.backends.cuda.matmul.allow_tf32 = False


@slow
@require_torch_gpu
class RepaintPipelineIntegrationTests(unittest.TestCase):
    def test_celebahq(self):
        original_image = load_image(
            "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/"
            "repaint/celeba_hq_256.png"
        )
        mask_image = load_image(
            "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/repaint/mask_256.png"
        )
39
        expected_image = load_numpy(
Revist's avatar
Revist committed
40
            "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/"
41
            "repaint/celeba_hq_256_result.npy"
Revist's avatar
Revist committed
42
43
44
45
        )

        model_id = "google/ddpm-ema-celebahq-256"
        unet = UNet2DModel.from_pretrained(model_id)
46
        scheduler = RePaintScheduler.from_pretrained(model_id)
Revist's avatar
Revist committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64

        repaint = RePaintPipeline(unet=unet, scheduler=scheduler).to(torch_device)

        generator = torch.Generator(device=torch_device).manual_seed(0)
        output = repaint(
            original_image,
            mask_image,
            num_inference_steps=250,
            eta=0.0,
            jump_length=10,
            jump_n_sample=10,
            generator=generator,
            output_type="np",
        )
        image = output.images[0]

        assert image.shape == (256, 256, 3)
        assert np.abs(expected_image - image).mean() < 1e-2