basicsr.utils.diffjpeg

Modified from https://github.com/mlomnitz/DiffJPEG

For images not divisible by 8 https://dsp.stackexchange.com/questions/35339/jpeg-dct-padding/35343#35343

class basicsr.utils.diffjpeg.BlockMerging[source]

Bases: Module

Merge patches into image

forward(patches, height, width)[source]
Parameters:
  • patches (tensor) –

  • height (int) –

  • width (int) –

Returns:

batch x height x width

Return type:

Tensor

training: bool
class basicsr.utils.diffjpeg.BlockSplitting[source]

Bases: Module

Splitting image into patches

forward(image)[source]
Parameters:

image (tensor) – batch x height x width

Returns:

batch x h*w/64 x h x w

Return type:

Tensor

training: bool
class basicsr.utils.diffjpeg.CDequantize[source]

Bases: Module

Dequantize CbCr channel

forward(image, factor=1)[source]
Parameters:

image (tensor) – batch x height x width

Returns:

batch x height x width

Return type:

Tensor

training: bool
class basicsr.utils.diffjpeg.CQuantize(rounding)[source]

Bases: Module

JPEG Quantization for CbCr channels

Parameters:

rounding (function) – rounding function to use

forward(image, factor=1)[source]
Parameters:

image (tensor) – batch x height x width

Returns:

batch x height x width

Return type:

Tensor

training: bool
class basicsr.utils.diffjpeg.ChromaSubsampling[source]

Bases: Module

Chroma subsampling on CbCr channels

forward(image)[source]
Parameters:

image (tensor) – batch x height x width x 3

Returns:

batch x height x width cb(tensor): batch x height/2 x width/2 cr(tensor): batch x height/2 x width/2

Return type:

y(tensor)

training: bool
class basicsr.utils.diffjpeg.ChromaUpsampling[source]

Bases: Module

Upsample chroma layers

forward(y, cb, cr)[source]
Parameters:
  • y (tensor) – y channel image

  • cb (tensor) – cb channel

  • cr (tensor) – cr channel

Returns:

batch x height x width x 3

Return type:

Tensor

training: bool
class basicsr.utils.diffjpeg.CompressJpeg(rounding=<built-in method round of type object>)[source]

Bases: Module

Full JPEG compression algorithm

Parameters:

rounding (function) – rounding function to use

forward(image, factor=1)[source]
Parameters:

image (tensor) – batch x 3 x height x width

Returns:

Compressed tensor with batch x h*w/64 x 8 x 8.

Return type:

dict(tensor)

training: bool
class basicsr.utils.diffjpeg.DCT8x8[source]

Bases: Module

Discrete Cosine Transformation

forward(image)[source]
Parameters:

image (tensor) – batch x height x width

Returns:

batch x height x width

Return type:

Tensor

training: bool
class basicsr.utils.diffjpeg.DeCompressJpeg(rounding=<built-in method round of type object>)[source]

Bases: Module

Full JPEG decompression algorithm

Parameters:

rounding (function) – rounding function to use

forward(y, cb, cr, imgh, imgw, factor=1)[source]
Parameters:
  • compressed (dict(tensor)) – batch x h*w/64 x 8 x 8

  • imgh (int) –

  • imgw (int) –

  • factor (float) –

Returns:

batch x 3 x height x width

Return type:

Tensor

training: bool
class basicsr.utils.diffjpeg.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.diffjpeg.RGB2YCbCrJpeg[source]

Bases: Module

Converts RGB image to YCbCr

forward(image)[source]
Parameters:

image (Tensor) – batch x 3 x height x width

Returns:

batch x height x width x 3

Return type:

Tensor

training: bool
class basicsr.utils.diffjpeg.YCbCr2RGBJpeg[source]

Bases: Module

Converts YCbCr image to RGB JPEG

forward(image)[source]
Parameters:

image (tensor) – batch x height x width x 3

Returns:

batch x 3 x height x width

Return type:

Tensor

training: bool
class basicsr.utils.diffjpeg.YDequantize[source]

Bases: Module

Dequantize Y channel

forward(image, factor=1)[source]
Parameters:

image (tensor) – batch x height x width

Returns:

batch x height x width

Return type:

Tensor

training: bool
class basicsr.utils.diffjpeg.YQuantize(rounding)[source]

Bases: Module

JPEG Quantization for Y channel

Parameters:

rounding (function) – rounding function to use

forward(image, factor=1)[source]
Parameters:

image (tensor) – batch x height x width

Returns:

batch x height x width

Return type:

Tensor

training: bool
basicsr.utils.diffjpeg.diff_round(x)[source]

Differentiable rounding function

class basicsr.utils.diffjpeg.iDCT8x8[source]

Bases: Module

Inverse discrete Cosine Transformation

forward(image)[source]
Parameters:

image (tensor) – batch x height x width

Returns:

batch x height x width

Return type:

Tensor

training: bool
basicsr.utils.diffjpeg.quality_to_factor(quality)[source]

Calculate factor corresponding to quality

Parameters:

quality (float) – Quality for jpeg compression.

Returns:

Compression factor.

Return type:

float