简体   繁体   English

用于转换和手动加载图像的Keras input_shape

[英]Keras input_shape for conv2d and manually loaded images

I am manually creating my dataset from a number of 384x286 b/w images. 我正在从多个384x286黑白图像中手动创建数据集。

I load an image like this: 我加载这样的图像:

x = []
for f in files:
        img = Image.open(f)
        img.load()
        data = np.asarray(img, dtype="int32")
        x.append(data)
x = np.array(x)

this results in x being an array (num_samples, 286, 384) 这导致x是一个数组(num_samples,286、384)

print(x.shape) => (100, 286, 384)

reading the keras documentation, and checking my backend, i should provide to the convolution step an input_shape composed by ( rows, cols, channels ) 阅读keras文档并检查我的后端,我应该向卷积步骤提供一个由(rows,cols,channels)组成的input_shape

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 )

the model is built as follows: 该模型的构建如下:

model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=input_shape))
# other steps...

passing as input_shape (286, 384, 1) causes: 作为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) 检查输入时出错:预期conv2d_1_input具有4个维,但数组的形状为(85,286,384)

passing as_input_shape (None, 286, 384, 1 ) causes: 传递as_input_shape(None,286,384,1)会导致:

Input 0 is incompatible with layer conv2d_1: expected ndim=4, found ndim=5 输入0与层conv2d_1不兼容:预期ndim = 4,找到的ndim = 5

what am i doing wrong ? 我究竟做错了什么 ? how do i have to reshape the input array? 我该如何重塑输入数组?

Set the input_shape to (286,384,1). input_shape设置为(286,384,1)。 Now the model expects an input with 4 dimensions. 现在,模型需要一个4维的输入。 This means that you have to reshape your image with .reshape(n_images, 286, 384, 1) . 这意味着您必须使用.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 ). 基本上,您需要将数据调整为( n_imagesx_shapey_shapechannels )。

The cool thing is that you also can use an RGB-image as input. 很棒的事情是您还可以使用RGB图像作为输入。 Just change channels to 3. 只需将channels更改为3。

Check also this answer: Keras input explanation: input_shape, units, batch_size, dim, etc 还要检查以下答案: Keras输入说明:input_shape,units,batch_size,dim等

Example

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)

your input_shape dimension is correct ie input_shape(286, 384, 1) 您的input_shape尺寸是正确的,即input_shape(286,384,1)

reshape your input_image to 4D [batch_size, img_height, img_width, number_of_channels] 将输入图像重塑为4D [batch_size,img_height,img_width,number_of_channels]

input_image=input_image.reshape(85,286, 384,1)

during

model.fit(input_image,label)

I think following might resolve your error. 我认为以下方法可以解决您的错误。

  1. input_shape we provide to first conv2d (first layer of sequential model) should be something like (286,384,1) or (width,height,channels). 我们提供给第一个conv2d(顺序模型的第一层)的input_shape应该类似于(286,384,1)或(宽度,高度,通道)。 No need of "None" dimension for batch_size in it. 其中batch_size不需要“无”维度。

  2. Shape of your input can be (batch_size,286,384,1) 您输入的形状可以是(batch_size,286,384,1)

Does this help you ?? 这对您有帮助吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Tensor Flow 2.0、keras 的 Conv2D 层中指定 input_shape - How to specify input_shape in Conv2D layer in Tensor Flow 2.0, keras 弄清楚如何为自己的数据集在Keras的Conv2D层中定义input_shape - Trouble figuring out how to define the input_shape in the Conv2D layer in Keras for my own dataset Keras Conv2D 输入形状 - Keras Conv2D input Shape Conv1D 层 Keras 的 input_shape - input_shape of Conv1D layer Keras 在keras中修复3D Conv的input_shape(后端的Tensorflow) - fix input_shape of 3D Conv in keras (Tensorflow at the backend) 为什么只为第一个 Conv2D 层指定 Conv2D 层的 input_shape 属性? - Why should the input_shape property of a Conv2D layer be specified only for the first Conv2D layer? Tensorflow Conv2D 层 input_shape 配置错误:ValueError: Input 0 of layer "sequential" is in compatible with the layer: - Tensorflow Conv2D layers input_shape configuration error: ValueError: Input 0 of layer "sequential" is incompatible with the layer: Tensorflow/Keras Conv2D 层中的输入形状错误 - Input shape error in Tensorflow/Keras Conv2D layer 了解 Conv1D、密集层(一维输入)的 keras input_shape - Understanding the keras input_shape for Conv1D, Dense layers (1-dimensional input) 我对keras conv1D图层的输入应该是什么?input_shape应该是什么? - What should my input to a keras conv1D layer be and what should the input_shape be?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM