Images get rotated randomly

Hi everyone,

I was experimenting with the notebook of lesson 2 and applied it on my own dataset which was a set of building images. When I assign the data to the parameter via the ImageDataBunch function and then make it show the data, some of the images appear to be rotated 90 degrees. I tried adding max_rotate = 0. to the transformation but that did not help. Does anyone have any idea on what to do?

Thanks

2 Likes

This is because of the flip_vert Transform. You can see all the options for the get_transforms function here: https://docs.fast.ai/vision.transform.html

Hi Brad, thanks for the reply. By flipping, the picture should not be rotated 90 degrees in clockwise or counterclockwise direction, but it should be mirrored upside down. Besides, the default value for flip_vert is set to be False. So, I don’t believe this has to do with the flipping.

I also tried setting do_flip to False but that helped neither. Here is a screenshot of what the data looks like (pay attention to the top-right corner):
image

flip_vert also does 90 degree rotations:

  • flip_vert : limit the flips to horizontal flips (when False) or to horizontal and vertical flips as well as 90-degrees rotations (when True)

It will be hard to help further without seeing any code.

Have you checked to make sure these source images are in the proper orientation on disk?

Thanks for the docs and help.
Looks like in some way that I don’t know some random images where rotated on disk. I will check out to see if rotating them will solve the problem.

I also had this problem (images being rotated by 90 degrees) when running the notebook of lesson 2:

https://github.com/fastai/course-v3/blob/master/nbs/dl1/lesson2-download.ipynb .

I tried get_transforms(max_rotate=None, do_flip=False) but I still have this issue.

I have verified that the images in disk are not rotated.

Anyone know the reason?

How have you verified that the images on the disk are not rotated?
There is an “Exif” tag called “orientation” which informs most modern programs as to which rotation to use for a photo. This is a property that is written upon the file and is sometimes very hard to find.

I suggest using a Python package, e.g. exif to review this tag for your photos. I wrote a code based on this StackOverflow answer for detecting all images in a directory which contain this tag and rotating them to the correct orientation without using this tag:

from exif import Image as ExifImage
from PIL import Image
from glob import glob
import os.path

transformation_funcs = {
    6: lambda img: img.rotate(-90, resample=Image.BICUBIC, expand=True),
    8: lambda img: img.rotate(90, resample=Image.BICUBIC, expand=True),
    3: lambda img: img.rotate(180, resample=Image.BICUBIC, expand=True),
    2: lambda img: img.transpose(Image.FLIP_LEFT_RIGHT),
    5: lambda img: img.rotate(-90, resample=Image.BICUBIC, expand=True).transpose(Image.FLIP_LEFT_RIGHT),
    7: lambda img: img.rotate(90, resample=Image.BICUBIC, expand=True).transpose(Image.FLIP_LEFT_RIGHT),
    4: lambda img: img.rotate(180, resample=Image.BICUBIC, expand=True).transpose(Image.FLIP_LEFT_RIGHT),
}

dir_path = [path to directory containing you images]

for img_path in glob(os.path.join(dir_path, '*.jpg')):
    print(img_path)
    with open(img_path, 'rb') as image_file:
        my_image = ExifImage(image_file)
    try:
        orientation = my_image.orientation.value
    except:
        print('  N/A skipping')
        continue
    if orientation == 1:
        print('  1 skipping')
        continue
    else:
        print(' ', orientation, 'transforming')
        
    my_image.orientation = 1
    with open(img_path, 'wb') as image_file:
        image_file.write(my_image.get_file())
        
    img = Image.open(img_path)
    img = transformation_funcs[orientation](img)
    img.save(img_path)

You need to have the exif and PIL libraries installed.
Sorry for the mess, by the way, I wrote this quick and dirty.

1 Like