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,513 @@
|
|
|
1
|
+
from torch import nn
|
|
2
|
+
import torch
|
|
3
|
+
import pytorch_lightning as pl
|
|
4
|
+
import os
|
|
5
|
+
import numpy as np
|
|
6
|
+
from PIL import Image
|
|
7
|
+
|
|
8
|
+
from .cldm.cldm import ControlLDM
|
|
9
|
+
from .cldm.model import create_model, load_state_dict
|
|
10
|
+
from .ldm.modules.diffusionmodules.openaimodel import (ResBlock, TimestepEmbedSequential, AttentionBlock,
|
|
11
|
+
Upsample, SpatialTransformer, Downsample)
|
|
12
|
+
from .ldm.modules.diffusionmodules.util import (
|
|
13
|
+
checkpoint,
|
|
14
|
+
conv_nd,
|
|
15
|
+
linear,
|
|
16
|
+
avg_pool_nd,
|
|
17
|
+
zero_module,
|
|
18
|
+
normalization,
|
|
19
|
+
timestep_embedding,
|
|
20
|
+
)
|
|
21
|
+
from .ldm.util import exists
|
|
22
|
+
|
|
23
|
+
class Post_Process_Net(nn.Module):
|
|
24
|
+
def __init__(
|
|
25
|
+
self,
|
|
26
|
+
image_size,
|
|
27
|
+
in_channels,
|
|
28
|
+
model_channels,
|
|
29
|
+
out_channels,
|
|
30
|
+
num_res_blocks,
|
|
31
|
+
attention_resolutions,
|
|
32
|
+
dropout=0,
|
|
33
|
+
channel_mult=(1, 2, 4, 8),
|
|
34
|
+
conv_resample=True,
|
|
35
|
+
dims=2,
|
|
36
|
+
num_classes=None,
|
|
37
|
+
use_checkpoint=False,
|
|
38
|
+
use_fp16=False,
|
|
39
|
+
num_heads=-1,
|
|
40
|
+
num_head_channels=-1,
|
|
41
|
+
num_heads_upsample=-1,
|
|
42
|
+
use_scale_shift_norm=False,
|
|
43
|
+
resblock_updown=False,
|
|
44
|
+
use_new_attention_order=False,
|
|
45
|
+
use_spatial_transformer=False, # custom transformer support
|
|
46
|
+
transformer_depth=1, # custom transformer support
|
|
47
|
+
context_dim=None, # custom transformer support
|
|
48
|
+
n_embed=None, # custom support for prediction of discrete ids into codebook of first stage vq model
|
|
49
|
+
legacy=True,
|
|
50
|
+
disable_self_attentions=None,
|
|
51
|
+
num_attention_blocks=None,
|
|
52
|
+
disable_middle_self_attn=False,
|
|
53
|
+
use_linear_in_transformer=False,
|
|
54
|
+
):
|
|
55
|
+
super().__init__()
|
|
56
|
+
self.image_size = image_size
|
|
57
|
+
self.in_channels = in_channels
|
|
58
|
+
self.model_channels = model_channels
|
|
59
|
+
self.out_channels = out_channels
|
|
60
|
+
if isinstance(num_res_blocks, int):
|
|
61
|
+
self.num_res_blocks = len(channel_mult) * [num_res_blocks]
|
|
62
|
+
else:
|
|
63
|
+
if len(num_res_blocks) != len(channel_mult):
|
|
64
|
+
raise ValueError("provide num_res_blocks either as an int (globally constant) or "
|
|
65
|
+
"as a list/tuple (per-level) with the same length as channel_mult")
|
|
66
|
+
self.num_res_blocks = num_res_blocks
|
|
67
|
+
if disable_self_attentions is not None:
|
|
68
|
+
# should be a list of booleans, indicating whether to disable self-attention in TransformerBlocks or not
|
|
69
|
+
assert len(disable_self_attentions) == len(channel_mult)
|
|
70
|
+
if num_attention_blocks is not None:
|
|
71
|
+
assert len(num_attention_blocks) == len(self.num_res_blocks)
|
|
72
|
+
assert all(map(lambda i: self.num_res_blocks[i] >= num_attention_blocks[i], range(len(num_attention_blocks))))
|
|
73
|
+
print(f"Constructor of UNetModel received num_attention_blocks={num_attention_blocks}. "
|
|
74
|
+
f"This option has LESS priority than attention_resolutions {attention_resolutions}, "
|
|
75
|
+
f"i.e., in cases where num_attention_blocks[i] > 0 but 2**i not in attention_resolutions, "
|
|
76
|
+
f"attention will still not be set.")
|
|
77
|
+
|
|
78
|
+
self.attention_resolutions = attention_resolutions
|
|
79
|
+
self.dropout = dropout
|
|
80
|
+
self.channel_mult = channel_mult
|
|
81
|
+
self.conv_resample = conv_resample
|
|
82
|
+
self.num_classes = num_classes
|
|
83
|
+
self.use_checkpoint = use_checkpoint
|
|
84
|
+
self.dtype = torch.float16 if use_fp16 else torch.float32
|
|
85
|
+
self.num_heads = num_heads
|
|
86
|
+
self.num_head_channels = num_head_channels
|
|
87
|
+
self.num_heads_upsample = num_heads_upsample
|
|
88
|
+
self.predict_codebook_ids = n_embed is not None
|
|
89
|
+
|
|
90
|
+
time_embed_dim = model_channels * 4
|
|
91
|
+
self.time_embed = nn.Sequential(
|
|
92
|
+
linear(model_channels, time_embed_dim),
|
|
93
|
+
nn.SiLU(),
|
|
94
|
+
linear(time_embed_dim, time_embed_dim),
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
if self.num_classes is not None:
|
|
98
|
+
if isinstance(self.num_classes, int):
|
|
99
|
+
self.label_emb = nn.Embedding(num_classes, time_embed_dim)
|
|
100
|
+
elif self.num_classes == "continuous":
|
|
101
|
+
print("setting up linear c_adm embedding layer")
|
|
102
|
+
self.label_emb = nn.Linear(1, time_embed_dim)
|
|
103
|
+
else:
|
|
104
|
+
raise ValueError()
|
|
105
|
+
|
|
106
|
+
self.input_blocks = nn.ModuleList(
|
|
107
|
+
[
|
|
108
|
+
TimestepEmbedSequential(
|
|
109
|
+
conv_nd(dims, in_channels, model_channels, 3, padding=1)
|
|
110
|
+
)
|
|
111
|
+
]
|
|
112
|
+
)
|
|
113
|
+
self._feature_size = model_channels
|
|
114
|
+
input_block_chans = [model_channels]
|
|
115
|
+
ch = model_channels
|
|
116
|
+
ds = 1
|
|
117
|
+
for level, mult in enumerate(channel_mult):
|
|
118
|
+
for nr in range(self.num_res_blocks[level]):
|
|
119
|
+
layers = [
|
|
120
|
+
ResBlock(
|
|
121
|
+
ch,
|
|
122
|
+
time_embed_dim,
|
|
123
|
+
dropout,
|
|
124
|
+
out_channels=mult * model_channels,
|
|
125
|
+
dims=dims,
|
|
126
|
+
use_checkpoint=use_checkpoint,
|
|
127
|
+
use_scale_shift_norm=use_scale_shift_norm,
|
|
128
|
+
)
|
|
129
|
+
]
|
|
130
|
+
ch = mult * model_channels
|
|
131
|
+
if ds in attention_resolutions:
|
|
132
|
+
if num_head_channels == -1:
|
|
133
|
+
dim_head = ch // num_heads
|
|
134
|
+
else:
|
|
135
|
+
num_heads = ch // num_head_channels
|
|
136
|
+
dim_head = num_head_channels
|
|
137
|
+
if legacy:
|
|
138
|
+
#num_heads = 1
|
|
139
|
+
dim_head = ch // num_heads if use_spatial_transformer else num_head_channels
|
|
140
|
+
if exists(disable_self_attentions):
|
|
141
|
+
disabled_sa = disable_self_attentions[level]
|
|
142
|
+
else:
|
|
143
|
+
disabled_sa = False
|
|
144
|
+
|
|
145
|
+
if not exists(num_attention_blocks) or nr < num_attention_blocks[level]:
|
|
146
|
+
layers.append(
|
|
147
|
+
AttentionBlock(
|
|
148
|
+
ch,
|
|
149
|
+
use_checkpoint=use_checkpoint,
|
|
150
|
+
num_heads=num_heads,
|
|
151
|
+
num_head_channels=dim_head,
|
|
152
|
+
use_new_attention_order=use_new_attention_order,
|
|
153
|
+
) if not use_spatial_transformer else SpatialTransformer(
|
|
154
|
+
ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim,
|
|
155
|
+
disable_self_attn=disabled_sa, use_linear=use_linear_in_transformer,
|
|
156
|
+
use_checkpoint=use_checkpoint
|
|
157
|
+
)
|
|
158
|
+
)
|
|
159
|
+
self.input_blocks.append(TimestepEmbedSequential(*layers))
|
|
160
|
+
self._feature_size += ch
|
|
161
|
+
input_block_chans.append(ch)
|
|
162
|
+
if level != len(channel_mult) - 1:
|
|
163
|
+
out_ch = ch
|
|
164
|
+
if resblock_updown:
|
|
165
|
+
stage_last_block = ResBlock(
|
|
166
|
+
ch,
|
|
167
|
+
time_embed_dim,
|
|
168
|
+
dropout,
|
|
169
|
+
out_channels=out_ch,
|
|
170
|
+
dims=dims,
|
|
171
|
+
use_checkpoint=use_checkpoint,
|
|
172
|
+
use_scale_shift_norm=use_scale_shift_norm,
|
|
173
|
+
down=True,
|
|
174
|
+
)
|
|
175
|
+
else:
|
|
176
|
+
stage_last_block = Downsample(
|
|
177
|
+
ch, conv_resample, dims=dims, out_channels=out_ch
|
|
178
|
+
)
|
|
179
|
+
self.input_blocks.append(
|
|
180
|
+
TimestepEmbedSequential(stage_last_block)
|
|
181
|
+
)
|
|
182
|
+
ch = out_ch
|
|
183
|
+
input_block_chans.append(ch)
|
|
184
|
+
ds *= 2
|
|
185
|
+
self._feature_size += ch
|
|
186
|
+
|
|
187
|
+
if num_head_channels == -1:
|
|
188
|
+
dim_head = ch // num_heads
|
|
189
|
+
else:
|
|
190
|
+
num_heads = ch // num_head_channels
|
|
191
|
+
dim_head = num_head_channels
|
|
192
|
+
if legacy:
|
|
193
|
+
#num_heads = 1
|
|
194
|
+
dim_head = ch // num_heads if use_spatial_transformer else num_head_channels
|
|
195
|
+
self.middle_block = TimestepEmbedSequential(
|
|
196
|
+
ResBlock(
|
|
197
|
+
ch,
|
|
198
|
+
time_embed_dim,
|
|
199
|
+
dropout,
|
|
200
|
+
dims=dims,
|
|
201
|
+
use_checkpoint=use_checkpoint,
|
|
202
|
+
use_scale_shift_norm=use_scale_shift_norm,
|
|
203
|
+
),
|
|
204
|
+
AttentionBlock(
|
|
205
|
+
ch,
|
|
206
|
+
use_checkpoint=use_checkpoint,
|
|
207
|
+
num_heads=num_heads,
|
|
208
|
+
num_head_channels=dim_head,
|
|
209
|
+
use_new_attention_order=use_new_attention_order,
|
|
210
|
+
) if not use_spatial_transformer else SpatialTransformer( # always uses a self-attn
|
|
211
|
+
ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim,
|
|
212
|
+
disable_self_attn=disable_middle_self_attn, use_linear=use_linear_in_transformer,
|
|
213
|
+
use_checkpoint=use_checkpoint
|
|
214
|
+
),
|
|
215
|
+
ResBlock(
|
|
216
|
+
ch,
|
|
217
|
+
time_embed_dim,
|
|
218
|
+
dropout,
|
|
219
|
+
dims=dims,
|
|
220
|
+
use_checkpoint=use_checkpoint,
|
|
221
|
+
use_scale_shift_norm=use_scale_shift_norm,
|
|
222
|
+
),
|
|
223
|
+
)
|
|
224
|
+
self._feature_size += ch
|
|
225
|
+
|
|
226
|
+
self.output_blocks1 = nn.ModuleList([])
|
|
227
|
+
self.output_blocks2 = nn.ModuleList([])
|
|
228
|
+
for level, mult in list(enumerate(channel_mult))[::-1]:
|
|
229
|
+
for i in range(self.num_res_blocks[level] + 1):
|
|
230
|
+
ich = input_block_chans.pop()
|
|
231
|
+
layers1 = [
|
|
232
|
+
ResBlock(
|
|
233
|
+
ch + ich,
|
|
234
|
+
time_embed_dim,
|
|
235
|
+
dropout,
|
|
236
|
+
out_channels=model_channels * mult,
|
|
237
|
+
dims=dims,
|
|
238
|
+
use_checkpoint=use_checkpoint,
|
|
239
|
+
use_scale_shift_norm=use_scale_shift_norm,
|
|
240
|
+
)
|
|
241
|
+
]
|
|
242
|
+
layers2 = [
|
|
243
|
+
ResBlock(
|
|
244
|
+
ch + ich,
|
|
245
|
+
time_embed_dim,
|
|
246
|
+
dropout,
|
|
247
|
+
out_channels=model_channels * mult,
|
|
248
|
+
dims=dims,
|
|
249
|
+
use_checkpoint=use_checkpoint,
|
|
250
|
+
use_scale_shift_norm=use_scale_shift_norm,
|
|
251
|
+
)
|
|
252
|
+
]
|
|
253
|
+
ch = model_channels * mult
|
|
254
|
+
if ds in attention_resolutions:
|
|
255
|
+
if num_head_channels == -1:
|
|
256
|
+
dim_head = ch // num_heads
|
|
257
|
+
else:
|
|
258
|
+
num_heads = ch // num_head_channels
|
|
259
|
+
dim_head = num_head_channels
|
|
260
|
+
if legacy:
|
|
261
|
+
#num_heads = 1
|
|
262
|
+
dim_head = ch // num_heads if use_spatial_transformer else num_head_channels
|
|
263
|
+
if exists(disable_self_attentions):
|
|
264
|
+
disabled_sa = disable_self_attentions[level]
|
|
265
|
+
else:
|
|
266
|
+
disabled_sa = False
|
|
267
|
+
|
|
268
|
+
if not exists(num_attention_blocks) or i < num_attention_blocks[level]:
|
|
269
|
+
layers1.append(
|
|
270
|
+
AttentionBlock(
|
|
271
|
+
ch,
|
|
272
|
+
use_checkpoint=use_checkpoint,
|
|
273
|
+
num_heads=num_heads_upsample,
|
|
274
|
+
num_head_channels=dim_head,
|
|
275
|
+
use_new_attention_order=use_new_attention_order,
|
|
276
|
+
) if not use_spatial_transformer else SpatialTransformer(
|
|
277
|
+
ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim,
|
|
278
|
+
disable_self_attn=disabled_sa, use_linear=use_linear_in_transformer,
|
|
279
|
+
use_checkpoint=use_checkpoint
|
|
280
|
+
)
|
|
281
|
+
)
|
|
282
|
+
layers2.append(
|
|
283
|
+
AttentionBlock(
|
|
284
|
+
ch,
|
|
285
|
+
use_checkpoint=use_checkpoint,
|
|
286
|
+
num_heads=num_heads_upsample,
|
|
287
|
+
num_head_channels=dim_head,
|
|
288
|
+
use_new_attention_order=use_new_attention_order,
|
|
289
|
+
) if not use_spatial_transformer else SpatialTransformer(
|
|
290
|
+
ch, num_heads, dim_head, depth=transformer_depth, context_dim=context_dim,
|
|
291
|
+
disable_self_attn=disabled_sa, use_linear=use_linear_in_transformer,
|
|
292
|
+
use_checkpoint=use_checkpoint
|
|
293
|
+
)
|
|
294
|
+
)
|
|
295
|
+
if level and i == self.num_res_blocks[level]:
|
|
296
|
+
out_ch = ch
|
|
297
|
+
layers1.append(ResBlock(ch,
|
|
298
|
+
time_embed_dim,
|
|
299
|
+
dropout,
|
|
300
|
+
out_channels=out_ch,
|
|
301
|
+
dims=dims,
|
|
302
|
+
use_checkpoint=use_checkpoint,
|
|
303
|
+
use_scale_shift_norm=use_scale_shift_norm,
|
|
304
|
+
up=True,
|
|
305
|
+
) if resblock_updown else
|
|
306
|
+
Upsample(ch, conv_resample, dims=dims, out_channels=out_ch))
|
|
307
|
+
layers2.append(ResBlock(ch,
|
|
308
|
+
time_embed_dim,
|
|
309
|
+
dropout,
|
|
310
|
+
out_channels=out_ch,
|
|
311
|
+
dims=dims,
|
|
312
|
+
use_checkpoint=use_checkpoint,
|
|
313
|
+
use_scale_shift_norm=use_scale_shift_norm,
|
|
314
|
+
up=True,
|
|
315
|
+
) if resblock_updown else
|
|
316
|
+
Upsample(ch, conv_resample, dims=dims, out_channels=out_ch))
|
|
317
|
+
ds //= 2
|
|
318
|
+
self.output_blocks1.append(TimestepEmbedSequential(*layers1))
|
|
319
|
+
self.output_blocks2.append(TimestepEmbedSequential(*layers2))
|
|
320
|
+
self._feature_size += ch
|
|
321
|
+
|
|
322
|
+
self.out1 = nn.Sequential(
|
|
323
|
+
normalization(ch),
|
|
324
|
+
nn.SiLU(),
|
|
325
|
+
zero_module(conv_nd(dims, model_channels, 3, 3, padding=1)),
|
|
326
|
+
)
|
|
327
|
+
self.out2 = nn.Sequential(
|
|
328
|
+
normalization(ch),
|
|
329
|
+
nn.SiLU(),
|
|
330
|
+
zero_module(conv_nd(dims, model_channels, 1, 3, padding=1)),
|
|
331
|
+
)
|
|
332
|
+
if self.predict_codebook_ids:
|
|
333
|
+
self.id_predictor = nn.Sequential(
|
|
334
|
+
normalization(ch),
|
|
335
|
+
conv_nd(dims, model_channels, n_embed, 1),
|
|
336
|
+
#nn.LogSoftmax(dim=1) # change to cross_entropy and produce non-normalized logits
|
|
337
|
+
)
|
|
338
|
+
|
|
339
|
+
def forward(self, x, timesteps=None, context=None, y=None,**kwargs):
|
|
340
|
+
"""
|
|
341
|
+
Apply the model to an input batch.
|
|
342
|
+
:param x: an [N x C x ...] Tensor of inputs.
|
|
343
|
+
:param timesteps: a 1-D batch of timesteps.
|
|
344
|
+
:param context: conditioning plugged in via crossattn
|
|
345
|
+
:param y: an [N] Tensor of labels, if class-conditional.
|
|
346
|
+
:return: an [N x C x ...] Tensor of outputs.
|
|
347
|
+
"""
|
|
348
|
+
assert (y is not None) == (
|
|
349
|
+
self.num_classes is not None
|
|
350
|
+
), "must specify y if and only if the model is class-conditional"
|
|
351
|
+
hs = []
|
|
352
|
+
t_emb = timestep_embedding(timesteps, self.model_channels, repeat_only=False)
|
|
353
|
+
emb = self.time_embed(t_emb)
|
|
354
|
+
|
|
355
|
+
if self.num_classes is not None:
|
|
356
|
+
assert y.shape[0] == x.shape[0]
|
|
357
|
+
emb = emb + self.label_emb(y)
|
|
358
|
+
|
|
359
|
+
h = x.type(self.dtype)
|
|
360
|
+
for module in self.input_blocks:
|
|
361
|
+
h = module(h, emb, context)
|
|
362
|
+
hs.append(h)
|
|
363
|
+
h = self.middle_block(h, emb, context)
|
|
364
|
+
h1 = h2 = h
|
|
365
|
+
for module1, module2 in zip(self.output_blocks1, self.output_blocks2):
|
|
366
|
+
pre_h = hs.pop()
|
|
367
|
+
h1 = torch.cat([h1, pre_h], dim=1)
|
|
368
|
+
h1 = module1(h1, emb, context)
|
|
369
|
+
h2 = torch.cat([h2, pre_h], dim=1)
|
|
370
|
+
h2 = module2(h2, emb, context)
|
|
371
|
+
h1 = h1.type(x.dtype)
|
|
372
|
+
h2 = h2.type(x.dtype)
|
|
373
|
+
return torch.cat([self.out1(h1), self.out2(h2)], dim=1)
|
|
374
|
+
|
|
375
|
+
|
|
376
|
+
class PostProcess(pl.LightningModule):
|
|
377
|
+
def __init__(self, model_path, control_net_path, infe_steps=50, *args, **kwargs):
|
|
378
|
+
super().__init__(*args, **kwargs)
|
|
379
|
+
self.post_process_net = Post_Process_Net(image_size=256,
|
|
380
|
+
in_channels=7,
|
|
381
|
+
out_channels=4,
|
|
382
|
+
model_channels=96,
|
|
383
|
+
attention_resolutions=[],
|
|
384
|
+
num_res_blocks=2,
|
|
385
|
+
channel_mult=[ 1, 2, 2, 4 ],
|
|
386
|
+
num_head_channels=64,
|
|
387
|
+
use_spatial_transformer=False,
|
|
388
|
+
use_linear_in_transformer=True,
|
|
389
|
+
transformer_depth=1,
|
|
390
|
+
context_dim=320,
|
|
391
|
+
legacy=False,
|
|
392
|
+
use_checkpoint=True)
|
|
393
|
+
self.model = create_model(model_path).cpu()
|
|
394
|
+
self.model.load_state_dict(load_state_dict(control_net_path, location='cuda'), strict=False)
|
|
395
|
+
self.learning_rate = 1e-5
|
|
396
|
+
self.infe_steps = infe_steps
|
|
397
|
+
|
|
398
|
+
def training_step(self, batch, batch_idx):
|
|
399
|
+
self.post_process_net.train()
|
|
400
|
+
# b,h,w,c
|
|
401
|
+
comp_img_scaled = batch['hint'][:, :, :, :3]
|
|
402
|
+
gt_mask_scaled = batch["gt_mask"]
|
|
403
|
+
gt_img_scaled = batch['jpg']
|
|
404
|
+
obj_mask = batch['hint'][:, :, :, 3:]
|
|
405
|
+
comp_img, gt_mask, gt_img = restore_img(comp_img_scaled, gt_mask_scaled, gt_img_scaled)
|
|
406
|
+
batch_size = gt_mask.shape[0]
|
|
407
|
+
|
|
408
|
+
with torch.no_grad():
|
|
409
|
+
if os.path.exists(os.path.join(self.generated_image_path, batch['name'][0])):
|
|
410
|
+
images = []
|
|
411
|
+
for name in batch['name']:
|
|
412
|
+
img_path = os.path.join(self.generated_image_path, name)
|
|
413
|
+
image = Image.open(img_path).convert('RGB').resize((256, 256),Image.NEAREST)
|
|
414
|
+
image = np.array(image)
|
|
415
|
+
images.append(torch.tensor(image[None, :, : ,:]))
|
|
416
|
+
image = torch.clamp(torch.concat(images, dim=0).to(comp_img.device) + 10, 0, 255)
|
|
417
|
+
image_scaled = (image / 127.5) - 1
|
|
418
|
+
else:
|
|
419
|
+
images = self.model.log_images(batch, mode='pndm', input=(comp_img_scaled*2-1).permute(0,3,1,2),
|
|
420
|
+
ddim_steps=self.infe_steps, add_noise_strength=1)
|
|
421
|
+
image_scaled = images['samples_cfg_scale_9.00'].permute(0,2,3,1)
|
|
422
|
+
image = torch.clamp(image_scaled, -1., 1.)
|
|
423
|
+
image = (image + 1.0) / 2.0
|
|
424
|
+
image = (image * 255).int() # bchw -> bhwc
|
|
425
|
+
for i in range(batch_size):
|
|
426
|
+
image_to_save = np.array(image[i].detach().cpu(), dtype=np.uint8)
|
|
427
|
+
img_path = os.path.join(self.generated_image_path, batch['name'][i])
|
|
428
|
+
image_to_save = Image.fromarray(image_to_save)
|
|
429
|
+
image_to_save.save(img_path)
|
|
430
|
+
image.to(comp_img.device)
|
|
431
|
+
|
|
432
|
+
input = torch.concat([image_scaled, comp_img_scaled * 2 - 1, obj_mask], dim=-1)
|
|
433
|
+
null_timeteps = torch.zeros(batch_size, device=input.device)
|
|
434
|
+
output = self.post_process_net(input.permute(0,3,1,2), timesteps=null_timeteps)
|
|
435
|
+
output = output.permute(0,2,3,1)
|
|
436
|
+
|
|
437
|
+
pred_mask = output[:, :, :, 3]
|
|
438
|
+
adjusted_img = output[:, :, :, :3]
|
|
439
|
+
|
|
440
|
+
loss1 = nn.functional.l1_loss(pred_mask, gt_mask_scaled * 2 - 1)
|
|
441
|
+
loss2 = nn.functional.l1_loss(adjusted_img, gt_img_scaled, reduction='none')
|
|
442
|
+
loss = loss1 + loss2.mean()
|
|
443
|
+
|
|
444
|
+
return loss
|
|
445
|
+
|
|
446
|
+
def configure_optimizers(self):
|
|
447
|
+
lr = self.learning_rate
|
|
448
|
+
params = list(self.post_process_net.parameters())
|
|
449
|
+
opt = torch.optim.AdamW(params, lr=lr)
|
|
450
|
+
return opt
|
|
451
|
+
|
|
452
|
+
# get pred_mask, adjusted img and new composite img for logging sake
|
|
453
|
+
def get_log(self, batch, batch_idx, log_num=1):
|
|
454
|
+
comp_img_scaled = batch['hint'][:, :, :, :3][:log_num]
|
|
455
|
+
gt_mask_scaled = batch["gt_mask"][:log_num]
|
|
456
|
+
gt_img_scaled = batch['jpg'][:log_num]
|
|
457
|
+
obj_mask = batch['hint'][:, :, :, 3:][:log_num]
|
|
458
|
+
comp_img, gt_mask, gt_img = restore_img(comp_img_scaled, gt_mask_scaled, gt_img_scaled)
|
|
459
|
+
trimed_batch = {}
|
|
460
|
+
for key, dataset in batch.items():
|
|
461
|
+
trimed_batch[key] = dataset[:log_num]
|
|
462
|
+
self.post_process_net.eval()
|
|
463
|
+
|
|
464
|
+
with torch.no_grad():
|
|
465
|
+
if os.path.exists(os.path.join(self.generated_image_path, trimed_batch['name'][0])):
|
|
466
|
+
images = []
|
|
467
|
+
for name in trimed_batch['name']:
|
|
468
|
+
img_path = os.path.join(self.generated_image_path, name)
|
|
469
|
+
image = Image.open(img_path).convert('RGB').resize((256, 256),Image.NEAREST)
|
|
470
|
+
image = np.array(image)
|
|
471
|
+
images.append(torch.tensor(image[None, :, : ,:]))
|
|
472
|
+
image = torch.clamp(torch.concat(images, dim=0).to(comp_img.device) + 10, 0, 255)
|
|
473
|
+
image_scaled = (image / 127.5) - 1
|
|
474
|
+
else:
|
|
475
|
+
images = self.model.log_images(trimed_batch, mode='pndm', input=(comp_img_scaled*2-1).permute(0,3,1,2),
|
|
476
|
+
ddim_steps=self.infe_steps, add_noise_strength=1)
|
|
477
|
+
image_scaled = images['samples_cfg_scale_9.00'].permute(0,2,3,1)
|
|
478
|
+
image = torch.clamp(image_scaled, -1., 1.)
|
|
479
|
+
image = (image + 1.0) / 2.0
|
|
480
|
+
image = (image * 255).int()
|
|
481
|
+
|
|
482
|
+
input = torch.concat([image_scaled, comp_img_scaled * 2 - 1, obj_mask], dim=-1)
|
|
483
|
+
null_timeteps = torch.zeros(log_num, device=input.device)
|
|
484
|
+
output = self.post_process_net(input.permute(0,3,1,2), timesteps=null_timeteps)
|
|
485
|
+
output = output.permute(0,2,3,1)
|
|
486
|
+
|
|
487
|
+
pred_mask = torch.greater_equal(output[:, :, :, 3], -0.1).int()
|
|
488
|
+
adjusted_img = output[:, :, :, :3]
|
|
489
|
+
adjusted_img = torch.clamp(adjusted_img, -1., 1.)
|
|
490
|
+
adjusted_img = (adjusted_img + 1.0) / 2.0
|
|
491
|
+
adjusted_img = (adjusted_img * 255).int()
|
|
492
|
+
new_composite_img = adjusted_img * pred_mask.unsqueeze(3) + (1-pred_mask.unsqueeze(3)) * comp_img
|
|
493
|
+
|
|
494
|
+
self.post_process_net.train()
|
|
495
|
+
|
|
496
|
+
log_info = {"gt_img": gt_img, "gt_mask":gt_mask, "original_pred_img":image, "adjusted_imgs":adjusted_img,
|
|
497
|
+
"new_comp_imgs": new_composite_img, "pred_masks":pred_mask*255}
|
|
498
|
+
for key, k in log_info.items():
|
|
499
|
+
log_info[key] = k.detach().cpu()
|
|
500
|
+
|
|
501
|
+
return log_info
|
|
502
|
+
|
|
503
|
+
@torch.no_grad()
|
|
504
|
+
def restore_img(comp_img, gt_mask, gt_img):
|
|
505
|
+
assert torch.all(torch.greater_equal(comp_img, 0)) and torch.all(torch.less_equal(comp_img, 1)), "wrong scale for comp_img"
|
|
506
|
+
assert torch.all(torch.greater_equal(gt_img, -1) ) and torch.all(torch.less_equal(gt_img, 1)), "wrong scale for gt_img"
|
|
507
|
+
assert torch.all(torch.greater_equal(gt_mask, 0) ) and torch.all(torch.less_equal(gt_mask, 1)), "wrong scale for gt_mask"
|
|
508
|
+
|
|
509
|
+
comp_img = comp_img * 255
|
|
510
|
+
gt_mask = gt_mask * 255
|
|
511
|
+
gt_img = (gt_img + 1) * 127.5
|
|
512
|
+
|
|
513
|
+
return comp_img, gt_mask, gt_img
|
|
File without changes
|
|
File without changes
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
import torch
|
|
2
|
+
import torch.nn as nn
|
|
3
|
+
import torchvision.models as models
|
|
4
|
+
|
|
5
|
+
class BaseNetwork(nn.Module):
|
|
6
|
+
def __init__(self):
|
|
7
|
+
super(BaseNetwork, self).__init__()
|
|
8
|
+
|
|
9
|
+
def init_weights(self, init_type='normal', gain=0.02):
|
|
10
|
+
def init_func(m):
|
|
11
|
+
classname = m.__class__.__name__
|
|
12
|
+
if hasattr(m, 'weight') and (classname.find('Conv') != -1 or classname.find('Linear') != -1):
|
|
13
|
+
if init_type == 'normal':
|
|
14
|
+
nn.init.normal_(m.weight.data, 0.0, gain)
|
|
15
|
+
elif init_type == 'xavier':
|
|
16
|
+
nn.init.xavier_normal_(m.weight.data, gain=gain)
|
|
17
|
+
elif init_type == 'kaiming':
|
|
18
|
+
nn.init.kaiming_normal_(m.weight.data, a=0, mode='fan_in')
|
|
19
|
+
elif init_type == 'orthogonal':
|
|
20
|
+
nn.init.orthogonal_(m.weight.data, gain=gain)
|
|
21
|
+
|
|
22
|
+
if hasattr(m, 'bias') and m.bias is not None:
|
|
23
|
+
nn.init.constant_(m.bias.data, 0.0)
|
|
24
|
+
|
|
25
|
+
elif classname.find('BatchNorm2d') != -1:
|
|
26
|
+
nn.init.normal_(m.weight.data, 1.0, gain)
|
|
27
|
+
nn.init.constant_(m.bias.data, 0.0)
|
|
28
|
+
|
|
29
|
+
self.apply(init_func)
|
|
30
|
+
|
|
31
|
+
class DoubleConv(BaseNetwork):
|
|
32
|
+
"""(convolution => [BN] => ReLU) * 2"""
|
|
33
|
+
def __init__(self, in_channels, out_channels, mid_channels=None, norm_layer = nn.InstanceNorm2d):
|
|
34
|
+
super().__init__()
|
|
35
|
+
self.norm_layer = norm_layer
|
|
36
|
+
if not mid_channels:
|
|
37
|
+
mid_channels = out_channels
|
|
38
|
+
self.double_conv = nn.Sequential(
|
|
39
|
+
nn.Conv2d(in_channels, mid_channels, kernel_size=3, padding=1),
|
|
40
|
+
self.norm_layer(mid_channels),
|
|
41
|
+
nn.ReLU(inplace=True),
|
|
42
|
+
nn.Conv2d(mid_channels, out_channels, kernel_size=3, padding=1),
|
|
43
|
+
self.norm_layer(out_channels),
|
|
44
|
+
nn.ReLU(inplace=True)
|
|
45
|
+
)
|
|
46
|
+
|
|
47
|
+
def forward(self, x):
|
|
48
|
+
return self.double_conv(x)
|
|
49
|
+
|
|
50
|
+
class Bottleneck(BaseNetwork):
|
|
51
|
+
def __init__(self,in_places,out_places,stride=1,downsampling=False,expansion=4, norm_layer = nn.InstanceNorm2d):
|
|
52
|
+
super(Bottleneck,self).__init__()
|
|
53
|
+
self.expansion = expansion
|
|
54
|
+
self.downsampling = downsampling
|
|
55
|
+
self.norm_layer = norm_layer
|
|
56
|
+
self.bottleneck = nn.Sequential(
|
|
57
|
+
nn.Conv2d(in_channels=in_places,out_channels=out_places,kernel_size=1,stride=1),
|
|
58
|
+
self.norm_layer(out_places),
|
|
59
|
+
nn.ReLU(inplace=True),
|
|
60
|
+
nn.Conv2d(in_channels=out_places,out_channels=out_places,kernel_size=3,stride=stride,padding=1),
|
|
61
|
+
self.norm_layer(out_places),
|
|
62
|
+
nn.ReLU(inplace=True),
|
|
63
|
+
nn.Conv2d(in_channels=out_places,out_channels=out_places*self.expansion,kernel_size=1,stride=1),
|
|
64
|
+
self.norm_layer(out_places*self.expansion)
|
|
65
|
+
)
|
|
66
|
+
|
|
67
|
+
if self.downsampling :
|
|
68
|
+
self.downsample = nn.Sequential(
|
|
69
|
+
nn.Conv2d(in_channels=in_places,out_channels=out_places*self.expansion,kernel_size=1,stride=stride),
|
|
70
|
+
self.norm_layer(out_places*self.expansion)
|
|
71
|
+
)
|
|
72
|
+
self.relu = nn.ReLU(inplace=True)
|
|
73
|
+
|
|
74
|
+
def forward(self,x):
|
|
75
|
+
residual = x
|
|
76
|
+
out = self.bottleneck(x)
|
|
77
|
+
|
|
78
|
+
if self.downsampling:
|
|
79
|
+
residual = self.downsample(x)
|
|
80
|
+
out += residual
|
|
81
|
+
out = self.relu(out)
|
|
82
|
+
return out
|
|
83
|
+
|
|
84
|
+
class ResNet(BaseNetwork):
|
|
85
|
+
def __init__(self, blocks, out_places=[64,16,32,64,64], input_nc = 4, expansion=4, norm_layer=nn.InstanceNorm2d):
|
|
86
|
+
super(ResNet, self).__init__()
|
|
87
|
+
self.expansion = expansion
|
|
88
|
+
self.norm_layer = norm_layer
|
|
89
|
+
self.bilinear = True
|
|
90
|
+
self.conv = DoubleConv(input_nc, 64, norm_layer=nn.BatchNorm2d) #TODO:
|
|
91
|
+
self.conv.eval()
|
|
92
|
+
|
|
93
|
+
self.layer1 = self.make_layer(in_places=out_places[0], out_places=out_places[1], block=blocks[0], stride=2,
|
|
94
|
+
norm_layer=self.norm_layer)
|
|
95
|
+
self.layer2 = self.make_layer(in_places=4*out_places[1], out_places=out_places[2], block=blocks[1], stride=2,
|
|
96
|
+
norm_layer=self.norm_layer)
|
|
97
|
+
self.layer3 = self.make_layer(in_places=4*out_places[2], out_places=out_places[3], block=blocks[2], stride=2,
|
|
98
|
+
norm_layer=self.norm_layer)
|
|
99
|
+
self.layer4 = self.make_layer(in_places=4*out_places[3], out_places=out_places[4], block=blocks[3], stride=2,
|
|
100
|
+
norm_layer=self.norm_layer)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
for m in self.modules():
|
|
104
|
+
if isinstance(m, nn.Conv2d):
|
|
105
|
+
nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
|
|
106
|
+
|
|
107
|
+
def make_layer(self, in_places, out_places, block, stride, norm_layer=nn.InstanceNorm2d):
|
|
108
|
+
layers = []
|
|
109
|
+
layers.append(
|
|
110
|
+
Bottleneck(in_places, out_places, stride, downsampling=True, norm_layer=norm_layer))
|
|
111
|
+
for i in range(1, block):
|
|
112
|
+
layers.append(
|
|
113
|
+
Bottleneck(out_places * self.expansion, out_places, norm_layer=norm_layer))
|
|
114
|
+
|
|
115
|
+
return nn.Sequential(*layers)
|
|
116
|
+
|
|
117
|
+
def forward(self, input):
|
|
118
|
+
f1 = self.conv(input)
|
|
119
|
+
f2 = self.layer1(f1)
|
|
120
|
+
f3 = self.layer2(f2)
|
|
121
|
+
f4 = self.layer3(f3)
|
|
122
|
+
f5 = self.layer4(f4)
|
|
123
|
+
return f5
|
|
124
|
+
|
|
125
|
+
class RegNetwork(BaseNetwork):
|
|
126
|
+
|
|
127
|
+
def __init__(self,input_dim=256, norm_layer=nn.InstanceNorm2d):
|
|
128
|
+
super(RegNetwork,self).__init__()
|
|
129
|
+
self.norm_layer = norm_layer
|
|
130
|
+
self.FeatureEncoder = ResNet([3,4,6,3],input_nc=4,norm_layer=self.norm_layer)
|
|
131
|
+
self.net = nn.Sequential(
|
|
132
|
+
nn.Conv2d(input_dim,input_dim*2,kernel_size=3,padding=1,stride=2),
|
|
133
|
+
self.norm_layer(input_dim*2),
|
|
134
|
+
nn.ReLU(inplace=True),
|
|
135
|
+
nn.Conv2d(input_dim*2,input_dim*2,kernel_size=3,padding=1,stride=1),
|
|
136
|
+
self.norm_layer(input_dim*2),
|
|
137
|
+
nn.ReLU(inplace=True),
|
|
138
|
+
nn.Conv2d(input_dim*2,input_dim*2,kernel_size=3,padding=1,stride=1),
|
|
139
|
+
self.norm_layer(input_dim*2),
|
|
140
|
+
nn.ReLU(inplace=True),
|
|
141
|
+
nn.AdaptiveAvgPool2d(1),
|
|
142
|
+
nn.Flatten(),
|
|
143
|
+
)
|
|
144
|
+
self.fc_bbx = nn.Linear(input_dim*2, 5)
|
|
145
|
+
|
|
146
|
+
def forward(self, x):
|
|
147
|
+
x = self.FeatureEncoder(x)
|
|
148
|
+
x = self.net(x)
|
|
149
|
+
pred_t = self.fc_bbx(x)
|
|
150
|
+
pred_t[:, 4] = torch.tanh(pred_t[:, 4]) * (torch.pi / 2)
|
|
151
|
+
return pred_t
|
|
152
|
+
|
|
153
|
+
class MaskCls(BaseNetwork):
|
|
154
|
+
|
|
155
|
+
def __init__(self, num_classes=256):
|
|
156
|
+
super(MaskCls, self).__init__()
|
|
157
|
+
# 加载预训练的ResNet模型,这里选择ResNet50作为基础模型
|
|
158
|
+
self.resnet = models.resnet50(pretrained=True)
|
|
159
|
+
self.resnet.conv1 = nn.Conv2d(4, 64, kernel_size=7, stride=2, padding=3, bias=False)
|
|
160
|
+
# 替换最后一层全连接层
|
|
161
|
+
num_features = self.resnet.fc.in_features
|
|
162
|
+
self.resnet.fc = nn.Linear(num_features, num_classes)
|
|
163
|
+
|
|
164
|
+
def forward(self, x):
|
|
165
|
+
x = self.resnet(x)
|
|
166
|
+
return x
|