Juan Felipe Contreras
Juan Felipe Contreras

Reputation: 75

Problems identifying images in pytorch

I am really new at deep learning and I am studying how to properly run neural networks using pytorch. Currently I am trying to read a dataset of images using the following code:

from torch.utils.data import Dataset, DataLoader
from torchvision import datasets, transforms
from torchvision import transforms, utils

transformations = transforms.Compose([
    transforms.Resize(255),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
train_set = datasets.ImageFolder('faces/train', transform = transformations)
test_set = datasets.ImageFolder('faces/test', transform = transformations)

train_loader = DataLoader(train_set, batch_size=60, shuffle = True) 
test_loader = DataLoader(test_set, batch_size=60, shuffle = True)

once done that, I am trying to read the images to start running the neural networks on the images, first by separating the imgs from the labels using:

img, labels = next(iter(train_loader))

And then I received the following message:

UnidentifiedImageError: cannot identify image file <_io.BufferedReader name='faces/train/karyadi/karyadi_straight_angry_open.pgm'>

The structure of the folders on my working directory where I have stored the images is as follows:

  1. faces:
    • Train: 10 folders, each one with around 90 images in pgm format.
    • Test: 1o folders, each one with around 90 images in pgm format.

does anyone know what the problem might be?

thanks in advance.

Upvotes: 1

Views: 1006

Answers (1)

Juan Felipe Contreras
Juan Felipe Contreras

Reputation: 75

I finally found the problem. This post was very useful. The gist of it is that PIL has problems importing images of certain sizes (I do not have all the details about this). At the end I use cv2 to import the pgm images one by one and then convert them into 32-bit ndarrays to export them in jpeg format. Here is my code for one image:

import cv2
img = cv2.imread('faces/train/karyadi/karyadi_straight_angry_open.pgm')
img = np.full(img.shape, img, dtype=np.float32)
cv2.imwrite('img.jpeg', img)

Upvotes: 1

Related Questions