def predict(config):
    # load json and create model
    json_file = open(config['output_model_path'], 'r')
    loaded_model_json = json_file.read()
    json_file.close()
    loaded_model = model_from_json(loaded_model_json)
    # load weights into new model
    loaded_model.load_weights(config['output_weight_path'])
    print("Loaded model from disk")

    #sgd = SGD(lr=1e-3, decay=1e-6, momentum=0.9, nesterov=True)
    #loaded_model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])

    new_model = to_heatmap(loaded_model)

    img = image.load_img('COLOR_MAP_0_C_A.png', target_size=(350, 150))
    #im = preprocess_image_batch(['examples/3.png'], color_mode="bgr")
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    #x = preprocess_input(x)

    out = new_model.predict(x)

    heatmap0 = out[0, 0]
    heatmap1 = out[0, 1]
    heatmap2 = out[0, 2]
    heatmap3 = out[0, 3]

    plt.imsave("heatmap0.png", heatmap0)
    plt.imsave("heatmap1.png", heatmap1)
    plt.imsave("heatmap2.png", heatmap2)
    plt.imsave("heatmap3.png", heatmap3)
Example #2
0
def calc_masked_image(image_filename, object_name, mask_threshold=0.5):
    """

    :param image_filename:
    :param object_name:
    :param mask_threshold:
    :return: masked_image, mask, fig, axs
    """

    from heatmap.imagenet1000_clsid_to_human import get_imagenet_classes_from_names

    class_ids = get_imagenet_classes_from_names()

    # model

    class_id = class_ids[object_name]
    model = ResNet50()
    new_model = to_heatmap(model)

    # calc

    original_image = imread(image_filename)
    image = image_proc.load_img(image_filename,
                                target_size=(IMAGE_HEIGHT, IMAGE_WIDTH))
    heatmap = calculate_heatmap(original_image, new_model, image, class_id,
                                preprocess_input)

    #

    masked_image, mask = analyse_heatmap(threshold, original_image, heatmap)
    fig, axs = display_graphical_results(original_image, heatmap, masked_image)

    return masked_image, mask, fig, axs
Example #3
0
def helper_test(model):
    img_path = "../examples/dog.jpg"
    new_model = to_heatmap(model)

    # Loading the image
    img = image.load_img(img_path, target_size=(800, 800))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    x = preprocess_input(x)

    out = new_model.predict(x)

    s = "n02084071"  # Imagenet code for "dog"
    ids = synset_to_dfs_ids(s)
    heatmap = out[0]
    if K.image_data_format() == 'channels_first':
        heatmap = heatmap[ids]
        heatmap = np.sum(heatmap, axis=0)
    else:
        heatmap = heatmap[:, :, ids]
        heatmap = np.sum(heatmap, axis=2)
    print(heatmap.shape)
    assert heatmap.shape[0] == heatmap.shape[1]
    K.clear_session()
Example #4
0
    # If you have more than 8GB of RAM, you can try to increase it.
    img = image.load_img(img_path, target_size=(800, 1280))
    x = image.img_to_array(img)
    x = np.expand_dims(x, axis=0)
    if preprocessing is not None:
        x = preprocess_input(x)

    out = new_model.predict(x)

    heatmap = out[0]  # Removing batch axis.

    if K.image_data_format() == 'channels_first':
        heatmap = heatmap[ids]
        if heatmap.ndim == 3:
            heatmap = np.sum(heatmap, axis=0)
    else:
        heatmap = heatmap[:, :, ids]
        if heatmap.ndim == 3:
            heatmap = np.sum(heatmap, axis=2)

    plt.imshow(heatmap, interpolation="none")
    plt.show()


model = VGG16()
new_model = to_heatmap(model)

s = "n02084071"  # Imagenet code for "dog"
ids = synset_to_dfs_ids(s)
display_heatmap(new_model, "./dog.jpg", ids, preprocess_input)
Example #5
0
    if K.image_data_format() == 'channels_first':
        heatmap = heatmap[ids]
        if heatmap.ndim == 3:
            heatmap = np.sum(heatmap, axis=0)
    else:
        heatmap = heatmap[:, :, ids]
        if heatmap.ndim == 3:
            heatmap = np.sum(heatmap, axis=2)

    plt.imshow(heatmap, interpolation="none")
    plt.show()


model = VGG16()
new_model = to_heatmap(model)

s = "n02084071"  # Imagenet code for "dog"
ids = synset_to_dfs_ids(s)
display_heatmap(new_model, "./dog.jpg", ids, preprocess_input)

# Also testing with a sequential model
sequential_model = Sequential()

sequential_model.add(
    Conv2D(64, (3, 3),
           activation='relu',
           padding='same',
           input_shape=(224, 224, 3)))
sequential_model.add(Conv2D(64, (3, 3), activation='relu', padding='same'))
sequential_model.add(MaxPooling2D((2, 2), strides=(2, 2)))