步幅的长度应为1、1或3,但应为2 [英] strides should be of length 1, 1 or 3 but was 2

查看:334
本文介绍了步幅的长度应为1、1或3,但应为2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将卷积神经网络与GRU堆叠在一起,以解决图像到文本的问题. 这是我的模特:

I have been trying to stack Convolutional neural networks with GRUs for an image to text problem. Here's my model :

model=Sequential()

model.add(TimeDistributed(Conv2D(16,kernel_size 
(3,3),data_format="channels_last",input_shape= 
(129,80,564,3),padding='SAME',strides=(1,1))))
model.add(TimeDistributed(Activation("relu")))
model.add(TimeDistributed(Conv2D(16,kernel_size =(3,3),strides=(1,1))))
model.add(TimeDistributed(Activation("relu")))
model.add(TimeDistributed(MaxPooling2D(pool_size=2,strides=(1,1) )))
model.add(TimeDistributed(Reshape((280*38*16,))))
model.add(TimeDistributed(Dense(32)))
model.add(GRU(512))
model.add(Dense(50))
model.add(Activation("softmax"))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics= 
 ['accuracy'])

当我尝试拟合模型时,出现以下错误:

When I try to fit my model I get the following error :

-------------------------------------------------------------------------
ValueError                                Traceback (most recent call 
last)
<ipython-input-125-c6a3c418689c> in <module>()
  1 nb_epoch = 100
 ----> 2 model.fit(X2,L2, epochs=100)

 10 frames
 /usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_ops.py 
 in _get_sequence(value, n, channel_index, name)
 71   else:
 72     raise ValueError("{} should be of length 1, {} or {} but was 
 {}".format(
 ---> 73         name, n, n + 2, current_n))
 74 
 75   if channel_index == 1:

 ValueError: strides should be of length 1, 1 or 3 but was 2

我什至无法开始思考出现此消息的原因.我已经为所有图层指定了步幅"参数.任何帮助将不胜感激.

I cannot even begin to wrap my head around why this message appears.I have specified the "strides" parameters for all layers. Any help will be deeply appreciated.

P.S-当我尝试在没有TimeDistributed图层的情况下拟合模型时,我没有任何问题.也许与此相关的事情会引发此错误.

P.S - I did not have any problems when I tried to fit a model without TimeDistributed layers. Maybe there is something to do with this that raises this error.

推荐答案

您在代码中犯了一些错误.

You have made several mistakes in your code.

  • 在第一层中,应指定TimeDistributed层的input_shape,而不是Conv2D层.
  • MaxPooling2D用于下采样图像的空间大小.但使用strides=(1,1)时,图像大小将保持不变并且不会减小.
  • 在第一层中使用padding='SAME'会在进行卷积时添加零填充,并且会在Reshape层中导致形状不匹配错误.相反,您可以使用Flatten层.
  • Conv2D中的步幅默认值为strides=(1,1),因此,可以不加提及.
  • In the first layer you should specify the input_shape of the TimeDistributed layer, not Conv2D layer.
  • MaxPooling2D is used for down-sampling the images spatial size. but with strides=(1,1) the image size will remain same and not be reduced.
  • Using padding='SAME' in the first layer will add zero-padding while doing convolution and will result in a shape mismatch error in the Reshape layer. Instead you can use Flatten layer.
  • Default value of stride in a Conv2D is strides=(1,1), so, it's optional to mention.

最后,工作代码应为以下内容:

Finally, the working code should be something following:

model=keras.models.Sequential()
model.add(keras.layers.TimeDistributed(keras.layers.Conv2D(16, kernel_size=(3,3), data_format="channels_last"),input_shape=(129,80,564,3)))
model.add(keras.layers.TimeDistributed(keras.layers.Activation("relu")))
model.add(keras.layers.TimeDistributed(keras.layers.Conv2D(16, kernel_size =(3,3))))
model.add(keras.layers.TimeDistributed(keras.layers.Activation("relu")))
model.add(keras.layers.TimeDistributed(keras.layers.MaxPooling2D(pool_size=2)))
# model.add(keras.layers.TimeDistributed(keras.layers.Flatten()))
model.add(keras.layers.TimeDistributed(keras.layers.Reshape((280*38*16,))))
model.add(keras.layers.TimeDistributed(keras.layers.Dense(32)))
model.add(keras.layers.GRU(512))
model.add(keras.layers.Dense(50))
model.add(keras.layers.Activation("softmax"))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics= ['accuracy'])

这篇关于步幅的长度应为1、1或3,但应为2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆