【发布时间】:2018-05-17 09:03:33
【问题描述】:
我正在一些图像上运行神经网络模型。最初,为了训练,我将所有图像转换为维度为(# of images in the dataset) x r x g x b 的 pandas 数据帧,其中r、g、b 是每个图像的颜色值。现在,当我尝试在单个外部下载的图像上测试模型时,它会给出尺寸错误,因为很明显,图像的尺寸仅为r x g x b。如何将图像数量作为维度添加到此图像中?
编辑:这是代码:
#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)
这会返回:
X_train shape: (13338, 32, 32, 3)
Downloaded image shape: (448, 720, 3)
我想让下载图像的形状为 (1, 448, 720, 3) 以匹配X_train 形状的尺寸,因为当我尝试预测下载图像的类别时,它返回尺寸错误:
pred = cnn_model.predict_classes(img)
print('Predicted:', lb.inverse_transform(pred))
这会返回:
ValueError: Error when checking : expected conv2d_71_input to have 4 dimensions, but got array with shape (960, 640, 3)
【问题讨论】:
-
您使用的是哪个库?
-
用于将图像转换为数据框?只是熊猫。
-
抱歉,您的意思是您将哪个库用于神经网络。
-
Keras 及其子模块,后端有 tensorflow
标签: python pandas dimensions