
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
							import torch

if is_vision_available():
							from PIL import Image

							from transformers import BlipImageProcessor







class 							lowercase						(   unittest.TestCase						):




							def __init__(							self  ,A__  ,A__=7  ,A__=3  ,A__=1_8  ,A__=3_0  ,A__=4_0_0  ,A__=True  ,A__=None  ,A__=True  ,A__=False  ,A__=[0.48145466, 0.4578275, 0.40821073]  ,A__=[0.26862954, 0.26130258, 0.27577711]  ,A__=True  ,):
												lowercase		      =		size if size is not None else {'''height''': 2_0, '''width''': 2_0}
												lowercase		      =		parent
												lowercase		      =		batch_size
												lowercase		      =		num_channels
												lowercase		      =		image_size
												lowercase		      =		min_resolution
												lowercase		      =		max_resolution
												lowercase		      =		do_resize
												lowercase		      =		size
												lowercase		      =		do_normalize
												lowercase		      =		image_mean
												lowercase		      =		image_std
												lowercase		      =		do_pad
												lowercase		      =		do_convert_rgb




							def 					A__ (							self):
												return {
												    "do_resize": self.do_resize,
												    "size": self.size,
												    "do_normalize": self.do_normalize,
												    "image_mean": self.image_mean,
												    "image_std": self.image_std,
												    "do_convert_rgb": self.do_convert_rgb,
												    "do_pad": self.do_pad,
												}





							def 					A__ (							self  ,A__=False  ,A__=False  ,A__=False):
												assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

												if equal_resolution:
																	lowercase		      =		[]
																	for i in range(self.batch_size):
																						image_inputs.append(
																						    np.random.randint(
																						        2_5_5  ,size=(self.num_channels, self.max_resolution, self.max_resolution)  ,dtype=np.uinta))
												else:
																	lowercase		      =		[]
																	for i in range(self.batch_size):
																						lowercase      ,		lowercase		      =		np.random.choice(np.arange(self.min_resolution  ,self.max_resolution)  ,2)
																						image_inputs.append(np.random.randint(2_5_5  ,size=(self.num_channels, width, height)  ,dtype=np.uinta))

												if not numpify and not torchify:
																	# PIL expects the channel dimension as last dimension
																	lowercase		      =		[Image.fromarray(np.moveaxis(A__  ,0  ,-1)) for x in image_inputs]

												if torchify:
																	lowercase		      =		[torch.from_numpy(A__) for x in image_inputs]

												return image_inputs







@require_torch
@require_vision
class 							lowercase						(   SCREAMING_SNAKE_CASE__			,    unittest.TestCase						):
							lowercase_			:			Dict			   =BlipImageProcessor if is_vision_available() else None




							def 					A__ (							self):
												lowercase		      =		BlipImageProcessingTester(self)




							@property
							def 					A__ (							self):
												return self.image_processor_tester.prepare_image_processor_dict()




							def 					A__ (							self):
												lowercase		      =		self.image_processing_class(**self.image_processor_dict)
												self.assertTrue(hasattr(A__  ,'''do_resize'''))
												self.assertTrue(hasattr(A__  ,'''size'''))
												self.assertTrue(hasattr(A__  ,'''do_normalize'''))
												self.assertTrue(hasattr(A__  ,'''image_mean'''))
												self.assertTrue(hasattr(A__  ,'''image_std'''))
												self.assertTrue(hasattr(A__  ,'''do_convert_rgb'''))




							def 					A__ (							self):
												pass




							def 					A__ (							self):
												# Initialize image_processor
												lowercase		      =		self.image_processing_class(**self.image_processor_dict)
												# create random PIL images
												lowercase		      =		self.image_processor_tester.prepare_inputs(equal_resolution=A__)
												for image in image_inputs:
																	self.assertIsInstance(A__  ,Image.Image)

												# Test not batched input
												lowercase		      =		image_processor(image_inputs[0]  ,return_tensors='''pt''').pixel_values
												self.assertEqual(
												    encoded_images.shape  ,(
												        1,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size['''height'''],
												        self.image_processor_tester.size['''width'''],
												    )  ,)

												# Test batched
												lowercase		      =		image_processor(A__  ,return_tensors='''pt''').pixel_values
												self.assertEqual(
												    encoded_images.shape  ,(
												        self.image_processor_tester.batch_size,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size['''height'''],
												        self.image_processor_tester.size['''width'''],
												    )  ,)




							def 					A__ (							self):
												# Initialize image_processor
												lowercase		      =		self.image_processing_class(**self.image_processor_dict)
												# create random numpy tensors
												lowercase		      =		self.image_processor_tester.prepare_inputs(equal_resolution=A__  ,numpify=A__)
												for image in image_inputs:
																	self.assertIsInstance(A__  ,np.ndarray)

												# Test not batched input
												lowercase		      =		image_processor(image_inputs[0]  ,return_tensors='''pt''').pixel_values
												self.assertEqual(
												    encoded_images.shape  ,(
												        1,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size['''height'''],
												        self.image_processor_tester.size['''width'''],
												    )  ,)

												# Test batched
												lowercase		      =		image_processor(A__  ,return_tensors='''pt''').pixel_values
												self.assertEqual(
												    encoded_images.shape  ,(
												        self.image_processor_tester.batch_size,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size['''height'''],
												        self.image_processor_tester.size['''width'''],
												    )  ,)





							def 					A__ (							self):
												# Initialize image_processor
												lowercase		      =		self.image_processing_class(**self.image_processor_dict)
												# create random PyTorch tensors
												lowercase		      =		self.image_processor_tester.prepare_inputs(equal_resolution=A__  ,torchify=A__)
												for image in image_inputs:
																	self.assertIsInstance(A__  ,torch.Tensor)

												# Test not batched input
												lowercase		      =		image_processor(image_inputs[0]  ,return_tensors='''pt''').pixel_values
												self.assertEqual(
												    encoded_images.shape  ,(
												        1,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size['''height'''],
												        self.image_processor_tester.size['''width'''],
												    )  ,)

												# Test batched
												lowercase		      =		image_processor(A__  ,return_tensors='''pt''').pixel_values
												self.assertEqual(
												    encoded_images.shape  ,(
												        self.image_processor_tester.batch_size,
												        self.image_processor_tester.num_channels,
												        self.image_processor_tester.size['''height'''],
												        self.image_processor_tester.size['''width'''],
												    )  ,)







@require_torch
@require_vision
class 							lowercase						(   SCREAMING_SNAKE_CASE__			,    unittest.TestCase						):
							lowercase_			:			Tuple			   =BlipImageProcessor if is_vision_available() else None




							def 					A__ (							self):
												lowercase		      =		BlipImageProcessingTester(self  ,num_channels=4)
												lowercase		      =		3




							@property
							def 					A__ (							self):
												return self.image_processor_tester.prepare_image_processor_dict()




							def 					A__ (							self):
												lowercase		      =		self.image_processing_class(**self.image_processor_dict)
												self.assertTrue(hasattr(A__  ,'''do_resize'''))
												self.assertTrue(hasattr(A__  ,'''size'''))
												self.assertTrue(hasattr(A__  ,'''do_normalize'''))
												self.assertTrue(hasattr(A__  ,'''image_mean'''))
												self.assertTrue(hasattr(A__  ,'''image_std'''))
												self.assertTrue(hasattr(A__  ,'''do_convert_rgb'''))




							def 					A__ (							self):
												pass





							def 					A__ (							self):
												# Initialize image_processor
												lowercase		      =		self.image_processing_class(**self.image_processor_dict)
												# create random PIL images
												lowercase		      =		self.image_processor_tester.prepare_inputs(equal_resolution=A__)
												for image in image_inputs:
																	self.assertIsInstance(A__  ,Image.Image)

												# Test not batched input
												lowercase		      =		image_processor(image_inputs[0]  ,return_tensors='''pt''').pixel_values
												self.assertEqual(
												    encoded_images.shape  ,(
												        1,
												        self.expected_encoded_image_num_channels,
												        self.image_processor_tester.size['''height'''],
												        self.image_processor_tester.size['''width'''],
												    )  ,)

												# Test batched
												lowercase		      =		image_processor(A__  ,return_tensors='''pt''').pixel_values
												self.assertEqual(
												    encoded_images.shape  ,(
												        self.image_processor_tester.batch_size,
												        self.expected_encoded_image_num_channels,
												        self.image_processor_tester.size['''height'''],
												        self.image_processor_tester.size['''width'''],
												    )  ,)






import unittest

import torch

from diffusers import DDIMScheduler, TextToVideoZeroPipeline
from diffusers.utils import load_pt, require_torch_gpu, slow

from ..test_pipelines_common import assert_mean_pixel_difference







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




							def 					A__ (							self):
												lowercase		      =		'''runwayml/stable-diffusion-v1-5'''
												lowercase		      =		TextToVideoZeroPipeline.from_pretrained(A__  ,torch_dtype=torch.floataa).to('''cuda''')
												lowercase		      =		DDIMScheduler.from_config(pipe.scheduler.config)
												lowercase		      =		torch.Generator(device='''cuda''').manual_seed(0)

												lowercase		      =		'''A bear is playing a guitar on Times Square'''
												lowercase		      =		pipe(prompt=A__  ,generator=A__).images

												lowercase		      =		load_pt(
												    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text-to-video/A bear is playing a guitar on Times Square.pt''')

												assert_mean_pixel_difference(A__  ,A__)




