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,377 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import math
|
|
3
|
+
from copy import deepcopy
|
|
4
|
+
from typing import List, Tuple
|
|
5
|
+
|
|
6
|
+
import torch
|
|
7
|
+
import torch.nn.functional as F
|
|
8
|
+
|
|
9
|
+
TILING_METHODS = ["average", "gaussian", "linear"]
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class Tiler:
|
|
13
|
+
def get_tiles(
|
|
14
|
+
self,
|
|
15
|
+
input: torch.Tensor,
|
|
16
|
+
tile_size: tuple,
|
|
17
|
+
overlap_size: tuple,
|
|
18
|
+
scale: int = 1,
|
|
19
|
+
out_channels: int = 3,
|
|
20
|
+
) -> List[List[torch.tensor]]:
|
|
21
|
+
"""Get tiles
|
|
22
|
+
Args:
|
|
23
|
+
input (torch.Tensor): input array of shape (batch_size, channels, height, width)
|
|
24
|
+
tile_size (tuple): tile size
|
|
25
|
+
overlap_size (tuple): overlap size
|
|
26
|
+
scale (int): scaling factor of the output wrt input
|
|
27
|
+
out_channels (int): number of output channels
|
|
28
|
+
Returns:
|
|
29
|
+
List[List[torch.Tensor]]: List of tiles
|
|
30
|
+
"""
|
|
31
|
+
# assert isinstance(scale, int)
|
|
32
|
+
assert (
|
|
33
|
+
overlap_size[0] <= tile_size[0]
|
|
34
|
+
), f"Overlap size {overlap_size} must be smaller than tile size {tile_size}"
|
|
35
|
+
assert (
|
|
36
|
+
overlap_size[1] <= tile_size[1]
|
|
37
|
+
), f"Overlap size {overlap_size} must be smaller than tile size {tile_size}"
|
|
38
|
+
|
|
39
|
+
B, C, H, W = input.shape
|
|
40
|
+
tile_size_H, tile_size_W = tile_size
|
|
41
|
+
|
|
42
|
+
# sets overlap to 0 if the input is smaller than the tile size (i.e. no overlap)
|
|
43
|
+
overlap_H, overlap_W = (
|
|
44
|
+
overlap_size[0] if H > tile_size_H else 0,
|
|
45
|
+
overlap_size[1] if W > tile_size_W else 0,
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
self.output_overlap_size = (
|
|
49
|
+
int(overlap_H * scale),
|
|
50
|
+
int(overlap_W * scale),
|
|
51
|
+
)
|
|
52
|
+
self.tile_size = tile_size
|
|
53
|
+
self.output_tile_size = (
|
|
54
|
+
int(tile_size_H * scale),
|
|
55
|
+
int(tile_size_W * scale),
|
|
56
|
+
)
|
|
57
|
+
self.output_shape = (
|
|
58
|
+
B,
|
|
59
|
+
out_channels,
|
|
60
|
+
int(H * scale),
|
|
61
|
+
int(W * scale),
|
|
62
|
+
)
|
|
63
|
+
tiles = []
|
|
64
|
+
logging.debug(f"(Tiler) Input shape: {(B, C, H, W)}")
|
|
65
|
+
logging.debug(f"(Tiler) Output shape: {self.output_shape}")
|
|
66
|
+
logging.debug(f"(Tiler) Tile size: {(tile_size_H, tile_size_W)}")
|
|
67
|
+
logging.debug(f"(Tiler) Overlap size: {(overlap_H, overlap_W)}")
|
|
68
|
+
# loop over all tiles in the image with overlap
|
|
69
|
+
for i in range(0, H, tile_size_H - overlap_H):
|
|
70
|
+
row = []
|
|
71
|
+
for j in range(0, W, tile_size_W - overlap_W):
|
|
72
|
+
tile = deepcopy(
|
|
73
|
+
input[
|
|
74
|
+
:,
|
|
75
|
+
:,
|
|
76
|
+
i : i + tile_size_H,
|
|
77
|
+
j : j + tile_size_W,
|
|
78
|
+
]
|
|
79
|
+
)
|
|
80
|
+
row.append(tile)
|
|
81
|
+
tiles.append(row)
|
|
82
|
+
return tiles
|
|
83
|
+
|
|
84
|
+
def merge_tiles(
|
|
85
|
+
self, tiles: List[List[torch.tensor]], tiling_method: str = "gaussian"
|
|
86
|
+
) -> torch.tensor:
|
|
87
|
+
"""Merge tiles by averaging the overlaping regions
|
|
88
|
+
Args:
|
|
89
|
+
tiles (Dict[str, Tile]): dictionary of processed tiles
|
|
90
|
+
tiling_method (str): tiling method. Can be "average", "gaussian" or "linear"
|
|
91
|
+
Returns:
|
|
92
|
+
torch.tensor: output image
|
|
93
|
+
"""
|
|
94
|
+
if tiling_method == "average":
|
|
95
|
+
return self._average_merge_tiles(tiles)
|
|
96
|
+
elif tiling_method == "gaussian":
|
|
97
|
+
return self._gaussian_merge_tiles(tiles)
|
|
98
|
+
elif tiling_method == "linear":
|
|
99
|
+
return self._linear_merge_tiles(tiles)
|
|
100
|
+
else:
|
|
101
|
+
raise ValueError(
|
|
102
|
+
f"Unknown tiling method {tiling_method}. Available methods are {TILING_METHODS}"
|
|
103
|
+
)
|
|
104
|
+
|
|
105
|
+
def _average_merge_tiles(self, tiles: List[List[torch.tensor]]) -> torch.tensor:
|
|
106
|
+
"""Merge tiles by averaging the overlaping regions
|
|
107
|
+
Args:
|
|
108
|
+
tiles (Dict[str, Tile]): dictionary of processed tiles
|
|
109
|
+
Returns:
|
|
110
|
+
torch.tensor: output image
|
|
111
|
+
"""
|
|
112
|
+
|
|
113
|
+
output = torch.zeros(self.output_shape)
|
|
114
|
+
|
|
115
|
+
# weights to store multiplicity
|
|
116
|
+
weights = torch.zeros(self.output_shape)
|
|
117
|
+
|
|
118
|
+
_, _, output_H, output_W = self.output_shape
|
|
119
|
+
output_overlap_size_H, output_overlap_size_W = self.output_overlap_size
|
|
120
|
+
output_tile_size_H, output_tile_size_W = self.output_tile_size
|
|
121
|
+
|
|
122
|
+
for id_i, i in enumerate(
|
|
123
|
+
range(
|
|
124
|
+
0,
|
|
125
|
+
output_H,
|
|
126
|
+
output_tile_size_H - output_overlap_size_H,
|
|
127
|
+
)
|
|
128
|
+
):
|
|
129
|
+
for id_j, j in enumerate(
|
|
130
|
+
range(
|
|
131
|
+
0,
|
|
132
|
+
output_W,
|
|
133
|
+
output_tile_size_W - output_overlap_size_W,
|
|
134
|
+
)
|
|
135
|
+
):
|
|
136
|
+
output[
|
|
137
|
+
:,
|
|
138
|
+
:,
|
|
139
|
+
i : i + output_tile_size_H,
|
|
140
|
+
j : j + output_tile_size_W,
|
|
141
|
+
] += (
|
|
142
|
+
tiles[id_i][id_j] * 1
|
|
143
|
+
)
|
|
144
|
+
weights[
|
|
145
|
+
:,
|
|
146
|
+
:,
|
|
147
|
+
i : i + output_tile_size_H,
|
|
148
|
+
j : j + output_tile_size_W,
|
|
149
|
+
] += 1
|
|
150
|
+
|
|
151
|
+
# outputs is summed up with this multiplicity
|
|
152
|
+
# so we need to divide by the weights wich is either 1, 2 or 4 depending on the region
|
|
153
|
+
output = output / weights
|
|
154
|
+
return output
|
|
155
|
+
|
|
156
|
+
def _gaussian_weights(
|
|
157
|
+
self, tile_width: int, tile_height: int, nbatches: int, channels: int
|
|
158
|
+
):
|
|
159
|
+
"""Generates a gaussian mask of weights for tile contributions.
|
|
160
|
+
|
|
161
|
+
Args:
|
|
162
|
+
tile_width (int): width of the tile
|
|
163
|
+
tile_height (int): height of the tile
|
|
164
|
+
nbatches (int): number of batches
|
|
165
|
+
channels (int): number of channels
|
|
166
|
+
Returns:
|
|
167
|
+
torch.tensor: weights
|
|
168
|
+
"""
|
|
169
|
+
import numpy as np
|
|
170
|
+
from numpy import exp, pi, sqrt
|
|
171
|
+
|
|
172
|
+
latent_width = tile_width
|
|
173
|
+
latent_height = tile_height
|
|
174
|
+
|
|
175
|
+
var = 0.01
|
|
176
|
+
midpoint = (
|
|
177
|
+
latent_width - 1
|
|
178
|
+
) / 2 # -1 because index goes from 0 to latent_width - 1
|
|
179
|
+
x_probs = [
|
|
180
|
+
exp(
|
|
181
|
+
-(x - midpoint)
|
|
182
|
+
* (x - midpoint)
|
|
183
|
+
/ (latent_width * latent_width)
|
|
184
|
+
/ (2 * var)
|
|
185
|
+
)
|
|
186
|
+
/ sqrt(2 * pi * var)
|
|
187
|
+
for x in range(latent_width)
|
|
188
|
+
]
|
|
189
|
+
midpoint = latent_height / 2
|
|
190
|
+
y_probs = [
|
|
191
|
+
exp(
|
|
192
|
+
-(y - midpoint)
|
|
193
|
+
* (y - midpoint)
|
|
194
|
+
/ (latent_height * latent_height)
|
|
195
|
+
/ (2 * var)
|
|
196
|
+
)
|
|
197
|
+
/ sqrt(2 * pi * var)
|
|
198
|
+
for y in range(latent_height)
|
|
199
|
+
]
|
|
200
|
+
|
|
201
|
+
weights = np.outer(y_probs, x_probs)
|
|
202
|
+
return torch.tile(
|
|
203
|
+
torch.tensor(weights, device="cpu"), (nbatches, channels, 1, 1)
|
|
204
|
+
)
|
|
205
|
+
|
|
206
|
+
def _gaussian_merge_tiles(self, tiles: List[List[torch.tensor]]) -> torch.tensor:
|
|
207
|
+
"""Merge tiles by averaging the overlaping regions
|
|
208
|
+
Args:
|
|
209
|
+
List[List[torch.tensor]]: List of processed tiles
|
|
210
|
+
Returns:
|
|
211
|
+
torch.tensor: output image
|
|
212
|
+
"""
|
|
213
|
+
B, output_C, output_H, output_W = self.output_shape
|
|
214
|
+
output_overlap_size_H, output_overlap_size_W = self.output_overlap_size
|
|
215
|
+
output_tile_size_H, output_tile_size_W = self.output_tile_size
|
|
216
|
+
|
|
217
|
+
output = torch.zeros(self.output_shape)
|
|
218
|
+
# weights to store multiplicity
|
|
219
|
+
weights = torch.zeros(self.output_shape)
|
|
220
|
+
|
|
221
|
+
for id_i, i in enumerate(
|
|
222
|
+
range(
|
|
223
|
+
0,
|
|
224
|
+
output_H,
|
|
225
|
+
output_tile_size_H - output_overlap_size_H,
|
|
226
|
+
)
|
|
227
|
+
):
|
|
228
|
+
for id_j, j in enumerate(
|
|
229
|
+
range(
|
|
230
|
+
0,
|
|
231
|
+
output_W,
|
|
232
|
+
output_tile_size_W - output_overlap_size_W,
|
|
233
|
+
)
|
|
234
|
+
):
|
|
235
|
+
w = self._gaussian_weights(
|
|
236
|
+
tiles[id_i][id_j].shape[3],
|
|
237
|
+
tiles[id_i][id_j].shape[2],
|
|
238
|
+
B,
|
|
239
|
+
output_C,
|
|
240
|
+
)
|
|
241
|
+
output[
|
|
242
|
+
:,
|
|
243
|
+
:,
|
|
244
|
+
i : i + output_tile_size_H,
|
|
245
|
+
j : j + output_tile_size_W,
|
|
246
|
+
] += (
|
|
247
|
+
tiles[id_i][id_j] * w
|
|
248
|
+
)
|
|
249
|
+
weights[
|
|
250
|
+
:,
|
|
251
|
+
:,
|
|
252
|
+
i : i + output_tile_size_H,
|
|
253
|
+
j : j + output_tile_size_W,
|
|
254
|
+
] += w
|
|
255
|
+
|
|
256
|
+
# outputs is summed up with this multiplicity
|
|
257
|
+
output = output / weights
|
|
258
|
+
return output
|
|
259
|
+
|
|
260
|
+
def _blend_v(
|
|
261
|
+
self, a: torch.Tensor, b: torch.Tensor, blend_extent: int
|
|
262
|
+
) -> torch.Tensor:
|
|
263
|
+
blend_extent = min(a.shape[2], b.shape[2], blend_extent)
|
|
264
|
+
for y in range(blend_extent):
|
|
265
|
+
b[:, :, y, :] = a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[
|
|
266
|
+
:, :, y, :
|
|
267
|
+
] * (y / blend_extent)
|
|
268
|
+
return b
|
|
269
|
+
|
|
270
|
+
def _blend_h(
|
|
271
|
+
self, a: torch.Tensor, b: torch.Tensor, blend_extent: int
|
|
272
|
+
) -> torch.Tensor:
|
|
273
|
+
blend_extent = min(a.shape[3], b.shape[3], blend_extent)
|
|
274
|
+
for x in range(blend_extent):
|
|
275
|
+
b[:, :, :, x] = a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[
|
|
276
|
+
:, :, :, x
|
|
277
|
+
] * (x / blend_extent)
|
|
278
|
+
return b
|
|
279
|
+
|
|
280
|
+
def _linear_merge_tiles(self, tiles: List[List[torch.tensor]]) -> torch.Tensor:
|
|
281
|
+
"""Merge tiles by blending the overlaping regions
|
|
282
|
+
Args:
|
|
283
|
+
tiles (List[List[torch.tensor]]): List of processed tiles
|
|
284
|
+
Returns:
|
|
285
|
+
torch.Tensor: output image
|
|
286
|
+
"""
|
|
287
|
+
output_overlap_size_H, output_overlap_size_W = self.output_overlap_size
|
|
288
|
+
output_tile_size_H, output_tile_size_W = self.output_tile_size
|
|
289
|
+
|
|
290
|
+
res_rows = []
|
|
291
|
+
tiles_copy = deepcopy(tiles)
|
|
292
|
+
|
|
293
|
+
# Cut the right and bottom overlap region
|
|
294
|
+
limit_i = output_tile_size_H - output_overlap_size_H
|
|
295
|
+
limit_j = output_tile_size_W - output_overlap_size_W
|
|
296
|
+
for i, tile_row in enumerate(tiles_copy):
|
|
297
|
+
res_row = []
|
|
298
|
+
for j, tile in enumerate(tile_row):
|
|
299
|
+
tile_val = tile
|
|
300
|
+
if j > 0:
|
|
301
|
+
tile_val = self._blend_h(
|
|
302
|
+
tile_row[j - 1], tile, output_overlap_size_W
|
|
303
|
+
)
|
|
304
|
+
tiles_copy[i][j] = tile_val
|
|
305
|
+
if i > 0:
|
|
306
|
+
tile_val = self._blend_v(
|
|
307
|
+
tiles_copy[i - 1][j], tile_val, output_overlap_size_H
|
|
308
|
+
)
|
|
309
|
+
tiles_copy[i][j] = tile_val
|
|
310
|
+
res_row.append(tile_val[:, :, :limit_i, :limit_j])
|
|
311
|
+
res_rows.append(torch.cat(res_row, dim=3))
|
|
312
|
+
output = torch.cat(res_rows, dim=2)
|
|
313
|
+
return output
|
|
314
|
+
|
|
315
|
+
|
|
316
|
+
def extract_into_tensor(
|
|
317
|
+
a: torch.Tensor, t: torch.Tensor, x_shape: Tuple[int, ...]
|
|
318
|
+
) -> torch.Tensor:
|
|
319
|
+
"""
|
|
320
|
+
Extracts values from a tensor into a new tensor using indices from another tensor.
|
|
321
|
+
|
|
322
|
+
:param a: the tensor to extract values from.
|
|
323
|
+
:param t: the tensor containing the indices.
|
|
324
|
+
:param x_shape: the shape of the tensor to extract values into.
|
|
325
|
+
:return: a new tensor containing the extracted values.
|
|
326
|
+
"""
|
|
327
|
+
|
|
328
|
+
b, *_ = t.shape
|
|
329
|
+
out = a.gather(-1, t)
|
|
330
|
+
return out.reshape(b, *((1,) * (len(x_shape) - 1)))
|
|
331
|
+
|
|
332
|
+
|
|
333
|
+
def pad(x: torch.Tensor, base_h: int, base_w: int) -> torch.Tensor:
|
|
334
|
+
"""
|
|
335
|
+
Pads a tensor to the nearest multiple of base_h and base_w.
|
|
336
|
+
|
|
337
|
+
:param x: the tensor to pad.
|
|
338
|
+
:param base_h: the base height.
|
|
339
|
+
:param base_w: the base width.
|
|
340
|
+
:return: the padded tensor.
|
|
341
|
+
"""
|
|
342
|
+
h, w = x.shape[-2:]
|
|
343
|
+
h_ = math.ceil(h / base_h) * base_h
|
|
344
|
+
w_ = math.ceil(w / base_w) * base_w
|
|
345
|
+
if w_ != w:
|
|
346
|
+
x = F.pad(x, (0, abs(w_ - w), 0, 0))
|
|
347
|
+
if h_ != h:
|
|
348
|
+
x = F.pad(x, (0, 0, 0, abs(h_ - h)))
|
|
349
|
+
return x
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def append_dims(x: torch.Tensor, target_dims: int) -> torch.Tensor:
|
|
353
|
+
"""Appends dimensions to the end of a tensor until it has target_dims dimensions."""
|
|
354
|
+
dims_to_append = target_dims - x.ndim
|
|
355
|
+
if dims_to_append < 0:
|
|
356
|
+
raise ValueError(
|
|
357
|
+
f"input has {x.ndim} dims but target_dims is {target_dims}, which is less"
|
|
358
|
+
)
|
|
359
|
+
return x[(...,) + (None,) * dims_to_append]
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
@torch.no_grad()
|
|
363
|
+
def update_ema(
|
|
364
|
+
target_params: List[torch.Tensor],
|
|
365
|
+
source_params: List[torch.Tensor],
|
|
366
|
+
rate: float = 0.99,
|
|
367
|
+
):
|
|
368
|
+
"""
|
|
369
|
+
Update target parameters to be closer to those of source parameters using
|
|
370
|
+
an exponential moving average.
|
|
371
|
+
|
|
372
|
+
:param target_params: the target parameter sequence.
|
|
373
|
+
:param source_params: the source parameter sequence.
|
|
374
|
+
:param rate: the EMA rate (closer to 1 means slower).
|
|
375
|
+
"""
|
|
376
|
+
for targ, src in zip(target_params, source_params):
|
|
377
|
+
targ.detach().mul_(rate).add_(src, alpha=1 - rate)
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
from diffusers.models import AutoencoderKL
|
|
3
|
+
|
|
4
|
+
from ..base.base_model import BaseModel
|
|
5
|
+
from ..utils import Tiler, pad
|
|
6
|
+
from .autoencoderKL_config import AutoencoderKLDiffusersConfig
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class AutoencoderKLDiffusers(BaseModel):
|
|
10
|
+
"""This is the VAE class used to work with latent models
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
|
|
14
|
+
config (AutoencoderKLDiffusersConfig): The config class which defines all the required parameters.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(self, config: AutoencoderKLDiffusersConfig):
|
|
18
|
+
BaseModel.__init__(self, config)
|
|
19
|
+
self.config = config
|
|
20
|
+
self.vae_model = AutoencoderKL.from_pretrained(
|
|
21
|
+
config.version,
|
|
22
|
+
subfolder=config.subfolder,
|
|
23
|
+
revision=config.revision,
|
|
24
|
+
)
|
|
25
|
+
self.tiling_size = config.tiling_size
|
|
26
|
+
self.tiling_overlap = config.tiling_overlap
|
|
27
|
+
|
|
28
|
+
# get downsampling factor
|
|
29
|
+
self._get_properties()
|
|
30
|
+
|
|
31
|
+
@torch.no_grad()
|
|
32
|
+
def _get_properties(self):
|
|
33
|
+
self.has_shift_factor = (
|
|
34
|
+
hasattr(self.vae_model.config, "shift_factor")
|
|
35
|
+
and self.vae_model.config.shift_factor is not None
|
|
36
|
+
)
|
|
37
|
+
self.shift_factor = (
|
|
38
|
+
self.vae_model.config.shift_factor if self.has_shift_factor else 0
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
# set latent channels
|
|
42
|
+
self.latent_channels = self.vae_model.config.latent_channels
|
|
43
|
+
self.has_latents_mean = (
|
|
44
|
+
hasattr(self.vae_model.config, "latents_mean")
|
|
45
|
+
and self.vae_model.config.latents_mean is not None
|
|
46
|
+
)
|
|
47
|
+
self.has_latents_std = (
|
|
48
|
+
hasattr(self.vae_model.config, "latents_std")
|
|
49
|
+
and self.vae_model.config.latents_std is not None
|
|
50
|
+
)
|
|
51
|
+
self.latents_mean = self.vae_model.config.latents_mean
|
|
52
|
+
self.latents_std = self.vae_model.config.latents_std
|
|
53
|
+
|
|
54
|
+
x = torch.randn(1, self.vae_model.config.in_channels, 32, 32)
|
|
55
|
+
z = self.encode(x)
|
|
56
|
+
|
|
57
|
+
# set downsampling factor
|
|
58
|
+
self.downsampling_factor = int(x.shape[2] / z.shape[2])
|
|
59
|
+
|
|
60
|
+
def encode(self, x: torch.tensor, batch_size: int = 8):
|
|
61
|
+
latents = []
|
|
62
|
+
for i in range(0, x.shape[0], batch_size):
|
|
63
|
+
latents.append(
|
|
64
|
+
self.vae_model.encode(x[i : i + batch_size]).latent_dist.sample()
|
|
65
|
+
)
|
|
66
|
+
latents = torch.cat(latents, dim=0)
|
|
67
|
+
latents = (latents - self.shift_factor) * self.vae_model.config.scaling_factor
|
|
68
|
+
|
|
69
|
+
return latents
|
|
70
|
+
|
|
71
|
+
def decode(self, z: torch.tensor):
|
|
72
|
+
|
|
73
|
+
if self.has_latents_mean and self.has_latents_std:
|
|
74
|
+
latents_mean = (
|
|
75
|
+
torch.tensor(self.latents_mean)
|
|
76
|
+
.view(1, self.latent_channels, 1, 1)
|
|
77
|
+
.to(z.device, z.dtype)
|
|
78
|
+
)
|
|
79
|
+
latents_std = (
|
|
80
|
+
torch.tensor(self.latents_std)
|
|
81
|
+
.view(1, self.latent_channels, 1, 1)
|
|
82
|
+
.to(z.device, z.dtype)
|
|
83
|
+
)
|
|
84
|
+
z = z * latents_std / self.vae_model.config.scaling_factor + latents_mean
|
|
85
|
+
else:
|
|
86
|
+
z = z / self.vae_model.config.scaling_factor + self.shift_factor
|
|
87
|
+
|
|
88
|
+
use_tiling = (
|
|
89
|
+
z.shape[2] > self.tiling_size[0] or z.shape[3] > self.tiling_size[1]
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
if use_tiling:
|
|
93
|
+
samples = []
|
|
94
|
+
for i in range(z.shape[0]):
|
|
95
|
+
|
|
96
|
+
z_i = z[i].unsqueeze(0)
|
|
97
|
+
|
|
98
|
+
tiler = Tiler()
|
|
99
|
+
tiles = tiler.get_tiles(
|
|
100
|
+
input=z_i,
|
|
101
|
+
tile_size=self.tiling_size,
|
|
102
|
+
overlap_size=self.tiling_overlap,
|
|
103
|
+
scale=self.downsampling_factor,
|
|
104
|
+
out_channels=3,
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
for i, tile_row in enumerate(tiles):
|
|
108
|
+
for j, tile in enumerate(tile_row):
|
|
109
|
+
tile_shape = tile.shape
|
|
110
|
+
# pad tile to inference size if tile is smaller than inference size
|
|
111
|
+
tile = pad(
|
|
112
|
+
tile,
|
|
113
|
+
base_h=self.tiling_size[0],
|
|
114
|
+
base_w=self.tiling_size[1],
|
|
115
|
+
)
|
|
116
|
+
tile_decoded = self.vae_model.decode(tile).sample
|
|
117
|
+
tiles[i][j] = (
|
|
118
|
+
tile_decoded[
|
|
119
|
+
0,
|
|
120
|
+
:,
|
|
121
|
+
: int(tile_shape[2] * self.downsampling_factor),
|
|
122
|
+
: int(tile_shape[3] * self.downsampling_factor),
|
|
123
|
+
]
|
|
124
|
+
.cpu()
|
|
125
|
+
.unsqueeze(0)
|
|
126
|
+
)
|
|
127
|
+
|
|
128
|
+
# merge tiles
|
|
129
|
+
samples.append(tiler.merge_tiles(tiles=tiles))
|
|
130
|
+
|
|
131
|
+
samples = torch.cat(samples, dim=0)
|
|
132
|
+
|
|
133
|
+
else:
|
|
134
|
+
samples = self.vae_model.decode(z).sample
|
|
135
|
+
|
|
136
|
+
return samples
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from typing import Tuple
|
|
2
|
+
|
|
3
|
+
from pydantic.dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
from ..base import ModelConfig
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@dataclass
|
|
9
|
+
class AutoencoderKLDiffusersConfig(ModelConfig):
|
|
10
|
+
"""This is the VAEConfig class which defines all the useful parameters to instantiate the model.
|
|
11
|
+
|
|
12
|
+
Args:
|
|
13
|
+
|
|
14
|
+
version (str): The version of the model. Defaults to "stabilityai/sdxl-vae".
|
|
15
|
+
subfolder (str): The subfolder of the model if loaded from another model. Defaults to "".
|
|
16
|
+
revision (str): The revision of the model. Defaults to "main".
|
|
17
|
+
input_key (str): The key of the input data in the batch. Defaults to "image".
|
|
18
|
+
tiling_size (Tuple[int, int]): The size of the tiling. Defaults to (64, 64).
|
|
19
|
+
tiling_overlap (Tuple[int, int]): The overlap of the tiling. Defaults to (16, 16).
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
version: str = "stabilityai/sdxl-vae"
|
|
23
|
+
subfolder: str = ""
|
|
24
|
+
revision: str = "main"
|
|
25
|
+
input_key: str = "image"
|
|
26
|
+
tiling_size: Tuple[int, int] = (64, 64)
|
|
27
|
+
tiling_overlap: Tuple[int, int] = (16, 16)
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""
|
|
2
|
+
This module contains the training pipeline and the training configuration along with all relevant parts
|
|
3
|
+
of the training pipeline such as loggers and callbacks.
|
|
4
|
+
|
|
5
|
+
The :mod:`cr.trainer` includes the following submodules:
|
|
6
|
+
|
|
7
|
+
- :mod:`cr.trainer.trainer`: the main training pipeline class for ClipDrop.
|
|
8
|
+
- :mod:`cr.trainer.training_config`: the configuration for the training pipeline.
|
|
9
|
+
- :mod:`cr.trainer.loggers`: the loggers for logging samples to wandb.
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
Examples
|
|
13
|
+
########
|
|
14
|
+
|
|
15
|
+
Train a model using the training pipeline
|
|
16
|
+
|
|
17
|
+
.. code-block:: python
|
|
18
|
+
|
|
19
|
+
from cr.trainer import TrainingPipeline, TrainingConfig
|
|
20
|
+
from cr.data import DataPipeline, DataConfig
|
|
21
|
+
from pytorch_lightning import Trainer
|
|
22
|
+
from cr.data.datasets import DataModule, DataModuleConfig
|
|
23
|
+
|
|
24
|
+
# Create a model to train
|
|
25
|
+
model = DummyModel()
|
|
26
|
+
|
|
27
|
+
# Create a training configuration
|
|
28
|
+
config = TrainingConfig(
|
|
29
|
+
experiment_id="test",
|
|
30
|
+
optimizers_name=["AdamW"],
|
|
31
|
+
optimizers_kwargs=[{}],
|
|
32
|
+
learning_rates=[1e-3],
|
|
33
|
+
lr_schedulers_name=[None],
|
|
34
|
+
lr_schedulers_kwargs=[{}],
|
|
35
|
+
trainable_params=[["./*"]],
|
|
36
|
+
log_keys="txt",
|
|
37
|
+
log_samples_model_kwargs={
|
|
38
|
+
"max_samples": 8,
|
|
39
|
+
"num_steps": 20,
|
|
40
|
+
"input_shape": (4, 32, 32),
|
|
41
|
+
"guidance_scale": 7.5,
|
|
42
|
+
}
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
# Create a training pipeline
|
|
46
|
+
pipeline = TrainingPipeline(model=model, pipeline_config=config)
|
|
47
|
+
|
|
48
|
+
# Create a DataModule
|
|
49
|
+
data_module = DataModule(
|
|
50
|
+
train_config=DataModuleConfig(
|
|
51
|
+
shards_path_or_urls="your urls or paths",
|
|
52
|
+
decoder="pil",
|
|
53
|
+
shuffle_buffer_size=100,
|
|
54
|
+
per_worker_batch_size=32,
|
|
55
|
+
num_workers=4,
|
|
56
|
+
),
|
|
57
|
+
train_filters_mappers=your_mappers_and_filters,
|
|
58
|
+
eval_config=DataModuleConfig(
|
|
59
|
+
shards_path_or_urls="your urls or paths",
|
|
60
|
+
decoder="pil",
|
|
61
|
+
shuffle_buffer_size=100,
|
|
62
|
+
per_worker_batch_size=32,
|
|
63
|
+
num_workers=4,
|
|
64
|
+
),
|
|
65
|
+
eval_filters_mappers=your_mappers_and_filters,
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
# Create a trainer
|
|
69
|
+
trainer = Trainer(
|
|
70
|
+
accelerator="cuda",
|
|
71
|
+
max_epochs=1,
|
|
72
|
+
devices=1,
|
|
73
|
+
log_every_n_steps=1,
|
|
74
|
+
default_root_dir="your dir",
|
|
75
|
+
max_steps=2,
|
|
76
|
+
)
|
|
77
|
+
|
|
78
|
+
# Train the model
|
|
79
|
+
trainer.fit(pipeline, data_module)
|
|
80
|
+
"""
|
|
81
|
+
|
|
82
|
+
from .trainer import TrainingPipeline
|
|
83
|
+
from .training_config import TrainingConfig
|
|
84
|
+
|
|
85
|
+
__all__ = ["TrainingPipeline", "TrainingConfig"]
|