How to create a dataset to work with Keras classification

Asked

Viewed 761 times

1

I have a series of images (12500 in total) in rgb format and I want to create a dataset to work on Keras. I want to leave them in a format similar to the Mnist dataset...with Shape (12500,50,50).

Only when I do a reshape with the numpy it gets dimension (5,50,50,1). I believe it is getting an extra dimension due to the fact that my images are with 3 channels (r,g and b).

How I make my dataset stick to the dimensions (12500,50,50) with only one color channel?

Below, follow my code.

Grateful for the attention.

import cv2
import glob
import numpy as np

X_data = []
files = glob.glob ("C:/Teste_datasets/PetImages/Cat/*.jpg")

for myFile in files:
    print(myFile)
    image = cv2.imread (myFile)
    X_data.append (image)

X_data = np.array(X_data).reshape(-1,50, 50,1 )

1 answer

2


When using a monochrome classifier, it is necessary, in some way, to discard information. I imagine that you are searching with the reshape, discard any two channels and pick one up. The problem is that the method reshape does not discard values, just tries to resize the matrix. The reason this resizing does not cause failures lies in the size dimension functionality-1, which causes the reshape by offsetting losses in the fourth dimension in increases in the first dimension.

Exchange the value -1 by the expected size

X_data = np.array(X_data)
X_data = X_data.reshape((X_data.shape[0], 50, 50,1))

causes a defect immediately.

Solution

It is possible to discard channels using Slices:

X_data = np.array(X_data)
print(X_data.shape)
X_data = X_data[:,:,:,0]
print(X_data.shape)
[daniel@pc keras]$ python3 testeKeras.py 
(2, 50, 50, 3)
(2, 50, 50)

Improved solution

Discarding channels in this way can result in loss of important information. It is preferable to load grayscale images directly, rather than to dispose of channels. This can be done by passing the parameter cv2.IMREAD_GRAYSCALE for the method cv2.imread.

X_data = []
files = glob.glob ("<caminho>/*.jpg")
for myFile in files:
    image = cv2.imread (myFile, cv2.IMREAD_GRAYSCALE)
    X_data.append (image)

X_data = np.array(X_data)
[daniel@pc keras]$ python3 testeKeras.py 
(2, 50, 50)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.