pyiqa.archs.arch_util

Module Contents

pyiqa.archs.arch_util.dist_to_mos(dist_score: torch.Tensor) torch.Tensor[source]

Convert distribution prediction to MOS score. For datasets with detailed score labels, such as AVA.

Parameters:

dist_score (torch.Tensor) – (*, C), C is the class number.

Returns:

(*, 1) MOS score.

Return type:

torch.Tensor

pyiqa.archs.arch_util.random_crop(input_list, crop_size, crop_num)[source]

Randomly crops the input tensor(s) to the specified size and number of crops.

Parameters:
  • input_list (list or torch.Tensor) – List of input tensors or a single input tensor.

  • crop_size (int or tuple) – Size of the crop. If an int is provided, a square crop of that size is used. If a tuple is provided, a crop of that size is used.

  • crop_num (int) – Number of crops to generate.

Returns:

If a single input tensor is provided, a tensor of cropped images is returned.

If a list of input tensors is provided, a list of tensors of cropped images is returned.

Return type:

torch.Tensor or list of torch.Tensor

pyiqa.archs.arch_util.uniform_crop(input_list, crop_size, crop_num)[source]

Crop the input_list of tensors into multiple crops with uniform steps according to input size and crop_num.

Parameters:
  • input_list (list or torch.Tensor) – List of input tensors or a single input tensor.

  • crop_size (int or tuple) – Size of the crops. If int, the same size will be used for height and width. If tuple, should be (height, width).

  • crop_num (int) – Number of crops to generate.

Returns:

Cropped tensors. If input_list is a list, the output will be a list

of cropped tensors. If input_list is a single tensor, the output will be a single tensor.

Return type:

torch.Tensor or list of torch.Tensor

pyiqa.archs.arch_util.clip_preprocess_tensor(x: torch.Tensor, model)[source]

Clip preprocess function with tensor input.

NOTE: Results are slightly different with original preprocess function with PIL image input, because of differences in resize function.

Parameters:
  • x (torch.Tensor) – Input tensor.

  • model – Model with visual input resolution.

Returns:

Preprocessed tensor.

Return type:

torch.Tensor

pyiqa.archs.arch_util.clean_state_dict(state_dict)[source]

Clean checkpoint by removing .module prefix from state dict if it exists from parallel training.

Parameters:

state_dict (dict) – State dictionary from a model checkpoint.

Returns:

Cleaned state dictionary.

Return type:

dict

pyiqa.archs.arch_util.get_url_from_name(name: str, store_base: str = 'hugging_face', base_url: str = None) str[source]

Get the URL for a given file name from a specified storage base.

Parameters:
  • name (str) – The name of the file.

  • store_base (str, optional) – The storage base to use. Options are “hugging_face” or “github”. Default is “hugging_face”.

  • base_url (str, optional) – Base URL to use if provided.

Returns:

The URL of the file.

Return type:

str

pyiqa.archs.arch_util.load_pretrained_network(net: torch.nn.Module, model_path: str, strict: bool = True, weight_keys: str = None) None[source]

Load a pretrained network from a given model path.

Parameters:
  • net (torch.nn.Module) – The network to load the weights into.

  • model_path (str) – Path to the model weights file. Can be a URL or a local file path.

  • strict (bool, optional) – Whether to strictly enforce that the keys in state_dict match the keys returned by net’s state_dict(). Default is True.

  • weight_keys (str, optional) – Specific key to extract from the state_dict. Default is None.

Returns:

None

pyiqa.archs.arch_util.to_1tuple[source]
pyiqa.archs.arch_util.to_2tuple[source]
pyiqa.archs.arch_util.to_3tuple[source]
pyiqa.archs.arch_util.to_4tuple[source]
pyiqa.archs.arch_util.to_ntuple[source]
pyiqa.archs.arch_util.default_init_weights(module_list, scale=1, bias_fill=0, **kwargs)[source]

Initializes the weights of the given module(s) using Kaiming Normal initialization.

Parameters:
  • module_list (list or nn.Module) – List of modules or a single module to initialize.

  • scale (float, optional) – Scaling factor for the weights. Default is 1.

  • bias_fill (float, optional) – Value to fill the biases with. Default is 0.

  • **kwargs – Additional arguments for the Kaiming Normal initialization.

Returns:

None

Example

>>> import torch.nn as nn
>>> from arch_util import default_init_weights
>>> model = nn.Sequential(
>>>     nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1),
>>>     nn.ReLU(),
>>>     nn.Conv2d(64, 64, kernel_size=3, stride=1, padding=1),
>>>     nn.ReLU(),
>>>     nn.Linear(64 * 32 * 32, 10)
>>> )
>>> default_init_weights(model, scale=0.1, bias_fill=0.01)