Training a CNN with TIFF images in pytorch

I am working on a project of object detection in a Kinect depth image in the TIFF format. I have coded the neural network but now I am Stuck.
I have no idea how to use the TIFF images stored on my computer to train the model and perform object detection. Please help.
I also have to draw a bounding box around the particular object if it is detdcted in the image.Any help regarding that or some similar Project which I can refer to would be very useful

Thanks and regards
Vijay

@chsasank

Working with tiff images shouldn’t be any different from working with jpeg. You can use some library for loading images, like PIL, and then use torchvision.transforms to transform it to a torch.Tensor:

from PIL import Image
from torchvision.transforms import ToTensor
image = Image.open('/path/to/image.tif')
image = ToTensor()(image)

To learn more about loading images, and piping them nicely to torch models, read this tutorial:

http://pytorch.org/tutorials/beginner/data_loading_tutorial.html

4 Likes

If you have any luck working with tiff images can you post the code please…

Hello there, my first post here so hopefully something useful. :slight_smile:

I am currently working with TIFF images and so far found the rasterio library the best option in terms of installation ease, functionalities, speed, and so on.

Here is a short code snippet to expand on what @jytug has already answered:

from torchvision.transforms import ToTensor
import rasterio
image = rasterio.open(f"path/to/image.tiff")
image = ToTensor()(image)

The only problem I had with PIL is that you need to change the max size otherwise it errors (preventing DOS bomb attack apparently).
Finally, notice that you need some amount of RAM if the image is big of course.

3 Likes

At the moment PIL cannot read multi-band floating-point images or int 16/32 images, see here: https://github.com/python-pillow/Pillow/issues/1888. It will throw “PIL.UnidentifiedImageError: cannot identify image file”.

You can use tifffile like this:

from tifffile import imread
from torchvision.transforms import ToTensor
image= imread(image_path)
image = ToTensor()(image)
1 Like

I am trying to use this and I found it it was missing one command. :smile:

from torchvision.transforms import ToTensor
import rasterio
image = rasterio.open(f"path/to/image.tiff")
image = image.read()
image = ToTensor()(image)

Sorry for this!