【问题标题】:Get Each Layer Output in Keras Model for a Single Image获取单个图像的 Keras 模型中的每一层输出
【发布时间】:2020-08-06 16:06:27
【问题描述】:

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

在这种情况下,我很困惑接下来要打印每一层的输出!

【问题讨论】:

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


    【解决方案1】:

    可以按照以下步骤收集层的输出:

    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的中间表示可以通过运行以下代码sn-p来复制:

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

    【讨论】:

    • 谢谢。这肯定非常有用,但是,我的目标是获取数组或列表格式的中间值。我正在寻找每一层的实际数值输出值,包括全连接层。我相信你的代码 sn-p 主要是导致可视化,这不是我现在的目标。
    • @AbdelrahmanHussein 请检查我的修订版,看看它是否能解决您的问题。
    猜你喜欢
    • 2017-05-25
    • 2019-02-05
    • 2019-03-28
    • 1970-01-01
    • 2020-08-09
    • 2019-03-18
    • 2017-11-21
    • 2017-06-02
    • 2019-01-27
    相关资源
    最近更新 更多