Finds a rectangular region that contains all masked ares in an image, and expands region to match the aspect ratio of the original image;
for example, if user drew mask in a 128x32 region, and the dimensions for processing are 512x512, the region will be expanded to 128x128.
Args:
mask_image (PIL.Image.Image): Mask image.
width (int): Width of the image to be processed.
height (int): Height of the image to be processed.
pad (int, optional): Padding to be added to the crop region. Defaults to 0.
Returns:
tuple: (x1, y1, x2, y2) represent a rectangular region that contains all masked ares in an image and matches the original aspect ratio.
"""
mask_image=mask_image.convert("L")
mask=np.array(mask_image)
# 1. find a rectangular region that contains all masked ares in an image
h,w=mask.shape
crop_left=0
foriinrange(w):
ifnot(mask[:,i]==0).all():
break
crop_left+=1
crop_right=0
foriinreversed(range(w)):
ifnot(mask[:,i]==0).all():
break
crop_right+=1
crop_top=0
foriinrange(h):
ifnot(mask[i]==0).all():
break
crop_top+=1
crop_bottom=0
foriinreversed(range(h)):
ifnot(mask[i]==0).all():
break
crop_bottom+=1
# 2. add padding to the crop region
x1,y1,x2,y2=(
int(max(crop_left-pad,0)),
int(max(crop_top-pad,0)),
int(min(w-crop_right+pad,w)),
int(min(h-crop_bottom+pad,h)),
)
# 3. expands crop region to match the aspect ratio of the image to be processed
ratio_crop_region=(x2-x1)/(y2-y1)
ratio_processing=width/height
ifratio_crop_region>ratio_processing:
desired_height=(x2-x1)/ratio_processing
desired_height_diff=int(desired_height-(y2-y1))
y1-=desired_height_diff//2
y2+=desired_height_diff-desired_height_diff//2
ify2>=mask_image.height:
diff=y2-mask_image.height
y2-=diff
y1-=diff
ify1<0:
y2-=y1
y1-=y1
ify2>=mask_image.height:
y2=mask_image.height
else:
desired_width=(y2-y1)*ratio_processing
desired_width_diff=int(desired_width-(x2-x1))
x1-=desired_width_diff//2
x2+=desired_width_diff-desired_width_diff//2
ifx2>=mask_image.width:
diff=x2-mask_image.width
x2-=diff
x1-=diff
ifx1<0:
x2-=x1
x1-=x1
ifx2>=mask_image.width:
x2=mask_image.width
returnx1,y1,x2,y2
def_resize_and_fill(
self,
image:PIL.Image.Image,
width:int,
height:int,
)->PIL.Image.Image:
"""
Resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image within the dimensions, filling empty with data from image.
Resize the image to fit within the specified width and height, maintaining the aspect ratio, and then center the image within the dimensions, cropping the excess.
"`text_encoder_lora_state_dict` is deprecated and will be removed in 0.27.0. Make sure to retrieve the weights using `get_peft_model`. See https://huggingface.co/docs/peft/v0.6.2/en/quicktour#peftmodel for more information.",
"`text_encoder_lora_state_dict` is deprecated and will be removed in 0.27.0. Make sure to retrieve the weights using `get_peft_model`. See https://huggingface.co/docs/peft/v0.6.2/en/quicktour#peftmodel for more information.",
"`image_encoder` cannot be loaded because `pretrained_model_name_or_path_or_dict` is a state dict."
)
else:
logger.warning(
"image_encoder is not loaded since `image_encoder_folder=None` passed. You will not be able to use `ip_adapter_image` when calling the pipeline with IP-Adapter."
"Use `ip_adapter_image_embeds` to pass pre-generated image embedding instead."
)
# create feature extractor if it has not been registered to the pipeline yet
LORA_DEPRECATION_MESSAGE="You are using an old version of LoRA backend. This will be deprecated in the next releases in favor of PEFT make sure to install the latest PEFT and transformers packages in the future."
classLoraLoaderMixin:
r"""
Load LoRA layers into [`UNet2DConditionModel`] and
f"Provided path contains more than one weights file in the {file_extension} format. Either specify `weight_name` in `load_lora_weights` or make sure there's only one `.safetensors` or `.bin` file in {pretrained_model_name_or_path_or_dict}."
)
weight_name=targeted_files[0]
returnweight_name
@classmethod
def_optionally_disable_offloading(cls,_pipeline):
"""
Optionally removes offloading in case the pipeline has been already sequentially offloaded to CPU.
Args:
_pipeline (`DiffusionPipeline`):
The pipeline to disable offloading for.
Returns:
tuple:
A tuple indicating if `is_model_cpu_offload` or `is_sequential_cpu_offload` is True.
"Accelerate hooks detected. Since you have called `load_lora_weights()`, the previous hooks will be first removed. Then the LoRA parameters will be loaded and the hooks will be applied again."
# Otherwise, we're dealing with the old format. This means the `state_dict` should only
# contain the module names of the `unet` as its keys WITHOUT any prefix.
ifnotUSE_PEFT_BACKEND:
warn_message="You have saved the LoRA weights using the old format. To convert the old LoRA weights to the new format, you can first load them in a dictionary and then create a new dictionary like the following: `new_state_dict = {f'unet.{module_name}': params for module_name, params in old_state_dict.items()}`."
logger.warning(warn_message)
iflen(state_dict.keys())>0:
ifadapter_nameingetattr(unet,"peft_config",{}):
raiseValueError(
f"Adapter name {adapter_name} already in use in the Unet - please select a new adapter name."
A class containing all functions for loading and using adapters weights that are supported in PEFT library. For
more details about adapters and injecting them in a transformer-based model, check out the PEFT [documentation](https://huggingface.co/docs/peft/index).
Install the latest version of PEFT, and use this mixin to:
- Attach new adapters in the model.
- Attach multiple adapters and iteratively activate/deactivate them.
- Activate/deactivate all adapters from the model.
"You are trying to set multiple adapters and you have a PEFT version that does not support multi-adapter inference. Please upgrade to the latest version of PEFT."
" `pip install -U peft` or `pip install -U git+https://github.com/huggingface/peft.git`"
)
else:
module.active_adapter=adapter_name
_adapters_has_been_set=True
ifnot_adapters_has_been_set:
raiseValueError(
"Did not succeeded in setting the adapter. Please make sure you are using a model that supports adapters."
)
defdisable_adapters(self)->None:
r"""
Disable all adapters attached to the model and fallback to inference with the base model only.
If you are not familiar with adapters and PEFT methods, we invite you to read more about them on the PEFT
Path to a directory where a downloaded pretrained model configuration is cached if the standard cache
is not used.
resume_download (`bool`, *optional*, defaults to `False`):
Whether or not to resume downloading the model weights and configuration files. If set to `False`, any
incompletely downloaded files are deleted.
proxies (`Dict[str, str]`, *optional*):
A dictionary of proxy servers to use by protocol or endpoint, for example, `{'http': 'foo.bar:3128',
'http://hostname': 'foo.bar:4012'}`. The proxies are used on each request.
local_files_only (`bool`, *optional*, defaults to `False`):
Whether to only load local model weights and configuration files or not. If set to `True`, the model
won't be downloaded from the Hub.
token (`str` or *bool*, *optional*):
The token to use as HTTP bearer authorization for remote files. If `True`, the token generated from
`diffusers-cli login` (stored in `~/.huggingface`) is used.
revision (`str`, *optional*, defaults to `"main"`):
The specific model version to use. It can be a branch name, a tag name, a commit id, or any identifier
allowed by Git.
original_config_file (`str`, *optional*):
The path to the original config file that was used to train the model. If not provided, the config file
will be inferred from the checkpoint file.
model_type (`str`, *optional*):
The type of model to load. If not provided, the model type will be inferred from the checkpoint file.
image_size (`int`, *optional*):
The size of the image output. It's used to configure the `sample_size` parameter of the UNet and VAE model.
load_safety_checker (`bool`, *optional*, defaults to `False`):
Whether to load the safety checker model or not. By default, the safety checker is not loaded unless a `safety_checker` component is passed to the `kwargs`.
num_in_channels (`int`, *optional*):
Specify the number of input channels for the UNet model. Read more about how to configure UNet model with this parameter
f"With local_files_only set to {local_files_only}, you must first locally save the configuration in the following path: 'openai/clip-vit-large-patch14'."
f"With local_files_only set to {local_files_only}, you must first locally save the text_encoder_2 and tokenizer_2 in the following path: {config_name} with `pad_token` set to '!'."
f"With local_files_only set to {local_files_only}, you must first locally save the text_encoder and tokenizer in the following path: 'openai/clip-vit-large-patch14'."
f"With local_files_only set to {local_files_only}, you must first locally save the text_encoder_2 and tokenizer_2 in the following path: {config_name} with `pad_token` set to '!'."