Home > database >  How to create a dataset array of images?
How to create a dataset array of images?

Time:11-14

I'm was messing around with tensorflow, and I has made an image classifier on the mnist dataset. Now I'm trying to recreate that, but with my own dataset, but i'm having trouble just creating the array I want. For example-

(X_train, y_train),(X_test, y_test)= mnist.load_data()
print(X_train.shape)

Output-

(60000, 28, 28)

Where 60000 is the number of images, and the 28, 28 is the image array.

Trying to create this 3d array with my 2 images, I wrote this program-

import numpy as np, random
import matplotlib.image as plt
X_train=[]
print("Preparing the dataset...")
for i in range(100):
    img= plt.imread(f"img/{random.randint(1,2)}.png")
    X_train= np.append(X_train, img)
print("Done...")
print(X_train.shape)

Here, I'm just trying to append my img array to another array X_train. But the output is-

Preparing the dataset...
Done...
(1510452,)

Why is it flattening the whole thing? I'm expecting the shape to be (100, x, y), where x and y is the image array. How do I fix this? Please help me. Thanks!

CodePudding user response:

This is what you should do

import numpy as np, random
import matplotlib.image as plt
X_train=[]
print("Preparing the dataset...")
for i in range(100):
    img=plt.imread(f"img/{random.randint(1,2)}.png")
    X_train.append( img)
X_train = np.array(X_train)
print("Done...")
print(X_train.shape)

upon running this on my images , i got this output

Preparing the dataset...
Done...
(100, 28, 28, 1)
  • Related