我在这个主题上经历了所有的问题。这里遇到一个奇怪的问题。我将图像路径存储在file_names
。
from skimage import io
import numpy as np
X = np.array([np.array(io.imread(i)) for i in file_names])
print(X.shape)
# (50,)
print(X[0].shape)
# (375, 500, 3)
我需要X
为(50, 375, 500, 3)
。我尝试reshape
,添加np.newaxis
等但都失败了。我的下一步是将其用于CNN
。基本上,我想用我的图像创建一个mnist_cnn类数据集。
下一行:
model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3),
activation='relu',
input_shape = (375, 500, 3)))
model.add(Flatten())
model.add(Dense(num_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='adam', metrics=['accuracy'])
model.fit(X, y, # y is (50,36) using one hot encoding
batch_size=10,
epochs=10,
verbose=2)
原因:
ValueError: Error when checking input: expected conv2d_3_input to have 4 dimensions, but got array with shape (50, 1)
答案 0 :(得分:2)
numpy-part看起来很简单:
from skimage import io
import numpy as np
# assumption: images are homogeneous in terms of dimensions and channels!
files = ['C:/TEMP/pic0.jpg', 'C:/TEMP/pic0.jpg', 'C:/TEMP/pic0.jpg', 'C:/TEMP/pic0.jpg']
image_array = np.stack([io.imread(i) for i in files]) # default: axis=0
image_array.shape
# (4, 720, 540, 3)