我不明白出了啥问题 InvalidArgumentError: Conv2DCustomBackpropInputOp only support NHWC

Posted

技术标签:

【中文标题】我不明白出了啥问题 InvalidArgumentError: Conv2DCustomBackpropInputOp only support NHWC【英文标题】:I dont understand what is wrong InvalidArgumentError: Conv2DCustomBackpropInputOp only supports NHWC我不明白出了什么问题 InvalidArgumentError: Conv2DCustomBackpropInputOp only support NHWC 【发布时间】:2020-06-18 17:28:03 【问题描述】:

我正在尝试在调制https://github.com/radioML/examples/blob/master/modulation_recognition/RML2016.10a_VTCNN2_example.ipynb 上执行此示例笔记本

执行后

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D , Reshape , ZeroPadding2D,BatchNormalization
from tensorflow.keras.callbacks import EarlyStopping


model = Sequential()

model.add(Reshape([1]+in_shp, input_shape=in_shp))

model.add(ZeroPadding2D((0, 2), data_format="channels_first"))
model.add(Conv2D(256, (1,3), data_format="channels_first"))
model.add(Dropout(0.5))

model.add(ZeroPadding2D((0, 1), data_format="channels_first"))
model.add(Conv2D(80, (2 ,3), data_format="channels_first" , activation="relu"))

model.add(Dropout(0.5))

model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(len(classes) , activation='softmax'))
model.add(Activation('softmax'))
model.add(Reshape([len(classes)]))

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

我明白了

Model: "sequential_4"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
reshape_7 (Reshape)          (None, 1, 2, 128)         0         
_________________________________________________________________
zero_padding2d_8 (ZeroPaddin (None, 1, 2, 132)         0         
_________________________________________________________________
conv2d_8 (Conv2D)            (None, 256, 2, 130)       1024      
_________________________________________________________________
dropout_10 (Dropout)         (None, 256, 2, 130)       0         
_________________________________________________________________
zero_padding2d_9 (ZeroPaddin (None, 256, 2, 132)       0         
_________________________________________________________________
conv2d_9 (Conv2D)            (None, 80, 1, 130)        122960    
_________________________________________________________________
dropout_11 (Dropout)         (None, 80, 1, 130)        0         
_________________________________________________________________
flatten_3 (Flatten)          (None, 10400)             0         
_________________________________________________________________
dense_6 (Dense)              (None, 256)               2662656   
_________________________________________________________________
dropout_12 (Dropout)         (None, 256)               0         
_________________________________________________________________
dense_7 (Dense)              (None, 11)                2827      
_________________________________________________________________
activation_3 (Activation)    (None, 11)                0         
_________________________________________________________________
reshape_8 (Reshape)          (None, 11)                0         
=================================================================
Total params: 2,789,467
Trainable params: 2,789,467
Non-trainable params: 0
_________________________________________________________________

然后当我运行它时

model_fit(model, X_train, Y_train, test_idx)

我收到了这个错误

**InvalidArgumentError:  Conv2DCustomBackpropInputOp only supports NHWC.**
     [[node Conv2DBackpropInput (defined at <ipython-input-17-9cd1191bc59a>:3) ]] [Op:__inference_distributed_function_3032]

Function call stack:
distributed_function

当我在其他机器上运行相同的代码时,它可以工作。所以我卸载了 anaconda、Keras、TensorFlow 并重新安装了所有东西。

inp_shp = [2, 128]
X_train.shape = (110000, 2, 128)

【问题讨论】:

【参考方案1】:

NHWC 代表 Num_samples x Height x Width x Channels

你有X_train.shape = (110000, 2, 128),但是如果你有灰度图像,你应该传递给模型的应该是这个形状X_train.shape = (110000, 2, 128, 1),如果它们是RGB图像,则应该是X_train.shape = (110000, 2, 128, 3)。你inp_shape 也应该改变。

【讨论】:

非常感谢。所以我在 X_train 和 inp_shape 上使用 reshape?还是有其他方法可以实现您的建议? 可能类似于:X_train = np.expand_dims(X_train, axis = 3) 所以我更改了X_train.shape = (110000, 2, 128, 1)inp_shp = [2, 128, 1] 并执行了。然后我得到了,ValueError: Input 0 of layer zero_padding2d_17 is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [None, 1, 2, 128, 1]

以上是关于我不明白出了啥问题 InvalidArgumentError: Conv2DCustomBackpropInputOp only support NHWC的主要内容,如果未能解决你的问题,请参考以下文章

discord.js 我的代码不起作用,在终端中显示 typeError 但我不知道出了啥问题

C#多线程 - 出了啥问题,如何使用 AutoResetEvent

C ++返回向量,无法弄清楚出了啥问题

创建类别模板出了啥问题

出了啥问题:任务':fluttertoast:compileDebugKotlin'执行失败

我的 SQL 查询出了啥问题?