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

Python 获取单个图像的Keras模型中的每一层输出,python,tensorflow,keras,deep-learning,neural-network,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.pr

我想知道如何获得预先训练的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)

在这种情况下,我真不知道下一步该怎么打印每一层的输出

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

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')

谢谢这确实非常有用,但是,我的目标是以数组或列表格式获取中间值。我正在寻找每个层的实际数值输出值,包括完全连接的层。我相信您的代码片段主要导致可视化,这不是我现在的目标。@AbdelrahmanHussein请检查我的修订,看看它是否解决了您的问题。