在Keras模型中获取单个图像的每个图层输出

时间:2020-08-06 16:06:27

标签: python tensorflow keras deep-learning neural-network

我想知道如何获得预训练的CNN Keras模型每一层的输出。我正在做的是获取模型中每一层的中间输出,这些中间输出与我要提供给模型的特定图像相关联。这是我所做的:

model = load_model('model.h5')
img = Image.open('img.jpg')
img_array = np.array (img)
img_array = img_array/255
img_array = img_array.reshape(-1,512,512,1)
pred = model.predict(img_array)

在这种情况下,我对于下一步如何打印每一层的输出感到很困惑!

1 个答案:

答案 0 :(得分:0)

可以通过以下步骤收集图层的输出:

from keras import backend as K 

model = load_model('model.h5')

inp = model.input                                         # input placeholder
out = [layer.output for layer in model.layers]            # all layer outputs
get_outputs = K.function([inp, K.learning_phase()], out)   

img = load_img('img.jpg')
x = img_to_array(img)
x = x.reshape((1,) + x.shape) 
x /= 255.
layer_outs = get_outputs([x, 1.])                                                
print(layer_outs)

可以通过运行以下代码段来复制输入图像img.jpg的中间表示形式:

from tensorflow.keras.preprocessing.image import img_to_array, load_img

model = load_model('model.h5')

# Define a new Model that will take an image as input, and will output 
# intermediate representations for all layers except the first layer.
layer_outputs = [layer.output for layer in model.layers[1:]]
visual_model = tf.keras.models.Model(inputs = model.input, outputs = layer_outputs)

# Read your image
img = load_img('img.jpg')
x = img_to_array(img)
x = x.reshape((1,) + x.shape) # add one extra dimension to the front
x /= 255. # rescale by 1/255.

# run your image through the network; make a prediction
feature_maps = visual_model.predict(x)

# Plotting intermediate representations for your image

# Collect the names of each layer except the first one for plotting
layer_names = [layer.name for layer in model.layers[1:]]

# Plotting intermediate representation images layer by layer
for layer_name, feature_map in zip(layer_names, feature_maps):
    if len(feature_map.shape) == 4: # skip fully connected layers
        # number of features in an individual feature map
        n_features = feature_map.shape[-1]  
        # The feature map is in shape of (1, size, size, n_features)
        size = feature_map.shape[1] 
        # Tile our feature images in matrix `display_grid
        display_grid = np.zeros((size, size * n_features))
        # Fill out the matrix by looping over all the feature images of your image
        for i in range(n_features):
            # Postprocess each feature of the layer to make it pleasible to your eyes
            x = feature_map[0, :, :, i]
            x -= x.mean()
            x /= x.std()
            x *= 64
            x += 128
            x = np.clip(x, 0, 255).astype('uint8')
            # We'll tile each filter into this big horizontal grid
            display_grid[:, i * size : (i + 1) * size] = x
        # Display the grid
        scale = 20. / n_features
        plt.figure(figsize=(scale * n_features, scale))
        plt.title(layer_name)
        plt.grid(False)
        plt.imshow(display_grid, aspect='auto', cmap='viridis')