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,747 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import numpy as np
|
|
3
|
+
import torchvision.transforms as T
|
|
4
|
+
from einops.layers.torch import Rearrange
|
|
5
|
+
import math
|
|
6
|
+
import torch.nn as nn
|
|
7
|
+
import numbers
|
|
8
|
+
import torch.nn.functional as F
|
|
9
|
+
from functools import partial
|
|
10
|
+
import os
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Bottleneck(torch.nn.Module):
|
|
14
|
+
expansion = 1
|
|
15
|
+
def __init__(self, in_channel, out_channel, stride=1, downsample=None,
|
|
16
|
+
groups=1, width_per_group=64):
|
|
17
|
+
super(Bottleneck, self).__init__()
|
|
18
|
+
|
|
19
|
+
width = int(out_channel * (width_per_group / 64.)) * groups
|
|
20
|
+
|
|
21
|
+
self.conv1 = torch.nn.Conv2d(in_channels=in_channel, out_channels=width,
|
|
22
|
+
kernel_size=1, stride=1, bias=False) # squeeze channels
|
|
23
|
+
self.bn1 = torch.nn.BatchNorm2d(width)
|
|
24
|
+
# -----------------------------------------
|
|
25
|
+
self.conv2 = torch.nn.Conv2d(in_channels=width, out_channels=width, groups=groups,
|
|
26
|
+
kernel_size=3, stride=stride, bias=False, padding=1)
|
|
27
|
+
self.bn2 = torch.nn.BatchNorm2d(width)
|
|
28
|
+
# -----------------------------------------
|
|
29
|
+
self.conv3 = torch.nn.Conv2d(in_channels=width, out_channels=out_channel*self.expansion,
|
|
30
|
+
kernel_size=1, stride=1, bias=False) # unsqueeze channels
|
|
31
|
+
self.bn3 = torch.nn.BatchNorm2d(out_channel*self.expansion)
|
|
32
|
+
self.relu = torch.nn.ReLU(inplace=True)
|
|
33
|
+
self.downsample = downsample
|
|
34
|
+
|
|
35
|
+
def forward(self, x):
|
|
36
|
+
identity = x
|
|
37
|
+
if self.downsample is not None:
|
|
38
|
+
identity = self.downsample(x)
|
|
39
|
+
|
|
40
|
+
out = self.conv1(x)
|
|
41
|
+
out = self.bn1(out)
|
|
42
|
+
out = self.relu(out)
|
|
43
|
+
|
|
44
|
+
out = self.conv2(out)
|
|
45
|
+
out = self.bn2(out)
|
|
46
|
+
out = self.relu(out)
|
|
47
|
+
|
|
48
|
+
out = self.conv3(out)
|
|
49
|
+
out = self.bn3(out)
|
|
50
|
+
|
|
51
|
+
out += identity
|
|
52
|
+
out = self.relu(out)
|
|
53
|
+
|
|
54
|
+
return out
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
class ResNet(torch.nn.Module):
|
|
58
|
+
def __init__(self, block, blocks_num, num_classes=1000, include_top=True):
|
|
59
|
+
super(ResNet, self).__init__()
|
|
60
|
+
self.include_top = include_top
|
|
61
|
+
self.in_channels = 32
|
|
62
|
+
|
|
63
|
+
self.conv1 = torch.nn.Conv2d(4, self.in_channels, kernel_size=3, stride=1, padding=1, bias=False)
|
|
64
|
+
self.bn1 = torch.nn.BatchNorm2d(self.in_channels)
|
|
65
|
+
self.relu = torch.nn.ReLU(inplace=True)
|
|
66
|
+
|
|
67
|
+
self.layer1 = self._make_layer(block, 32, blocks_num[0])
|
|
68
|
+
self.layer2 = self._make_layer(block, 64, blocks_num[1], stride=2)
|
|
69
|
+
self.layer3 = self._make_layer(block, 128, blocks_num[2], stride=2)
|
|
70
|
+
self.layer4 = self._make_layer(block, 256, blocks_num[3], stride=2)
|
|
71
|
+
|
|
72
|
+
if self.include_top:
|
|
73
|
+
self.avgpool = torch.nn.AdaptiveAvgPool2d((1, 1))
|
|
74
|
+
self.fc = torch.nn.Linear(512*block.expansion, num_classes)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def _make_layer(self, block, channel, block_num, stride=1):
|
|
78
|
+
downsample = None
|
|
79
|
+
|
|
80
|
+
if stride != 1 or self.in_channels != channel * block.expansion:
|
|
81
|
+
downsample = torch.nn.Sequential(
|
|
82
|
+
torch.nn.Conv2d(self.in_channels, channel * block.expansion, kernel_size=1, stride=stride, bias=False),
|
|
83
|
+
torch.nn.BatchNorm2d(channel * block.expansion) )
|
|
84
|
+
|
|
85
|
+
layers = []
|
|
86
|
+
layers.append(block(self.in_channels, channel, downsample=downsample, stride=stride))
|
|
87
|
+
self.in_channels = channel * block.expansion
|
|
88
|
+
|
|
89
|
+
for _ in range(1, block_num):
|
|
90
|
+
layers.append(block(self.in_channels, channel))
|
|
91
|
+
|
|
92
|
+
return torch.nn.Sequential(*layers)
|
|
93
|
+
|
|
94
|
+
def forward(self, x):
|
|
95
|
+
x = self.conv1(x)
|
|
96
|
+
x = self.bn1(x)
|
|
97
|
+
x = self.relu(x)
|
|
98
|
+
|
|
99
|
+
x = self.layer1(x)
|
|
100
|
+
x = self.layer2(x)
|
|
101
|
+
x = self.layer3(x)
|
|
102
|
+
x = self.layer4(x)
|
|
103
|
+
|
|
104
|
+
if self.include_top:
|
|
105
|
+
x = self.avgpool(x)
|
|
106
|
+
x = torch.flatten(x, 1)
|
|
107
|
+
x = self.fc(x)
|
|
108
|
+
|
|
109
|
+
return x
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def resnet50(num_classes=1000, include_top=True):
|
|
113
|
+
return ResNet(Bottleneck, [3, 4, 6, 3], num_classes=num_classes, include_top=include_top)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class Resnet50(nn.Module):
|
|
117
|
+
def __init__(self):
|
|
118
|
+
super(Resnet50, self).__init__()
|
|
119
|
+
self.model = resnet50()
|
|
120
|
+
self.block_channels = [32,64,128,256]
|
|
121
|
+
|
|
122
|
+
def forward(self, x, backbone_features):
|
|
123
|
+
outputs = []
|
|
124
|
+
x = self.model.conv1(x)
|
|
125
|
+
x = self.model.bn1(x)
|
|
126
|
+
x = self.model.relu(x)
|
|
127
|
+
|
|
128
|
+
x2 = self.model.layer1(x)
|
|
129
|
+
outputs.append(x2)
|
|
130
|
+
x3 = self.model.layer2(x2)
|
|
131
|
+
outputs.append(x3)
|
|
132
|
+
x4 = self.model.layer3(x3)
|
|
133
|
+
outputs.append(x4)
|
|
134
|
+
x5 = self.model.layer4(x4)
|
|
135
|
+
outputs.append(x5)
|
|
136
|
+
|
|
137
|
+
return outputs[::-1]
|
|
138
|
+
|
|
139
|
+
|
|
140
|
+
class UNetDecoder(nn.Module):
|
|
141
|
+
def __init__(self, depth, encoder_blocks_channels, norm_layer,
|
|
142
|
+
attention_layer=None, attend_from=3, image_fusion=True):
|
|
143
|
+
super(UNetDecoder, self).__init__()
|
|
144
|
+
self.up_blocks = nn.ModuleList()
|
|
145
|
+
self.image_fusion = image_fusion
|
|
146
|
+
in_channels = encoder_blocks_channels.pop()
|
|
147
|
+
out_channels = in_channels
|
|
148
|
+
# Last encoder layer doesn't pool, so there're only (depth - 1) deconvs
|
|
149
|
+
for d in range(depth - 1):
|
|
150
|
+
out_channels = encoder_blocks_channels.pop() if len(encoder_blocks_channels) else in_channels // 2
|
|
151
|
+
stage_attention_layer = attention_layer if 0 <= attend_from <= d else None
|
|
152
|
+
self.up_blocks.append(UNetUpBlock(
|
|
153
|
+
in_channels, out_channels, out_channels,
|
|
154
|
+
norm_layer=norm_layer, activation=partial(nn.ReLU, inplace=True),
|
|
155
|
+
padding=1,
|
|
156
|
+
attention_layer=stage_attention_layer,
|
|
157
|
+
))
|
|
158
|
+
in_channels = out_channels
|
|
159
|
+
|
|
160
|
+
if self.image_fusion:
|
|
161
|
+
self.conv_attention = nn.Conv2d(out_channels, 1, kernel_size=1)
|
|
162
|
+
self.to_rgb = nn.Conv2d(out_channels, 3, kernel_size=1)
|
|
163
|
+
|
|
164
|
+
def forward(self, encoder_outputs, input_image, mask):
|
|
165
|
+
output = encoder_outputs[0]
|
|
166
|
+
for block, skip_output in zip(self.up_blocks, encoder_outputs[1:]):
|
|
167
|
+
output = block(output, skip_output, mask)
|
|
168
|
+
output_map = output
|
|
169
|
+
if self.image_fusion:
|
|
170
|
+
attention_map = torch.sigmoid(3.0 * self.conv_attention(output))
|
|
171
|
+
output = attention_map * input_image + (1.0 - attention_map) * self.to_rgb(output)
|
|
172
|
+
else:
|
|
173
|
+
output = self.to_rgb(output)
|
|
174
|
+
|
|
175
|
+
return output, output_map
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
class UNetDownBlock(nn.Module):
|
|
179
|
+
def __init__(self, in_channels, out_channels, norm_layer, activation, pool, padding):
|
|
180
|
+
super(UNetDownBlock, self).__init__()
|
|
181
|
+
self.convs = UNetDoubleConv(
|
|
182
|
+
in_channels, out_channels,
|
|
183
|
+
norm_layer=norm_layer, activation=activation, padding=padding,
|
|
184
|
+
)
|
|
185
|
+
self.pooling = nn.MaxPool2d(2, 2) if pool else nn.Identity()
|
|
186
|
+
|
|
187
|
+
def forward(self, x):
|
|
188
|
+
conv_x = self.convs(x)
|
|
189
|
+
return conv_x, self.pooling(conv_x)
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
class UNetUpBlock(nn.Module):
|
|
193
|
+
def __init__(
|
|
194
|
+
self,
|
|
195
|
+
in_channels_decoder, in_channels_encoder, out_channels,
|
|
196
|
+
norm_layer, activation, padding,
|
|
197
|
+
attention_layer,
|
|
198
|
+
):
|
|
199
|
+
super(UNetUpBlock, self).__init__()
|
|
200
|
+
self.upconv = nn.Sequential(
|
|
201
|
+
nn.Upsample(scale_factor=2, mode='bilinear', align_corners=True),
|
|
202
|
+
ConvBlock(
|
|
203
|
+
in_channels_decoder, out_channels,
|
|
204
|
+
kernel_size=3, stride=1, padding=1,
|
|
205
|
+
norm_layer=None, activation=activation,
|
|
206
|
+
)
|
|
207
|
+
)
|
|
208
|
+
self.convs = UNetDoubleConv(
|
|
209
|
+
in_channels_encoder + out_channels, out_channels,
|
|
210
|
+
norm_layer=norm_layer, activation=activation, padding=padding,
|
|
211
|
+
)
|
|
212
|
+
if attention_layer is not None:
|
|
213
|
+
self.attention = attention_layer(in_channels_encoder + out_channels, norm_layer, activation)
|
|
214
|
+
else:
|
|
215
|
+
self.attention = None
|
|
216
|
+
|
|
217
|
+
def forward(self, x, encoder_out, mask=None):
|
|
218
|
+
upsample_x = self.upconv(x)
|
|
219
|
+
x_cat_encoder = torch.cat([encoder_out, upsample_x], dim=1)
|
|
220
|
+
if self.attention is not None:
|
|
221
|
+
x_cat_encoder = self.attention(x_cat_encoder, mask)
|
|
222
|
+
return self.convs(x_cat_encoder)
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
class UNetDoubleConv(nn.Module):
|
|
226
|
+
def __init__(self, in_channels, out_channels, norm_layer, activation, padding):
|
|
227
|
+
super(UNetDoubleConv, self).__init__()
|
|
228
|
+
self.block = nn.Sequential(
|
|
229
|
+
ConvBlock(
|
|
230
|
+
in_channels, out_channels,
|
|
231
|
+
kernel_size=3, stride=1, padding=padding,
|
|
232
|
+
norm_layer=norm_layer, activation=activation,
|
|
233
|
+
),
|
|
234
|
+
ConvBlock(
|
|
235
|
+
out_channels, out_channels,
|
|
236
|
+
kernel_size=3, stride=1, padding=padding,
|
|
237
|
+
norm_layer=norm_layer, activation=activation,
|
|
238
|
+
),
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
def forward(self, x):
|
|
242
|
+
return self.block(x)
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
class Weight_predictor_issam(nn.Module):
|
|
246
|
+
def __init__(self, in_channels=256, out_channels=3, fb=True):
|
|
247
|
+
super(Weight_predictor_issam, self).__init__()
|
|
248
|
+
|
|
249
|
+
# self.dp = nn.Dropout(p=0.5)
|
|
250
|
+
self.mid_ch = 256
|
|
251
|
+
self.fb = fb
|
|
252
|
+
self.conv = nn.Conv2d(in_channels, self.mid_ch, 1, 1, padding=0)
|
|
253
|
+
self.avg_pooling = nn.AdaptiveAvgPool2d(1)
|
|
254
|
+
if self.fb:
|
|
255
|
+
# print(self.mid_ch*2)
|
|
256
|
+
self.fc = nn.Conv2d(self.mid_ch*3, out_channels, 1, 1, padding=0)
|
|
257
|
+
else:
|
|
258
|
+
# print(self.mid_ch)
|
|
259
|
+
self.fc = nn.Conv2d(self.mid_ch, out_channels, 1, 1, padding=0)
|
|
260
|
+
|
|
261
|
+
|
|
262
|
+
def forward(self, encoder_outputs, mask):
|
|
263
|
+
fea_input = encoder_outputs[0]
|
|
264
|
+
if len(fea_input.shape) == 3:
|
|
265
|
+
L, bz, d = fea_input.shape
|
|
266
|
+
fea_input = fea_input.permute(1,2,0).view(bz, d, int(math.sqrt(L)), int(math.sqrt(L)))
|
|
267
|
+
x = self.conv(fea_input)
|
|
268
|
+
if self.fb:
|
|
269
|
+
down_mask = F.interpolate(mask, size=fea_input.shape[2:], mode='bilinear', align_corners=True)
|
|
270
|
+
'''
|
|
271
|
+
sum_mask = torch.sum(down_mask,dim=[1,2,3]).unsqueeze(-1).unsqueeze(-1).unsqueeze(-1)
|
|
272
|
+
sum_mask[sum_mask<10] = 10
|
|
273
|
+
sum_1mask = torch.sum(1-down_mask,dim=[1,2,3]).unsqueeze(-1).unsqueeze(-1).unsqueeze(-1)
|
|
274
|
+
sum_1mask[sum_1mask<10] = 10
|
|
275
|
+
fg_feature = self.avg_pooling(x*down_mask) * 1024.0 / sum_mask
|
|
276
|
+
bg_feature = self.avg_pooling(x*(1-down_mask)) * 1024.0 / sum_1mask
|
|
277
|
+
'''
|
|
278
|
+
fg_mask = down_mask.detach()
|
|
279
|
+
bg_mask = (1-fg_mask).detach()
|
|
280
|
+
full_mask = (fg_mask + bg_mask).detach()
|
|
281
|
+
m_fg = fg_mask.sum(axis=[2,3], keepdim=True)
|
|
282
|
+
m_bg = bg_mask.sum(axis=[2,3], keepdim=True)
|
|
283
|
+
m_full = full_mask.sum(axis=[2,3], keepdim=True)
|
|
284
|
+
fg_feature_map = x*fg_mask/(m_fg+1e-6)
|
|
285
|
+
bg_feature_map = x*bg_mask/(m_bg+1e-6)
|
|
286
|
+
full_feature_map = x*full_mask/(m_full+1e-6)
|
|
287
|
+
|
|
288
|
+
fg_feature = fg_feature_map.sum(axis=[2,3]).unsqueeze(-1).unsqueeze(-1)
|
|
289
|
+
bg_feature = bg_feature_map.sum(axis=[2,3]).unsqueeze(-1).unsqueeze(-1)
|
|
290
|
+
full_feature = full_feature_map.sum(axis=[2,3]).unsqueeze(-1).unsqueeze(-1)
|
|
291
|
+
fgbg_fea = torch.cat((fg_feature, bg_feature, full_feature),1)
|
|
292
|
+
x = self.fc(fgbg_fea)
|
|
293
|
+
else:
|
|
294
|
+
feature = self.avg_pooling(x)
|
|
295
|
+
x = self.fc(feature)
|
|
296
|
+
return x
|
|
297
|
+
|
|
298
|
+
|
|
299
|
+
class LUT(nn.Module):
|
|
300
|
+
def __init__(self, in_channels=1024, n_lut=3, backbone='issam', fb=True, clamp=False):
|
|
301
|
+
super(LUT, self).__init__()
|
|
302
|
+
self.n_lut = n_lut
|
|
303
|
+
self.fb = fb
|
|
304
|
+
self.clamp = clamp
|
|
305
|
+
# print(self.fb)
|
|
306
|
+
|
|
307
|
+
if self.n_lut==6:
|
|
308
|
+
self.LUT0 = Generator3DLUT_identity()
|
|
309
|
+
self.LUT1 = Generator3DLUT_zero()
|
|
310
|
+
self.LUT2 = Generator3DLUT_zero()
|
|
311
|
+
self.LUT3 = Generator3DLUT_zero()
|
|
312
|
+
self.LUT4 = Generator3DLUT_zero()
|
|
313
|
+
self.LUT5 = Generator3DLUT_zero()
|
|
314
|
+
else:
|
|
315
|
+
raise NotImplementedError
|
|
316
|
+
|
|
317
|
+
self.backbone=backbone
|
|
318
|
+
self.classifier = Weight_predictor_issam(in_channels, n_lut, self.fb)
|
|
319
|
+
|
|
320
|
+
# def initial_luts(self, lut_num=3):
|
|
321
|
+
# self.lut_list.append(Generator3DLUT_identity())
|
|
322
|
+
# for ii in range(lut_num-1):
|
|
323
|
+
# self.lut_list.append(Generator3DLUT_zero())
|
|
324
|
+
# return self.lut_list
|
|
325
|
+
|
|
326
|
+
def forward(self, encoder_outputs, image, mask):
|
|
327
|
+
pred_weights = self.classifier(encoder_outputs, mask)
|
|
328
|
+
if len(pred_weights.shape) == 1:
|
|
329
|
+
pred_weights = pred_weights.unsqueeze(0)
|
|
330
|
+
pred_weights = F.softmax(pred_weights, dim=1)
|
|
331
|
+
combine_A = image.new(image.size())
|
|
332
|
+
if self.n_lut==6:
|
|
333
|
+
gen_A0 = self.LUT0(image)
|
|
334
|
+
gen_A1 = self.LUT1(image)
|
|
335
|
+
gen_A2 = self.LUT2(image)
|
|
336
|
+
gen_A3 = self.LUT3(image)
|
|
337
|
+
gen_A4 = self.LUT4(image)
|
|
338
|
+
gen_A5 = self.LUT5(image)
|
|
339
|
+
for b in range(image.size(0)):
|
|
340
|
+
combine_A[b,:,:,:] = pred_weights[b,0] * gen_A0[b,:,:,:] + pred_weights[b,1] * gen_A1[b,:,:,:] + pred_weights[b,2] * gen_A2[b,:,:,:] \
|
|
341
|
+
+ pred_weights[b,3] * gen_A3[b,:,:,:] + pred_weights[b,4] * gen_A4[b,:,:,:] + pred_weights[b,5] * gen_A5[b,:,:,:]
|
|
342
|
+
else:
|
|
343
|
+
raise NotImplementedError
|
|
344
|
+
if self.clamp:
|
|
345
|
+
combine_A = torch.clamp(combine_A,0,1)
|
|
346
|
+
|
|
347
|
+
combine_A = combine_A*mask+image*(1-mask)
|
|
348
|
+
|
|
349
|
+
return combine_A#,pred_weights.squeeze().detach().cpu().numpy()
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
class Generator3DLUT_identity(nn.Module):
|
|
353
|
+
def __init__(self, dim=33):
|
|
354
|
+
super(Generator3DLUT_identity, self).__init__()
|
|
355
|
+
filepath = '/'.join(os.path.realpath(__file__).split("/")[0:-2])
|
|
356
|
+
filepath = os.environ.get('LIBCOM_MODEL_DIR',filepath)
|
|
357
|
+
if dim == 33:
|
|
358
|
+
file = open(filepath + "/pretrained_models/IdentityLUT33.txt", 'r')
|
|
359
|
+
lines = file.readlines()
|
|
360
|
+
buffer = np.zeros((3,dim,dim,dim), dtype=np.float32)
|
|
361
|
+
|
|
362
|
+
for i in range(0,dim):
|
|
363
|
+
for j in range(0,dim):
|
|
364
|
+
for k in range(0,dim):
|
|
365
|
+
n = i * dim*dim + j * dim + k
|
|
366
|
+
x = lines[n].split()
|
|
367
|
+
buffer[0,i,j,k] = float(x[0])
|
|
368
|
+
buffer[1,i,j,k] = float(x[1])
|
|
369
|
+
buffer[2,i,j,k] = float(x[2])
|
|
370
|
+
self.LUT = nn.Parameter(torch.from_numpy(buffer).requires_grad_(True))
|
|
371
|
+
self.TrilinearInterpolation = TrilinearInterpolation()
|
|
372
|
+
|
|
373
|
+
def forward(self, x):
|
|
374
|
+
_, output = self.TrilinearInterpolation(self.LUT, x)
|
|
375
|
+
#self.LUT, output = self.TrilinearInterpolation(self.LUT, x)
|
|
376
|
+
return output
|
|
377
|
+
|
|
378
|
+
class Generator3DLUT_zero(nn.Module):
|
|
379
|
+
def __init__(self, dim=33):
|
|
380
|
+
super(Generator3DLUT_zero, self).__init__()
|
|
381
|
+
|
|
382
|
+
self.LUT = torch.zeros(3,dim,dim,dim, dtype=torch.float)
|
|
383
|
+
self.LUT = nn.Parameter(self.LUT.clone().detach().requires_grad_(True))
|
|
384
|
+
self.TrilinearInterpolation = TrilinearInterpolation()
|
|
385
|
+
|
|
386
|
+
def forward(self, x):
|
|
387
|
+
|
|
388
|
+
_, output = self.TrilinearInterpolation(self.LUT, x)
|
|
389
|
+
return output
|
|
390
|
+
|
|
391
|
+
class TrilinearInterpolationFunction(torch.autograd.Function):
|
|
392
|
+
@staticmethod
|
|
393
|
+
def forward(ctx, lut, x):
|
|
394
|
+
x = x.contiguous()
|
|
395
|
+
|
|
396
|
+
output = x.new(x.size())
|
|
397
|
+
dim = lut.size()[-1]
|
|
398
|
+
shift = dim ** 3
|
|
399
|
+
binsize = 1.000001 / (dim-1)
|
|
400
|
+
W = x.size(2)
|
|
401
|
+
H = x.size(3)
|
|
402
|
+
batch = x.size(0)
|
|
403
|
+
|
|
404
|
+
assert 1 == trilinear.forward(lut,
|
|
405
|
+
x,
|
|
406
|
+
output,
|
|
407
|
+
dim,
|
|
408
|
+
shift,
|
|
409
|
+
binsize,
|
|
410
|
+
W,
|
|
411
|
+
H,
|
|
412
|
+
batch)
|
|
413
|
+
|
|
414
|
+
int_package = torch.IntTensor([dim, shift, W, H, batch])
|
|
415
|
+
float_package = torch.FloatTensor([binsize])
|
|
416
|
+
variables = [lut, x, int_package, float_package]
|
|
417
|
+
|
|
418
|
+
ctx.save_for_backward(*variables)
|
|
419
|
+
|
|
420
|
+
return lut, output
|
|
421
|
+
|
|
422
|
+
@staticmethod
|
|
423
|
+
def backward(ctx, lut_grad, x_grad):
|
|
424
|
+
|
|
425
|
+
lut, x, int_package, float_package = ctx.saved_variables
|
|
426
|
+
dim, shift, W, H, batch = int_package
|
|
427
|
+
dim, shift, W, H, batch = int(dim), int(shift), int(W), int(H), int(batch)
|
|
428
|
+
binsize = float(float_package[0])
|
|
429
|
+
|
|
430
|
+
assert 1 == trilinear.backward(x,
|
|
431
|
+
x_grad,
|
|
432
|
+
lut_grad,
|
|
433
|
+
dim,
|
|
434
|
+
shift,
|
|
435
|
+
binsize,
|
|
436
|
+
W,
|
|
437
|
+
H,
|
|
438
|
+
batch)
|
|
439
|
+
return lut_grad, x_grad
|
|
440
|
+
|
|
441
|
+
|
|
442
|
+
class TrilinearInterpolation(torch.nn.Module):
|
|
443
|
+
def __init__(self):
|
|
444
|
+
super(TrilinearInterpolation, self).__init__()
|
|
445
|
+
|
|
446
|
+
def forward(self, lut, x):
|
|
447
|
+
return TrilinearInterpolationFunction.apply(lut, x)
|
|
448
|
+
|
|
449
|
+
|
|
450
|
+
class TrilinearInterpolationGS(torch.nn.Module):
|
|
451
|
+
def __init__(self):
|
|
452
|
+
super(TrilinearInterpolationGS, self).__init__()
|
|
453
|
+
|
|
454
|
+
def forward(self, lut, img):
|
|
455
|
+
# scale im between -1 and 1 since its used as grid input in grid_sample
|
|
456
|
+
img = (img - .5) * 2.
|
|
457
|
+
# grid_sample expects NxD_outxH_outxW_outx3 (1x1xHxWx3)
|
|
458
|
+
img = img.permute(0, 2, 3, 1)[:, None]
|
|
459
|
+
# add batch dim to LUT
|
|
460
|
+
lut = lut[None] # [B,C,D_in,H_in,W_in] -> [B,3,M,M,M]
|
|
461
|
+
# grid sample
|
|
462
|
+
result = F.grid_sample(lut, img, mode='bilinear', padding_mode='border', align_corners=True) # [B, C, D_out, H_out, W_out ]
|
|
463
|
+
# drop added dimensions and permute back
|
|
464
|
+
result = result[:, :, 0,:,:]
|
|
465
|
+
# print('after result', result.shape)
|
|
466
|
+
return lut,result
|
|
467
|
+
|
|
468
|
+
|
|
469
|
+
class ConvBlock(nn.Module):
|
|
470
|
+
def __init__(
|
|
471
|
+
self,
|
|
472
|
+
in_channels, out_channels,
|
|
473
|
+
kernel_size=4, stride=2, padding=1,
|
|
474
|
+
norm_layer=nn.BatchNorm2d, activation=nn.ELU,
|
|
475
|
+
bias=True,
|
|
476
|
+
):
|
|
477
|
+
super(ConvBlock, self).__init__()
|
|
478
|
+
self.block = nn.Sequential(
|
|
479
|
+
nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=bias),
|
|
480
|
+
norm_layer(out_channels) if norm_layer is not None else nn.Identity(),
|
|
481
|
+
activation(),
|
|
482
|
+
)
|
|
483
|
+
# self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=padding, bias=bias)
|
|
484
|
+
# self.norm = norm_layer(out_channels) if norm_layer is not None else nn.Identity()
|
|
485
|
+
# self.act = activation()
|
|
486
|
+
|
|
487
|
+
def forward(self, x):
|
|
488
|
+
# x = self.conv(x)
|
|
489
|
+
# x = self.norm(x)
|
|
490
|
+
# x = self.act(x)
|
|
491
|
+
return self.block(x)
|
|
492
|
+
|
|
493
|
+
|
|
494
|
+
class GaussianSmoothing(nn.Module):
|
|
495
|
+
"""
|
|
496
|
+
https://discuss.pytorch.org/t/is-there-anyway-to-do-gaussian-filtering-for-an-image-2d-3d-in-pytorch/12351/10
|
|
497
|
+
Apply gaussian smoothing on a tensor (1d, 2d, 3d).
|
|
498
|
+
Filtering is performed seperately for each channel in the input using a depthwise convolution.
|
|
499
|
+
Arguments:
|
|
500
|
+
channels (int, sequence): Number of channels of the input tensors.
|
|
501
|
+
Output will have this number of channels as well.
|
|
502
|
+
kernel_size (int, sequence): Size of the gaussian kernel.
|
|
503
|
+
sigma (float, sequence): Standard deviation of the gaussian kernel.
|
|
504
|
+
dim (int, optional): The number of dimensions of the data. Default value is 2 (spatial).
|
|
505
|
+
"""
|
|
506
|
+
def __init__(self, channels, kernel_size, sigma, padding=0, dim=2):
|
|
507
|
+
super(GaussianSmoothing, self).__init__()
|
|
508
|
+
if isinstance(kernel_size, numbers.Number):
|
|
509
|
+
kernel_size = [kernel_size] * dim
|
|
510
|
+
if isinstance(sigma, numbers.Number):
|
|
511
|
+
sigma = [sigma] * dim
|
|
512
|
+
|
|
513
|
+
# The gaussian kernel is the product of the gaussian function of each dimension.
|
|
514
|
+
kernel = 1.
|
|
515
|
+
meshgrids = torch.meshgrid([torch.arange(size, dtype=torch.float32) for size in kernel_size])
|
|
516
|
+
for size, std, grid in zip(kernel_size, sigma, meshgrids):
|
|
517
|
+
mean = (size - 1) / 2.
|
|
518
|
+
kernel *= torch.exp(-((grid - mean) / std) ** 2 / 2) / (std * (2 * math.pi) ** 0.5)
|
|
519
|
+
# Make sure sum of values in gaussian kernel equals 1.
|
|
520
|
+
kernel = kernel / torch.sum(kernel)
|
|
521
|
+
# Reshape to depthwise convolutional weight.
|
|
522
|
+
kernel = kernel.view(1, 1, *kernel.size())
|
|
523
|
+
kernel = torch.repeat_interleave(kernel, channels, 0)
|
|
524
|
+
|
|
525
|
+
self.register_buffer('weight', kernel)
|
|
526
|
+
self.groups = channels
|
|
527
|
+
self.padding = padding
|
|
528
|
+
|
|
529
|
+
if dim == 1:
|
|
530
|
+
self.conv = F.conv1d
|
|
531
|
+
elif dim == 2:
|
|
532
|
+
self.conv = F.conv2d
|
|
533
|
+
elif dim == 3:
|
|
534
|
+
self.conv = F.conv3d
|
|
535
|
+
else:
|
|
536
|
+
raise RuntimeError('Only 1, 2 and 3 dimensions are supported. Received {}.'.format(dim))
|
|
537
|
+
|
|
538
|
+
def forward(self, input):
|
|
539
|
+
"""
|
|
540
|
+
Apply gaussian filter to input.
|
|
541
|
+
Arguments:
|
|
542
|
+
input (torch.Tensor): Input to apply gaussian filter on.
|
|
543
|
+
Returns:
|
|
544
|
+
filtered (torch.Tensor): Filtered output.
|
|
545
|
+
"""
|
|
546
|
+
return self.conv(input, weight=self.weight, padding=self.padding, groups=self.groups)
|
|
547
|
+
|
|
548
|
+
|
|
549
|
+
class ChannelAttention(nn.Module):
|
|
550
|
+
def __init__(self, in_channels):
|
|
551
|
+
super(ChannelAttention, self).__init__()
|
|
552
|
+
self.global_pools = nn.ModuleList([
|
|
553
|
+
nn.AdaptiveAvgPool2d(1),
|
|
554
|
+
nn.AdaptiveMaxPool2d(1),
|
|
555
|
+
])
|
|
556
|
+
intermediate_channels_count = max(in_channels // 16, 8)
|
|
557
|
+
self.attention_transform = nn.Sequential(
|
|
558
|
+
nn.Linear(len(self.global_pools) * in_channels, intermediate_channels_count),
|
|
559
|
+
nn.ReLU(),
|
|
560
|
+
nn.Linear(intermediate_channels_count, in_channels),
|
|
561
|
+
nn.Sigmoid(),
|
|
562
|
+
)
|
|
563
|
+
|
|
564
|
+
def forward(self, x):
|
|
565
|
+
pooled_x = []
|
|
566
|
+
for global_pool in self.global_pools:
|
|
567
|
+
pooled_x.append(global_pool(x))
|
|
568
|
+
pooled_x = torch.cat(pooled_x, dim=1).flatten(start_dim=1)
|
|
569
|
+
channel_attention_weights = self.attention_transform(pooled_x)[..., None, None]
|
|
570
|
+
return channel_attention_weights * x
|
|
571
|
+
|
|
572
|
+
|
|
573
|
+
class ViT_Harmonizer(nn.Module):
|
|
574
|
+
def __init__(self, output_nc, ksize=4, tr_r_enc_head=2, tr_r_enc_layers=9, input_nc=3, dim_forward=2, tr_act='gelu'):
|
|
575
|
+
super(ViT_Harmonizer, self).__init__()
|
|
576
|
+
dim = 256
|
|
577
|
+
self.patch_to_embedding = nn.Sequential(
|
|
578
|
+
Rearrange('b c (h p1) (w p2) -> b (h w) (p1 p2 c)', p1 = ksize, p2 = ksize),
|
|
579
|
+
nn.Linear(ksize*ksize*(input_nc+1), dim)
|
|
580
|
+
)
|
|
581
|
+
self.transformer_enc = nn.TransformerEncoder(nn.TransformerEncoderLayer(dim,nhead=tr_r_enc_head, dim_feedforward=dim*dim_forward, activation=tr_act), num_layers=tr_r_enc_layers)
|
|
582
|
+
self.dec = nn.Sequential(
|
|
583
|
+
nn.ConvTranspose2d(dim, output_nc, kernel_size=ksize, stride=ksize, padding=0),
|
|
584
|
+
nn.Tanh()
|
|
585
|
+
)
|
|
586
|
+
|
|
587
|
+
def forward(self, inputs, backbone_features=None):
|
|
588
|
+
patch_embedding = self.patch_to_embedding(inputs)
|
|
589
|
+
content = self.transformer_enc(patch_embedding.permute(1, 0, 2))
|
|
590
|
+
bs, L, C = patch_embedding.size()
|
|
591
|
+
harmonized = self.dec(content.permute(1,2,0).view(bs, C, int(math.sqrt(L)), int(math.sqrt(L))))
|
|
592
|
+
return harmonized
|
|
593
|
+
|
|
594
|
+
class PCT():
|
|
595
|
+
'''
|
|
596
|
+
Pixel-Wise Color Transform
|
|
597
|
+
applies specified PCT function to image given a parameter map
|
|
598
|
+
|
|
599
|
+
transform_type : str
|
|
600
|
+
PCT function name
|
|
601
|
+
dim : int
|
|
602
|
+
dimension of input vector (usually 3)
|
|
603
|
+
affine : bool
|
|
604
|
+
tranform has a translational component
|
|
605
|
+
color_space : str
|
|
606
|
+
transforms input to 'HSV' or 'YUV' to apply transform, does nothing for RGB
|
|
607
|
+
mean : list
|
|
608
|
+
input normalization mean value
|
|
609
|
+
std : list
|
|
610
|
+
input normalization standard deviation value
|
|
611
|
+
unnorm : bool
|
|
612
|
+
before applying the transformation the input normalization is reversed
|
|
613
|
+
clamp: bool
|
|
614
|
+
clamp output to [0, 1] (before applying input normalization again)
|
|
615
|
+
'''
|
|
616
|
+
|
|
617
|
+
def __init__(self, transform_type, dim, affine,
|
|
618
|
+
color_space='RGB', mean = [.485, .456, .406], std = [.229, .224, .225],
|
|
619
|
+
unnorm=False, clamp=True):
|
|
620
|
+
|
|
621
|
+
# Color Space
|
|
622
|
+
self.color_trf_in = lambda x: x
|
|
623
|
+
self.color_trf_out = lambda x: x
|
|
624
|
+
|
|
625
|
+
# Normalization
|
|
626
|
+
self.norm = T.Normalize(mean=mean, std=std)
|
|
627
|
+
self.unnorm = unnorm
|
|
628
|
+
if color_space in ['HSV', 'YUV'] or unnorm:
|
|
629
|
+
self.unnorm = torch.transforms.Normalize(mean=-mean/std, std=1/std)
|
|
630
|
+
|
|
631
|
+
self.clamp = clamp
|
|
632
|
+
|
|
633
|
+
# Transform Functions
|
|
634
|
+
if transform_type == 'identity':
|
|
635
|
+
self.transform = lambda input, param: param
|
|
636
|
+
self.out_dim = 3
|
|
637
|
+
elif transform_type == 'mul':
|
|
638
|
+
self.transform = lambda input, param: input * param
|
|
639
|
+
self.out_dim = 3
|
|
640
|
+
elif transform_type == 'add':
|
|
641
|
+
self.transform = lambda input, param: input + param
|
|
642
|
+
self.out_dim = 3
|
|
643
|
+
elif 'linear' in transform_type:
|
|
644
|
+
type = transform_type.split('_')[-1]
|
|
645
|
+
self.transform = Linear_PCT(dim, affine, type)
|
|
646
|
+
self.out_dim = self.transform.out_dim
|
|
647
|
+
elif transform_type == 'polynomial':
|
|
648
|
+
self.transform = Linear_PCT(dim, affine, 'linear', 'polynomial')
|
|
649
|
+
self.out_dim = 27
|
|
650
|
+
elif transform_type == 'quadratic':
|
|
651
|
+
self.transform == Polynomial_PCT(dim, 2)
|
|
652
|
+
self.out_dim = 6
|
|
653
|
+
elif transform_type == 'cubic':
|
|
654
|
+
self.transform == Polynomial_PCT(dim, 3)
|
|
655
|
+
self.out_dim = 9
|
|
656
|
+
else:
|
|
657
|
+
self.out_dim = 0
|
|
658
|
+
print('Error: Invalid transform type')
|
|
659
|
+
|
|
660
|
+
def __call__(self, input, param):
|
|
661
|
+
|
|
662
|
+
if self.unnorm:
|
|
663
|
+
input = self.unnorm(input)
|
|
664
|
+
input = self.color_trf_in(input)
|
|
665
|
+
|
|
666
|
+
output = self.transform(input, param)
|
|
667
|
+
|
|
668
|
+
output = self.color_trf_out(output)
|
|
669
|
+
if self.clamp:
|
|
670
|
+
output = torch.clamp(output, 0, 1)
|
|
671
|
+
output = self.norm(output)
|
|
672
|
+
|
|
673
|
+
return output
|
|
674
|
+
|
|
675
|
+
def get_out_dim(self):
|
|
676
|
+
return self.out_dim
|
|
677
|
+
|
|
678
|
+
|
|
679
|
+
class Linear_PCT():
|
|
680
|
+
|
|
681
|
+
def __init__(self, dim, affine, type='linear', projection=None):
|
|
682
|
+
|
|
683
|
+
self.dim = dim
|
|
684
|
+
self.affine = affine
|
|
685
|
+
self.type = type
|
|
686
|
+
self.projection = projection
|
|
687
|
+
|
|
688
|
+
if type=='linear':
|
|
689
|
+
self.out_dim = 9
|
|
690
|
+
elif type=='sym':
|
|
691
|
+
self.out_dim = 6
|
|
692
|
+
if affine:
|
|
693
|
+
self.out_dim += 3
|
|
694
|
+
|
|
695
|
+
def __call__(self, input, param):
|
|
696
|
+
|
|
697
|
+
N, C_in, H, W = input.shape
|
|
698
|
+
out = torch.zeros_like(input)
|
|
699
|
+
|
|
700
|
+
L0 = 3
|
|
701
|
+
L = self.dim
|
|
702
|
+
for n in range(N):
|
|
703
|
+
x = input[n].movedim(0, -1).view(-1, C_in).unsqueeze(2) # (HW, C_in, 1)
|
|
704
|
+
|
|
705
|
+
if self.projection == 'polynomial':
|
|
706
|
+
xr, xb, xg = x[:,:1], x[:,0:1], x[:,2:]
|
|
707
|
+
x = torch.cat([xr, xb, xg, xr*xg, xr*xb, xg*xb, xr**2, xg**2, xb**2], axis=1)
|
|
708
|
+
L0 = 9
|
|
709
|
+
elif self.projection == 'sine':
|
|
710
|
+
x = torch.cat([torch.sin(2**(i//2) * x * np.pi/2 * (i % 2) ) for i in range(9)], axis=1) # (H*W, 3*L, 1)
|
|
711
|
+
L0 = 9
|
|
712
|
+
|
|
713
|
+
# Linear Matrix Multiplication
|
|
714
|
+
if self.type == 'sym':
|
|
715
|
+
L0 = 2
|
|
716
|
+
M = param[n, 0:L0*L].movedim(0, -1).view(-1, L0*L)
|
|
717
|
+
M = torch.stack( [ torch.stack([M[:,0], M[:,3], M[:,5]], dim=1),
|
|
718
|
+
torch.stack([M[:,3], M[:,1], M[:,4]], dim=1),
|
|
719
|
+
torch.stack([M[:,5], M[:,4], M[:,2]], dim=1)], dim=2)
|
|
720
|
+
else:
|
|
721
|
+
M = param[n, 0:L0*L].movedim(0, -1).view(-1, L, L0) # (HW, L, L0)
|
|
722
|
+
|
|
723
|
+
y = torch.matmul(M, x)
|
|
724
|
+
if self.affine:
|
|
725
|
+
b = param[n,L0*L:(L0+1)*L].movedim(0, -1).view(-1, L).unsqueeze(2) # (HW, L, L0)
|
|
726
|
+
y = y + b
|
|
727
|
+
out[n] = y.view(H, W, C_in).movedim(-1, 0) # (H, W, 3)
|
|
728
|
+
|
|
729
|
+
return out
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
class Polynomial_PCT():
|
|
733
|
+
|
|
734
|
+
def __init__(self, dim, deg):
|
|
735
|
+
self.dim = dim
|
|
736
|
+
self.deg = deg
|
|
737
|
+
|
|
738
|
+
def __call__(self, input, param):
|
|
739
|
+
|
|
740
|
+
N, C_in, H, W = input.shape
|
|
741
|
+
out = torch.zeros_like(input)
|
|
742
|
+
|
|
743
|
+
param = param.view(N, self.C_in, self.dim, H, W)
|
|
744
|
+
for l in range(self.deg+1):
|
|
745
|
+
out += param[:, l] * torch.pow(input, l)
|
|
746
|
+
|
|
747
|
+
return out
|