pyiqa.utils

Submodules

Package Contents

pyiqa.utils.crop_border(imgs, crop_border)[source]

Crop borders of images.

Parameters:
  • imgs (list[ndarray] | ndarray) – Images with shape (h, w, c).

  • crop_border (int) – Crop border for each end of height and weight.

Returns:

Cropped images.

Return type:

list[ndarray]

pyiqa.utils.imfrombytes(content, flag='color', float32=False)[source]

Read an image from bytes.

Parameters:
  • content (bytes) – Image bytes got from files or other streams.

  • flag (str) – Flags specifying the color type of a loaded image, candidates are color, grayscale and unchanged.

  • float32 (bool) – Whether to change to float32., If True, will also norm to [0, 1]. Default: False.

Returns:

Loaded image array.

Return type:

ndarray

pyiqa.utils.img2tensor(imgs, bgr2rgb=True, float32=True)[source]

Numpy array to tensor.

Parameters:
  • imgs (list[ndarray] | ndarray) – Input images.

  • bgr2rgb (bool) – Whether to change bgr to rgb.

  • float32 (bool) – Whether to change to float32.

Returns:

Tensor images. If returned results only have

one element, just return tensor.

Return type:

list[tensor] | tensor

pyiqa.utils.imwrite(img, file_path, params=None, auto_mkdir=True)[source]

Write image to file.

Parameters:
  • img (ndarray) – Image array to be written.

  • file_path (str) – Image file path.

  • params (None or list) – Same as opencv’s imwrite() interface.

  • auto_mkdir (bool) – If the parent folder of file_path does not exist, whether to create it automatically.

Returns:

Successful or not.

Return type:

bool

pyiqa.utils.tensor2img(tensor, rgb2bgr=True, out_type=np.uint8, min_max=(0, 1))[source]

Convert torch Tensors into image numpy arrays.

After clamping to [min, max], values will be normalized to [0, 1].

Parameters:
  • tensor (Tensor or list[Tensor]) – Accept shapes: 1) 4D mini-batch Tensor of shape (B x 3/1 x H x W); 2) 3D Tensor of shape (3/1 x H x W); 3) 2D Tensor of shape (H x W). Tensor channel should be in RGB order.

  • rgb2bgr (bool) – Whether to change rgb to bgr.

  • out_type (numpy type) – output types. If np.uint8, transform outputs to uint8 type with range [0, 255]; otherwise, float type with range [0, 1]. Default: np.uint8.

  • min_max (tuple[int]) – min and max values for clamp.

Returns:

3D ndarray of shape (H x W x C) OR 2D ndarray of shape (H x W). The channel order is BGR.

Return type:

(Tensor or list)

pyiqa.utils.imread2tensor(img_source, rgb=False)[source]

Read image to tensor.

Parameters:
  • img_source (str, bytes, or PIL.Image) – image filepath string, image contents as a bytearray or a PIL Image instance

  • rgb – convert input to RGB if true

pyiqa.utils.scandir_images(dir, max_dataset_size=float('inf'), followlinks=True)[source]

Get all image files from a directory and return a sorted list of fullpath.

class pyiqa.utils.AvgTimer(window=200)[source]
start()[source]
record()[source]
get_current_time()[source]
get_avg_time()[source]
class pyiqa.utils.MessageLogger(opt, start_iter=1, tb_logger=None)[source]

Message logger for printing.

Parameters:
  • opt (dict) – Config. It contains the following keys: name (str): Exp name. logger (dict): Contains ‘print_freq’ (str) for logger interval. train (dict): Contains ‘total_iter’ (int) for total iters. use_tb_logger (bool): Use tensorboard logger.

  • start_iter (int) – Start iter. Default: 1.

  • (obj (tb_logger) – tb_logger): Tensorboard logger. Default: None.

reset_start_time()[source]
pyiqa.utils.get_env_info()[source]

Get environment information.

Currently, only log the software version.

pyiqa.utils.get_root_logger(logger_name='pyiqa', log_level=logging.INFO, log_file=None)[source]

Get the root logger.

The logger will be initialized if it has not been initialized. By default a StreamHandler will be added. If log_file is specified, a FileHandler will also be added.

Parameters:
  • logger_name (str) – root logger name. Default: ‘basicsr’.

  • log_file (str | None) – The log filename. If specified, a FileHandler will be added to the root logger.

  • log_level (int) – The root logger level. Note that only the process of rank 0 is affected, while other processes will set the level to “Error” and be silent most of the time.

Returns:

The root logger.

Return type:

logging.Logger

pyiqa.utils.init_tb_logger(log_dir)[source]
pyiqa.utils.init_wandb_logger(opt)[source]

We now only use wandb to sync tensorboard log.

pyiqa.utils.check_resume(opt, resume_iter)[source]

Check resume states and pretrain_network paths.

Parameters:
  • opt (dict) – Options.

  • resume_iter (int) – Resume iteration.

pyiqa.utils.get_time_str()[source]
pyiqa.utils.make_exp_dirs(opt)[source]

Make dirs for experiments.

pyiqa.utils.mkdir_and_rename(path)[source]

mkdirs. If path exists, rename it with timestamp, create a new one, and move it to archive folder.

Parameters:

path (str) – Folder path.

pyiqa.utils.scandir(dir_path, suffix=None, recursive=False, full_path=False)[source]

Scan a directory to find the interested files.

Parameters:
  • dir_path (str) – Path of the directory.

  • suffix (str | tuple(str), optional) – File suffix that we are interested in. Default: None.

  • recursive (bool, optional) – If set to True, recursively scan the directory. Default: False.

  • full_path (bool, optional) – If set to True, include the dir_path. Default: False.

Returns:

A generator for all the interested files with relative paths.

pyiqa.utils.set_random_seed(seed=123)[source]

Set random seeds.

pyiqa.utils.sizeof_fmt(size, suffix='B')[source]

Get human readable file size.

Parameters:
  • size (int) – File size.

  • suffix (str) – Suffix. Default: ‘B’.

Returns:

Formatted file size.

Return type:

str

pyiqa.utils.download_file_from_google_drive(file_id, save_path)[source]

Download files from google drive.

Ref: https://stackoverflow.com/questions/25010369/wget-curl-large-file-from-google-drive # noqa E501

Parameters:
  • file_id (str) – File id.

  • save_path (str) – Save path.

pyiqa.utils.load_file_from_url(url, model_dir=None, progress=True, file_name=None)[source]

Load file form http url, will download models if necessary.

Ref:https://github.com/1adrianb/face-alignment/blob/master/face_alignment/utils.py

Parameters:
  • url (str) – URL to be downloaded.

  • model_dir (str) – The path to save the downloaded model. Should be a full path. If None, use pytorch hub_dir. Default: None.

  • progress (bool) – Whether to show the download progress. Default: True.

  • file_name (str) – The downloaded file name. If None, use the file name in the url. Default: None.

Returns:

The path to the downloaded file.

Return type:

str

pyiqa.utils.rgb2ycbcr(x: torch.Tensor) torch.Tensor[source]

Convert a batch of RGB images to a batch of YCbCr images

It implements the ITU-R BT.601 conversion for standard-definition television. See more details in https://en.wikipedia.org/wiki/YCbCr#ITU-R_BT.601_conversion.

Parameters:

x – Batch of images with shape (N, 3, H, W). RGB color space, range [0, 1].

Returns:

Batch of images with shape (N, 3, H, W). YCbCr color space.

pyiqa.utils.ycbcr2rgb(x: torch.Tensor) torch.Tensor[source]

Convert a batch of YCbCr images to a batch of RGB images

It implements the inversion of the above rgb2ycbcr function.

Parameters:

x – Batch of images with shape (N, 3, H, W). YCbCr color space, range [0, 1].

Returns:

Batch of images with shape (N, 3, H, W). RGB color space.