Keras input_shape 用于 conv2d 和手动加载的图像
Keras input_shape for conv2d and manually loaded images
我正在从许多 384x286 b/w 图像手动创建我的数据集。
我加载这样的图像:
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)
print(x.shape) => (100, 286, 384)
阅读 keras 文档并检查我的后端,我应该向卷积步骤提供一个由(行、列、通道)
组成的 input_shape
因为我不知道样本大小,所以我希望通过输入大小,类似于
( 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) 原因:
Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (85, 286, 384)
传递为_input_shape (None, 286, 384, 1 ) 原因:
Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=5
我做错了什么?我该如何重塑输入数组?
我认为以下内容可能会解决您的错误。
input_shape 我们提供给第一个 conv2d(顺序模型的第一层)应该类似于 (286,384,1) 或 (width,height,channels)。其中 batch_size 不需要 "None" 维度。
输入的形状可以是(batch_size,286,384,1)
这对你有帮助吗??
将 input_shape
设置为 (286,384,1)。现在该模型需要 4 个维度的输入。这意味着您必须使用 .reshape(n_images, 286, 384, 1)
重塑图像。现在您已经在不更改数据的情况下添加了一个额外的维度,并且您的模型已准备好 运行。基本上,您需要将数据重塑为 (n_images
、x_shape
、y_shape
、channels
).
很棒的是您还可以使用 RGB 图像作为输入。只需将 channels
更改为 3.
同时检查这个答案:
例子
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)
您的 input_shape 维度是正确的,即 input_shape(286, 384, 1)
将您的 input_image 重塑为 4D [batch_size、img_height、img_width、number_of_channels]
input_image=input_image.reshape(85,286, 384,1)
在
期间
model.fit(input_image,label)
我正在从许多 384x286 b/w 图像手动创建我的数据集。
我加载这样的图像:
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)
print(x.shape) => (100, 286, 384)
阅读 keras 文档并检查我的后端,我应该向卷积步骤提供一个由(行、列、通道)
组成的 input_shape因为我不知道样本大小,所以我希望通过输入大小,类似于
( 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) 原因:
Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (85, 286, 384)
传递为_input_shape (None, 286, 384, 1 ) 原因:
Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=5
我做错了什么?我该如何重塑输入数组?
我认为以下内容可能会解决您的错误。
input_shape 我们提供给第一个 conv2d(顺序模型的第一层)应该类似于 (286,384,1) 或 (width,height,channels)。其中 batch_size 不需要 "None" 维度。
输入的形状可以是(batch_size,286,384,1)
这对你有帮助吗??
将 input_shape
设置为 (286,384,1)。现在该模型需要 4 个维度的输入。这意味着您必须使用 .reshape(n_images, 286, 384, 1)
重塑图像。现在您已经在不更改数据的情况下添加了一个额外的维度,并且您的模型已准备好 运行。基本上,您需要将数据重塑为 (n_images
、x_shape
、y_shape
、channels
).
很棒的是您还可以使用 RGB 图像作为输入。只需将 channels
更改为 3.
同时检查这个答案:
例子
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)
您的 input_shape 维度是正确的,即 input_shape(286, 384, 1)
将您的 input_image 重塑为 4D [batch_size、img_height、img_width、number_of_channels]
input_image=input_image.reshape(85,286, 384,1)
在
期间model.fit(input_image,label)