Dev.to AI 🤖 Ai 👁 0 📖 1 min read

Your Labellers Drew Boxes on Upright Photos and Your Training Script Loaded Them Sideways

The returns team wanted a model to find the damage in customer photos. Dents. Cracks. Torn packaging. Labellers drew boxes around each fault in a browser tool. Eight thousand photos. The model trained. On landscape

The returns team
wanted a model
to find the damage
in customer photos.

Dents. Cracks. Torn packaging.

Labellers drew boxes
around each fault
in a browser tool.

Eight thousand photos.

The model trained.

On landscape photos
it was good.

On portrait photos
it was close to useless.

It drew confident boxes
on blank stretches of wall.

Here is the mechanism.

A phone camera
often saves the pixels
the way the sensor sits,
sideways,
and adds a tag
in the EXIF data:

rotate this
when you show it.

Browsers obey that tag.

They have by default
since around 2020.

So the labellers
saw every photo upright
and drew their boxes
on the upright picture.

The training script
opened the files
with a library
that ignores the tag.

Pillow's Image.open
does not apply it.

torchvision's decode_image
leaves it off by default.

The model got
the raw sideways pixels,
with boxes
measured on a picture
it never saw.

For every portrait photo,
the box sat
on the wrong part of the image.

The model learned
from thousands of those.

Landscape photos
need no rotation,
so they were fine.

Which is why
the overall score
looked believable.

Nothing errored.

Coordinates are just numbers.

Here is the fix.

Apply the orientation
once, at ingestion,
before anything else
touches the image:

img = ImageOps.exif_transpose(Image.open(path))

Then save it upright
and strip the tag,
so no later tool
can rotate it twice.

Do the same
in the serving path.

If training rotates
and production does not,
you have moved the bug,
not fixed it.

And look at the data
the way the model sees it.

Draw ten boxes
onto ten tensors,
straight from the loader,
and open the result.

You would have seen it
in a minute.

Split the score
by orientation too.

A good average
can hide a group
where the model
never learned a thing.

– Serguey Asael Shinder

📰 Read the original article on Dev.to AI

Originally published by Dev.to AI. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.