输入形状为[?,40,1,8]

2024-04-25 12:30:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在做一个机器学习项目,涉及到狗的卷积神经网络分类。我正在Keras建立自己的神经网络,使用经过预训练的Xception神经网络进行转移学习。在创建神经网络的过程中,我遇到了以下错误:

Negative dimension size caused by subtracting 2 from 1 for 'max_pooling2d_2/MaxPool' (op: 'MaxPool') with input shapes: [?,1,1,40].

下面是我用来加载数据集和训练模型的代码。请注意:我没有访问Exception神经网络的权限,因此我不能简单地将模型的输出张量添加到模型中。

### TODO: Obtain bottleneck features from another pre-trained CNN.
bottleneck_features = np.load('/data/bottleneck_features/DogXceptionData.npz')
train_Xception = bottleneck_features['train']
valid_Xception = bottleneck_features['valid']
test_Xception = bottleneck_features['test']
### TODO: Define your architecture.
import keras
print(keras.__version__)
transfertrained_model = Sequential()
transfertrained_model.add(Conv2D(20,(2,2),input_shape=(train_Xception.shape[1],train_Xception.shape[2],3)))
transfertrained_model.add(Conv2D(40,(2,2)))
transfertrained_model.add(MaxPooling2D())
transfertrained_model.add(Conv2D(40,(2,2)))
transfertrained_model.add(MaxPooling2D())
transfertrained_model.add(Conv2D(80,(2,2)))
transfertrained_model.add(MaxPooling2D())
transfertrained_model.add(GlobalAveragePooling2D())
transfertrained_model.add(Dense(133))
transfertrained_model.add(Activation('sigmoid'))
transfertrained_model.summary()

设置:

Python 3 Keras 2.0.9

我已经阅读过的问题

GitHub(https://github.com/keras-team/keras/issues/7611)上的一篇文章建议我应该使用Keras2API。既然我是,这个建议不适用于我。

另一个堆栈溢出答案(Negative dimension size caused by subtracting 3 from 1 for 'conv2d_2/convolution')建议我应该将数据\u格式设置为“channels_first”。我做了,然后maxpoolg2d层抱怨了同样的错误。

请帮帮我!


Tags: from模型addmodeltrain神经网络建议keras

热门问题