如何在训练后使用其实际权重从 cnn 模型中提取特征?
How to extract features from a cnn model using its actuall weights after training?
首先,我在 Cifar10 上训练了 Alexnet,准确率达到了 80%。但是,我想使用给出 80% 准确度的权重从最后一个丢失层中提取特征。这是型号
Alexnet=keras.Sequential([
keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu', padding="same", input_shape=(32,32,3)),
keras.layers.BatchNormalization(),
keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu'),
keras.layers.MaxPool2D(pool_size=(2,2)),
keras.layers.Dropout(0.2),
keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding="same"),
keras.layers.BatchNormalization(),
keras.layers.Conv2D(filters=64, kernel_size=(1,1), activation='relu'),
keras.layers.BatchNormalization(),
keras.layers.MaxPool2D(pool_size=(2,2)),
keras.layers.Dropout(0.2),
keras.layers.Flatten(),
keras.layers.Dense(1024,activation='relu'),
keras.layers.Dropout(0.2),
keras.layers.Dense(10, activation='softmax')
])
这就是我想从最后一个 dropout 层中提取特征(输出)的方式
feature_extractor = keras.Model(
inputs=Alexnet.inputs,
outputs=Alexnet.get_layer(name="dropout_2").output,
)
我想在训练后使用模型的权重来执行此操作。有人可以帮我吗?
提前致谢,
您可以按如下方式构建模型:
inputs = keras.layers.Input(shape = (32,32,3))
x = keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu',
padding="same")(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu')(x)
x = keras.layers.MaxPool2D(pool_size=(2,2))(x)
x = keras.layers.Dropout(0.2)(x)
x = keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu',
padding="same")(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Conv2D(filters=64, kernel_size=(1,1), activation='relu')(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.MaxPool2D(pool_size=(2,2))(x)
x = keras.layers.Dropout(0.2)(x)
x = keras.layers.Flatten()(x)
x = keras.layers.Dense(1024,activation='relu')(x)
x = keras.layers.Dropout(0.2)(x)
intermediary_model = keras.Model(inputs, x)
x = keras.layers.Dense(10, activation='softmax')(x)
model = keras.Model(inputs,x)
然后您只训练最终模型,intermediary_model 将自动学习与最终模型相同的权重。并且可以通过intermediary_model.predict(some_input)
访问到你想要的feature map
首先,我在 Cifar10 上训练了 Alexnet,准确率达到了 80%。但是,我想使用给出 80% 准确度的权重从最后一个丢失层中提取特征。这是型号
Alexnet=keras.Sequential([
keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu', padding="same", input_shape=(32,32,3)),
keras.layers.BatchNormalization(),
keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu'),
keras.layers.MaxPool2D(pool_size=(2,2)),
keras.layers.Dropout(0.2),
keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding="same"),
keras.layers.BatchNormalization(),
keras.layers.Conv2D(filters=64, kernel_size=(1,1), activation='relu'),
keras.layers.BatchNormalization(),
keras.layers.MaxPool2D(pool_size=(2,2)),
keras.layers.Dropout(0.2),
keras.layers.Flatten(),
keras.layers.Dense(1024,activation='relu'),
keras.layers.Dropout(0.2),
keras.layers.Dense(10, activation='softmax')
])
这就是我想从最后一个 dropout 层中提取特征(输出)的方式
feature_extractor = keras.Model(
inputs=Alexnet.inputs,
outputs=Alexnet.get_layer(name="dropout_2").output,
)
我想在训练后使用模型的权重来执行此操作。有人可以帮我吗?
提前致谢,
您可以按如下方式构建模型:
inputs = keras.layers.Input(shape = (32,32,3))
x = keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu',
padding="same")(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu')(x)
x = keras.layers.MaxPool2D(pool_size=(2,2))(x)
x = keras.layers.Dropout(0.2)(x)
x = keras.layers.Conv2D(filters=64, kernel_size=(3,3), activation='relu',
padding="same")(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.Conv2D(filters=64, kernel_size=(1,1), activation='relu')(x)
x = keras.layers.BatchNormalization()(x)
x = keras.layers.MaxPool2D(pool_size=(2,2))(x)
x = keras.layers.Dropout(0.2)(x)
x = keras.layers.Flatten()(x)
x = keras.layers.Dense(1024,activation='relu')(x)
x = keras.layers.Dropout(0.2)(x)
intermediary_model = keras.Model(inputs, x)
x = keras.layers.Dense(10, activation='softmax')(x)
model = keras.Model(inputs,x)
然后您只训练最终模型,intermediary_model 将自动学习与最终模型相同的权重。并且可以通过intermediary_model.predict(some_input)
访问到你想要的feature map