I am a management consultant exploring the World of Artificial intelligence.

How to load images into an ndarray as input for a CNN

Since I couldn't find a satisfying answer on Google, Stackexchange, etc., here's my solution in case anyone else is looking for something like this:

 

The various tutorials for CNN or GAN processing usually use the  machine learning libraries integrated in keras (MNIST, CIFAR, etc.), since this is enough to demonstrate whatever they want to demonstrate. They do this by simply calling (in this case for mnist)

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

However, if you want to load your own set of images, you need to fill the X_train array yourself. It took me a while to figure out how this works, so I thought I'd post it here. Oh, and since you probably want to preprocess those images as well, I'll spare you the hassle wondering why you can't display them properly using img.show(). This is my solution.

all_img_paths = glob.glob(os.path.join(root_dir, '*.jpg'))
np.random.shuffle(all_img_paths)
imgs = np.empty((0,128,128,3), dtype=np.float32)

for img_path in all_img_paths:
    img = io.imread(img_path)        
    img = transform.resize(img, (IMG_SIZE_X, IMG_SIZE_Y))
    imgs = np.insert(imgs, 0, img, 0)

X_train = imgs

image = X_train[467]

import matplotlib.pyplot as plt
plt.imshow(image)
plt.show()

X_train = (X_train.astype(np.float32) - 127.5)/127.5

could not set cudnn tensor descriptor: Always your fault!

How an online class in machine learning prepares you for the real world

How an online class in machine learning prepares you for the real world