我有一个训练有素的cnn模型。我试图从每个卷积层提取输出并绘制结果以探索图像的哪些区域具有高激活。关于如何做到这一点的任何想法?
以下是我训练过的网络。
input_shape = (3,227,227)
x = Input(input_shape)
# Conv Layer 1
x = Convolution2D(96, 7,7,subsample=(4,4),activation='relu',
name='conv_1', init='he_normal')(x_input)
x = MaxPooling2D((3, 3), strides=(2,2), name='maxpool')(x)
x = BatchNormalization()(x)
x = ZeroPadding2D((2,2))(x)
# Conv Layer 2
x = Convolution2D(256, 5,5,activation='relu',name='conv_2', init='he_normal')(x)
x = MaxPooling2D((3, 3), strides=(2,2),name='maxpool2')(x)
x = BatchNormalization()(x)
x = ZeroPadding2D((2,2))(x)
# Conv Layer 3
x = Convolution2D(384, 3,3,activation='relu',
name='conv_3', init='he_normal')(x)
x = MaxPooling2D((3, 3), strides=(2,2),name='maxpool3')(x)
x = Flatten()(x)
x = Dense(512, activation = "relu")(x)
x = Dropout(0.5)(x)
x = Dense(512, activation ="relu")(x)
x = Dropout(0.5)(x)
predictions = Dense(2, activation="softmax")(x)
model = Model(inputs = x_input, outputs = predictions)
谢谢!
答案 0 :(得分:1)
请查看此GitHub issue和常见问题解答How can I obtain the output of an intermediate layer?。似乎最简单的方法是使用您想要的输出定义新模型。例如:
onSubmit(f:NgForm)
{
if(f.valid){
this._styleService.addCrop(new CropModel(this.cropModel.bottomMargin,
this.cropModel.leftMargin,
this.cropModel.rightMargin,
this.cropModel.topMargin,
this.cropModel.styleId,
this.cropModel.documentType));
f.reset();
}
}
请注意,由于您在两个模型中使用相同的对象,因此权重会自动在它们之间共享。
可以找到更完整的激活可视化示例here。在这种情况下,他们使用input_shape = (3,227,227)
x = Input(input_shape)
# Conv Layer 1
# Save layer in a variable
conv1 = Convolution2D(96, 7, 7, subsample=(4,4), activation='relu',
name='conv_1', init='he_normal')(x_input)
x = conv1
x = MaxPooling2D(...)(x)
# ...
conv2 = Convolution2D(...)(x)
x = conv2
# ...
conv3 = Convolution2D(...)(x)
x = conv3
# ...
predictions = Dense(2, activation="softmax")(x)
# Main model
model = Model(inputs=x_input, outputs=predictions)
# Intermediate evaluation model
conv_layers_model = Model(inputs=x_input, outputs=[conv1, conv2, conv3])
# After training is done, retrieve intermediate evaluations for data
conv1_val, conv2_val, conv3_val = conv_layers_model.predict(data)
方法。