【问题标题】:Modify Neural Network to Predict on 3 examples at a time修改神经网络以一次预测 3 个示例
【发布时间】:2019-08-20 06:10:57
【问题描述】:

我这里有一个卷积神经网络,它采用 96x96x3 图片并输出 1x128 编码。 (未定义的函数只是一系列的层)

如何修改架构以获取 3x96x96x3 输入并产生 3x128 输出(使用相同参数前向传播 3 次)?

def faceRecoModel(input_shape):
    """
    Implementation of the Inception model used for FaceNet

    Arguments:
    input_shape -- shape of the images of the dataset

    Returns:
    model -- a Model() instance in Keras
    """

    # Define the input as a tensor with shape input_shape
    X_input = Input(input_shape)

    # Zero-Padding
    X = ZeroPadding2D((3, 3))(X_input)

    # First Block
    X = Conv2D(64, (7, 7), strides=(2, 2), name='conv1')(X)
    X = BatchNormalization(axis=1, name='bn1')(X)
    X = Activation('relu')(X)

    # Zero-Padding + MAXPOOL
    X = ZeroPadding2D((1, 1))(X)
    X = MaxPooling2D((3, 3), strides=2)(X)

    # Second Block
    X = Conv2D(64, (1, 1), strides=(1, 1), name='conv2')(X)
    X = BatchNormalization(axis=1, epsilon=0.00001, name='bn2')(X)
    X = Activation('relu')(X)

    # Zero-Padding + MAXPOOL
    X = ZeroPadding2D((1, 1))(X)

    # Second Block
    X = Conv2D(192, (3, 3), strides=(1, 1), name='conv3')(X)
    X = BatchNormalization(axis=1, epsilon=0.00001, name='bn3')(X)
    X = Activation('relu')(X)

    # Zero-Padding + MAXPOOL
    X = ZeroPadding2D((1, 1))(X)
    X = MaxPooling2D(pool_size=3, strides=2)(X)

    # Inception 1: a/b/c
    X = inception_block_1a(X)
    X = inception_block_1b(X)
    X = inception_block_1c(X)

    # Inception 2: a/b
    X = inception_block_2a(X)
    X = inception_block_2b(X)

    # Inception 3: a/b
    X = inception_block_3a(X)
    X = inception_block_3b(X)

    # Top layer
    X = AveragePooling2D(pool_size=(3, 3), strides=(1, 1), data_format='channels_first')(X)
    X = Flatten()(X)
    X = Dense(128, name='dense_layer')(X)

    # L2 normalization
    X = Lambda(lambda x: K.l2_normalize(x, axis=1))(X)

    # Create model instance
    model = Model(inputs=X_input, outputs=X, name='FaceRecoModel')

    return model

【问题讨论】:

  • Keras 中的模型采用 batch 个输入样本,因此您可以为模型提供尽可能多的输入样本。
  • 我希望网络一次处理 3 个图像,因此数据集的形状为 (m,3,96,96,3)

标签: python machine-learning keras conv-neural-network tensor


【解决方案1】:

如果您想获取形状为 (batch_size, 3, 96, 96, 3) 的输入,即将 3 张图像作为单个输入样本处理,那么您需要创建一个新模型,该模型使用您经过训练的模型并将其分别应用于这 3 张图像中的每一张。您可以使用 Keras 中的 TimeDistributed 包装器轻松实现此目的:

from keras.layers import TimeDistributed

inp = Input(shape=(3, 96, 96, 3))
out = TimeDistributed(the_trained_face_rec_model)(inp)

model = Model(inp, out)

这个新模型的输出形状为(batch_size, 3, 128)。无需编译或训练这个新模型,因为它只是您先前训练的模型的包装。所以你可以使用:predictions = model.predict(my_images)

【讨论】:

    【解决方案2】:

    您不需要修改任何内容,Keras 中输入的第一个维度始终是批处理维度,因此您实际上需要输入形状为 (3, 96, 96, 3) 的输入,您将得到对应的输出 (3, 128) .无需修改代码。

    【讨论】:

      【解决方案3】:

      已编辑:正如 op 所述,网络应一次处理 3 张图像,因为数据集的形状为 (m,3,96,96,3),那么一种简单的方法是并行创建三个标准初始网络时尚,然后连接从他们每个人收到的输出。

      【讨论】:

      • 这可行,但我无法使用 keras fit 函数,因为我的损失函数需要所有 3 个输出,而且微积分很难:(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      • 2018-04-28
      • 1970-01-01
      • 2017-12-02
      • 1970-01-01
      • 2012-05-06
      • 2017-12-28
      相关资源
      最近更新 更多