Exemplo n.º 1
0
def cam_weighted_image(model, image_path, character_idx):
    pixels = np.load(image_path)['pixels']
    cam = visualize_cam(model,
                        layer_idx=-1,
                        filter_indices=[character_idx],
                        seed_input=pixels)
    return np.uint8(pixels * np.dstack([cam] * 3))
Exemplo n.º 2
0
    def plot(predictions,
             targets,
             images,
             original_images,
             path_info,
             prefix=''):
        for i in range(len(targets)):
            # if i < 140: continue
            file_path = '../results/CAM/{}/{}{}'.format(
                args['sherpa_trial'], prefix, i)

            cam = visualize_cam(model,
                                len(model.layers) - 1, predictions[i],
                                images[i][np.newaxis])

            plt.subplot(1, 2, 1)
            plt.imshow(original_images[i])
            plt.title('Original Image')
            plt.tight_layout()
            plt.axis('off')

            plt.subplot(1, 2, 2)
            plt.imshow(overlay(cam, (original_images[i]) * 255.))
            plt.title('Heat Map')
            plt.tight_layout()
            plt.axis('off')

            plt.suptitle(path_info.iloc[i]['path'].replace(
                '/baldig/physicstest/ValleyFeverDogs/', ''))
            plt.savefig(file_path)
def grad_cam(model, image):
    model.layers[-1].activation = keras.activations.linear
    return visualize_cam(
        model,
        layer_idx=len(model.layers) -
        1,  # modify this as the network changes, must be the last layer
        filter_indices=[1],  # index of importance in the layer, change
        seed_input=image,  # image of interest
        penultimate_layer_idx=147,  # layer prior to the GLOBAL max pooling layer
        backprop_modifier=None,  # no need to modify
        grad_modifier=None)  # no need to modify
Exemplo n.º 4
0
weights = ['java_python.h5'
           ]  #,'java_python_no_code.h5','java_python_pv_no_code.h5']
for weight in weights:
    if weight == 'all_four.h5':
        options = four_options
        model = VGG(shape, 4)
    else:
        options = two_options
        model = VGG(shape, 2)
    # load weights file
    model.load_weights('../../jp_Fold_0/' + weight)
    # make directory for images
    os.mkdir('jp_Images/' + weight.replace('.h5', '/'))
    # predict classes for testing images
    predicitions = model.predict(images)

    # iterate over all the predictions to produce cam
    for i in range(len(predicitions)):
        # get class label from prediction
        code = np.argmax(predicitions[i])
        # label photo
        name = '_Predicted ' + options[code]

        location = 'jp_Images/' + weight.replace('.h5', '/') + str(i) + name
        cam = visualize_cam(model,
                            len(model.layers) - 1, code,
                            images[i].reshape((1, ) + shape))
        img = images[i]
        plt.imshow(overlay(cam, img))
        plt.savefig(location)
Exemplo n.º 5
0
        X_TRAIN, Y_TRAIN, X_TEST, Y_TEST)
    # load weights file
    model.load_weights('../../' + weights)
    # make directory for images
    os.mkdir('Images/' + weights.replace('.h5', '/'))
    # predict classes for testing images
    predicitions = model.predict(x_test)
    # set options
    if len(y_test[0]) > 2:
        options = four_options
    else:
        options = two_options
    # iterate over all the predictions to produce cam
    for i in range(len(predicitions)):
        # get class label from prediction
        code = np.argmax(predicitions[i])
        # label photo as correct/incorrect with label predicted
        if np.argmax(y_test[i]) == code:
            name = 'correct ' + options[code]
        else:
            name = 'Actual ' + options[np.argmax(
                y_test[i])] + ' Predicted ' + options[code]

        location = 'Images/' + weights.replace('.h5', '/') + str(i) + name
        cam = visualize_cam(model,
                            len(model.layers) - 1, code,
                            x_test[i].reshape(1, 300, 300, 3))
        img = x_test[i]
        plt.imshow(overlay(cam, img))
        plt.savefig(location)