jsmacifar.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:AdversarialMachineLearning_COMP551 作者: arunrawlani 项目源码 文件源码
def cnn_model(logits=False, input_ph=None, img_rows=28, img_cols=28,
              channels=1, nb_filters=64, nb_classes=10):
    """
        Defines a CNN model using Keras sequential model
        :param logits: If set to False, returns a Keras model, otherwise will also
        return logits tensor
        :param input_ph: The TensorFlow tensor for the input
        (needed if returning logits)
        ("ph" stands for placeholder but it need not actually be a
        placeholder)
        :param img_rows: number of row in the image
        :param img_cols: number of columns in the image
        :param channels: number of color channels (e.g., 1 for MNIST)
        :param nb_filters: number of convolutional filters per layer
        :param nb_classes: the number of output classes
        :return:
        """
    model = Sequential()

    # Define the layers successively (convolution layers are version dependent)
    if keras.backend.image_dim_ordering() == 'th':
        input_shape = (channels, img_rows, img_cols)
    else:
        input_shape = (img_rows, img_cols, channels)

    layers = [Dropout(0.2, input_shape=input_shape),
              conv_2d(nb_filters, (8, 8), (2, 2), "same"),
              Activation('relu'),
              conv_2d((nb_filters * 2), (6, 6), (2, 2), "valid"),
              Activation('relu'),
              conv_2d((nb_filters * 2), (5, 5), (1, 1), "valid"),
              Activation('relu'),
              Dropout(0.5),
              Flatten(),
              Dense(nb_classes)]

    for layer in layers:
        model.add(layer)

    if logits:
        logits_tensor = model(input_ph)
    model.add(Activation('softmax'))

    if logits:
        return model, logits_tensor
    else:
        return model
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号