r""" Instantiate a [`ControlNetModel`] from [`UNet2DConditionModel`]. Parameters: unet (`UNet2DConditionModel`): The UNet model weights to copy to the [`ControlNetModel`]. All configuration options are also copied where applicable.
(
cls,
unet: UNet2DConditionModel,
controlnet_conditioning_channel_order: str = "rgb",
conditioning_embedding_out_channels: Optional[Tuple[int, ...]] = (16, 32, 96, 256),
load_weights_from_unet: bool = True,
conditioning_channels: int = 3,
)
| 443 | |
| 444 | @classmethod |
| 445 | def from_unet( |
| 446 | cls, |
| 447 | unet: UNet2DConditionModel, |
| 448 | controlnet_conditioning_channel_order: str = "rgb", |
| 449 | conditioning_embedding_out_channels: Optional[Tuple[int, ...]] = (16, 32, 96, 256), |
| 450 | load_weights_from_unet: bool = True, |
| 451 | conditioning_channels: int = 3, |
| 452 | ): |
| 453 | r""" |
| 454 | Instantiate a [`ControlNetModel`] from [`UNet2DConditionModel`]. |
| 455 | |
| 456 | Parameters: |
| 457 | unet (`UNet2DConditionModel`): |
| 458 | The UNet model weights to copy to the [`ControlNetModel`]. All configuration options are also copied |
| 459 | where applicable. |
| 460 | """ |
| 461 | transformer_layers_per_block = ( |
| 462 | unet.config.transformer_layers_per_block if "transformer_layers_per_block" in unet.config else 1 |
| 463 | ) |
| 464 | encoder_hid_dim = unet.config.encoder_hid_dim if "encoder_hid_dim" in unet.config else None |
| 465 | encoder_hid_dim_type = unet.config.encoder_hid_dim_type if "encoder_hid_dim_type" in unet.config else None |
| 466 | addition_embed_type = unet.config.addition_embed_type if "addition_embed_type" in unet.config else None |
| 467 | addition_time_embed_dim = ( |
| 468 | unet.config.addition_time_embed_dim if "addition_time_embed_dim" in unet.config else None |
| 469 | ) |
| 470 | |
| 471 | controlnet = cls( |
| 472 | encoder_hid_dim=encoder_hid_dim, |
| 473 | encoder_hid_dim_type=encoder_hid_dim_type, |
| 474 | addition_embed_type=addition_embed_type, |
| 475 | addition_time_embed_dim=addition_time_embed_dim, |
| 476 | transformer_layers_per_block=transformer_layers_per_block, |
| 477 | in_channels=unet.config.in_channels, |
| 478 | flip_sin_to_cos=unet.config.flip_sin_to_cos, |
| 479 | freq_shift=unet.config.freq_shift, |
| 480 | down_block_types=unet.config.down_block_types, |
| 481 | only_cross_attention=unet.config.only_cross_attention, |
| 482 | block_out_channels=unet.config.block_out_channels, |
| 483 | layers_per_block=unet.config.layers_per_block, |
| 484 | downsample_padding=unet.config.downsample_padding, |
| 485 | mid_block_scale_factor=unet.config.mid_block_scale_factor, |
| 486 | act_fn=unet.config.act_fn, |
| 487 | norm_num_groups=unet.config.norm_num_groups, |
| 488 | norm_eps=unet.config.norm_eps, |
| 489 | cross_attention_dim=unet.config.cross_attention_dim, |
| 490 | attention_head_dim=unet.config.attention_head_dim, |
| 491 | num_attention_heads=unet.config.num_attention_heads, |
| 492 | use_linear_projection=unet.config.use_linear_projection, |
| 493 | class_embed_type=unet.config.class_embed_type, |
| 494 | num_class_embeds=unet.config.num_class_embeds, |
| 495 | upcast_attention=unet.config.upcast_attention, |
| 496 | resnet_time_scale_shift=unet.config.resnet_time_scale_shift, |
| 497 | projection_class_embeddings_input_dim=unet.config.projection_class_embeddings_input_dim, |
| 498 | mid_block_type=unet.config.mid_block_type, |
| 499 | controlnet_conditioning_channel_order=controlnet_conditioning_channel_order, |
| 500 | conditioning_embedding_out_channels=conditioning_embedding_out_channels, |
| 501 | conditioning_channels=conditioning_channels, |
| 502 | ) |