如何重塑图像的尺寸以包含图像的数量(即 1)?
How do I reshape the dimensions of an image to contain the number of images (i.e., 1) as well?
我是运行一些图像上的神经网络模型。最初,为了训练,我将所有图像转换为维度 (# 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)
这个returns:
X_train shape: (13338, 32, 32, 3)
Downloaded image shape: (448, 720, 3)
我想让下载的图像的形状为 (1, 448, 720, 3) 以便它匹配 X_train
的形状尺寸,因为当我尝试预测 class的下载图片,returns尺寸错误:
pred = cnn_model.predict_classes(img)
print('Predicted:', lb.inverse_transform(pred))
这个returns:
ValueError: Error when checking : expected conv2d_71_input to have 4 dimensions, but got array with shape (960, 640, 3)
从您的描述来看,您似乎并不是真的要将图像数量用作特征,而是用作样本权重。从概念上讲,您可能想要转换
k x r x g x b
至
r x g x b
... # repeat k times
r x g x b
这自然会使输入和输出维度相同,顺便说一句。如果这会过多地增加学习时间,并且您的图书馆有样本权重参数,您应该考虑使用它。
如果您只想从技术上添加维度,可以使用 np.expand_dims
:
>>> np.expand_dims(np.array([[1, 2, 3], [3, 4, 5]]), axis=0).shape
(1, 2, 3)
但是,我不能说我确定这从根本上就是你所想的。
我是运行一些图像上的神经网络模型。最初,为了训练,我将所有图像转换为维度 (# 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)
这个returns:
X_train shape: (13338, 32, 32, 3)
Downloaded image shape: (448, 720, 3)
我想让下载的图像的形状为 (1, 448, 720, 3) 以便它匹配 X_train
的形状尺寸,因为当我尝试预测 class的下载图片,returns尺寸错误:
pred = cnn_model.predict_classes(img)
print('Predicted:', lb.inverse_transform(pred))
这个returns:
ValueError: Error when checking : expected conv2d_71_input to have 4 dimensions, but got array with shape (960, 640, 3)
从您的描述来看,您似乎并不是真的要将图像数量用作特征,而是用作样本权重。从概念上讲,您可能想要转换
k x r x g x b
至
r x g x b
... # repeat k times
r x g x b
这自然会使输入和输出维度相同,顺便说一句。如果这会过多地增加学习时间,并且您的图书馆有样本权重参数,您应该考虑使用它。
如果您只想从技术上添加维度,可以使用 np.expand_dims
:
>>> np.expand_dims(np.array([[1, 2, 3], [3, 4, 5]]), axis=0).shape
(1, 2, 3)
但是,我不能说我确定这从根本上就是你所想的。