python Keras建立CNN模型的模板

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python Keras建立CNN模型的模板相关的知识,希望对你有一定的参考价值。

import keras.backend as K
K.set_image_data_format('channels_last')

def Model(input_shape):
    # Define the input placeholder as a tensor with shape input_shape. Think of this as your input image!
    X_input = Input(input_shape)

    # Zero-Padding: pads the border of X_input with zeroes
    X = ZeroPadding2D((3, 3))(X_input)

    # CONV -> BN -> RELU Block applied to X
    X = Conv2D(32, (7, 7), strides = (1, 1), name = 'conv0')(X)
    X = BatchNormalization(axis = 3, name = 'bn0')(X)
    X = Activation('relu')(X)

    # MAXPOOL
    X = MaxPooling2D((2, 2), name='max_pool')(X)

    # FLATTEN X (means convert it to a vector) + FULLYCONNECTED
    X = Flatten()(X)
    X = Dense(1, activation='sigmoid', name='fc')(X)

    # Create model. This creates your Keras model instance, you'll use this instance to train/test the model.
    model = Model(inputs = X_input, outputs = X, name='HappyModel')

    return model
    
model = Model(input_shape=(64, 64, 3))
model.compile(optimizer='adam', loss='binary_crossentropy', metrics=["accuracy"])
model.fit(x=X_train, y=Y_train, batch_size=16, epochs=10) # (600, 64, 64, 3), (600, 1)
preds = model.evaluate(x=X_test, y=Y_test)

以上是关于python Keras建立CNN模型的模板的主要内容,如果未能解决你的问题,请参考以下文章

莫凡Python 3

验证码是怎么被机器识别的?Keras+CNN模型验证码识别详解

对比学习用 Keras 搭建 CNN RNN 等常用神经网络

TensorFlow学习笔记--- 使用CPABD实现最简单的CNN模型

人脸检测及识别python实现系列——卷积神经网络(CNN)入门

Traffic Signs Recognition with 95% Accuracy using CNN&Keras