1

So I've been using Tensorflow's tutorials for neural networks. I completed the "basic classification" that is essentially just MNIST and have been working on making my own custom variation as a little thought experiment. Everything is pretty self explanatory except putting the datasets into a usable form as the tutorial uses a premade dataset and looks like it cuts some corners. All I would like to know is how to put a colored photo into a usable piece of data. I assume that will just be a 1D array. As a side question, will a neural network lose any effectiveness if a 2d photo is stored in a 1d array if its not a CNN.

3
  • 1
    The minst number and fashion datasets with keras/tensorflow are monochromatic 2d arrays. A color photo would have color channels, so that's a 3 dimensional array.
    – user11563547
    Commented Jul 14, 2019 at 3:55
  • Okay, thank you for the info. So as per my question, how do I import? Commented Jul 14, 2019 at 5:02
  • @MikeSperry ^^^ Commented Jul 14, 2019 at 5:46

1 Answer 1

5

Datasets included in Keras are premade and usually preprocessed so that beginner could easily try a hand on them. For using your own images, like for a cat-dog image classification problem, you can place the images in two separate directories, for example, in images/cats and images/dogs. Now, we parse each and every image in these directories,

import os
from PIL import Image

master_dir = 'images'
img_dirs = os.listdir( master_dir ) 
for img_dir in img_dirs:
    img_names = os.listdir( os.path.join( master_dir , img_dir ) ) 
    for name in img_names:
        img_path = os.path.join( master_dir , img_dir , name ) 
        image = Image.open( img_path ).resize( ( 64 , 64 ) ).convert( 'L' ) 
        # Store this image in an array with its corresponding label

Here. the image will be an array of shape (64, 64 ) which indicates that the image is grayscale. Besides .convert( 'L' ) in the code, we can use .convert( 'RGB' ) to have an image of shape (64,64,3) RGB image.

Now,

  • Collect all the images and labels in a Python list.
  • Convert the lists to NumPy arrays.
  • Store the NumPy arrays in a .npy file using the np.save() method.
  • In the file which trains the model, load these files using np.load() method and feed them to the model.
3
  • Perfect, this is exactly what I needed. Thank you. Commented Jul 14, 2019 at 15:53
  • 2
    Also, you can add this code in different scripts and run those scripts separately, so each script will read some files and produce processed data Commented Jul 14, 2019 at 16:05
  • So when I try to convert it into a numpy array, it still seems to think I am storing it as an "image" rather than a 2d array and will not let me convert it into a numpy array. Commented Jul 14, 2019 at 17:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.