basicsr.utils.__init__
- class basicsr.utils.__init__.DiffJPEG(differentiable=True)[source]
Bases:
Module
This JPEG algorithm result is slightly different from cv2. DiffJPEG supports batch processing.
- Parameters
differentiable (bool) – If True, uses custom differentiable rounding function, if False, uses standard torch.round
- forward(x, quality)[source]
- Parameters
x (Tensor) – Input image, bchw, rgb, [0, 1]
quality (float) – Quality factor for jpeg compression scheme.
- training: bool
- class basicsr.utils.__init__.FileClient(backend='disk', **kwargs)[source]
Bases:
object
A general file client to access files in different backend.
The client loads a file or text in a specified backend from its path and return it as a binary file. it can also register other backend accessor with a given name and backend class.
- backend
The storage backend type. Options are “disk”, “memcached” and “lmdb”.
- Type
str
- client
The backend object.
- Type
BaseStorageBackend
- class basicsr.utils.__init__.MessageLogger(opt, start_iter=1, tb_logger=None)[source]
Bases:
object
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.
- class basicsr.utils.__init__.USMSharp(radius=50, sigma=0)[source]
Bases:
Module
- forward(img, weight=0.5, threshold=10)[source]
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- training: bool
- basicsr.utils.__init__.bgr2ycbcr(img, y_only=False)[source]
Convert a BGR image to YCbCr image.
The bgr version of rgb2ycbcr. 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.
It differs from a similar function in cv2.cvtColor: BGR <-> YCrCb. In OpenCV, it implements a JPEG conversion. See more details in https://en.wikipedia.org/wiki/YCbCr#JPEG_conversion.
- Parameters
img (ndarray) – The input image. It accepts: 1. np.uint8 type with range [0, 255]; 2. np.float32 type with range [0, 1].
y_only (bool) – Whether to only return Y channel. Default: False.
- Returns
- The converted YCbCr image. The output image has the same type
and range as input image.
- Return type
ndarray
- basicsr.utils.__init__.check_resume(opt, resume_iter)[source]
Check resume states and pretrain_network paths.
- Parameters
opt (dict) – Options.
resume_iter (int) – Resume iteration.
- basicsr.utils.__init__.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]
- basicsr.utils.__init__.get_env_info()[source]
Get environment information.
Currently, only log the software version.
- basicsr.utils.__init__.get_root_logger(logger_name='basicsr', log_level=20, 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
- basicsr.utils.__init__.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
- basicsr.utils.__init__.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
- basicsr.utils.__init__.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
- basicsr.utils.__init__.init_wandb_logger(opt)[source]
We now only use wandb to sync tensorboard log.
- basicsr.utils.__init__.mkdir_and_rename(path)[source]
mkdirs. If path exists, rename it with timestamp and create a new one.
- Parameters
path (str) – Folder path.
- basicsr.utils.__init__.rgb2ycbcr(img, y_only=False)[source]
Convert a RGB image to YCbCr image.
This function produces the same results as Matlab’s rgb2ycbcr function. 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.
It differs from a similar function in cv2.cvtColor: RGB <-> YCrCb. In OpenCV, it implements a JPEG conversion. See more details in https://en.wikipedia.org/wiki/YCbCr#JPEG_conversion.
- Parameters
img (ndarray) – The input image. It accepts: 1. np.uint8 type with range [0, 255]; 2. np.float32 type with range [0, 1].
y_only (bool) – Whether to only return Y channel. Default: False.
- Returns
- The converted YCbCr image. The output image has the same type
and range as input image.
- Return type
ndarray
- basicsr.utils.__init__.rgb2ycbcr_pt(img, y_only=False)[source]
Convert RGB images to YCbCr images (PyTorch version).
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
img (Tensor) – Images with shape (n, 3, h, w), the range [0, 1], float, RGB format. y_only (bool): Whether to only return Y channel. Default: False.
- Returns
converted images with the shape (n, 3/1, h, w), the range [0, 1], float.
- Return type
(Tensor)
- basicsr.utils.__init__.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.
- basicsr.utils.__init__.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
- basicsr.utils.__init__.tensor2img(tensor, rgb2bgr=True, out_type=<class 'numpy.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)
- basicsr.utils.__init__.usm_sharp(img, weight=0.5, radius=50, threshold=10)[source]
USM sharpening.
Input image: I; Blurry image: B. 1. sharp = I + weight * (I - B) 2. Mask = 1 if abs(I - B) > threshold, else: 0 3. Blur mask: 4. Out = Mask * sharp + (1 - Mask) * I
- Parameters
img (Numpy array) – Input image, HWC, BGR; float32, [0, 1].
weight (float) – Sharp weight. Default: 1.
radius (float) – Kernel size of Gaussian blur. Default: 50.
threshold (int) –
- basicsr.utils.__init__.ycbcr2bgr(img)[source]
Convert a YCbCr image to BGR image.
The bgr version of ycbcr2rgb. 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.
It differs from a similar function in cv2.cvtColor: YCrCb <-> BGR. In OpenCV, it implements a JPEG conversion. See more details in https://en.wikipedia.org/wiki/YCbCr#JPEG_conversion.
- Parameters
img (ndarray) – The input image. It accepts: 1. np.uint8 type with range [0, 255]; 2. np.float32 type with range [0, 1].
- Returns
- The converted BGR image. The output image has the same type
and range as input image.
- Return type
ndarray
- basicsr.utils.__init__.ycbcr2rgb(img)[source]
Convert a YCbCr image to RGB image.
This function produces the same results as Matlab’s ycbcr2rgb function. 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.
It differs from a similar function in cv2.cvtColor: YCrCb <-> RGB. In OpenCV, it implements a JPEG conversion. See more details in https://en.wikipedia.org/wiki/YCbCr#JPEG_conversion.
- Parameters
img (ndarray) – The input image. It accepts: 1. np.uint8 type with range [0, 255]; 2. np.float32 type with range [0, 1].
- Returns
- The converted RGB image. The output image has the same type
and range as input image.
- Return type
ndarray