проблемы при использовании тепловой карты CAM для предварительно обученного CNN - PullRequest
0 голосов
/ 30 марта 2020

Я тренировал CNN для классификации рентгеновских снимков пневмонии и рентгеновских снимков у здоровых людей, затем я попытался использовать карту активации классов, вот код:

import cv2
from keras import backend as K
import numpy as np
from keras.models import load_model  
def visualize_class_activation_map(model_path, img_path, output_path):
    model = load_model(model_path)
    original_img = cv2.imread(img_path, 1)
    width, height, _ = original_img.shape

    #Reshape to the network input shape (3, w, h).
    img = np.array([np.transpose(np.float32(original_img), (2, 0, 1))])

    #Get the 512 input weights to the softmax.
    class_weights = model.layers[-1].get_weights()[0]

    final_conv_layer = model.get_layer('vgg19').get_layer('block3_conv1').output
    get_output = K.function([model.layers[0].input], [final_conv_layer.output, model.layers[-1].output])
    [conv_outputs, predictions] = get_output([img])
    conv_outputs = conv_outputs[0, :, :, :]

    #Create the class activation map.
    cam = np.zeros(dtype = np.float32, shape = conv_outputs.shape[1:3])
    target_class = 1
    for i, w in enumerate(class_weights[:, target_class]):
        cam += w * conv_outputs[i, :, :]

visualize_class_activation_map('covid_t93.h5','/home/workstation/Desktop/covid/1.jpeg','block3_conv1')

теперь, когда я пытаюсь чтобы использовать CAM, я получаю эту ошибку

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-1-bfc49ce56c97> in <module>
     25         cam += w * conv_outputs[i, :, :]
     26 
---> 27 visualize_class_activation_map('covid_t93.h5','/home/workstation/Desktop/covid/1.jpeg','block3_conv1')

<ipython-input-1-bfc49ce56c97> in visualize_class_activation_map(model_path, img_path, output_path)
     15 
     16     final_conv_layer = model.get_layer('vgg19').get_layer('block3_conv1').output
---> 17     get_output = K.function([model.layers[0].input], [final_conv_layer.output, model.layers[-1].output])
     18     [conv_outputs, predictions] = get_output([img])
     19     conv_outputs = conv_outputs[0, :, :, :]

~/anaconda3/envs/tf-gpu/lib/python3.6/site-packages/keras/engine/base_layer.py in input(self)
    782         if len(self._inbound_nodes) > 1:
    783             raise AttributeError('Layer ' + self.name +
--> 784                                  ' has multiple inbound nodes, '
    785                                  'hence the notion of "layer input" '
    786                                  'is ill-defined. '

AttributeError: Layer vgg19 has multiple inbound nodes, hence the notion of "layer input" is ill-defined. Use `get_input_at(node_index)` instead.

Я надеялся, что любой из вас может помочь мне заставить ее работать, я читал об ошибке, но не могу заставить ее работать, я пытался изменить слои [0]. вход для get_inputat (), но я думаю, что я делаю это неправильно, спасибо за чтение

...