perceptual¶
- class mfai.pytorch.losses.perceptual.BaseNet[source]¶
Bases:
Module- forward(x)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
list[Tensor]Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mfai.pytorch.losses.perceptual.LPIPS(device='cuda', multi_scale=False, channel_iterative_mode=False, in_channels=3, pre_trained=False, resize_input=False, style_layer_ids=[], style_block_ids=[], feature_layer_ids=[4, 9, 16, 23, 30], feature_block_ids=[0, 1, 2, 3, 4], alpha_style=0, alpha_feature=1)[source]¶
Bases:
ModuleCreates a criterion that measures Learned Perceptual Image Patch Similarity (LPIPS). For more info see : Zhang et al. The Unreasonable Effectiveness of Deep Features as a Perceptual Metric (https://arxiv.org/pdf/1801.03924).
This code is inspired from : https://github.com/richzhang/PerceptualSimilarity/
- Arguments :
device: (str) - Device where to store the Neural Network (default = ‘cuda’) multi_scale: (bool) - Multi Scale mode to compute Perceptual Loss at different scales (default = False) channel_iterative_mode: (bool) - To compute the Perceptual Loss over channels of the input (default = False) in_channels: (int) - Number of input channels for perceptual Loss - [Used only if channel_iterative_mode=False] (default = 1) pre_trained: (bool) - To use a pre-trained or a random version of the VGG16 (default = False) resize_input: (bool) - To adapt input size to ImageNet Dataset size (224x224) (default = False) style_layer_ids: (list) - Ids of Style Layers used for Perceptual Loss (default = []) feature_layer_ids: (list) - Ids of Feature Layers used for Perceptual Loss (default = [4,9,16,23,30]) alpha_style: (float) - Weight of Style Loss (default = 0) alpha_feature (float) - Weight of Feature Loss (default = 1)
- Parameters:
- forward(x, y)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
TensorNote
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class mfai.pytorch.losses.perceptual.LinLayers(n_channels_list)[source]¶
Bases:
ModuleList
- class mfai.pytorch.losses.perceptual.PerceptualLoss(device='cuda', multi_scale=False, channel_iterative_mode=True, in_channels=3, pre_trained=False, resize_input=False, style_layer_ids=[], style_block_ids=[], feature_layer_ids=[4, 9, 16, 23, 30], feature_block_ids=[0, 1, 2, 3, 4], alpha_style=0, alpha_feature=1)[source]¶
Bases:
Module- Parameters:
- compute_perceptual_features(x, return_features_and_styles=False)[source]¶
Compute the features of a single image.
- Arguments :
x: (Tensor) return_features_and_styles: (bool)
# In case you need to compare different targets to the same input inputs = torch.rand(25, 5, 128, 128) perceptual_loss_class = PerceptualLoss(channel_iterative_mode=True)
# The features of the inputs are computed and stored in the memory perceptual_loss_class.compute_perceptual_features(inputs)
for _ in range():
targets = torch.rand(25, 5, 128, 128)
# The features of the targets are computed and compared to the input features perceptual_loss = perceptual_loss_class(targets)
- class mfai.pytorch.losses.perceptual.VGG16[source]¶
Bases:
BaseNet- forward(x)[source]¶
Define the computation performed at every call.
Should be overridden by all subclasses. :rtype:
list[Tensor]Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.