r""" Instantiate a [`BrushNetModel`] from [`UNet2DConditionModel`]. Parameters: unet (`UNet2DConditionModel`): The UNet model weights to copy to the [`BrushNetModel`]. All configuration options are also copied where applicable.
(
cls,
unet: UNet2DConditionModel,
brushnet_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 = 5,
)
| 451 | |
| 452 | @classmethod |
| 453 | def from_unet( |
| 454 | cls, |
| 455 | unet: UNet2DConditionModel, |
| 456 | brushnet_conditioning_channel_order: str = "rgb", |
| 457 | conditioning_embedding_out_channels: Optional[Tuple[int, ...]] = (16, 32, 96, 256), |
| 458 | load_weights_from_unet: bool = True, |
| 459 | conditioning_channels: int = 5, |
| 460 | ): |
| 461 | r""" |
| 462 | Instantiate a [`BrushNetModel`] from [`UNet2DConditionModel`]. |
| 463 | |
| 464 | Parameters: |
| 465 | unet (`UNet2DConditionModel`): |
| 466 | The UNet model weights to copy to the [`BrushNetModel`]. All configuration options are also copied |
| 467 | where applicable. |
| 468 | """ |
| 469 | transformer_layers_per_block = ( |
| 470 | unet.config.transformer_layers_per_block if "transformer_layers_per_block" in unet.config else 1 |
| 471 | ) |
| 472 | encoder_hid_dim = unet.config.encoder_hid_dim if "encoder_hid_dim" in unet.config else None |
| 473 | encoder_hid_dim_type = unet.config.encoder_hid_dim_type if "encoder_hid_dim_type" in unet.config else None |
| 474 | addition_embed_type = unet.config.addition_embed_type if "addition_embed_type" in unet.config else None |
| 475 | addition_time_embed_dim = ( |
| 476 | unet.config.addition_time_embed_dim if "addition_time_embed_dim" in unet.config else None |
| 477 | ) |
| 478 | |
| 479 | brushnet = cls( |
| 480 | in_channels=unet.config.in_channels, |
| 481 | conditioning_channels=conditioning_channels, |
| 482 | flip_sin_to_cos=unet.config.flip_sin_to_cos, |
| 483 | freq_shift=unet.config.freq_shift, |
| 484 | down_block_types=["DownBlock2D" for block_name in unet.config.down_block_types], |
| 485 | mid_block_type='MidBlock2D', |
| 486 | up_block_types=["UpBlock2D" for block_name in unet.config.down_block_types], |
| 487 | only_cross_attention=unet.config.only_cross_attention, |
| 488 | block_out_channels=unet.config.block_out_channels, |
| 489 | layers_per_block=unet.config.layers_per_block, |
| 490 | downsample_padding=unet.config.downsample_padding, |
| 491 | mid_block_scale_factor=unet.config.mid_block_scale_factor, |
| 492 | act_fn=unet.config.act_fn, |
| 493 | norm_num_groups=unet.config.norm_num_groups, |
| 494 | norm_eps=unet.config.norm_eps, |
| 495 | cross_attention_dim=unet.config.cross_attention_dim, |
| 496 | transformer_layers_per_block=transformer_layers_per_block, |
| 497 | encoder_hid_dim=encoder_hid_dim, |
| 498 | encoder_hid_dim_type=encoder_hid_dim_type, |
| 499 | attention_head_dim=unet.config.attention_head_dim, |
| 500 | num_attention_heads=unet.config.num_attention_heads, |
| 501 | use_linear_projection=unet.config.use_linear_projection, |
| 502 | class_embed_type=unet.config.class_embed_type, |
| 503 | addition_embed_type=addition_embed_type, |
| 504 | addition_time_embed_dim=addition_time_embed_dim, |
| 505 | num_class_embeds=unet.config.num_class_embeds, |
| 506 | upcast_attention=unet.config.upcast_attention, |
| 507 | resnet_time_scale_shift=unet.config.resnet_time_scale_shift, |
| 508 | projection_class_embeddings_input_dim=unet.config.projection_class_embeddings_input_dim, |
| 509 | brushnet_conditioning_channel_order=brushnet_conditioning_channel_order, |
| 510 | conditioning_embedding_out_channels=conditioning_embedding_out_channels, |