Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a dataset array of images?

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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!

>Solution :

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading