






'''simple docstring'''

import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
   from PIL import Image
else:







   class 	SCREAMING_SNAKE_CASE	:




          @staticmethod
          def 					a_					(					*A__      : Any	,    **A__      : int					):


               """simple docstring"""







               pass







@is_pipeline_test
@require_vision
@require_torch
class 	SCREAMING_SNAKE_CASE	(      unittest.TestCase  ):
       snake_case__    :     Optional[Any]   		=			MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING




       def 					a_					(					self      : Optional[Any]	,    A__      : Union[str, Any]	,    A__      : Any	,    A__      : Optional[Any]					):


            """simple docstring"""







            __lowerCamelCase						:       List[str]				   =				pipeline(
                """zero-shot-object-detection"""	,    model="""hf-internal-testing/tiny-random-owlvit-object-detection"""					)

            __lowerCamelCase						:       Dict				   =				[
                {
                    """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
                    """candidate_labels""": ["""cat""", """remote""", """couch"""],
                }
            ]
            return object_detector, examples




       def 					a_					(					self      : Any	,    A__      : Tuple	,    A__      : Optional[Any]					):


            """simple docstring"""







            __lowerCamelCase						:       List[str]				   =				object_detector(examples[0]	,    threshold=0.0					)

            __lowerCamelCase						:       Any				   =				len(A__					)
            self.assertGreater(A__	,    0					)
            self.assertEqual(
                A__	,    [
                    {
                        """score""": ANY(A__					),
                        """label""": ANY(A__					),
                        """box""": {"""xmin""": ANY(A__					), """ymin""": ANY(A__					), """xmax""": ANY(A__					), """ymax""": ANY(A__					)},
                    }
                    for i in range(A__					)
                ]	,    )




       @require_tf
       @unittest.skip("""Zero Shot Object Detection not implemented in TF"""					)
       def 					a_					(					self      : Any					):


            """simple docstring"""







            pass




       @require_torch
       def 					a_					(					self      : List[str]					):


            """simple docstring"""







            __lowerCamelCase						:       Dict				   =				pipeline(
                """zero-shot-object-detection"""	,    model="""hf-internal-testing/tiny-random-owlvit-object-detection"""					)

            __lowerCamelCase						:       Dict				   =				object_detector(
                """./tests/fixtures/tests_samples/COCO/000000039769.png"""	,    candidate_labels=["""cat""", """remote""", """couch"""]	,    threshold=0.64	,    )

            self.assertEqual(
                nested_simplify(A__	,    decimals=4					)	,    [
                    {"""score""": 0.7235, """label""": """cat""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}},
                    {"""score""": 0.7218, """label""": """remote""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}},
                    {"""score""": 0.7184, """label""": """couch""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}},
                    {"""score""": 0.6748, """label""": """remote""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}},
                    {"""score""": 0.6656, """label""": """cat""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}},
                    {"""score""": 0.6614, """label""": """couch""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}},
                    {"""score""": 0.6456, """label""": """remote""", """box""": {"""xmin""": 494, """ymin""": 105, """xmax""": 521, """ymax""": 127}},
                    {"""score""": 0.642, """label""": """remote""", """box""": {"""xmin""": 67, """ymin""": 274, """xmax""": 93, """ymax""": 297}},
                    {"""score""": 0.6419, """label""": """cat""", """box""": {"""xmin""": 494, """ymin""": 105, """xmax""": 521, """ymax""": 127}},
                ]	,    )

            __lowerCamelCase						:       List[str]				   =				object_detector(
                [
                    {
                        """image""": """./tests/fixtures/tests_samples/COCO/000000039769.png""",
                        """candidate_labels""": ["""cat""", """remote""", """couch"""],
                    }
                ]	,    threshold=0.64	,    )

            self.assertEqual(
                nested_simplify(A__	,    decimals=4					)	,    [
                    [
                        {"""score""": 0.7235, """label""": """cat""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}},
                        {"""score""": 0.7218, """label""": """remote""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}},
                        {"""score""": 0.7184, """label""": """couch""", """box""": {"""xmin""": 204, """ymin""": 167, """xmax""": 232, """ymax""": 190}},
                        {"""score""": 0.6748, """label""": """remote""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}},
                        {"""score""": 0.6656, """label""": """cat""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}},
                        {"""score""": 0.6614, """label""": """couch""", """box""": {"""xmin""": 571, """ymin""": 83, """xmax""": 598, """ymax""": 103}},
                        {"""score""": 0.6456, """label""": """remote""", """box""": {"""xmin""": 494, """ymin""": 105, """xmax""": 521, """ymax""": 127}},
                        {"""score""": 0.642, """label""": """remote""", """box""": {"""xmin""": 67, """ymin""": 274, """xmax""": 93, """ymax""": 297}},
                        {"""score""": 0.6419, """label""": """cat""", """box""": {"""xmin""": 494, """ymin""": 105, """xmax""": 521, """ymax""": 127}},
                    ]
                ]	,    )




       @require_torch
       @slow
       def 					a_					(					self      : Union[str, Any]					):


            """simple docstring"""







            __lowerCamelCase						:       Tuple				   =				pipeline("""zero-shot-object-detection"""					)

            __lowerCamelCase						:       Any				   =				object_detector(
                """http://images.cocodataset.org/val2017/000000039769.jpg"""	,    candidate_labels=["""cat""", """remote""", """couch"""]	,    )
            self.assertEqual(
                nested_simplify(A__	,    decimals=4					)	,    [
                    {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 324, """ymin""": 20, """xmax""": 640, """ymax""": 373}},
                    {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 177, """ymax""": 115}},
                    {"""score""": 0.2537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 315, """ymax""": 472}},
                    {"""score""": 0.1474, """label""": """remote""", """box""": {"""xmin""": 335, """ymin""": 74, """xmax""": 371, """ymax""": 187}},
                    {"""score""": 0.1208, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 642, """ymax""": 476}},
                ]	,    )

            __lowerCamelCase						:       Tuple				   =				object_detector(
                [
                    {
                        """image""": """http://images.cocodataset.org/val2017/000000039769.jpg""",
                        """candidate_labels""": ["""cat""", """remote""", """couch"""],
                    },
                    {
                        """image""": """http://images.cocodataset.org/val2017/000000039769.jpg""",
                        """candidate_labels""": ["""cat""", """remote""", """couch"""],
                    },
                ]	,    )
            self.assertEqual(
                nested_simplify(A__	,    decimals=4					)	,    [
                    [
                        {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 324, """ymin""": 20, """xmax""": 640, """ymax""": 373}},
                        {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 177, """ymax""": 115}},
                        {"""score""": 0.2537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 315, """ymax""": 472}},
                        {"""score""": 0.1474, """label""": """remote""", """box""": {"""xmin""": 335, """ymin""": 74, """xmax""": 371, """ymax""": 187}},
                        {"""score""": 0.1208, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 642, """ymax""": 476}},
                    ],
                    [
                        {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 324, """ymin""": 20, """xmax""": 640, """ymax""": 373}},
                        {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 177, """ymax""": 115}},
                        {"""score""": 0.2537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 315, """ymax""": 472}},
                        {"""score""": 0.1474, """label""": """remote""", """box""": {"""xmin""": 335, """ymin""": 74, """xmax""": 371, """ymax""": 187}},
                        {"""score""": 0.1208, """label""": """couch""", """box""": {"""xmin""": 4, """ymin""": 0, """xmax""": 642, """ymax""": 476}},
                    ],
                ]	,    )




       @require_tf
       @unittest.skip("""Zero Shot Object Detection not implemented in TF"""					)
       def 					a_					(					self      : Tuple					):


            """simple docstring"""







            pass




       @require_torch
       @slow
       def 					a_					(					self      : Optional[Any]					):


            """simple docstring"""







            __lowerCamelCase						:       Optional[Any]				   =				0.2
            __lowerCamelCase						:       Any				   =				pipeline("""zero-shot-object-detection"""					)

            __lowerCamelCase						:       Optional[int]				   =				object_detector(
                """http://images.cocodataset.org/val2017/000000039769.jpg"""	,    candidate_labels=["""cat""", """remote""", """couch"""]	,    threshold=A__	,    )
            self.assertEqual(
                nested_simplify(A__	,    decimals=4					)	,    [
                    {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 324, """ymin""": 20, """xmax""": 640, """ymax""": 373}},
                    {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 177, """ymax""": 115}},
                    {"""score""": 0.2537, """label""": """cat""", """box""": {"""xmin""": 1, """ymin""": 55, """xmax""": 315, """ymax""": 472}},
                ]	,    )




       @require_torch
       @slow
       def 					a_					(					self      : Union[str, Any]					):


            """simple docstring"""







            __lowerCamelCase						:       Union[str, Any]				   =				2
            __lowerCamelCase						:       Dict				   =				pipeline("""zero-shot-object-detection"""					)

            __lowerCamelCase						:       Any				   =				object_detector(
                """http://images.cocodataset.org/val2017/000000039769.jpg"""	,    candidate_labels=["""cat""", """remote""", """couch"""]	,    top_k=A__	,    )
            self.assertEqual(
                nested_simplify(A__	,    decimals=4					)	,    [
                    {"""score""": 0.2868, """label""": """cat""", """box""": {"""xmin""": 324, """ymin""": 20, """xmax""": 640, """ymax""": 373}},
                    {"""score""": 0.277, """label""": """remote""", """box""": {"""xmin""": 40, """ymin""": 72, """xmax""": 177, """ymax""": 115}},
                ]	,    )











'''simple docstring'''

import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()







@slow
@require_torch_gpu
class 	SCREAMING_SNAKE_CASE	(      unittest.TestCase  ):




       def 					a_					(					self      : Union[str, Any]					):


            """simple docstring"""







            super().tearDown()
            gc.collect()
            torch.cuda.empty_cache()




       def 					a_					(					self      : Optional[int]					):


            """simple docstring"""







            __lowerCamelCase						:       Dict				   =				StableDiffusionKDiffusionPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4"""					)
            __lowerCamelCase						:       Tuple				   =				sd_pipe.to(A__					)
            sd_pipe.set_progress_bar_config(disable=A__					)

            sd_pipe.set_scheduler("""sample_euler"""					)

            __lowerCamelCase						:       Any				   =				"""A painting of a squirrel eating a burger"""
            __lowerCamelCase						:       List[Any]				   =				torch.manual_seed(0					)
            __lowerCamelCase						:       Dict				   =				sd_pipe([prompt]	,    generator=A__	,    guidance_scale=9.0	,    num_inference_steps=20	,    output_type="""np"""					)

            __lowerCamelCase						:       Tuple				   =				output.images

            __lowerCamelCase						:       int				   =				image[0, -3:, -3:, -1]

            assert image.shape == (1, 512, 512, 3)
            __lowerCamelCase						:       List[str]				   =				np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339]					)

            assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-2




       def 					a_					(					self      : Dict					):


            """simple docstring"""







            __lowerCamelCase						:       Any				   =				StableDiffusionKDiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""					)
            __lowerCamelCase						:       int				   =				sd_pipe.to(A__					)
            sd_pipe.set_progress_bar_config(disable=A__					)

            sd_pipe.set_scheduler("""sample_euler"""					)

            __lowerCamelCase						:       List[Any]				   =				"""A painting of a squirrel eating a burger"""
            __lowerCamelCase						:       Union[str, Any]				   =				torch.manual_seed(0					)
            __lowerCamelCase						:       int				   =				sd_pipe([prompt]	,    generator=A__	,    guidance_scale=9.0	,    num_inference_steps=20	,    output_type="""np"""					)

            __lowerCamelCase						:       List[str]				   =				output.images

            __lowerCamelCase						:       Optional[int]				   =				image[0, -3:, -3:, -1]

            assert image.shape == (1, 512, 512, 3)
            __lowerCamelCase						:       Optional[int]				   =				np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112]					)

            assert np.abs(image_slice.flatten() - expected_slice					).max() < 5e-1




       def 					a_					(					self      : str					):


            """simple docstring"""







            __lowerCamelCase						:       List[str]				   =				StableDiffusionKDiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""					)
            __lowerCamelCase						:       List[str]				   =				sd_pipe.to(A__					)
            sd_pipe.set_progress_bar_config(disable=A__					)

            sd_pipe.set_scheduler("""sample_dpmpp_2m"""					)

            __lowerCamelCase						:       int				   =				"""A painting of a squirrel eating a burger"""
            __lowerCamelCase						:       Tuple				   =				torch.manual_seed(0					)
            __lowerCamelCase						:       Union[str, Any]				   =				sd_pipe(
                [prompt]	,    generator=A__	,    guidance_scale=7.5	,    num_inference_steps=15	,    output_type="""np"""	,    use_karras_sigmas=A__	,    )

            __lowerCamelCase						:       int				   =				output.images

            __lowerCamelCase						:       Any				   =				image[0, -3:, -3:, -1]

            assert image.shape == (1, 512, 512, 3)
            __lowerCamelCase						:       int				   =				np.array(
                [0.1138_1689, 0.1211_2921, 0.138_9457, 0.1254_9606, 0.124_4964, 0.1083_1517, 0.1156_2866, 0.1086_7816, 0.1049_9048]					)

            assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-2



