






'''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       __lowercase					(unittest.TestCase    ):

	def      __UpperCamelCase							(      self   :						Optional[Any]):
			# clean up the VRAM after each test
			super().tearDown()
			gc.collect()
			torch.cuda.empty_cache()

	def      __UpperCamelCase							(      self   :						Tuple):
			UpperCamelCase__				:					Optional[int]					=					StableDiffusionKDiffusionPipeline.from_pretrained('CompVis/stable-diffusion-v1-4')
			UpperCamelCase__				:					Optional[int]					=					sd_pipe.to(UpperCAmelCase_)
			sd_pipe.set_progress_bar_config(disable=UpperCAmelCase_)

			sd_pipe.set_scheduler('sample_euler')

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

			UpperCamelCase__				:					Dict					=					output.images

			UpperCamelCase__				:					Union[str, Any]					=					image[0, -3:, -3:, -1]

			assert image.shape == (1, 512, 512, 3)
			UpperCamelCase__				:					List[str]					=					np.array([0.04_47, 0.04_92, 0.04_68, 0.04_08, 0.03_83, 0.04_08, 0.03_54, 0.03_80, 0.03_39])

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

	def      __UpperCamelCase							(      self   :						str):
			UpperCamelCase__				:					Union[str, Any]					=					StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base')
			UpperCamelCase__				:					Dict					=					sd_pipe.to(UpperCAmelCase_)
			sd_pipe.set_progress_bar_config(disable=UpperCAmelCase_)

			sd_pipe.set_scheduler('sample_euler')

			UpperCamelCase__				:					Tuple					=					'A painting of a squirrel eating a burger'
			UpperCamelCase__				:					Union[str, Any]					=					torch.manual_seed(0)
			UpperCamelCase__				:					Any					=					sd_pipe([prompt]  ,					generator=UpperCAmelCase_  ,					guidance_scale=9.0  ,					num_inference_steps=20  ,					output_type='np')

			UpperCamelCase__				:					Optional[int]					=					output.images

			UpperCamelCase__				:					Tuple					=					image[0, -3:, -3:, -1]

			assert image.shape == (1, 512, 512, 3)
			UpperCamelCase__				:					str					=					np.array([0.12_37, 0.13_20, 0.14_38, 0.13_59, 0.13_90, 0.11_32, 0.12_77, 0.11_75, 0.11_12])

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





	def      __UpperCamelCase							(      self   :						Optional[int]):
			UpperCamelCase__				:					Tuple					=					StableDiffusionKDiffusionPipeline.from_pretrained('stabilityai/stable-diffusion-2-1-base')
			UpperCamelCase__				:					Tuple					=					sd_pipe.to(UpperCAmelCase_)
			sd_pipe.set_progress_bar_config(disable=UpperCAmelCase_)

			sd_pipe.set_scheduler('sample_dpmpp_2m')

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

			UpperCamelCase__				:					List[Any]					=					output.images

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

			assert image.shape == (1, 512, 512, 3)
			UpperCamelCase__				:					Union[str, Any]					=					np.array(
			    [0.11_38_16_89, 0.12_11_29_21, 0.1_38_94_57, 0.12_54_96_06, 0.1_24_49_64, 0.10_83_15_17, 0.11_56_28_66, 0.10_86_78_16, 0.10_49_90_48])

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












'''simple docstring'''




import numpy as np
from PIL import Image







def       __UpperCAmelCase						(  lowerCamelCase_     ,      lowerCamelCase_     ,      lowerCamelCase_)							->     np.ndarray:
		UpperCamelCase__				:					List[Any]					=					np.array(lowerCamelCase_)
		if arr.shape[0] != arr.shape[1]:
				raise ValueError('The input array is not a square matrix')
		UpperCamelCase__				:					Tuple					=					0
		UpperCamelCase__				:					int					=					0
		UpperCamelCase__				:					Optional[int]					=					0
		UpperCamelCase__				:					str					=					0

		# compute the shape of the output matrix
		UpperCamelCase__				:					int					=					(arr.shape[0] - size) // stride + 1
		# initialize the output matrix with zeros of shape maxpool_shape
		UpperCamelCase__				:					Dict					=					np.zeros((maxpool_shape, maxpool_shape))

		while i < arr.shape[0]:
				if i + size > arr.shape[0]:
						# if the end of the matrix is reached, break
						break
				while j < arr.shape[1]:
						# if the end of the matrix is reached, break
						if j + size > arr.shape[1]:
								break
						# compute the maximum of the pooling matrix
						UpperCamelCase__				:					Dict					=					np.max(arr[i : i + size, j : j + size])
						# shift the pooling matrix by stride of column pixels
						j += stride
						mat_j += 1

				# shift the pooling matrix by stride of row pixels
				i += stride
				mat_i += 1

				# reset the column index to 0
				UpperCamelCase__				:					List[Any]					=					0
				UpperCamelCase__				:					Optional[int]					=					0

		return updated_arr







def       __UpperCAmelCase						(  lowerCamelCase_     ,      lowerCamelCase_     ,      lowerCamelCase_)							->     np.ndarray:
		UpperCamelCase__				:					Tuple					=					np.array(lowerCamelCase_)
		if arr.shape[0] != arr.shape[1]:
				raise ValueError('The input array is not a square matrix')
		UpperCamelCase__				:					Optional[int]					=					0
		UpperCamelCase__				:					int					=					0
		UpperCamelCase__				:					List[str]					=					0
		UpperCamelCase__				:					List[Any]					=					0

		# compute the shape of the output matrix
		UpperCamelCase__				:					str					=					(arr.shape[0] - size) // stride + 1
		# initialize the output matrix with zeros of shape avgpool_shape
		UpperCamelCase__				:					Union[str, Any]					=					np.zeros((avgpool_shape, avgpool_shape))

		while i < arr.shape[0]:
				# if the end of the matrix is reached, break
				if i + size > arr.shape[0]:
						break
				while j < arr.shape[1]:
						# if the end of the matrix is reached, break
						if j + size > arr.shape[1]:
								break
						# compute the average of the pooling matrix
						UpperCamelCase__				:					List[Any]					=					int(np.average(arr[i : i + size, j : j + size]))
						# shift the pooling matrix by stride of column pixels
						j += stride
						mat_j += 1

				# shift the pooling matrix by stride of row pixels
				i += stride
				mat_i += 1
				# reset the column index to 0
				UpperCamelCase__				:					Union[str, Any]					=					0
				UpperCamelCase__				:					Optional[Any]					=					0

		return updated_arr


# Main Function
if __name__ == "__main__":
		from doctest import testmod

		testmod(name='avgpooling', verbose=True)

		# Loading the image
		lowerCAmelCase__          =						Image.open('path_to_image')

		# Converting the image to numpy array and maxpooling, displaying the result
		# Ensure that the image is a square matrix

		Image.fromarray(maxpooling(np.array(image), size=3, stride=2)).show()

		# Converting the image to numpy array and averagepooling, displaying the result
		# Ensure that the image is a square matrix

		Image.fromarray(avgpooling(np.array(image), size=3, stride=2)).show()




