


"""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


A	:  Any            =     logging.get_logger(__name__)
class     _UpperCamelCase    (  lowerCAmelCase__						):
							'''simple docstring'''







							__UpperCAmelCase		:       Optional[Any]							 =["""pixel_values"""]

							def __init__(	self       ,  __a = True       ,  __a = None       ,  __a = PILImageResampling.BILINEAR       ,  __a = True       ,  __a = 1 / 2_55       ,  __a = True       ,  __a = None       ,  __a = True       ,  **__a       ,  ):
										super().__init__(**__a      )
										__lowerCAmelCase							       =     size if size is not None else {"shortest_edge": 2_24}
										__lowerCAmelCase							       =     get_size_dict(__a       ,  default_to_square=__a      )
										__lowerCAmelCase							       =     crop_size if crop_size is not None else {"height": 2_56, "width": 2_56}
										__lowerCAmelCase							       =     get_size_dict(__a       ,  param_name="crop_size"      )

										__lowerCAmelCase							       =     do_resize
										__lowerCAmelCase							       =     size
										__lowerCAmelCase							       =     resample
										__lowerCAmelCase							       =     do_rescale
										__lowerCAmelCase							       =     rescale_factor
										__lowerCAmelCase							       =     do_center_crop
										__lowerCAmelCase							       =     crop_size
										__lowerCAmelCase							       =     do_flip_channel_order

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a = PIL.Image.BILINEAR       ,  __a = None       ,  **__a       ,  ):
										__lowerCAmelCase							       =     get_size_dict(__a       ,  default_to_square=__a      )
										if "shortest_edge" not in size:
													raise ValueError(f"The `size` dictionary must contain the key `shortest_edge`. Got {size.keys()}"      )
										__lowerCAmelCase							       =     get_resize_output_image_size(__a       ,  size=size["shortest_edge"]       ,  default_to_square=__a      )
										return resize(__a       ,  size=__a       ,  resample=__a       ,  data_format=__a       ,  **__a      )

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a = None       ,  **__a       ,  ):
										__lowerCAmelCase							       =     get_size_dict(__a      )
										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(__a       ,  size=(size["height"], size["width"])       ,  data_format=__a       ,  **__a      )

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a = None       ,  **__a       ,  ):
										return rescale(__a       ,  scale=__a       ,  data_format=__a       ,  **__a      )

							def      snake_case							(	self       ,  __a       ,  __a = None      ):
										return flip_channel_order(__a       ,  data_format=__a      )

							def      snake_case							(	self       ,  __a       ,  __a = None       ,  __a = None       ,  __a = None       ,  __a = None       ,  __a = None       ,  __a = None       ,  __a = None       ,  __a = None       ,  __a = None       ,  __a = ChannelDimension.FIRST       ,  **__a       ,  ):
										__lowerCAmelCase							       =     do_resize if do_resize is not None else self.do_resize
										__lowerCAmelCase							       =     resample if resample is not None else self.resample
										__lowerCAmelCase							       =     do_rescale if do_rescale is not None else self.do_rescale
										__lowerCAmelCase							       =     rescale_factor if rescale_factor is not None else self.rescale_factor
										__lowerCAmelCase							       =     do_center_crop if do_center_crop is not None else self.do_center_crop
										__lowerCAmelCase							       =     (
										    do_flip_channel_order if do_flip_channel_order is not None else self.do_flip_channel_order
										)

										__lowerCAmelCase							       =     size if size is not None else self.size
										__lowerCAmelCase							       =     get_size_dict(__a       ,  default_to_square=__a      )
										__lowerCAmelCase							       =     crop_size if crop_size is not None else self.crop_size
										__lowerCAmelCase							       =     get_size_dict(__a       ,  param_name="crop_size"      )

										__lowerCAmelCase							       =     make_list_of_images(__a      )

										if not valid_images(__a      ):
													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.
										__lowerCAmelCase							       =     [to_numpy_array(__a      ) for image in images]

										if do_resize:
													__lowerCAmelCase							       =     [self.resize(image=__a       ,  size=__a       ,  resample=__a      ) for image in images]

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

										if do_rescale:
													__lowerCAmelCase							       =     [self.rescale(image=__a       ,  scale=__a      ) for image in images]

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

										__lowerCAmelCase							       =     [to_channel_dimension_format(__a       ,  __a      ) for image in images]

										__lowerCAmelCase							       =     {"pixel_values": images}
										return BatchFeature(data=__a       ,  tensor_type=__a      )



							def      snake_case							(	self       ,  __a       ,  __a = None      ):
										__lowerCAmelCase							       =     outputs.logits

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

													if is_torch_tensor(__a      ):
																__lowerCAmelCase							       =     target_sizes.numpy()

													__lowerCAmelCase							       =     []

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

										return semantic_segmentation








"""simple docstring"""


import os
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

import torch
from torch import nn

from ...models.controlnet import ControlNetModel, ControlNetOutput
from ...models.modeling_utils import ModelMixin
from ...utils import logging


A	:  List[Any]            =     logging.get_logger(__name__)
class     _UpperCamelCase    (  lowerCAmelCase__						):
							'''simple docstring'''







							def __init__(	self       ,  __a      ):
										super().__init__()
										__lowerCAmelCase							       =     nn.ModuleList(__a      )

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a = None       ,  __a = None       ,  __a = None       ,  __a = None       ,  __a = False       ,  __a = True       ,  ):
										for i, (image, scale, controlnet) in enumerate(zip(__a       ,  __a       ,  self.nets      )      ):
													__lowerCAmelCase       ,      __lowerCAmelCase							       =     controlnet(
													    __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  )

													# merge samples
													if i == 0:
																__lowerCAmelCase       ,      __lowerCAmelCase							       =     down_samples, mid_sample
													else:
																__lowerCAmelCase							       =     [
																    samples_prev + samples_curr
																    for samples_prev, samples_curr in zip(__a       ,  __a      )
																]
																mid_block_res_sample += mid_sample

										return down_block_res_samples, mid_block_res_sample

							def      snake_case							(	self       ,  __a       ,  __a = True       ,  __a = None       ,  __a = False       ,  __a = None       ,  ):
										__lowerCAmelCase							       =     0
										__lowerCAmelCase							       =     save_directory
										for controlnet in self.nets:
													controlnet.save_pretrained(
													    __a       ,  is_main_process=__a       ,  save_function=__a       ,  safe_serialization=__a       ,  variant=__a       ,  )

													idx += 1
													__lowerCAmelCase							       =     model_path_to_save + f"_{idx}"



							@classmethod
							def      snake_case							(	cls       ,  __a       ,  **__a      ):
										__lowerCAmelCase							       =     0
										__lowerCAmelCase							       =     []

										# load controlnet and append to list until no controlnet directory exists anymore
										# first controlnet has to be saved under `./mydirectory/controlnet` to be compliant with `DiffusionPipeline.from_prertained`
										# second, third, ... controlnets have to be saved under `./mydirectory/controlnet_1`, `./mydirectory/controlnet_2`, ...
										__lowerCAmelCase							       =     pretrained_model_path
										while os.path.isdir(__a      ):
													__lowerCAmelCase							       =     ControlNetModel.from_pretrained(__a       ,  **__a      )
													controlnets.append(__a      )

													idx += 1
													__lowerCAmelCase							       =     pretrained_model_path + f"_{idx}"

										logger.info(f"{len(__a      )} controlnets loaded from {pretrained_model_path}."      )

										if len(__a      ) == 0:
													raise ValueError(
													    f"No ControlNets found under {os.path.dirname(__a      )}. Expected at least {pretrained_model_path + '_0'}."      )

										return cls(__a      )




