问题描述
我正在从384x286黑白图像中手动创建数据集.
I am manually creating my dataset from a number of 384x286 b/w images.
我加载这样的图像:
x = []
for f in files:
img = Image.open(f)
img.load()
data = np.asarray(img, dtype="int32")
x.append(data)
x = np.array(x)
这导致x为数组(num_samples,286、384)
this results in x being an array (num_samples, 286, 384)
print(x.shape) => (100, 286, 384)
阅读keras文档,并检查我的后端,我应该向卷积步骤提供由(rows,cols,channels)组成的input_shape
reading the keras documentation, and checking my backend, i should provide to the convolution step an input_shape composed by ( rows, cols, channels )
由于我不随意知道样本大小,因此我希望将其作为输入大小,类似
since i don't arbitrarily know the sample size, i would have expected to pass as an input size, something similar to
( None, 286, 384, 1 )
该模型的构建如下:
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
# other steps...
作为input_shape(286、384、1)传递会导致:
passing as input_shape (286, 384, 1) causes:
传递as_input_shape(无,286、384、1)会导致:
passing as_input_shape (None, 286, 384, 1 ) causes:
我在做什么错?我该如何重塑输入数组的形状?
what am i doing wrong ? how do i have to reshape the input array?
推荐答案
将input_shape
设置为(286,384,1).现在,模型需要一个4维的输入.这意味着您必须使用.reshape(n_images, 286, 384, 1)
重塑图像.现在,您已经添加了一个额外的维度,而无需更改数据,并且模型可以运行了.基本上,您需要将数据重塑为(n_images
,x_shape
,y_shape
,channels
).
Set the input_shape
to (286,384,1). Now the model expects an input with 4 dimensions. This means that you have to reshape your image with .reshape(n_images, 286, 384, 1)
. Now you have added an extra dimension without changing the data and your model is ready to run. Basically, you need to reshape your data to (n_images
, x_shape
, y_shape
, channels
).
很棒的事情是您还可以使用RGB图像作为输入.只需将channels
更改为3.
The cool thing is that you also can use an RGB-image as input. Just change channels
to 3.
还要检查以下答案: Keras输入说明:input_shape,units,batch_size,dim,等等
Check also this answer:Keras input explanation: input_shape, units, batch_size, dim, etc
示例
import numpy as np
from keras.models import Sequential
from keras.layers.convolutional import Convolution2D
from keras.layers.core import Flatten, Dense, Activation
from keras.utils import np_utils
#Create model
model = Sequential()
model.add(Convolution2D(32, kernel_size=(3, 3), activation='relu', input_shape=(286,384,1)))
model.add(Flatten())
model.add(Dense(2))
model.add(Activation('softmax'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
#Create random data
n_images=100
data = np.random.randint(0,2,n_images*286*384)
labels = np.random.randint(0,2,n_images)
labels = np_utils.to_categorical(list(labels))
#add dimension to images
data = data.reshape(n_images,286,384,1)
#Fit model
model.fit(data, labels, verbose=1)
这篇关于用于转换和手动加载图像的Keras input_shape的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!