



"""simple docstring"""

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,
    flip_channel_order,
    get_resize_output_image_size,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_torch_available, is_torch_tensor, is_vision_available, logging


if is_vision_available():
	import PIL

if is_torch_available():
	import torch


lowercase__			   =  logging.get_logger(__name__)




class SCREAMING_SNAKE_CASE__					(		__UpperCAmelCase      ):
						_lowerCAmelCase							=					["pixel_values"]



						def __init__(self						,     _lowercase = True						,     _lowercase = None						,     _lowercase = PILImageResampling.BILINEAR						,     _lowercase = True						,     _lowercase = 1 / 255						,     _lowercase = True						,     _lowercase = None						,     _lowercase = True						,     **_lowercase						,     ):





													'''simple docstring'''

													super().__init__(**_lowerCamelCase							)
													__a		:		Dict									=       size if size is not None else {"""shortest_edge""": 224}
													__a		:		Optional[int]									=       get_size_dict(_lowerCamelCase						,     default_to_square=_lowerCamelCase							)
													__a		:		Tuple									=       crop_size if crop_size is not None else {"""height""": 256, """width""": 256}
													__a		:		List[str]									=       get_size_dict(_lowerCamelCase						,     param_name="""crop_size"""							)

													__a		:		Any									=       do_resize
													__a		:		Any									=       size
													__a		:		Dict									=       resample
													__a		:		Union[str, Any]									=       do_rescale
													__a		:		int									=       rescale_factor
													__a		:		int									=       do_center_crop
													__a		:		int									=       crop_size
													__a		:		Union[str, Any]									=       do_flip_channel_order



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase = PIL.Image.BILINEAR						,     _lowercase = None						,     **_lowercase						,     ):





													'''simple docstring'''

													__a		:		List[Any]									=       get_size_dict(_lowerCamelCase						,     default_to_square=_lowerCamelCase							)
													if "shortest_edge" not in size:
																				raise ValueError(F'''The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}'''							)
													__a		:		Any									=       get_resize_output_image_size(_lowerCamelCase						,     size=size["""shortest_edge"""]						,     default_to_square=_lowerCamelCase							)
													return resize(_lowerCamelCase						,     size=_lowerCamelCase						,     resample=_lowerCamelCase						,     data_format=_lowerCamelCase						,     **_lowerCamelCase							)



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase = None						,     **_lowercase						,     ):





													'''simple docstring'''

													__a		:		Optional[Any]									=       get_size_dict(_lowerCamelCase							)
													if "height" not in size or "width" not in size:
																				raise ValueError(F'''The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}'''							)
													return center_crop(_lowerCamelCase						,     size=(size["""height"""], size["""width"""])						,     data_format=_lowerCamelCase						,     **_lowerCamelCase							)



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase						,     _lowercase = None						,     **_lowercase						,     ):





													'''simple docstring'''

													return rescale(_lowerCamelCase						,     scale=_lowerCamelCase						,     data_format=_lowerCamelCase						,     **_lowerCamelCase							)



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase = None							):





													'''simple docstring'''

													return flip_channel_order(_lowerCamelCase						,     data_format=_lowerCamelCase							)



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase = None						,     _lowercase = None						,     _lowercase = None						,     _lowercase = None						,     _lowercase = None						,     _lowercase = None						,     _lowercase = None						,     _lowercase = None						,     _lowercase = None						,     _lowercase = ChannelDimension.FIRST						,     **_lowercase						,     ):





													'''simple docstring'''

													__a		:		Optional[Any]									=       do_resize if do_resize is not None else self.do_resize
													__a		:		List[str]									=       resample if resample is not None else self.resample
													__a		:		Dict									=       do_rescale if do_rescale is not None else self.do_rescale
													__a		:		int									=       rescale_factor if rescale_factor is not None else self.rescale_factor
													__a		:		str									=       do_center_crop if do_center_crop is not None else self.do_center_crop
													__a		:		int									=       (
													    do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order
													)

													__a		:		Any									=       size if size is not None else self.size
													__a		:		Tuple									=       get_size_dict(_lowerCamelCase						,     default_to_square=_lowerCamelCase							)
													__a		:		List[Any]									=       crop_size if crop_size is not None else self.crop_size
													__a		:		Optional[Any]									=       get_size_dict(_lowerCamelCase						,     param_name="""crop_size"""							)

													__a		:		Optional[Any]									=       make_list_of_images(_lowerCamelCase							)

													if not valid_images(_lowerCamelCase							):
																				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_rescale and rescale_factor is None:
																				raise ValueError("""Rescale factor must be specified if do_rescale is True."""							)

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

													# All transformations expect numpy arrays.
													__a		:		Tuple									=       [to_numpy_array(_lowerCamelCase							) for image in images]

													if do_resize:
																				__a		:		Union[str, Any]									=       [self.resize(image=_lowerCamelCase						,     size=_lowerCamelCase						,     resample=_lowerCamelCase							) for image in images]

													if do_center_crop:
																				__a		:		Any									=       [self.center_crop(image=_lowerCamelCase						,     size=_lowerCamelCase							) for image in images]

													if do_rescale:
																				__a		:		Union[str, Any]									=       [self.rescale(image=_lowerCamelCase						,     scale=_lowerCamelCase							) for image in images]

													# the pretrained checkpoints assume images are BGR, not RGB
													if do_flip_channel_order:
																				__a		:		Dict									=       [self.flip_channel_order(image=_lowerCamelCase							) for image in images]

													__a		:		Any									=       [to_channel_dimension_format(_lowerCamelCase						,     _lowerCamelCase							) for image in images]

													__a		:		List[Any]									=       {"""pixel_values""": images}
													return BatchFeature(data=_lowerCamelCase						,     tensor_type=_lowerCamelCase							)





						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase = None							):





													'''simple docstring'''

													__a		:		Tuple									=       outputs.logits

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

																				if is_torch_tensor(_lowerCamelCase							):
																											__a		:		Dict									=       target_sizes.numpy()

																				__a		:		Optional[int]									=       []

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

													return semantic_segmentation






"""simple docstring"""

from manim import *




class SCREAMING_SNAKE_CASE__					(		__snake_case      ):



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		List[str]									=       Rectangle(height=0.5						,     width=0.5							)
													__a		:		Union[str, Any]									=       Rectangle(height=0.25						,     width=0.25							)
													__a		:		Dict									=       Rectangle(height=0.46						,     width=0.46							).set_stroke(width=0							)

													__a		:		Dict									=       [mem.copy() for i in range(6							)]
													__a		:		str									=       [mem.copy() for i in range(6							)]
													__a		:		Tuple									=       VGroup(*_lowercase							).arrange(_lowercase						,     buff=0							)
													__a		:		List[Any]									=       VGroup(*_lowercase							).arrange(_lowercase						,     buff=0							)
													__a		:		List[Any]									=       VGroup(_lowercase						,     _lowercase							).arrange(_lowercase						,     buff=0							)
													__a		:		Union[str, Any]									=       Text("""CPU"""						,     font_size=24							)
													__a		:		Dict									=       Group(_lowercase						,     _lowercase							).arrange(_lowercase						,     buff=0.5						,     aligned_edge=_lowercase							)
													cpu.move_to([-2.5, -0.5, 0]							)
													self.add(_lowercase							)

													__a		:		Optional[Any]									=       [mem.copy() for i in range(4							)]
													__a		:		Dict									=       VGroup(*_lowercase							).arrange(_lowercase						,     buff=0							)
													__a		:		List[str]									=       Text("""GPU"""						,     font_size=24							)
													__a		:		Any									=       Group(_lowercase						,     _lowercase							).arrange(_lowercase						,     buff=0.5						,     aligned_edge=_lowercase							)
													gpu.move_to([-1, -1, 0]							)
													self.add(_lowercase							)

													__a		:		List[Any]									=       [mem.copy() for i in range(6							)]
													__a		:		Any									=       VGroup(*_lowercase							).arrange(_lowercase						,     buff=0							)

													__a		:		Optional[Any]									=       Text("""Model"""						,     font_size=24							)
													__a		:		Any									=       Group(_lowercase						,     _lowercase							).arrange(_lowercase						,     buff=0.5						,     aligned_edge=_lowercase							)
													model.move_to([3, -1.0, 0]							)
													self.add(_lowercase							)

													__a		:		Tuple									=       []
													__a		:		Tuple									=       []
													__a		:		Optional[int]									=       []

													for i, rect in enumerate(_lowercase							):
																				rect.set_stroke(_lowercase							)

																				__a		:		str									=       Rectangle(height=0.46 / 4						,     width=0.46 / 3							).set_stroke(width=0.0							).set_fill(_lowercase						,     opacity=0.7							)

																				if i == 0:
																											cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT							)						,     buff=0.02						,     direction=_lowercase							)
																											cpu_target.set_x(cpu_target.get_x() + 0.1							)
																				elif i == 3:
																											cpu_target.next_to(model_cpu_arr[0]						,     direction=_lowercase						,     buff=0.0							)
																				else:
																											cpu_target.next_to(model_cpu_arr[i - 1]						,     direction=_lowercase						,     buff=0.0							)
																				self.add(_lowercase							)
																				model_cpu_arr.append(_lowercase							)

													self.add(*_lowercase						,     *_lowercase						,     *_lowercase							)

													__a		:		Optional[Any]									=       [mem.copy() for i in range(6							)]
													__a		:		Union[str, Any]									=       VGroup(*_lowercase							).arrange(_lowercase						,     buff=0							)

													__a		:		Any									=       Text("""Loaded Checkpoint"""						,     font_size=24							)
													__a		:		str									=       Group(_lowercase						,     _lowercase							).arrange(_lowercase						,     buff=0.5						,     aligned_edge=_lowercase							)
													checkpoint.move_to([3, 0.5, 0]							)

													self.add(_lowercase							)

													__a		:		Dict									=       []
													__a		:		int									=       []

													for i, rect in enumerate(_lowercase							):
																				__a		:		List[str]									=       fill.copy().set_fill(_lowercase						,     opacity=0.7							)
																				target.move_to(_lowercase							)
																				ckpt_arr.append(_lowercase							)

																				__a		:		Union[str, Any]									=       target.copy()
																				if i < 5:
																											cpu_target.move_to(cpu_left_col_base[i + 1]							)
																				else:
																											cpu_target.move_to(cpu_right_col_base[i - 5]							)
																				ckpt_cpu_arr.append(_lowercase							)
													self.add(*_lowercase						,     *_lowercase							)

													__a		:		List[str]									=       Square(side_length=2.2							)
													key.move_to([-5, 2, 0]							)

													__a		:		List[Any]									=       MarkupText(
													    F'''<b>Key:</b>\n\n<span fgcolor=\'{YELLOW}\'>●</span> Empty Model'''						,     font_size=18						,     )

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

													self.add(_lowercase						,     _lowercase							)

													__a		:		str									=       MarkupText(
													    F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'''						,     font_size=18						,     )

													blue_text.next_to(_lowercase						,     DOWN * 2.4						,     aligned_edge=key_text.get_left()							)
													self.add(_lowercase							)

													__a		:		Optional[int]									=       MarkupText(
													    F'''Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.'''						,     font_size=24						,     )
													step_a.move_to([2, 2, 0]							)

													__a		:		List[Any]									=       [meta_mem.copy() for i in range(6							)]
													__a		:		Optional[int]									=       [meta_mem.copy() for i in range(6							)]
													__a		:		List[Any]									=       VGroup(*_lowercase							).arrange(_lowercase						,     buff=0							)
													__a		:		List[str]									=       VGroup(*_lowercase							).arrange(_lowercase						,     buff=0							)
													__a		:		Tuple									=       VGroup(_lowercase						,     _lowercase							).arrange(_lowercase						,     buff=0							)
													__a		:		Dict									=       Text("""Disk"""						,     font_size=24							)
													__a		:		Dict									=       Group(_lowercase						,     _lowercase							).arrange(_lowercase						,     buff=0.5						,     aligned_edge=_lowercase							)
													disk.move_to([-4.0, -1.25, 0]							)
													self.play(Write(_lowercase						,     run_time=3							)						,     Write(_lowercase						,     run_time=1							)						,     Create(_lowercase						,     run_time=1							)							)

													__a		:		Optional[Any]									=       []
													for i, rect in enumerate(_lowercase							):
																				__a		:		List[str]									=       rect.copy()
																				target.generate_target()
																				target.target.move_to(disk_left_col_base[i]							).scale(0.5							)
																				animations.append(MoveToTarget(_lowercase						,     run_time=1.5							)							)
													self.play(*_lowercase							)

													self.play(FadeOut(_lowercase							)							)

													__a		:		List[str]									=       MarkupText(F'''Then, the checkpoint is removed from memory\nthrough garbage collection.'''						,     font_size=24							)
													step_a.move_to([2, 2, 0]							)

													self.play(Write(_lowercase						,     run_time=3							)							)

													self.play(
													    FadeOut(_lowercase						,     _lowercase						,     *_lowercase						,     *_lowercase							)						,     )

													self.wait()

