




from typing import Dict, List, Optional, Tuple, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_torch_available, is_torch_tensor, logging


if is_torch_available():
		import torch


lowerCamelCase__            = logging.get_logger(__name__)






class 		_lowerCAmelCase       (							__UpperCamelCase	):
			"""simple docstring"""
			lowerCAmelCase__           =['''pixel_values''']
			def __init__(					self     ,		__SCREAMING_SNAKE_CASE = True     ,		__SCREAMING_SNAKE_CASE = None     ,		__SCREAMING_SNAKE_CASE = PILImageResampling.BILINEAR     ,		__SCREAMING_SNAKE_CASE = True     ,		__SCREAMING_SNAKE_CASE = None     ,		__SCREAMING_SNAKE_CASE = True     ,		__SCREAMING_SNAKE_CASE = 1 / 255     ,		__SCREAMING_SNAKE_CASE = True     ,		__SCREAMING_SNAKE_CASE = None     ,		__SCREAMING_SNAKE_CASE = None     ,		**__SCREAMING_SNAKE_CASE     ,		)		->							None:





				"""simple docstring"""







				super().__init__(**snake_case_       )
				snake_case__						:     Dict		=size if size is not None else {'''shortest_edge''': 256}
				snake_case__						:     Tuple		=get_size_dict(snake_case_     ,		default_to_square=snake_case_       )
				snake_case__						:     int		=crop_size if crop_size is not None else {'''height''': 224, '''width''': 224}
				snake_case__						:     Dict		=get_size_dict(snake_case_     ,		param_name='''crop_size'''       )
				snake_case__						:     Tuple		=do_resize
				snake_case__						:     int		=size
				snake_case__						:     Any		=resample
				snake_case__						:     Optional[int]		=do_center_crop
				snake_case__						:     Union[str, Any]		=crop_size
				snake_case__						:     List[str]		=do_rescale
				snake_case__						:     Dict		=rescale_factor
				snake_case__						:     List[Any]		=do_normalize
				snake_case__						:     List[str]		=image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
				snake_case__						:     Optional[Any]		=image_std if image_std is not None else IMAGENET_STANDARD_STD
			def 					UpperCAmelCase     (					self     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE = PILImageResampling.BICUBIC     ,		__SCREAMING_SNAKE_CASE = None     ,		**__SCREAMING_SNAKE_CASE     ,		)		->							np.ndarray:





				"""simple docstring"""







				snake_case__						:     str		=get_size_dict(snake_case_     ,		default_to_square=snake_case_       )
				if "shortest_edge" not in size:
					raise ValueError(f'''The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}'''       )
				snake_case__						:     Tuple		=get_resize_output_image_size(snake_case_     ,		size=size['''shortest_edge''']     ,		default_to_square=snake_case_       )
				return resize(snake_case_     ,		size=snake_case_     ,		resample=snake_case_     ,		data_format=snake_case_     ,		**snake_case_       )
			def 					UpperCAmelCase     (					self     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE = None     ,		**__SCREAMING_SNAKE_CASE     ,		)		->							np.ndarray:





				"""simple docstring"""







				snake_case__						:     List[str]		=get_size_dict(snake_case_       )
				if "height" not in size or "width" not in size:
					raise ValueError(f'''The `size` parameter must contain the keys `height` and `width`. Got {size.keys()}'''       )
				return center_crop(snake_case_     ,		size=(size['''height'''], size['''width'''])     ,		data_format=snake_case_     ,		**snake_case_       )
			def 					UpperCAmelCase     (					self     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE = None     ,		**__SCREAMING_SNAKE_CASE       )		->							np.ndarray:





				"""simple docstring"""







				return rescale(snake_case_     ,		scale=snake_case_     ,		data_format=snake_case_     ,		**snake_case_       )
			def 					UpperCAmelCase     (					self     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE = None     ,		**__SCREAMING_SNAKE_CASE     ,		)		->							np.ndarray:





				"""simple docstring"""







				return normalize(snake_case_     ,		mean=snake_case_     ,		std=snake_case_     ,		data_format=snake_case_     ,		**snake_case_       )
			def 					UpperCAmelCase     (					self     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE = None     ,		__SCREAMING_SNAKE_CASE = None     ,		__SCREAMING_SNAKE_CASE = None     ,		__SCREAMING_SNAKE_CASE = None     ,		__SCREAMING_SNAKE_CASE = None     ,		__SCREAMING_SNAKE_CASE = None     ,		__SCREAMING_SNAKE_CASE = None     ,		__SCREAMING_SNAKE_CASE = None     ,		__SCREAMING_SNAKE_CASE = None     ,		__SCREAMING_SNAKE_CASE = None     ,		__SCREAMING_SNAKE_CASE = None     ,		__SCREAMING_SNAKE_CASE = ChannelDimension.FIRST     ,		**__SCREAMING_SNAKE_CASE     ,		)		->							str:





				"""simple docstring"""







				snake_case__						:     Optional[int]		=do_resize if do_resize is not None else self.do_resize
				snake_case__						:     str		=size if size is not None else self.size
				snake_case__						:     int		=get_size_dict(snake_case_     ,		default_to_square=snake_case_       )
				snake_case__						:     Dict		=resample if resample is not None else self.resample
				snake_case__						:     List[Any]		=do_center_crop if do_center_crop is not None else self.do_center_crop
				snake_case__						:     str		=crop_size if crop_size is not None else self.crop_size
				snake_case__						:     Tuple		=get_size_dict(snake_case_     ,		param_name='''crop_size'''       )
				snake_case__						:     Tuple		=do_rescale if do_rescale is not None else self.do_rescale
				snake_case__						:     List[Any]		=rescale_factor if rescale_factor is not None else self.rescale_factor
				snake_case__						:     Dict		=do_normalize if do_normalize is not None else self.do_normalize
				snake_case__						:     Optional[Any]		=image_mean if image_mean is not None else self.image_mean
				snake_case__						:     Union[str, Any]		=image_std if image_std is not None else self.image_std

				snake_case__						:     Tuple		=make_list_of_images(snake_case_       )

				if not valid_images(snake_case_       ):
					raise ValueError(
					    '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '''
					    '''torch.Tensor, tf.Tensor or jax.ndarray.'''       )

				if do_resize and size is None:
					raise ValueError('''Size must be specified if do_resize is True.'''       )

				if do_center_crop and crop_size is None:
					raise ValueError('''Crop size must be specified if do_center_crop is True.'''       )

				if do_rescale and rescale_factor is None:
					raise ValueError('''Rescale factor must be specified if do_rescale is True.'''       )

				if do_normalize and (image_mean is None or image_std is None):
					raise ValueError('''Image mean and std must be specified if do_normalize is True.'''       )

				# All transformations expect numpy arrays.
				snake_case__						:     Union[str, Any]		=[to_numpy_array(snake_case_       ) for image in images]

				if do_resize:
					snake_case__						:     int		=[self.resize(image=snake_case_     ,		size=snake_case_     ,		resample=snake_case_       ) for image in images]

				if do_center_crop:
					snake_case__						:     Union[str, Any]		=[self.center_crop(image=snake_case_     ,		size=snake_case_       ) for image in images]

				if do_rescale:
					snake_case__						:     str		=[self.rescale(image=snake_case_     ,		scale=snake_case_       ) for image in images]

				if do_normalize:
					snake_case__						:     str		=[self.normalize(image=snake_case_     ,		mean=snake_case_     ,		std=snake_case_       ) for image in images]

				snake_case__						:     int		=[to_channel_dimension_format(snake_case_     ,		snake_case_       ) for image in images]

				snake_case__						:     Union[str, Any]		={'''pixel_values''': images}
				return BatchFeature(data=snake_case_     ,		tensor_type=snake_case_       )


			def 					UpperCAmelCase     (					self     ,		__SCREAMING_SNAKE_CASE     ,		__SCREAMING_SNAKE_CASE = None       )		->							Optional[int]:





				"""simple docstring"""







				snake_case__						:     Tuple		=outputs.logits

				# Resize logits and compute semantic segmentation maps
				if target_sizes is not None:
					if len(snake_case_       ) != len(snake_case_       ):
						raise ValueError(
						    '''Make sure that you pass in as many target sizes as the batch dimension of the logits'''       )

					if is_torch_tensor(snake_case_       ):
						snake_case__						:     int		=target_sizes.numpy()

					snake_case__						:     str		=[]

					for idx in range(len(snake_case_       )       ):
						snake_case__						:     Tuple		=torch.nn.functional.interpolate(
						    logits[idx].unsqueeze(dim=0       )     ,		size=target_sizes[idx]     ,		mode='''bilinear'''     ,		align_corners=snake_case_       )
						snake_case__						:     Optional[Any]		=resized_logits[0].argmax(dim=0       )
						semantic_segmentation.append(snake_case_       )
				else:
					snake_case__						:     List[Any]		=logits.argmax(dim=1       )
					snake_case__						:     Optional[int]		=[semantic_segmentation[i] for i in range(semantic_segmentation.shape[0]       )]

				return semantic_segmentation









from manim import *






class     __a			(  SCREAMING_SNAKE_CASE					):



	def 		UpperCamelCase				(    self				:     Tuple)->				Dict:
						__lowerCAmelCase						 =Rectangle(height=0.5					,						width=0.5)
						__lowerCAmelCase						 =Rectangle(height=0.4_6					,						width=0.4_6).set_stroke(width=0)

						__lowerCAmelCase						 =[mem.copy() for i in range(6)]
						__lowerCAmelCase						 =[mem.copy() for i in range(6)]
						__lowerCAmelCase						 =VGroup(*snake_case_).arrange(snake_case_					,						buff=0)
						__lowerCAmelCase						 =VGroup(*snake_case_).arrange(snake_case_					,						buff=0)
						__lowerCAmelCase						 =VGroup(snake_case_					,						snake_case_).arrange(snake_case_					,						buff=0)
						__lowerCAmelCase						 =Text("""CPU"""					,						font_size=24)
						__lowerCAmelCase						 =Group(snake_case_					,						snake_case_).arrange(snake_case_					,						buff=0.5					,						aligned_edge=snake_case_)
						cpu.move_to([-2.5, -0.5, 0])
						self.add(snake_case_)

						__lowerCAmelCase						 =[mem.copy() for i in range(1)]
						__lowerCAmelCase						 =VGroup(*snake_case_).arrange(snake_case_					,						buff=0)
						__lowerCAmelCase						 =Text("""GPU"""					,						font_size=24)
						__lowerCAmelCase						 =Group(snake_case_					,						snake_case_).arrange(snake_case_					,						buff=0.5					,						aligned_edge=snake_case_)
						gpu.align_to(snake_case_					,						snake_case_)
						gpu.set_x(gpu.get_x() - 1)

						self.add(snake_case_)

						__lowerCAmelCase						 =[mem.copy() for i in range(6)]
						__lowerCAmelCase						 =VGroup(*snake_case_).arrange(snake_case_					,						buff=0)

						__lowerCAmelCase						 =Text("""Model"""					,						font_size=24)
						__lowerCAmelCase						 =Group(snake_case_					,						snake_case_).arrange(snake_case_					,						buff=0.5					,						aligned_edge=snake_case_)
						model.move_to([3, -1.0, 0])

						self.play(
						    Create(snake_case_					,						run_time=1)					,						Create(snake_case_					,						run_time=1)					,						Create(snake_case_					,						run_time=1)					,						)

						__lowerCAmelCase						 =MarkupText(
						    F"""First, an empty model skeleton is loaded\ninto <span fgcolor='{YELLOW}'>memory</span> without using much RAM."""					,						font_size=24					,						)

						__lowerCAmelCase						 =Square(side_length=2.2)
						key.move_to([-5, 2, 0])

						__lowerCAmelCase						 =MarkupText(
						    F"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model"""					,						font_size=18					,						)

						key_text.move_to([-5, 2.4, 0])

						step_a.move_to([2, 2, 0])
						self.play(Write(snake_case_					,						run_time=2.5)					,						Write(snake_case_)					,						Write(snake_case_))

						self.add(snake_case_)

						__lowerCAmelCase						 =[]
						__lowerCAmelCase						 =[]
						__lowerCAmelCase						 =[]
						for i, rect in enumerate(snake_case_):
											__lowerCAmelCase						 =Rectangle(height=0.4_6					,						width=0.4_6).set_stroke(width=0.0).set_fill(snake_case_					,						opacity=0.7)
											cpu_target.move_to(snake_case_)
											cpu_target.generate_target()
											__lowerCAmelCase						 =0.4_6 / 4
											__lowerCAmelCase						 =0.4_6 / 3

											if i == 0:
																cpu_target.target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT)					,						buff=0.0_2					,						direction=snake_case_)
																cpu_target.target.set_x(cpu_target.target.get_x() + 0.1)
											elif i == 3:
																cpu_target.target.next_to(cpu_targs[0].target					,						direction=snake_case_					,						buff=0.0)
											else:
																cpu_target.target.next_to(cpu_targs[i - 1].target					,						direction=snake_case_					,						buff=0.0)
											cpu_targs.append(snake_case_)

											first_animations.append(rect.animate(run_time=0.5).set_stroke(snake_case_))
											second_animations.append(MoveToTarget(snake_case_					,						run_time=1.5))

						self.play(*snake_case_)
						self.play(*snake_case_)

						self.wait()




