Surn Apps
All articles

LIGHT: the ColorJitter line, and the 2-photo test behind it

Same apple, 2 lights: the pixel numbers moved a lot, and an off-the-shelf vision model still said red apple both times. Here is the test (the photo, the re-light, the readings), why your own model can still learn the lamp, and the 1 torchvision line I'd add to your training.

Same apple, 2 lights. The pixel numbers moved a lot. The vision model's answer didn't: CLIP said "a red apple" at 94.5% by daylight and 94.8% under a 2700 K lamp.

So why does your own model still need 1 extra PyTorch line? This page has the line, the test I ran (the photo, the re-light and the readings) and the classifier script, so you can run it on your own photos.

One note first: the lamp photo is re-lit in code from the daylight photo, and the green and blue lights further down are simulated. I couldn't find a freely licensed photo of the same apple under 2 real lamps, so I made the second light from the physics. How is below.

The 1 line

Add ColorJitter to your torchvision training transforms:

from torchvision import transforms

train_tf = transforms.Compose([
    transforms.ColorJitter(
        brightness=0.4,
        contrast=0.4,
        saturation=0.4,
        hue=0.1),
    transforms.ToTensor(),
])

Every training image gets a random shift in brightness, contrast, saturation and hue, and the label stays the same. Your model trains under many lights instead of one. Put it in the training transforms only, not in the ones you use to test.

The parameters are in the ColorJitter docs. Hue is the strong one: in my test below, a hue shift moved the model far more than the lamp did, so start small.

The test

  • The photo: "Red Apple" by Abhijit Tembhekar on Wikimedia Commons, CC BY 2.0. White paper behind the apple, lit by a bounced flash, which is daylight-balanced (about 5500 K).
  • The re-light: a copy re-lit in code to a 2700 K tungsten lamp, with the camera's white balance locked (script below).
  • The readings: the same spot on the skin in both photos, averaged over a 41 x 41 pixel patch.
Apple skin (R, G, B)White paper (R, G, B)
Daylight234, 128, 67244, 245, 247
2700 K lamp (re-lit)234, 91, 21244, 178, 98

Green and blue dropped hard. Red stayed.

Then I asked CLIP (openai/clip-vit-base-patch32, zero-shot, no training) what each photo shows:

What is it?What colour?
Daylighta red apple, 94.5%an orange object, 55.8%
2700 K lamp (re-lit)a red apple, 94.8%an orange object, 65.1%

Same answer both times.

Why the numbers move

Your camera never stores "red".

  • The apple sends back mostly red light and absorbs the rest.
  • Each pixel on the sensor records 1 colour, through a mosaic called a Bayer filter (half green, a quarter red, a quarter blue).
  • Software fills in the other 2 colours from the neighbouring pixels.

So what reaches the model is light turned into 3 numbers per pixel. Change the light, and the numbers change.

Push the light to an extreme and it gets obvious. Simulated from the photo's own pixels, with idealised narrow-band light and white balance locked, the deepest red skin reads:

  • daylight: 201, 25, 3
  • the 2700 K re-light: 201, 15, 1
  • pure green light: 0, 25, 0
  • pure blue light: 0, 0, 3

Under a light with no red in it, a red apple is near black.

Why you don't notice

The rest is for members.

A free account unlocks every article and the older ones in each category.