libcom 0.1.8__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- libcom/__init__.py +23 -0
- libcom/color_transfer/__init__.py +3 -0
- libcom/color_transfer/reinhard.py +57 -0
- libcom/fopa_heat_map/__init__.py +4 -0
- libcom/fopa_heat_map/fopa_heat_map.py +212 -0
- libcom/fopa_heat_map/source/__init__.py +0 -0
- libcom/fopa_heat_map/source/backbone/ResNet.py +229 -0
- libcom/fopa_heat_map/source/backbone/__init__.py +0 -0
- libcom/fopa_heat_map/source/data/OBdataset.py +292 -0
- libcom/fopa_heat_map/source/data/__init__.py +0 -0
- libcom/fopa_heat_map/source/data/all_transforms.py +25 -0
- libcom/fopa_heat_map/source/network/BaseBlocks.py +26 -0
- libcom/fopa_heat_map/source/network/DynamicModules.py +43 -0
- libcom/fopa_heat_map/source/network/ObPlaNet_simple.py +112 -0
- libcom/fopa_heat_map/source/network/__init__.py +1 -0
- libcom/fopa_heat_map/source/network/tensor_ops.py +37 -0
- libcom/fopa_heat_map/source/prepare_multi_fg_scales.py +85 -0
- libcom/fos_score/__init__.py +4 -0
- libcom/fos_score/fos_score_prediction.py +218 -0
- libcom/fos_score/source/__init__.py +0 -0
- libcom/fos_score/source/config/__init__.py +1 -0
- libcom/fos_score/source/config/config.py +68 -0
- libcom/fos_score/source/config/config.sfosd.yaml +43 -0
- libcom/fos_score/source/config/config_rfosd.yaml +43 -0
- libcom/fos_score/source/loss_function.py +118 -0
- libcom/fos_score/source/networks.py +355 -0
- libcom/harmony_score/__init__.py +3 -0
- libcom/harmony_score/harmony_score_prediction.py +119 -0
- libcom/harmony_score/source/__init__.py +0 -0
- libcom/harmony_score/source/bargainnet.py +226 -0
- libcom/image_harmonization/__init__.py +4 -0
- libcom/image_harmonization/image_harmonization.py +193 -0
- libcom/image_harmonization/source/__init__.py +0 -0
- libcom/image_harmonization/source/functions.py +747 -0
- libcom/image_harmonization/source/pct_net.py +85 -0
- libcom/image_harmonization/source/src/__init__.py +0 -0
- libcom/image_harmonization/source/src/lbm/__init__.py +0 -0
- libcom/image_harmonization/source/src/lbm/config.py +141 -0
- libcom/image_harmonization/source/src/lbm/data/__init__.py +62 -0
- libcom/image_harmonization/source/src/lbm/data/datasets/__init__.py +9 -0
- libcom/image_harmonization/source/src/lbm/data/datasets/collation_fn.py +41 -0
- libcom/image_harmonization/source/src/lbm/data/datasets/dataset.py +243 -0
- libcom/image_harmonization/source/src/lbm/data/datasets/datasets_config.py +42 -0
- libcom/image_harmonization/source/src/lbm/data/filters/__init__.py +12 -0
- libcom/image_harmonization/source/src/lbm/data/filters/base.py +21 -0
- libcom/image_harmonization/source/src/lbm/data/filters/filter_wrapper.py +36 -0
- libcom/image_harmonization/source/src/lbm/data/filters/filters.py +33 -0
- libcom/image_harmonization/source/src/lbm/data/filters/filters_config.py +32 -0
- libcom/image_harmonization/source/src/lbm/data/mappers/__init__.py +19 -0
- libcom/image_harmonization/source/src/lbm/data/mappers/base.py +26 -0
- libcom/image_harmonization/source/src/lbm/data/mappers/mappers.py +135 -0
- libcom/image_harmonization/source/src/lbm/data/mappers/mappers_config.py +109 -0
- libcom/image_harmonization/source/src/lbm/data/mappers/mappers_wrapper.py +31 -0
- libcom/image_harmonization/source/src/lbm/inference/__init__.py +4 -0
- libcom/image_harmonization/source/src/lbm/inference/inference.py +70 -0
- libcom/image_harmonization/source/src/lbm/inference/utils.py +222 -0
- libcom/image_harmonization/source/src/lbm/models/__init__.py +0 -0
- libcom/image_harmonization/source/src/lbm/models/base/__init__.py +4 -0
- libcom/image_harmonization/source/src/lbm/models/base/base_model.py +66 -0
- libcom/image_harmonization/source/src/lbm/models/base/model_config.py +8 -0
- libcom/image_harmonization/source/src/lbm/models/embedders/__init__.py +4 -0
- libcom/image_harmonization/source/src/lbm/models/embedders/base/__init__.py +4 -0
- libcom/image_harmonization/source/src/lbm/models/embedders/base/base_conditioner.py +60 -0
- libcom/image_harmonization/source/src/lbm/models/embedders/base/base_conditioner_config.py +27 -0
- libcom/image_harmonization/source/src/lbm/models/embedders/conditioners_wrapper.py +114 -0
- libcom/image_harmonization/source/src/lbm/models/embedders/latents_concat/__init__.py +4 -0
- libcom/image_harmonization/source/src/lbm/models/embedders/latents_concat/latents_concat_embedder_config.py +31 -0
- libcom/image_harmonization/source/src/lbm/models/embedders/latents_concat/latents_concat_embedder_model.py +80 -0
- libcom/image_harmonization/source/src/lbm/models/lbm/__init__.py +4 -0
- libcom/image_harmonization/source/src/lbm/models/lbm/lbm_config.py +101 -0
- libcom/image_harmonization/source/src/lbm/models/lbm/lbm_model.py +359 -0
- libcom/image_harmonization/source/src/lbm/models/unets/__init__.py +14 -0
- libcom/image_harmonization/source/src/lbm/models/unets/unet.py +148 -0
- libcom/image_harmonization/source/src/lbm/models/utils.py +377 -0
- libcom/image_harmonization/source/src/lbm/models/vae/__init__.py +4 -0
- libcom/image_harmonization/source/src/lbm/models/vae/autoencoderKL.py +136 -0
- libcom/image_harmonization/source/src/lbm/models/vae/autoencoderKL_config.py +27 -0
- libcom/image_harmonization/source/src/lbm/trainer/__init__.py +85 -0
- libcom/image_harmonization/source/src/lbm/trainer/loggers.py +324 -0
- libcom/image_harmonization/source/src/lbm/trainer/trainer.py +199 -0
- libcom/image_harmonization/source/src/lbm/trainer/training_config.py +82 -0
- libcom/image_harmonization/source/src/lbm/trainer/utils.py +193 -0
- libcom/inharmonious_region_localization/__init__.py +4 -0
- libcom/inharmonious_region_localization/inharmonious_region_localization.py +97 -0
- libcom/inharmonious_region_localization/source/__init__.py +0 -0
- libcom/inharmonious_region_localization/source/blocks.py +368 -0
- libcom/inharmonious_region_localization/source/functions.py +679 -0
- libcom/inharmonious_region_localization/source/madis_net.py +79 -0
- libcom/kontext_blending_harmonization/__init__.py +3 -0
- libcom/kontext_blending_harmonization/kontext_blending_harmonization.py +148 -0
- libcom/kontext_blending_harmonization/source/__init__.py +0 -0
- libcom/kontext_blending_harmonization/source/pipeline.py +1135 -0
- libcom/naive_composition/__init__.py +3 -0
- libcom/naive_composition/generate_composite_image.py +116 -0
- libcom/opa_score/__init__.py +3 -0
- libcom/opa_score/opa_score_prediction.py +101 -0
- libcom/opa_score/source/__init__.py +3 -0
- libcom/opa_score/source/object_place_net.py +55 -0
- libcom/opa_score/source/oblect_place_config.py +66 -0
- libcom/opa_score/source/resnet_4ch.py +213 -0
- libcom/os_insert/__init__.py +3 -0
- libcom/os_insert/diffusers_osinsert/__init__.py +78 -0
- libcom/os_insert/diffusers_osinsert/_patched_pipeline_flux_fill.py +878 -0
- libcom/os_insert/os_insert.py +534 -0
- libcom/os_insert/source/__init__.py +1 -0
- libcom/os_insert/source/ia_utils.py +183 -0
- libcom/os_insert/source/insertanything_infer.py +529 -0
- libcom/os_insert/source/ldm/__init__.py +0 -0
- libcom/os_insert/source/ldm/data/__init__.py +0 -0
- libcom/os_insert/source/ldm/data/base.py +23 -0
- libcom/os_insert/source/ldm/data/datamodule.py +128 -0
- libcom/os_insert/source/ldm/data/imagenet.py +394 -0
- libcom/os_insert/source/ldm/data/lsun.py +92 -0
- libcom/os_insert/source/ldm/data/open_images.py +728 -0
- libcom/os_insert/source/ldm/lr_scheduler.py +98 -0
- libcom/os_insert/source/ldm/models/__init__.py +0 -0
- libcom/os_insert/source/ldm/models/autoencoder.py +443 -0
- libcom/os_insert/source/ldm/models/diffusion/__init__.py +0 -0
- libcom/os_insert/source/ldm/models/diffusion/classifier.py +267 -0
- libcom/os_insert/source/ldm/models/diffusion/ddim.py +263 -0
- libcom/os_insert/source/ldm/models/diffusion/ddpm.py +1417 -0
- libcom/os_insert/source/ldm/models/diffusion/plms.py +252 -0
- libcom/os_insert/source/ldm/modules/__init__.py +0 -0
- libcom/os_insert/source/ldm/modules/attention.py +261 -0
- libcom/os_insert/source/ldm/modules/diffusionmodules/__init__.py +0 -0
- libcom/os_insert/source/ldm/modules/diffusionmodules/model.py +835 -0
- libcom/os_insert/source/ldm/modules/diffusionmodules/openaimodel.py +1108 -0
- libcom/os_insert/source/ldm/modules/diffusionmodules/util.py +267 -0
- libcom/os_insert/source/ldm/modules/distributions/__init__.py +0 -0
- libcom/os_insert/source/ldm/modules/distributions/distributions.py +92 -0
- libcom/os_insert/source/ldm/modules/ema.py +76 -0
- libcom/os_insert/source/ldm/modules/encoders/__init__.py +0 -0
- libcom/os_insert/source/ldm/modules/encoders/modules.py +190 -0
- libcom/os_insert/source/ldm/modules/encoders/xf.py +130 -0
- libcom/os_insert/source/ldm/modules/losses/__init__.py +1 -0
- libcom/os_insert/source/ldm/modules/losses/contperceptual.py +111 -0
- libcom/os_insert/source/ldm/modules/losses/vqperceptual.py +167 -0
- libcom/os_insert/source/ldm/modules/x_transformer.py +641 -0
- libcom/os_insert/source/ldm/util.py +203 -0
- libcom/os_insert/source/objectstitch_data.py +66 -0
- libcom/os_insert/source/objectstitch_infer.py +618 -0
- libcom/os_insert/source/sam_on_objectstitch.py +253 -0
- libcom/os_insert/source/utils.py +41 -0
- libcom/os_insert/source/v1.yaml +108 -0
- libcom/painterly_image_harmonization/__init__.py +4 -0
- libcom/painterly_image_harmonization/painterly_image_harmonization.py +215 -0
- libcom/painterly_image_harmonization/source/PHDNet/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDNet/networks.py +202 -0
- libcom/painterly_image_harmonization/source/PHDNet/phdnet.py +47 -0
- libcom/painterly_image_harmonization/source/PHDNet/phdnet_config.py +18 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/inference_base.py +300 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/lr_scheduler.py +98 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/autoencoder.py +211 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/diffusion/__init__.py +3 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/diffusion/ddim.py +336 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/diffusion/ddpm.py +1400 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/diffusion/dpm_solver/__init__.py +1 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/diffusion/dpm_solver/dpm_solver.py +1217 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/diffusion/dpm_solver/sampler.py +87 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/diffusion/plms.py +243 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/models/diffusion/scheduling_pndm.py +518 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/attention.py +378 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/diffusionmodules/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/diffusionmodules/model.py +857 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/diffusionmodules/openaimodel.py +823 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/diffusionmodules/util.py +270 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/distributions/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/distributions/distributions.py +92 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/ema.py +80 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/encoders/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/encoders/adapter.py +213 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/encoders/modules.py +82 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/__init__.py +1 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/api.py +269 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/api.py +175 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/midas/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/midas/base_model.py +16 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/midas/blocks.py +342 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/midas/dpt_depth.py +109 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/midas/midas_net.py +76 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/midas/midas_net_custom.py +128 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/midas/transforms.py +234 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/midas/vit.py +491 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/midas/utils.py +189 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/model_edge.py +653 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/openpose/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/openpose/api.py +35 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/openpose/body.py +209 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/openpose/hand.py +75 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/openpose/model.py +178 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/openpose/util.py +203 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/extra_condition/utils.py +72 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/fusion/__init__.py +0 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/fusion/dual_encoder_fusion.py +93 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/image_degradation/__init__.py +2 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/image_degradation/bsrgan.py +730 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/image_degradation/bsrgan_light.py +651 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/modules/image_degradation/utils_image.py +916 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/ldm/util.py +200 -0
- libcom/painterly_image_harmonization/source/PHDiffusion/stable_diffusion.yaml +75 -0
- libcom/painterly_image_harmonization/source/__init__.py +0 -0
- libcom/reflection_generation/__init__.py +3 -0
- libcom/reflection_generation/reflection_generation.py +174 -0
- libcom/reflection_generation/source/PostProcessModel.py +513 -0
- libcom/reflection_generation/source/__init__.py +0 -0
- libcom/reflection_generation/source/cldm/__init__.py +0 -0
- libcom/reflection_generation/source/cldm/base_network.py +166 -0
- libcom/reflection_generation/source/cldm/cldm.py +509 -0
- libcom/reflection_generation/source/cldm/ddim_hacked.py +317 -0
- libcom/reflection_generation/source/cldm/hack.py +111 -0
- libcom/reflection_generation/source/cldm/logger.py +127 -0
- libcom/reflection_generation/source/cldm/model.py +26 -0
- libcom/reflection_generation/source/cldm_v15.yaml +81 -0
- libcom/reflection_generation/source/cluster_feature.py +109 -0
- libcom/reflection_generation/source/ldm/__init__.py +0 -0
- libcom/reflection_generation/source/ldm/models/__init__.py +0 -0
- libcom/reflection_generation/source/ldm/models/autoencoder.py +219 -0
- libcom/reflection_generation/source/ldm/models/diffusion/__init__.py +0 -0
- libcom/reflection_generation/source/ldm/models/diffusion/attention_processor.py +188 -0
- libcom/reflection_generation/source/ldm/models/diffusion/ddim.py +340 -0
- libcom/reflection_generation/source/ldm/models/diffusion/ddpm.py +1892 -0
- libcom/reflection_generation/source/ldm/models/diffusion/dpm_solver/__init__.py +1 -0
- libcom/reflection_generation/source/ldm/models/diffusion/dpm_solver/dpm_solver.py +1154 -0
- libcom/reflection_generation/source/ldm/models/diffusion/dpm_solver/sampler.py +87 -0
- libcom/reflection_generation/source/ldm/models/diffusion/ip_adapter.py +57 -0
- libcom/reflection_generation/source/ldm/models/diffusion/plms.py +244 -0
- libcom/reflection_generation/source/ldm/models/diffusion/sampling_util.py +22 -0
- libcom/reflection_generation/source/ldm/modules/__init__.py +0 -0
- libcom/reflection_generation/source/ldm/modules/attention.py +341 -0
- libcom/reflection_generation/source/ldm/modules/diffusionmodules/__init__.py +0 -0
- libcom/reflection_generation/source/ldm/modules/diffusionmodules/model.py +852 -0
- libcom/reflection_generation/source/ldm/modules/diffusionmodules/openaimodel.py +786 -0
- libcom/reflection_generation/source/ldm/modules/diffusionmodules/upscaling.py +81 -0
- libcom/reflection_generation/source/ldm/modules/diffusionmodules/util.py +280 -0
- libcom/reflection_generation/source/ldm/modules/distributions/__init__.py +0 -0
- libcom/reflection_generation/source/ldm/modules/distributions/distributions.py +92 -0
- libcom/reflection_generation/source/ldm/modules/ema.py +80 -0
- libcom/reflection_generation/source/ldm/modules/encoders/__init__.py +0 -0
- libcom/reflection_generation/source/ldm/modules/encoders/modules.py +214 -0
- libcom/reflection_generation/source/ldm/modules/image_degradation/__init__.py +0 -0
- libcom/reflection_generation/source/ldm/modules/image_degradation/bsrgan.py +730 -0
- libcom/reflection_generation/source/ldm/modules/image_degradation/bsrgan_light.py +651 -0
- libcom/reflection_generation/source/ldm/modules/image_degradation/utils_image.py +916 -0
- libcom/reflection_generation/source/ldm/util.py +198 -0
- libcom/reflection_generation/source/local_clip.py +749 -0
- libcom/reflection_generation/source/tool_add_control.py +49 -0
- libcom/shadow_generation/__init__.py +3 -0
- libcom/shadow_generation/shadow_generation.py +754 -0
- libcom/shadow_generation/source/PostProcessModel.py +511 -0
- libcom/shadow_generation/source/__init__.py +0 -0
- libcom/shadow_generation/source/attention_processor.py +234 -0
- libcom/shadow_generation/source/cldm/__init__.py +0 -0
- libcom/shadow_generation/source/cldm/base_network.py +166 -0
- libcom/shadow_generation/source/cldm/cldm.py +524 -0
- libcom/shadow_generation/source/cldm/model.py +28 -0
- libcom/shadow_generation/source/ldm/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/models/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/models/autoencoder.py +219 -0
- libcom/shadow_generation/source/ldm/models/diffusion/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/models/diffusion/attention_processor.py +188 -0
- libcom/shadow_generation/source/ldm/models/diffusion/ddim.py +340 -0
- libcom/shadow_generation/source/ldm/models/diffusion/ddpm.py +1893 -0
- libcom/shadow_generation/source/ldm/models/diffusion/dpm_solver/__init__.py +1 -0
- libcom/shadow_generation/source/ldm/models/diffusion/dpm_solver/dpm_solver.py +1154 -0
- libcom/shadow_generation/source/ldm/models/diffusion/dpm_solver/sampler.py +87 -0
- libcom/shadow_generation/source/ldm/models/diffusion/ip_adapter.py +57 -0
- libcom/shadow_generation/source/ldm/models/diffusion/plms.py +244 -0
- libcom/shadow_generation/source/ldm/models/diffusion/sampling_util.py +22 -0
- libcom/shadow_generation/source/ldm/modules/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/modules/attention.py +341 -0
- libcom/shadow_generation/source/ldm/modules/diffusionmodules/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/modules/diffusionmodules/model.py +852 -0
- libcom/shadow_generation/source/ldm/modules/diffusionmodules/openaimodel.py +786 -0
- libcom/shadow_generation/source/ldm/modules/diffusionmodules/upscaling.py +81 -0
- libcom/shadow_generation/source/ldm/modules/diffusionmodules/util.py +270 -0
- libcom/shadow_generation/source/ldm/modules/distributions/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/modules/distributions/distributions.py +92 -0
- libcom/shadow_generation/source/ldm/modules/ema.py +80 -0
- libcom/shadow_generation/source/ldm/modules/encoders/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/modules/encoders/modules.py +214 -0
- libcom/shadow_generation/source/ldm/modules/image_degradation/__init__.py +2 -0
- libcom/shadow_generation/source/ldm/modules/image_degradation/bsrgan.py +730 -0
- libcom/shadow_generation/source/ldm/modules/image_degradation/bsrgan_light.py +651 -0
- libcom/shadow_generation/source/ldm/modules/image_degradation/utils_image.py +916 -0
- libcom/shadow_generation/source/ldm/modules/midas/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/modules/midas/api.py +170 -0
- libcom/shadow_generation/source/ldm/modules/midas/midas/__init__.py +0 -0
- libcom/shadow_generation/source/ldm/modules/midas/midas/base_model.py +16 -0
- libcom/shadow_generation/source/ldm/modules/midas/midas/blocks.py +342 -0
- libcom/shadow_generation/source/ldm/modules/midas/midas/dpt_depth.py +109 -0
- libcom/shadow_generation/source/ldm/modules/midas/midas/midas_net.py +76 -0
- libcom/shadow_generation/source/ldm/modules/midas/midas/midas_net_custom.py +128 -0
- libcom/shadow_generation/source/ldm/modules/midas/midas/transforms.py +234 -0
- libcom/shadow_generation/source/ldm/modules/midas/midas/vit.py +491 -0
- libcom/shadow_generation/source/ldm/modules/midas/utils.py +189 -0
- libcom/shadow_generation/source/ldm/util.py +199 -0
- libcom/utils/__init__.py +0 -0
- libcom/utils/environment.py +12 -0
- libcom/utils/model_download.py +67 -0
- libcom/utils/process_image.py +108 -0
- libcom-0.1.8.dist-info/METADATA +157 -0
- libcom-0.1.8.dist-info/RECORD +310 -0
- libcom-0.1.8.dist-info/WHEEL +5 -0
- libcom-0.1.8.dist-info/licenses/LICENSE +201 -0
- libcom-0.1.8.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
import torch.nn.functional as F
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def calc_mean_std(feat, eps=1e-5):
|
|
7
|
+
# eps is a small value added to the variance to avoid divide-by-zero.
|
|
8
|
+
size = feat.size()
|
|
9
|
+
assert (len(size) == 4)
|
|
10
|
+
N, C = size[:2]
|
|
11
|
+
feat_var = feat.contiguous().view(N, C, -1).var(dim=2) + eps
|
|
12
|
+
feat_std = feat_var.sqrt().view(N, C, 1, 1)
|
|
13
|
+
feat_mean = feat.contiguous().view(N, C, -1).mean(dim=2).view(N, C, 1, 1)
|
|
14
|
+
return feat_mean, feat_std
|
|
15
|
+
|
|
16
|
+
def adain_fg(comp_feat, mask):
|
|
17
|
+
#assert (content_feat.size()[:2] == style_feat.size()[:2])
|
|
18
|
+
size = comp_feat.size()
|
|
19
|
+
#style_mean, style_std = calc_mean_std(style_feat) # the style features
|
|
20
|
+
downsample_mask_style = 1 - mask
|
|
21
|
+
style_mean, style_std = get_foreground_mean_std(comp_feat, downsample_mask_style) # the style features
|
|
22
|
+
fore_mean, fore_std = get_foreground_mean_std(comp_feat, mask) # the foreground features
|
|
23
|
+
|
|
24
|
+
normalized_feat = (comp_feat - fore_mean.expand(size)) / fore_std.expand(size)
|
|
25
|
+
return (normalized_feat * style_std.expand(size) + style_mean.expand(size)) * mask + (comp_feat * (1 - mask))
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
decoder_cat = nn.Sequential(
|
|
29
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
30
|
+
nn.Conv2d(512, 256, (3, 3)),
|
|
31
|
+
nn.ReLU(), # relu1-1
|
|
32
|
+
nn.Upsample(scale_factor=2, mode='nearest'),
|
|
33
|
+
nn.ReflectionPad2d((1, 1, 1, 1)), # 4
|
|
34
|
+
nn.Conv2d(512, 256, (3, 3)),
|
|
35
|
+
nn.ReLU(),
|
|
36
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
37
|
+
nn.Conv2d(256, 256, (3, 3)),
|
|
38
|
+
nn.ReLU(),
|
|
39
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
40
|
+
nn.Conv2d(256, 256, (3, 3)),
|
|
41
|
+
nn.ReLU(),
|
|
42
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
43
|
+
nn.Conv2d(256, 128, (3, 3)),
|
|
44
|
+
nn.ReLU(), # relu2-1
|
|
45
|
+
nn.Upsample(scale_factor=2, mode='nearest'),
|
|
46
|
+
nn.ReflectionPad2d((1, 1, 1, 1)), # 17
|
|
47
|
+
nn.Conv2d(256, 128, (3, 3)),
|
|
48
|
+
nn.ReLU(),
|
|
49
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
50
|
+
nn.Conv2d(128, 64, (3, 3)),
|
|
51
|
+
nn.ReLU(),
|
|
52
|
+
nn.Upsample(scale_factor=2, mode='nearest'),
|
|
53
|
+
nn.ReflectionPad2d((1, 1, 1, 1)), # 24
|
|
54
|
+
nn.Conv2d(128, 64, (3, 3)),
|
|
55
|
+
nn.ReLU(),
|
|
56
|
+
nn.Conv2d(65, 1, (1, 1),padding=0,stride=1), ##matting layer
|
|
57
|
+
nn.ReflectionPad2d((1, 1, 1, 1)), # 29
|
|
58
|
+
nn.Conv2d(64, 3, (3, 3)),
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
vgg = nn.Sequential(
|
|
63
|
+
nn.Conv2d(3, 3, (1, 1)),
|
|
64
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
65
|
+
nn.Conv2d(3, 64, (3, 3)),
|
|
66
|
+
nn.ReLU(), # relu1-1
|
|
67
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
68
|
+
nn.Conv2d(64, 64, (3, 3)),
|
|
69
|
+
nn.ReLU(), # relu1-2
|
|
70
|
+
nn.MaxPool2d((2, 2), (2, 2), (0, 0), ceil_mode=True),
|
|
71
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
72
|
+
nn.Conv2d(64, 128, (3, 3)),
|
|
73
|
+
nn.ReLU(), # relu2-1
|
|
74
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
75
|
+
nn.Conv2d(128, 128, (3, 3)),
|
|
76
|
+
nn.ReLU(), # relu2-2
|
|
77
|
+
nn.MaxPool2d((2, 2), (2, 2), (0, 0), ceil_mode=True),
|
|
78
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
79
|
+
nn.Conv2d(128, 256, (3, 3)),
|
|
80
|
+
nn.ReLU(), # relu3-1
|
|
81
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
82
|
+
nn.Conv2d(256, 256, (3, 3)),
|
|
83
|
+
nn.ReLU(), # relu3-2
|
|
84
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
85
|
+
nn.Conv2d(256, 256, (3, 3)),
|
|
86
|
+
nn.ReLU(), # relu3-3
|
|
87
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
88
|
+
nn.Conv2d(256, 256, (3, 3)),
|
|
89
|
+
nn.ReLU(), # relu3-4
|
|
90
|
+
nn.MaxPool2d((2, 2), (2, 2), (0, 0), ceil_mode=True),
|
|
91
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
92
|
+
nn.Conv2d(256, 512, (3, 3)),
|
|
93
|
+
nn.ReLU(), # relu4-1, this is the last layer used
|
|
94
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
95
|
+
nn.Conv2d(512, 512, (3, 3)),
|
|
96
|
+
nn.ReLU(), # relu4-2
|
|
97
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
98
|
+
nn.Conv2d(512, 512, (3, 3)),
|
|
99
|
+
nn.ReLU(), # relu4-3
|
|
100
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
101
|
+
nn.Conv2d(512, 512, (3, 3)),
|
|
102
|
+
nn.ReLU(), # relu4-4
|
|
103
|
+
nn.MaxPool2d((2, 2), (2, 2), (0, 0), ceil_mode=True),
|
|
104
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
105
|
+
nn.Conv2d(512, 512, (3, 3)),
|
|
106
|
+
nn.ReLU(), # relu5-1
|
|
107
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
108
|
+
nn.Conv2d(512, 512, (3, 3)),
|
|
109
|
+
nn.ReLU(), # relu5-2
|
|
110
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
111
|
+
nn.Conv2d(512, 512, (3, 3)),
|
|
112
|
+
nn.ReLU(), # relu5-3
|
|
113
|
+
nn.ReflectionPad2d((1, 1, 1, 1)),
|
|
114
|
+
nn.Conv2d(512, 512, (3, 3)),
|
|
115
|
+
nn.ReLU() # relu5-4
|
|
116
|
+
)
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def get_foreground_mean_std(features, mask, eps=1e-5):
|
|
121
|
+
region = features * mask
|
|
122
|
+
sum = torch.sum(region, dim=[2, 3]) # (B, C)
|
|
123
|
+
num = torch.sum(mask, dim=[2, 3]) # (B, C)
|
|
124
|
+
mu = sum / (num + eps)
|
|
125
|
+
mean = mu[:, :, None, None]
|
|
126
|
+
var = torch.sum((region + (1 - mask)*mean - mean) ** 2, dim=[2, 3]) / (num + eps)
|
|
127
|
+
var = var[:, :, None, None]
|
|
128
|
+
std = torch.sqrt(var+eps)
|
|
129
|
+
return mean, std
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
class PHDNet(nn.Module):
|
|
133
|
+
def __init__(self, vgg, decoder):
|
|
134
|
+
super(PHDNet, self).__init__()
|
|
135
|
+
# load the pretrained VGG encoder
|
|
136
|
+
vgg_layers = list(vgg.children())
|
|
137
|
+
self.enc_1 = nn.Sequential(*vgg_layers[:4]) # input -> relu1_1
|
|
138
|
+
self.enc_2 = nn.Sequential(*vgg_layers[4:11]) # relu1_1 -> relu2_1
|
|
139
|
+
self.enc_3 = nn.Sequential(*vgg_layers[11:18]) # relu2_1 -> relu3_1
|
|
140
|
+
self.enc_4 = nn.Sequential(*vgg_layers[18:31]) # relu3_1 -> relu4_1
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
# define the decoder
|
|
144
|
+
self.decoder = decoder
|
|
145
|
+
dec_layers = list(decoder.children())
|
|
146
|
+
self.dec_1 = nn.Sequential(*dec_layers[:4])
|
|
147
|
+
self.dec_2 = nn.Sequential(*dec_layers[4:17])
|
|
148
|
+
self.dec_3 = nn.Sequential(*dec_layers[17:24])
|
|
149
|
+
self.dec_4 = nn.Sequential(*dec_layers[24:27])
|
|
150
|
+
self.conv_attention = nn.Sequential(*dec_layers[27:28])
|
|
151
|
+
self.dec_4_2 = nn.Sequential(*dec_layers[28:])
|
|
152
|
+
|
|
153
|
+
# fix the VGG encoder
|
|
154
|
+
for name in ['enc_1', 'enc_2', 'enc_3', 'enc_4']:
|
|
155
|
+
for param in getattr(self, name).parameters():
|
|
156
|
+
param.requires_grad = False
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
# extract relu1_1, relu2_1, relu3_1, relu4_1 from input image
|
|
160
|
+
def encode_with_intermediate(self, input):
|
|
161
|
+
results = [input]
|
|
162
|
+
for i in range(4):
|
|
163
|
+
func = getattr(self, 'enc_{:d}'.format(i + 1))
|
|
164
|
+
results.append(func(results[-1]))
|
|
165
|
+
return results[1:]
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def decode(self, comp, mask, enc_features):
|
|
169
|
+
width = height = enc_features[-1].size(-1)
|
|
170
|
+
downsample_mask = self.downsample(mask, width, height)
|
|
171
|
+
t = adain_fg(enc_features[-1], downsample_mask)
|
|
172
|
+
dec_feature = self.dec_1(t)
|
|
173
|
+
|
|
174
|
+
for i in range(1,4):
|
|
175
|
+
func = getattr(self, 'dec_{:d}'.format(i + 1))
|
|
176
|
+
width = height = enc_features[-(i+1)].size(-1)
|
|
177
|
+
downsample_mask = self.downsample(mask, width, height)
|
|
178
|
+
t = adain_fg(enc_features[-(i+1)], downsample_mask)
|
|
179
|
+
dec_feature = func(torch.cat([dec_feature, t], dim=1))
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
width = height = dec_feature.size(-1)
|
|
183
|
+
downsample_mask = self.downsample(mask, width, height)
|
|
184
|
+
|
|
185
|
+
attention_map = torch.sigmoid(self.conv_attention(torch.cat([dec_feature, downsample_mask], dim=1)))
|
|
186
|
+
coarse_output = self.dec_4_2(dec_feature)
|
|
187
|
+
output = attention_map * coarse_output + (1.0 - attention_map) * comp
|
|
188
|
+
return output
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def downsample(self, image_tensor, width, height):
|
|
192
|
+
image_upsample_tensor = torch.nn.functional.interpolate(image_tensor, size=[width, height])
|
|
193
|
+
image_upsample_tensor = image_upsample_tensor.clamp(0, 1)
|
|
194
|
+
return image_upsample_tensor
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def forward(self, comp, mask):
|
|
198
|
+
comb_feats = self.encode_with_intermediate(comp)
|
|
199
|
+
final_output = self.decode(comp, mask, comb_feats)
|
|
200
|
+
|
|
201
|
+
return final_output
|
|
202
|
+
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from . import networks
|
|
3
|
+
from torch import nn
|
|
4
|
+
import os
|
|
5
|
+
|
|
6
|
+
class PHDNet(nn.Module):
|
|
7
|
+
def __init__(self, device):
|
|
8
|
+
super(PHDNet, self).__init__()
|
|
9
|
+
# define networks
|
|
10
|
+
self.device = device
|
|
11
|
+
self.netvgg = networks.vgg
|
|
12
|
+
self.netvgg = nn.Sequential(*list(self.netvgg.children())[:31])
|
|
13
|
+
self.netDecoder = networks.decoder_cat
|
|
14
|
+
self.netG = networks.PHDNet(self.netvgg, self.netDecoder)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def forward(self, comp, mask):
|
|
18
|
+
"""Employ generator to generate the output"""
|
|
19
|
+
if len(comp.shape) == 3:
|
|
20
|
+
comp = comp.unsqueeze(0)
|
|
21
|
+
mask = mask.unsqueeze(0)
|
|
22
|
+
|
|
23
|
+
self.comp = comp.to(self.device)
|
|
24
|
+
self.mask = mask.to(self.device)
|
|
25
|
+
|
|
26
|
+
output = self.netG(self.comp, self.mask)
|
|
27
|
+
return output
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def load_networks(self, load_path):
|
|
31
|
+
"""Load all the networks from the disk.
|
|
32
|
+
|
|
33
|
+
Parameters:
|
|
34
|
+
load_path (str) -- path to load parameters
|
|
35
|
+
"""
|
|
36
|
+
if os.path.exists(load_path):
|
|
37
|
+
net = getattr(self, 'netG')
|
|
38
|
+
if isinstance(net, torch.nn.DataParallel):
|
|
39
|
+
net = net.module
|
|
40
|
+
# print('loading the model from %s' % load_path)
|
|
41
|
+
state_dict = torch.load(load_path, map_location=self.device,weights_only=True)
|
|
42
|
+
if hasattr(state_dict, '_metadata'):
|
|
43
|
+
del state_dict._metadata
|
|
44
|
+
|
|
45
|
+
net.load_state_dict(state_dict, strict=True)
|
|
46
|
+
else:
|
|
47
|
+
print('failed loading model from {}'.format(load_path))
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import os
|
|
2
|
+
|
|
3
|
+
class Config(object):
|
|
4
|
+
# config
|
|
5
|
+
backbone = 'resnet18'
|
|
6
|
+
model = 'phdnet'
|
|
7
|
+
dataset_mode = 'phd'
|
|
8
|
+
preprocess = 'none'
|
|
9
|
+
batch_size = 1
|
|
10
|
+
num_threads = 6
|
|
11
|
+
gpu_ids = [2]
|
|
12
|
+
load_size = 512
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
opt = Config()
|
|
16
|
+
|
|
17
|
+
if __name__ == "__main__":
|
|
18
|
+
print(opt.exp_root)
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import argparse
|
|
2
|
+
import torch
|
|
3
|
+
from omegaconf import OmegaConf
|
|
4
|
+
|
|
5
|
+
from libcom.painterly_image_harmonization.source.ldm.models.diffusion.ddim import DDIMSampler
|
|
6
|
+
from libcom.painterly_image_harmonization.source.ldm.models.diffusion.plms import PLMSSampler
|
|
7
|
+
from libcom.painterly_image_harmonization.source.ldm.modules.encoders.adapter import Adapter, StyleAdapter, Adapter_light
|
|
8
|
+
from libcom.painterly_image_harmonization.source.ldm.modules.extra_condition.api import ExtraCondition
|
|
9
|
+
from libcom.painterly_image_harmonization.source.ldm.util import fix_cond_shapes, load_model_from_config, read_state_dict
|
|
10
|
+
|
|
11
|
+
DEFAULT_NEGATIVE_PROMPT = 'longbody, lowres, bad anatomy, bad hands, missing fingers, extra digit, ' \
|
|
12
|
+
'fewer digits, cropped, worst quality, low quality'
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def get_base_argument_parser() -> argparse.ArgumentParser:
|
|
16
|
+
"""get the base argument parser for inference scripts"""
|
|
17
|
+
parser = argparse.ArgumentParser()
|
|
18
|
+
parser.add_argument(
|
|
19
|
+
'--outdir',
|
|
20
|
+
type=str,
|
|
21
|
+
help='dir to write results to',
|
|
22
|
+
default=None,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
parser.add_argument(
|
|
26
|
+
'--prompt',
|
|
27
|
+
type=str,
|
|
28
|
+
nargs='?',
|
|
29
|
+
default=None,
|
|
30
|
+
help='positive prompt',
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
parser.add_argument(
|
|
34
|
+
'--neg_prompt',
|
|
35
|
+
type=str,
|
|
36
|
+
default=DEFAULT_NEGATIVE_PROMPT,
|
|
37
|
+
help='negative prompt',
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
parser.add_argument(
|
|
41
|
+
'--cond_path',
|
|
42
|
+
type=str,
|
|
43
|
+
default=None,
|
|
44
|
+
help='condition image path',
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
parser.add_argument(
|
|
48
|
+
'--cond_inp_type',
|
|
49
|
+
type=str,
|
|
50
|
+
default='image',
|
|
51
|
+
help='the type of the input condition image, take depth T2I as example, the input can be raw image, '
|
|
52
|
+
'which depth will be calculated, or the input can be a directly a depth map image',
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
parser.add_argument(
|
|
56
|
+
'--sampler',
|
|
57
|
+
type=str,
|
|
58
|
+
default='ddim',
|
|
59
|
+
choices=['ddim', 'plms'],
|
|
60
|
+
help='sampling algorithm, currently, only ddim and plms are supported, more are on the way',
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
parser.add_argument(
|
|
64
|
+
'--steps',
|
|
65
|
+
type=int,
|
|
66
|
+
default=50,
|
|
67
|
+
help='number of sampling steps',
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
parser.add_argument(
|
|
71
|
+
'--sd_ckpt',
|
|
72
|
+
type=str,
|
|
73
|
+
default='models/sd-v1-4.ckpt',
|
|
74
|
+
help='path to checkpoint of stable diffusion model, both .ckpt and .safetensor are supported',
|
|
75
|
+
)
|
|
76
|
+
|
|
77
|
+
parser.add_argument(
|
|
78
|
+
'--vae_ckpt',
|
|
79
|
+
type=str,
|
|
80
|
+
default=None,
|
|
81
|
+
help='vae checkpoint, anime SD models usually have seperate vae ckpt that need to be loaded',
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
parser.add_argument(
|
|
85
|
+
'--adapter_ckpt',
|
|
86
|
+
type=str,
|
|
87
|
+
default=None,
|
|
88
|
+
help='path to checkpoint of adapter',
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
parser.add_argument(
|
|
92
|
+
'--config',
|
|
93
|
+
type=str,
|
|
94
|
+
default='configs/stable-diffusion/sd-v1-inference.yaml',
|
|
95
|
+
help='path to config which constructs SD model',
|
|
96
|
+
)
|
|
97
|
+
|
|
98
|
+
parser.add_argument(
|
|
99
|
+
'--max_resolution',
|
|
100
|
+
type=float,
|
|
101
|
+
default=512 * 512,
|
|
102
|
+
help='max image height * width, only for computer with limited vram',
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
parser.add_argument(
|
|
106
|
+
'--resize_short_edge',
|
|
107
|
+
type=int,
|
|
108
|
+
default=None,
|
|
109
|
+
help='resize short edge of the input image, if this arg is set, max_resolution will not be used',
|
|
110
|
+
)
|
|
111
|
+
|
|
112
|
+
parser.add_argument(
|
|
113
|
+
'--C',
|
|
114
|
+
type=int,
|
|
115
|
+
default=4,
|
|
116
|
+
help='latent channels',
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
parser.add_argument(
|
|
120
|
+
'--f',
|
|
121
|
+
type=int,
|
|
122
|
+
default=8,
|
|
123
|
+
help='downsampling factor',
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
parser.add_argument(
|
|
127
|
+
'--scale',
|
|
128
|
+
type=float,
|
|
129
|
+
default=7.5,
|
|
130
|
+
help='unconditional guidance scale: eps = eps(x, empty) + scale * (eps(x, cond) - eps(x, empty))',
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
parser.add_argument(
|
|
134
|
+
'--cond_tau',
|
|
135
|
+
type=float,
|
|
136
|
+
default=1.0,
|
|
137
|
+
help='timestamp parameter that determines until which step the adapter is applied, '
|
|
138
|
+
'similar as Prompt-to-Prompt tau',
|
|
139
|
+
)
|
|
140
|
+
|
|
141
|
+
parser.add_argument(
|
|
142
|
+
'--style_cond_tau',
|
|
143
|
+
type=float,
|
|
144
|
+
default=1.0,
|
|
145
|
+
help='timestamp parameter that determines until which step the adapter is applied, '
|
|
146
|
+
'similar as Prompt-to-Prompt tau',
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
parser.add_argument(
|
|
150
|
+
'--cond_weight',
|
|
151
|
+
type=float,
|
|
152
|
+
default=1.0,
|
|
153
|
+
help='the adapter features are multiplied by the cond_weight. The larger the cond_weight, the more aligned '
|
|
154
|
+
'the generated image and condition will be, but the generated quality may be reduced',
|
|
155
|
+
)
|
|
156
|
+
|
|
157
|
+
parser.add_argument(
|
|
158
|
+
'--seed',
|
|
159
|
+
type=int,
|
|
160
|
+
default=42,
|
|
161
|
+
)
|
|
162
|
+
|
|
163
|
+
parser.add_argument(
|
|
164
|
+
'--n_samples',
|
|
165
|
+
type=int,
|
|
166
|
+
default=4,
|
|
167
|
+
help='# of samples to generate',
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
return parser
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def get_sd_models(opt):
|
|
174
|
+
"""
|
|
175
|
+
build stable diffusion model, sampler
|
|
176
|
+
"""
|
|
177
|
+
# SD
|
|
178
|
+
config = OmegaConf.load(f"{opt.config}")
|
|
179
|
+
model = load_model_from_config(config, opt.sd_ckpt, opt.vae_ckpt)
|
|
180
|
+
sd_model = model.to(opt.device)
|
|
181
|
+
|
|
182
|
+
# sampler
|
|
183
|
+
if opt.sampler == 'plms':
|
|
184
|
+
sampler = PLMSSampler(model)
|
|
185
|
+
elif opt.sampler == 'ddim':
|
|
186
|
+
sampler = DDIMSampler(model)
|
|
187
|
+
else:
|
|
188
|
+
raise NotImplementedError
|
|
189
|
+
|
|
190
|
+
return sd_model, sampler
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def get_t2i_adapter_models(opt):
|
|
194
|
+
config = OmegaConf.load(f"{opt.config}")
|
|
195
|
+
model = load_model_from_config(config, opt.sd_ckpt, opt.vae_ckpt)
|
|
196
|
+
adapter_ckpt_path = getattr(opt, f'{opt.which_cond}_adapter_ckpt', None)
|
|
197
|
+
if adapter_ckpt_path is None:
|
|
198
|
+
adapter_ckpt_path = getattr(opt, 'adapter_ckpt')
|
|
199
|
+
adapter_ckpt = read_state_dict(adapter_ckpt_path)
|
|
200
|
+
new_state_dict = {}
|
|
201
|
+
for k, v in adapter_ckpt.items():
|
|
202
|
+
if not k.startswith('adapter.'):
|
|
203
|
+
new_state_dict[f'adapter.{k}'] = v
|
|
204
|
+
else:
|
|
205
|
+
new_state_dict[k] = v
|
|
206
|
+
m, u = model.load_state_dict(new_state_dict, strict=False)
|
|
207
|
+
if len(u) > 0:
|
|
208
|
+
print(f"unexpected keys in loading adapter ckpt {adapter_ckpt_path}:")
|
|
209
|
+
print(u)
|
|
210
|
+
|
|
211
|
+
model = model.to(opt.device)
|
|
212
|
+
|
|
213
|
+
# sampler
|
|
214
|
+
if opt.sampler == 'plms':
|
|
215
|
+
sampler = PLMSSampler(model)
|
|
216
|
+
elif opt.sampler == 'ddim':
|
|
217
|
+
sampler = DDIMSampler(model)
|
|
218
|
+
else:
|
|
219
|
+
raise NotImplementedError
|
|
220
|
+
|
|
221
|
+
return model, sampler
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def get_cond_ch(cond_type: ExtraCondition):
|
|
225
|
+
if cond_type == ExtraCondition.sketch or cond_type == ExtraCondition.canny:
|
|
226
|
+
return 1
|
|
227
|
+
return 3
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
def get_adapters(opt, cond_type: ExtraCondition):
|
|
231
|
+
adapter = {}
|
|
232
|
+
cond_weight = getattr(opt, f'{cond_type.name}_weight', None)
|
|
233
|
+
if cond_weight is None:
|
|
234
|
+
cond_weight = getattr(opt, 'cond_weight')
|
|
235
|
+
adapter['cond_weight'] = cond_weight
|
|
236
|
+
|
|
237
|
+
if cond_type == ExtraCondition.style:
|
|
238
|
+
adapter['model'] = StyleAdapter(width=1024, context_dim=768, num_head=8, n_layes=3, num_token=8).to(opt.device)
|
|
239
|
+
elif cond_type == ExtraCondition.color:
|
|
240
|
+
adapter['model'] = Adapter_light(
|
|
241
|
+
cin=64 * get_cond_ch(cond_type),
|
|
242
|
+
channels=[320, 640, 1280, 1280],
|
|
243
|
+
nums_rb=4).to(opt.device)
|
|
244
|
+
else:
|
|
245
|
+
adapter['model'] = Adapter(
|
|
246
|
+
cin=64 * get_cond_ch(cond_type),
|
|
247
|
+
channels=[320, 640, 1280, 1280][:4],
|
|
248
|
+
nums_rb=2,
|
|
249
|
+
ksize=1,
|
|
250
|
+
sk=True,
|
|
251
|
+
use_conv=False).to(opt.device)
|
|
252
|
+
ckpt_path = getattr(opt, f'{cond_type.name}_adapter_ckpt', None)
|
|
253
|
+
if ckpt_path is None:
|
|
254
|
+
ckpt_path = getattr(opt, 'adapter_ckpt')
|
|
255
|
+
state_dict = read_state_dict(ckpt_path)
|
|
256
|
+
new_state_dict = {}
|
|
257
|
+
for k, v in state_dict.items():
|
|
258
|
+
if k.startswith('adapter.'):
|
|
259
|
+
new_state_dict[k[len('adapter.'):]] = v
|
|
260
|
+
else:
|
|
261
|
+
new_state_dict[k] = v
|
|
262
|
+
|
|
263
|
+
adapter['model'].load_state_dict(new_state_dict)
|
|
264
|
+
|
|
265
|
+
return adapter
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def diffusion_inference(opt, model, sampler, adapter_features, append_to_context=None):
|
|
269
|
+
# get text embedding
|
|
270
|
+
c = model.get_learned_conditioning([opt.prompt])
|
|
271
|
+
if opt.scale != 1.0:
|
|
272
|
+
uc = model.get_learned_conditioning([opt.neg_prompt])
|
|
273
|
+
else:
|
|
274
|
+
uc = None
|
|
275
|
+
c, uc = fix_cond_shapes(model, c, uc)
|
|
276
|
+
|
|
277
|
+
if not hasattr(opt, 'H'):
|
|
278
|
+
opt.H = 512
|
|
279
|
+
opt.W = 512
|
|
280
|
+
shape = [opt.C, opt.H // opt.f, opt.W // opt.f]
|
|
281
|
+
|
|
282
|
+
samples_latents, _ = sampler.sample(
|
|
283
|
+
S=opt.steps,
|
|
284
|
+
conditioning=c,
|
|
285
|
+
batch_size=1,
|
|
286
|
+
shape=shape,
|
|
287
|
+
verbose=False,
|
|
288
|
+
unconditional_guidance_scale=opt.scale,
|
|
289
|
+
unconditional_conditioning=uc,
|
|
290
|
+
x_T=None,
|
|
291
|
+
features_adapter=adapter_features,
|
|
292
|
+
append_to_context=append_to_context,
|
|
293
|
+
cond_tau=opt.cond_tau,
|
|
294
|
+
style_cond_tau=opt.style_cond_tau,
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
x_samples = model.decode_first_stage(samples_latents)
|
|
298
|
+
x_samples = torch.clamp((x_samples + 1.0) / 2.0, min=0.0, max=1.0)
|
|
299
|
+
|
|
300
|
+
return x_samples
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class LambdaWarmUpCosineScheduler:
|
|
5
|
+
"""
|
|
6
|
+
note: use with a base_lr of 1.0
|
|
7
|
+
"""
|
|
8
|
+
def __init__(self, warm_up_steps, lr_min, lr_max, lr_start, max_decay_steps, verbosity_interval=0):
|
|
9
|
+
self.lr_warm_up_steps = warm_up_steps
|
|
10
|
+
self.lr_start = lr_start
|
|
11
|
+
self.lr_min = lr_min
|
|
12
|
+
self.lr_max = lr_max
|
|
13
|
+
self.lr_max_decay_steps = max_decay_steps
|
|
14
|
+
self.last_lr = 0.
|
|
15
|
+
self.verbosity_interval = verbosity_interval
|
|
16
|
+
|
|
17
|
+
def schedule(self, n, **kwargs):
|
|
18
|
+
if self.verbosity_interval > 0:
|
|
19
|
+
if n % self.verbosity_interval == 0: print(f"current step: {n}, recent lr-multiplier: {self.last_lr}")
|
|
20
|
+
if n < self.lr_warm_up_steps:
|
|
21
|
+
lr = (self.lr_max - self.lr_start) / self.lr_warm_up_steps * n + self.lr_start
|
|
22
|
+
self.last_lr = lr
|
|
23
|
+
return lr
|
|
24
|
+
else:
|
|
25
|
+
t = (n - self.lr_warm_up_steps) / (self.lr_max_decay_steps - self.lr_warm_up_steps)
|
|
26
|
+
t = min(t, 1.0)
|
|
27
|
+
lr = self.lr_min + 0.5 * (self.lr_max - self.lr_min) * (
|
|
28
|
+
1 + np.cos(t * np.pi))
|
|
29
|
+
self.last_lr = lr
|
|
30
|
+
return lr
|
|
31
|
+
|
|
32
|
+
def __call__(self, n, **kwargs):
|
|
33
|
+
return self.schedule(n,**kwargs)
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
class LambdaWarmUpCosineScheduler2:
|
|
37
|
+
"""
|
|
38
|
+
supports repeated iterations, configurable via lists
|
|
39
|
+
note: use with a base_lr of 1.0.
|
|
40
|
+
"""
|
|
41
|
+
def __init__(self, warm_up_steps, f_min, f_max, f_start, cycle_lengths, verbosity_interval=0):
|
|
42
|
+
assert len(warm_up_steps) == len(f_min) == len(f_max) == len(f_start) == len(cycle_lengths)
|
|
43
|
+
self.lr_warm_up_steps = warm_up_steps
|
|
44
|
+
self.f_start = f_start
|
|
45
|
+
self.f_min = f_min
|
|
46
|
+
self.f_max = f_max
|
|
47
|
+
self.cycle_lengths = cycle_lengths
|
|
48
|
+
self.cum_cycles = np.cumsum([0] + list(self.cycle_lengths))
|
|
49
|
+
self.last_f = 0.
|
|
50
|
+
self.verbosity_interval = verbosity_interval
|
|
51
|
+
|
|
52
|
+
def find_in_interval(self, n):
|
|
53
|
+
interval = 0
|
|
54
|
+
for cl in self.cum_cycles[1:]:
|
|
55
|
+
if n <= cl:
|
|
56
|
+
return interval
|
|
57
|
+
interval += 1
|
|
58
|
+
|
|
59
|
+
def schedule(self, n, **kwargs):
|
|
60
|
+
cycle = self.find_in_interval(n)
|
|
61
|
+
n = n - self.cum_cycles[cycle]
|
|
62
|
+
if self.verbosity_interval > 0:
|
|
63
|
+
if n % self.verbosity_interval == 0: print(f"current step: {n}, recent lr-multiplier: {self.last_f}, "
|
|
64
|
+
f"current cycle {cycle}")
|
|
65
|
+
if n < self.lr_warm_up_steps[cycle]:
|
|
66
|
+
f = (self.f_max[cycle] - self.f_start[cycle]) / self.lr_warm_up_steps[cycle] * n + self.f_start[cycle]
|
|
67
|
+
self.last_f = f
|
|
68
|
+
return f
|
|
69
|
+
else:
|
|
70
|
+
t = (n - self.lr_warm_up_steps[cycle]) / (self.cycle_lengths[cycle] - self.lr_warm_up_steps[cycle])
|
|
71
|
+
t = min(t, 1.0)
|
|
72
|
+
f = self.f_min[cycle] + 0.5 * (self.f_max[cycle] - self.f_min[cycle]) * (
|
|
73
|
+
1 + np.cos(t * np.pi))
|
|
74
|
+
self.last_f = f
|
|
75
|
+
return f
|
|
76
|
+
|
|
77
|
+
def __call__(self, n, **kwargs):
|
|
78
|
+
return self.schedule(n, **kwargs)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class LambdaLinearScheduler(LambdaWarmUpCosineScheduler2):
|
|
82
|
+
|
|
83
|
+
def schedule(self, n, **kwargs):
|
|
84
|
+
cycle = self.find_in_interval(n)
|
|
85
|
+
n = n - self.cum_cycles[cycle]
|
|
86
|
+
if self.verbosity_interval > 0:
|
|
87
|
+
if n % self.verbosity_interval == 0: print(f"current step: {n}, recent lr-multiplier: {self.last_f}, "
|
|
88
|
+
f"current cycle {cycle}")
|
|
89
|
+
|
|
90
|
+
if n < self.lr_warm_up_steps[cycle]:
|
|
91
|
+
f = (self.f_max[cycle] - self.f_start[cycle]) / self.lr_warm_up_steps[cycle] * n + self.f_start[cycle]
|
|
92
|
+
self.last_f = f
|
|
93
|
+
return f
|
|
94
|
+
else:
|
|
95
|
+
f = self.f_min[cycle] + (self.f_max[cycle] - self.f_min[cycle]) * (self.cycle_lengths[cycle] - n) / (self.cycle_lengths[cycle])
|
|
96
|
+
self.last_f = f
|
|
97
|
+
return f
|
|
98
|
+
|
|
File without changes
|