Search code examples
pythonpandasdimensions

How do I reshape the dimensions of an image to contain the number of images (i.e., 1) as well?


I am running a neural network model on some images. Initially, for training, I converted all the images into a pandas dataframe of dimension (# of images in the dataset) x r x g x b, where r, g, b are the colour values of each image. Now when I am trying to test the model on a single externally downloaded image, it is giving a dimension error as, obviously, the image's dimension is only r x g x b. How do I add the number of images as a dimension into this image?

EDIT: Here's the code:

#load the data as a pandas data frame
import pandas as pd
dataset = pd.read_csv(os.path.join(data_path, 'data.csv'))

# split into input (X) and output (Y) variables
X = dataset.values[:,0]
Y = dataset.values[:,1]

# Load all the images and resize them into a single numpy array of consistent dimension
from scipy.misc import imresize
from scipy.misc import imread
import numpy as np

temp = []
for img_name in X:
    img_path = os.path.join(data_dir, 'Train', img_name)
    img = imread(img_path)
    img = imresize(img, (32, 32))
    img = img.astype('float32')
    temp.append(img)

X = np.stack(temp)

# Convert the data classes from words into a number format readable by the program
from sklearn.preprocessing import LabelEncoder
lb = LabelEncoder()
Y = lb.fit_transform(Y)
Y = keras.utils.np_utils.to_categorical(Y)

# Split the data into 67% for training and 33% for testing
X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=0.33)

### Define the neural network model
### Compile and train the model on the data
### Evaluate it

# Test it on an externally downloaded image
img = imread(os.path.join(image_folder, downloaded_image)).astype('float32')
plt.imshow(imresize(img, (128, 128)))

print('X_train shape: ', X_train.shape)
print('Downloaded image shape: ', img.shape)

This returns:

X_train shape: (13338, 32, 32, 3)
Downloaded image shape: (448, 720, 3)

I want to make the downloaded image's shape to be (1, 448, 720, 3) so that it matches the dimensions of X_train's shape, because when I try to predict the class of the downloaded image, it returns a dimension error:

pred = cnn_model.predict_classes(img)
print('Predicted:', lb.inverse_transform(pred))

This returns:

ValueError: Error when checking : expected conv2d_71_input to have 4 dimensions, but got array with shape (960, 640, 3)

Solution

  • From your description, it seems like you don't really mean to use the number of images as a feature, but rather as a sample weight. Conceptually, you probably want to transform

    k x r x g x b
    

    to

    r x g x b
    ...         # repeat k times
    r x g x b
    

    which would naturally make the input and output dimensions identical, BTW. If this increases learning time too much, and your library has a sample weight parameter, you should consider using it.


    If you'd like to just technically add a dimension, you can use np.expand_dims:

    >>> np.expand_dims(np.array([[1, 2, 3], [3, 4, 5]]), axis=0).shape
    (1, 2, 3)
    

    However, I cannot say I'm sure that this is fundamentally what you what.