修改神经网络以一次预测 3 个示例
Modify Neural Network to Predict on 3 examples at a time
我这里有一个卷积神经网络,它采用 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
已编辑:如 op 所述,网络应一次处理 3 张图像,因为数据集的形状为 (m,3,96,96,3),然后一种简单的方法是在中创建三个标准初始网络并行方式,然后连接从每个接收到的输出。
你不需要修改任何东西,Keras中输入的第一个维度始终是batch维度,所以你实际上需要输入一个形状为(3, 96, 96, 3)
的输入,你会得到一个相应的输出(3, 128)
个。无需修改代码。
如果您想获取形状为 (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)
.
我这里有一个卷积神经网络,它采用 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
已编辑:如 op 所述,网络应一次处理 3 张图像,因为数据集的形状为 (m,3,96,96,3),然后一种简单的方法是在中创建三个标准初始网络并行方式,然后连接从每个接收到的输出。
你不需要修改任何东西,Keras中输入的第一个维度始终是batch维度,所以你实际上需要输入一个形状为(3, 96, 96, 3)
的输入,你会得到一个相应的输出(3, 128)
个。无需修改代码。
如果您想获取形状为 (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)
.