简体   繁体   English

Python Keras model 输入与层不兼容

[英]Python Keras model input incompatible with layer

I have 2 versions of models built using keras that seem to build well, but when it comes to compiling and fitting, I receive the same error for both of them.我有 2 个使用 keras 构建的模型版本,它们似乎构建得很好,但是在编译和拟合时,我收到了同样的错误。 I'm not sure what the problem is.我不确定问题是什么。

def build_cnn():
    model = models.Sequential([
        layers.Conv2D(32, (3, 3), input_shape=(32, 32, 3), padding='same', name='conv1'),
        layers.MaxPooling2D((2, 2),  name='maxpooling1'),
        layers.Conv2D(64, (3, 3), activation='relu', name='conv2'),
        layers.MaxPooling2D((2, 2), name = 'maxpooling2'),
        layers.Conv2D(64, (3, 3), activation='relu', name = 'conv3'),
        layers.Flatten(name = 'flatten'),
        layers.Dense(64, activation='relu', name='dense1'),
        layers.Dense(10, name='dense2')
        ], name='CNN')
    return model

def build_cnn2():
    model = models.Sequential([
        tf.keras.layers.Input(shape=(32,32,3)),
        tf.keras.layers.Conv2D(32, (3,3), padding='same', name='conv2d'),
        tf.keras.layers.MaxPool2D(pool_size=(2,2), name='maxpooling'),
        tf.keras.layers.Flatten(name='flatten'),
        tf.keras.layers.Dense(10, activation='softmax', name='dense'),
        ], name='conv2d')
    return model

def train(model):
    model.compile(optimizer='adam',
                 loss='binary_crossentropy',
                 metrics=['acc'])
    return model.fit(x_train, y_train,
                    epochs=5,
                    validation_split=0.1)

model2 = build_cnn()
log2 = train(model2)

I receive this order for both model2 = build_cnn() and model2 = build_cnn2() :我收到了model2 = build_cnn()model2 = build_cnn2()的订单:

 ValueError: Input 0 of layer CNN is incompatible with the layer: : expected min_ndim=4, found ndim=2. Full shape received: (None, 30)

According to the error, it seems like you didn't preprocess your data properly - as it says, it expects 4D - (None, h, w, c) but yours somehow (None, 30) .根据错误,您似乎没有正确预处理数据 - 正如它所说,它需要4D - (None, h, w, c)但不知何故是(None, 30)

Also, one of your model's last activation is None , another is set as softmax but yet you set the loss function binary_crossentropy rather than CategoricalCrossentropy .此外,您的模型的最后一个激活之一是None ,另一个设置为softmax但您设置了loss function binary_crossentropy而不是CategoricalCrossentropy

Here is a possible solution for you (by resolving your above issue).这是一个可能的解决方案(通过解决您的上述问题)。

def build_cnn():
    model = Sequential([
        layers.Conv2D(32, (3, 3), input_shape=(32, 32, 3), 
                       padding='same', name='conv1'),
        layers.MaxPooling2D((2, 2),  name='maxpooling1'),
        layers.Conv2D(64, (3, 3), activation='relu', name='conv2'),
        layers.MaxPooling2D((2, 2), name = 'maxpooling2'),
        layers.Conv2D(64, (3, 3), activation='relu', name = 'conv3'),
        layers.Flatten(name = 'flatten'),
        layers.Dense(64, activation='relu', name='dense1'),
        layers.Dense(10, activation='softmax', name='dense2')
        ], name='CNN')
    return model

def train(model, x_train, y_train):
    model.compile(optimizer='adam',
                 loss=tf.keras.losses.CategoricalCrossentropy(),
                 metrics=['acc'])
    return model.fit(x_train, y_train,
                    epochs=5, verbose=2,
                    validation_split=0.1)

model2 = build_cnn()

I use mnist for demonstration.我使用mnist进行演示。 Its shape is (28 x 28) but as your model takes ( 32, 32, 3 ) - the mnist needs to be preprocessed.它的形状是(28 x 28) ,但是当您的 model 采用 ( 32, 32, 3 ) - mnist需要进行预处理。 Hopefully, you can adapt to your case.希望你能适应你的情况。

DataSet数据集

(x_train, y_train), (_, _) = tf.keras.datasets.mnist.load_data()

print(x_train.shape, y_train.shape)

# expand new axis, channel axis 
x_train = np.expand_dims(x_train, axis=-1)
print(x_train.shape)

# need 3 channel (instead of 1)
x_train = np.repeat(x_train, 3, axis=-1)
print(x_train.shape)

# it's always better to normalize 
x_train = x_train.astype('float32') / 255
print(x_train.shape)

# resize the input shape , i.e. old shape: 28, new shape: 32
x_train = tf.image.resize(x_train, [32,32]) # if we want to resize 
print(x_train.shape)

# train set / target 
y_train = tf.keras.utils.to_categorical(y_train , num_classes=10)
print(y_train.shape)
(60000, 28, 28) (60000,)
(60000, 28, 28, 1)
(60000, 28, 28, 3)
(60000, 28, 28, 3)
(60000, 32, 32, 3)
(60000, 10)

Now, you can train your model.现在,您可以训练您的 model。

log2 = train(model2, x_train , y_train)
Epoch 1/5
4ms/step - loss: 0.2792 - acc: 0.9133 - val_loss: 0.0676 - val_acc: 0.9815
Epoch 2/5
4ms/step - loss: 0.0454 - acc: 0.9864 - val_loss: 0.0400 - val_acc: 0.9883
Epoch 3/5
4ms/step - loss: 0.0336 - acc: 0.9892 - val_loss: 0.0415 - val_acc: 0.9900
Epoch 4/5
4ms/step - loss: 0.0235 - acc: 0.9926 - val_loss: 0.0359 - val_acc: 0.9907
Epoch 5/5
4ms/step - loss: 0.0163 - acc: 0.9948 - val_loss: 0.0295 - val_acc: 0.9918

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM