pyiqa.utils =========== .. py:module:: pyiqa.utils Submodules ---------- .. toctree:: :maxdepth: 1 /autoapi/pyiqa/utils/color_util/index /autoapi/pyiqa/utils/dist_util/index /autoapi/pyiqa/utils/download_util/index /autoapi/pyiqa/utils/img_util/index /autoapi/pyiqa/utils/logger/index /autoapi/pyiqa/utils/misc/index /autoapi/pyiqa/utils/options/index /autoapi/pyiqa/utils/registry/index Package Contents ---------------- .. py:function:: crop_border(imgs, crop_border) Crop borders of images. :param imgs: Images with shape (h, w, c). :type imgs: list[ndarray] | ndarray :param crop_border: Crop border for each end of height and weight. :type crop_border: int :returns: Cropped images. :rtype: list[ndarray] .. py:function:: imfrombytes(content, flag='color', float32=False) Read an image from bytes. :param content: Image bytes got from files or other streams. :type content: bytes :param flag: Flags specifying the color type of a loaded image, candidates are `color`, `grayscale` and `unchanged`. :type flag: str :param float32: Whether to change to float32., If True, will also norm to [0, 1]. Default: False. :type float32: bool :returns: Loaded image array. :rtype: ndarray .. py:function:: img2tensor(imgs, bgr2rgb=True, float32=True) Numpy array to tensor. :param imgs: Input images. :type imgs: list[ndarray] | ndarray :param bgr2rgb: Whether to change bgr to rgb. :type bgr2rgb: bool :param float32: Whether to change to float32. :type float32: bool :returns: Tensor images. If returned results only have one element, just return tensor. :rtype: list[tensor] | tensor .. py:function:: imwrite(img, file_path, params=None, auto_mkdir=True) Write image to file. :param img: Image array to be written. :type img: ndarray :param file_path: Image file path. :type file_path: str :param params: Same as opencv's :func:`imwrite` interface. :type params: None or list :param auto_mkdir: If the parent folder of `file_path` does not exist, whether to create it automatically. :type auto_mkdir: bool :returns: Successful or not. :rtype: bool .. py:function:: tensor2img(tensor, rgb2bgr=True, out_type=np.uint8, min_max=(0, 1)) Convert torch Tensors into image numpy arrays. After clamping to [min, max], values will be normalized to [0, 1]. :param 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. :type tensor: Tensor or list[Tensor] :param rgb2bgr: Whether to change rgb to bgr. :type rgb2bgr: bool :param out_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``. :type out_type: numpy type :param min_max: min and max values for clamp. :type min_max: tuple[int] :returns: 3D ndarray of shape (H x W x C) OR 2D ndarray of shape (H x W). The channel order is BGR. :rtype: (Tensor or list) .. py:function:: imread2tensor(img_source, rgb=False) Read image to tensor. :param img_source: image filepath string, image contents as a bytearray or a PIL Image instance :type img_source: str, bytes, or PIL.Image :param rgb: convert input to RGB if true .. py:function:: scandir_images(dir, max_dataset_size=float('inf'), followlinks=True) Get all image files from a directory and return a sorted list of fullpath. .. py:class:: AvgTimer(window=200) .. py:method:: start() .. py:method:: record() .. py:method:: get_current_time() .. py:method:: get_avg_time() .. py:class:: MessageLogger(opt, start_iter=1, tb_logger=None) Message logger for printing. :param opt: 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. :type opt: dict :param start_iter: Start iter. Default: 1. :type start_iter: int :param tb_logger (obj: `tb_logger`): Tensorboard logger. Default: None. .. py:method:: reset_start_time() .. py:function:: get_env_info() Get environment information. Currently, only log the software version. .. py:function:: get_root_logger(logger_name='pyiqa', log_level=logging.INFO, log_file=None) 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. :param logger_name: root logger name. Default: 'basicsr'. :type logger_name: str :param log_file: The log filename. If specified, a FileHandler will be added to the root logger. :type log_file: str | None :param log_level: 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. :type log_level: int :returns: The root logger. :rtype: logging.Logger .. py:function:: init_tb_logger(log_dir) .. py:function:: init_wandb_logger(opt) We now only use wandb to sync tensorboard log. .. py:function:: check_resume(opt, resume_iter) Check resume states and pretrain_network paths. :param opt: Options. :type opt: dict :param resume_iter: Resume iteration. :type resume_iter: int .. py:function:: get_time_str() .. py:function:: make_exp_dirs(opt) Make dirs for experiments. .. py:function:: mkdir_and_rename(path) mkdirs. If path exists, rename it with timestamp, create a new one, and move it to archive folder. :param path: Folder path. :type path: str .. py:function:: scandir(dir_path, suffix=None, recursive=False, full_path=False) Scan a directory to find the interested files. :param dir_path: Path of the directory. :type dir_path: str :param suffix: File suffix that we are interested in. Default: None. :type suffix: str | tuple(str), optional :param recursive: If set to True, recursively scan the directory. Default: False. :type recursive: bool, optional :param full_path: If set to True, include the dir_path. Default: False. :type full_path: bool, optional :returns: A generator for all the interested files with relative paths. .. py:function:: set_random_seed(seed=123) Set random seeds. .. py:function:: sizeof_fmt(size, suffix='B') Get human readable file size. :param size: File size. :type size: int :param suffix: Suffix. Default: 'B'. :type suffix: str :returns: Formatted file size. :rtype: str .. py:function:: download_file_from_google_drive(file_id, save_path) Download files from google drive. Ref: https://stackoverflow.com/questions/25010369/wget-curl-large-file-from-google-drive # noqa E501 :param file_id: File id. :type file_id: str :param save_path: Save path. :type save_path: str .. py:function:: load_file_from_url(url, model_dir=None, progress=True, file_name=None) Load file form http url, will download models if necessary. Ref:https://github.com/1adrianb/face-alignment/blob/master/face_alignment/utils.py :param url: URL to be downloaded. :type url: str :param model_dir: The path to save the downloaded model. Should be a full path. If None, use pytorch hub_dir. Default: None. :type model_dir: str :param progress: Whether to show the download progress. Default: True. :type progress: bool :param file_name: The downloaded file name. If None, use the file name in the url. Default: None. :type file_name: str :returns: The path to the downloaded file. :rtype: str .. py:function:: rgb2ycbcr(x: torch.Tensor) -> torch.Tensor 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. :param 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. .. py:function:: ycbcr2rgb(x: torch.Tensor) -> torch.Tensor Convert a batch of YCbCr images to a batch of RGB images It implements the inversion of the above rgb2ycbcr function. :param 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.