neural_de.transformations.diffusion packageο
Subpackagesο
- neural_de.transformations.diffusion.unet package
- Submodules
- neural_de.transformations.diffusion.unet.attention_block module
- neural_de.transformations.diffusion.unet.downsample module
- neural_de.transformations.diffusion.unet.nn module
- neural_de.transformations.diffusion.unet.qkv_attention module
- neural_de.transformations.diffusion.unet.qkv_attention_legacy module
- neural_de.transformations.diffusion.unet.res_block module
- neural_de.transformations.diffusion.unet.timestep_block module
- neural_de.transformations.diffusion.unet.timestep_embed_sequential module
- neural_de.transformations.diffusion.unet.unet_model module
- neural_de.transformations.diffusion.unet.upsample module
- neural_de.transformations.diffusion.unet.utils module
- Module contents
Submodulesο
neural_de.transformations.diffusion.diffpure_config moduleο
- class neural_de.transformations.diffusion.diffpure_config.DiffPureConfig(weights_path=PosixPath('/home/runner/.neuralde/diffpure/256x256_diffusion_uncond.pt'), img_shape=(3, 256, 256), attention_resolutions=<factory>, num_classes=None, dims=2, learn_sigma=True, num_channels=256, num_head_channels=64, num_res_blocks=2, resblock_updown=True, use_fp16=True, use_scale_shift_norm=True, num_heads=4, num_heads_upsample=-1, channel_mult=None, dropout=0.0, use_new_attention_order=False, t=150, t_delta=15, use_bm=False, use_checkpoint=False, conv_resample=True, sample_step=1, rand_t=False)[source]ο
Bases:
object
A dataclass to configure and provide parameters for the internal diffusion model of diffusion_enhancer.
Most of the parameters are available to allow a custom usage of a different pre-trained diffusion models, based on the U-net architecture and code. The one which can be modified with the provided model are t, t_delta and sample_steps.
- weights_pathο
Path of the pre-trained weights, to provide custom weights files.
- img_shapeο
the shape of each input image of the diffusion model (by default (3, 256, 256)). Dimension are hannel-first.
- attention_resolutionsο
resolution, in pixels, of the attention-layers of the model
- num_classesο
int. (by default None). Number of classes the diffusion model is trained of.
- dimsο
int. images 1D, 2D or 3D (by default = 2)
- learn_sigmaο
bool (by default = True). If true, the output channel number will be 6 instead of 3.
- num_channelsο
int (by default 256). Base channel number for the layers of the diffusion model architecture.
- num_head_channelsο
int (by default 64). Number of channel per head of the attention blocks.
- num_res_blocksο
int (by default 2). Number of residual block of the architecture.
- resblock_updownο
bool (by default True). Whether to apply a downsampling after each residual block of the underlying Unet architecture.
- use_fp16ο
bool (by default True). Use 16bit floating -point precision. If cuda is not available, will be set as false (fp32).
- use_scale_shift_normο
bool (by default True). Normalisation of the output of each block of layers in the Unet architecture.
- num_headsο
int (by default 4). Number of attention heads.
- num_heads_upsampleο
int (by default -1). Num head for upsampling attention layers.
- channel_multο
tuple (by default None). Will be computed if not provided. Depending on the resolution, multiply the base channel number to get the final one for each residual layer of the Unet model.
- dropoutο
float (by default 0.0). Dropout rate.
- use_new_attention_orderο
bool (by default False). If true, the unet will use QKVAttention layers, if False, will use QKVAttentionLegacy.
- tο
int (by default 150). Number of diffusion steps applied for each image.
- t_deltaο
int (by default 15). Strength of the noise added before the diffusion process.
- use_bmο
float (by default False) #Erreur sur la valeur?
- use_checkpointο
bool (by default False). gradient checkpointing for training
- conv_resampleο
bool (by default True). Use learned convolutions for upsampling and downsampling. If false, interpolation (nearest) will be used.
- sample_stepο
int (by default 1). Number of time the diffusion process (noise addition + denoising) is repeated for each image.
- rand_tο
bool (by default False). If true, add random noise before denoising. The noise is sampled uniformly between -t_delta and +t_delta.
-
attention_resolutions:
list
[int
]ο
-
channel_mult:
tuple
= Noneο
-
conv_resample:
bool
= Trueο
-
dims:
int
= 2ο
-
dropout:
float
= 0.0ο
-
img_shape:
tuple
= (3, 256, 256)ο
-
learn_sigma:
bool
= Trueο
-
num_channels:
int
= 256ο
-
num_classes:
int
= Noneο
-
num_head_channels:
int
= 64ο
-
num_heads:
int
= 4ο
-
num_heads_upsample:
int
= -1ο
-
num_res_blocks:
int
= 2ο
-
rand_t:
bool
= Falseο
-
resblock_updown:
bool
= Trueο
-
sample_step:
int
= 1ο
-
t:
int
= 150ο
-
t_delta:
int
= 15ο
-
use_bm:
float
= Falseο
-
use_checkpoint:
bool
= Falseο
-
use_fp16:
bool
= Trueο
-
use_new_attention_order:
bool
= Falseο
-
use_scale_shift_norm:
bool
= Trueο
-
weights_path:
Path
= PosixPath('/home/runner/.neuralde/diffpure/256x256_diffusion_uncond.pt')ο
neural_de.transformations.diffusion.diffusion_enhancer moduleο
- class neural_de.transformations.diffusion.diffusion_enhancer.DiffusionEnhancer(device=None, config=DiffPureConfig(weights_path=PosixPath('/home/runner/.neuralde/diffpure/256x256_diffusion_uncond.pt'), img_shape=(3, 256, 256), attention_resolutions=[32, 16, 8], num_classes=None, dims=2, learn_sigma=True, num_channels=256, num_head_channels=64, num_res_blocks=2, resblock_updown=True, use_fp16=True, use_scale_shift_norm=True, num_heads=4, num_heads_upsample=-1, channel_mult=None, dropout=0.0, use_new_attention_order=False, t=150, t_delta=15, use_bm=False, use_checkpoint=False, conv_resample=True, sample_step=1, rand_t=False), logger=None)[source]ο
Bases:
BaseTransformation
The goal of this class is to purify a batch of images, to reduce noise and to increase robustness against potential adversarial attacks contained in the images. The weights given in this librairy are adapted for an output in 256*256 format. Of course, all sizes are supported in input but the enhancer will resize the images to 256*256.
- Parameters:
device (
Optional
[DeviceObjType
]) β some steps can be computed with cpu but a gpu is highly recommended.config (
Optional
[DiffPureConfig
]) β an instance of the DiffPureConfig class. The most important attributes are: t, sample_step and t_delta. Higher t or sample step will lead to a stronger denoising, at the cost of processing time. t_delta is the quantity of noise added by the method before itβs diffusion process : the higher, the higher the chances to remove adversarial attacks, at the cost of a potentiel loss of quality in the images. The other attributes of DiffPureConfig should be modified for a custom Diffusion model.
- forward(x)[source]ο
Apply the diffusion process to a tensor of images.
- Parameters:
x (
Tensor
) β Tensor of batch images- Returns:
Tensor of images after diffusion.
- transform(image_batch)[source]ο
βPurifyβ (removes noise and noise-based adverserial attacks) a batch of input images by applying a diffusion process to the images.
The images are resized to the diffusion model supported size (currently 256*256) : you may want to resize/enhance the resolution of the output images. If the input images do not have the same h and w, the resizing process will crop to a square image, thus losing some information.
- Parameters:
image_batch (
Union
[ndarray
,Tensor
]) β Batch of images to purify (numpy array or torch.Tensor).- Returns:
The batch of purified images (numpy array).
neural_de.transformations.diffusion.rev_guided_diffusion moduleο
- class neural_de.transformations.diffusion.rev_guided_diffusion.RevGuidedDiffusion(config, device=None, logger=None)[source]ο
Bases:
Module
Implements the rev-guided diffusion.
- Parameters:
device (
Optional
[DeviceObjType
]) β βcudaβ or βcpuβ. Gpu is highly recommended but somme steps are available with cpu.config (
DiffPureConfig
) β An instance of DiffPureConfig, it has been created in the input of the DiffusionEhnancer class.logger (
Optional
[Logger
]) β logger.
- image_editing_sample(img)[source]ο
This method apply the rev-guided diffusion to a batch of images.
- Parameters:
img (
Tensor
) β Tensor (batch of images)- Returns:
Tensor (batch of images)
-
training:
bool
ο
neural_de.transformations.diffusion.rev_vpsde moduleο
- class neural_de.transformations.diffusion.rev_vpsde.RevVPSDE(model, beta_min=0.1, beta_max=20, N=1000, img_shape=(3, 256, 256), logger=None)[source]ο
Bases:
Module
Constructs a Variance Preserving SDE.
- Parameters:
model (
Module
) β diffusion modelbeta_min (
float
) β min value of beta for normalisationbeta_max (
float
) β max value of beta for normalisationN (
int
) β scaling factorimg_shape (
tuple
) β Image dimension, channel-first.logger (
Optional
[Logger
]) β logger (logging.Logger)
- f(t, x)[source]ο
Creates the drift function -f(x, 1-t) (by tβ = 1 - t) Sdeint only support a 2D tensor (batch_size, c*h*w)
- Parameters:
t (
Tensor
) β current stepx (
Tensor
) β batch of input images
- Return type:
Tensor
- g(t, x)[source]ο
Create the diffusion function g(1-t) (by tβ = 1 - t) sdeint only support a 2D tensor (batch_size, c*h*w)
- Parameters:
t (
Tensor
) β current stepx (
Tensor
) β batch of input images
- Return type:
Tensor
- rvpsde_fn(t, x, return_type='drift')[source]ο
Create the drift and diffusion functions for the reverse SDE
- Parameters:
t (
Tensor
) β current stepx (
Tensor
) β batch of input imagesreturn_type (
str
) β if βdriftβ, will apply a drift following the diffusion. If not, only the diffusion will be performed.
-
training:
bool
ο