Search code examples
python-3.xkerasdeep-learningmnistnumpy-ndarray

How to create a similar image dataset of mnist with shape (12500, 50,50)


How to create a similar image dataset of mnist with shape (12500, 50,50)

I have a folder with 12500 images. I want to generate a datset with these images to work with sorting images in keras. I want to generate a dataset similar to mnist so that with shape (12500, 50,50). I'm wrapped up in creating the code to generate the dataset. I'm trying to create a numpy array but it's not getting the format I want. I believe I should use the opencv resize function to leave all images with height and width with 50x50 shape. 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, cv2.IMREAD_GRAYSCALE)
    X_data.append (image)

X_data = np.array(X_data)

X_data = X_data.astype('float32') / 255

IMG_SIZE = 50

X_data = cv2.resize(X_data, (IMG_SIZE, IMG_SIZE))

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

Solution

  • You should resize images, instead of the whole dataset:

    import cv2
    import glob
    import numpy as np
    
    X_data = []
    files = glob.glob ("C:/Teste_datasets/PetImages/Cat/*.jpg")
    IMG_SIZE = 50
    
    for myFile in files:
        print(myFile)
        image = cv2.imread (myFile, cv2.IMREAD_GRAYSCALE)
        image = cv2.resize(image, (IMG_SIZE, IMG_SIZE))
        X_data.append (image)
    
    X_data = np.array(X_data)
    X_data = X_data.astype('float32') / 255
    
    print(X_data.shape)
    
    [daniel@daniel-pc Keras]$ python3 testeKeras.py
    <...>/2.png
    <...>/1.png
    (2, 50, 50)
    

    Also, there's no reason to reshape, because since you load the image with cv2.IMREAD_GRAYSCALE you already have a single value representing the color.