How to interpret arguments to torchvision.transform.ColorJitter?

Can someone provide more clarity about the meaning of the ColorJitter arguments? I understand that we can separately control (or disable) brightness, contrast, saturation and hue modifications, but how do the actual values entered relate to results? The documentation says “brightness_factor is chosen uniformly from [max(0, 1 - brightness), 1 + brightness]” but how is the brightness_factor applied to modify the image values?

Suppose I wanted to vary brightness within a range of +/- 25% of the existing values. What would be appropriate argument values for brightness?

Thank you in advance!

For your use case brightness = 0.25 should work. This will sample the random brightness factor in the range [0.75, 1.25].

So is the brightness factor simply applied multiplicatively to the image data value?

What about contrast? I understand the sampling issue; what I am trying to figure out is how the value that is randomly selected is applied to modify the relevant image property. I’d expect this to be different for the four different properties. In particular, it seems that changing contrast would require a histogram remapping, not just a fixed modification of each value.

(BTW I really appreciate your quick responses!)

Ah OK, then I have misunderstood the question. :wink:

I can’t find the underlying implementation in PIL, so take this information with a grain of salt:

  • brightness: is an additive variable, which is directly added (or subtracted) to the image
  • contrast: slope, which is a multiplicative factor for each pixel value
  • hue: not sure, if there are other methods, but you could transform the RGB values to HSV and change the hue on the color wheel.
  • saturation: HSV conversion would work, otherwise should be:
color_max = max(R, G, B)
color_min = min(R, G, B)

if R == G == B:
    S = 0
else:
    S = 255 * (color_max - color_min) / (color_max + color_min)

Thank you! Your previous reply was also useful!

Hello,
I want to do contrast and brightness adjustment with the pytorch color jitter. I want the implementation the way you explained it (brightness should be an additive factor and contrast a multiplicative factor.

However I don’t think it is like that in the pytoch color jitter, I did some test :

t_color = torchvision.transforms.ColorJitter(brightness = (0,0))
img = t_color(img)

I did this on the image :
image_2021-11-05_132658

and the result was a black image.

There should be no change if it was an additive factor.

I Don’t know if there is an augment that implement the brightness with an additive factor but if someone know how to do it I would be happy.

PS : sorry for my approximate english, I’m not very good at it.

You are setting the brightness to zero, so a black image would be expected.
ColorJitter with a brightness argument is calling into adjust_brightness, which is blending two images, the input image and a zero image (black). The brightness factor would be zero, which is used as ratio in _blend.
As you can see, you have an additive transformation as:

return (ratio * img1 + (1.0 - ratio) * img2).clamp(0, bound).to(img1.dtype)

where ratio=0. and you are thus getting a black output image.