简体   繁体   中英

Extract activations of a VGG16 model

I'm using Tensorflow 2.0 and a pre-trained VGG16 model. I would like to visualize the activations. Therefore I want to extract them.

Currently I'm doing the following:

model = tf.keras.applications.VGG16(input_shape=(224, 224, 3), weights='imagenet')
model.outputs = [layer.output for layer in model.layers]
model.build(input_shape=(1, 224, 224 ,3))
activations = model(image_data)

However, I'm getting the following error when I try to call the last line:

ValueError: Structure is a scalar but len(flat_sequence) == 23 > 1

Would this approach: https://machinelearningmastery.com/how-to-visualize-filters-and-feature-maps-in-convolutional-neural-networks/

work for TF 2.0?

from keras.applications.vgg16 import VGG16
from matplotlib import pyplot
# load the model
model = VGG16()
# retrieve weights from the second hidden layer
filters, biases = model.layers[1].get_weights()
# normalize filter values to 0-1 so we can visualize them
f_min, f_max = filters.min(), filters.max()
filters = (filters - f_min) / (f_max - f_min)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM