Example #1
0
def visualize_top_images(layer, f, constrast):
    """
    Visualize the activating pixels of the 9 images that maximally activate a given filter in a layer
    """
    conv_base_model = AlexNet().model
    get_from_folder = 'Layer{}_Strongest_max_IMG'.format(layer)
    save_to_folder = 'Layer{}_Projections_and_Images'.format(layer)
    if not os.path.exists(save_to_folder):
        os.makedirs(save_to_folder)

    for t in range(1, 10):
        file_name = '/Layer{}_Filter{}_Top{}.JPEG'.format(layer, f, t)

        projection = Deconvolution(conv_base_model).project_down(
            get_from_folder + file_name, layer, f)
        original_image = preprocess_image_batch(get_from_folder + file_name)

        activation_filename = save_to_folder + '/Layer{}_Filter{}_Top{}_Activations.JPEG'.format(
            layer, f, t)
        if os.path.exists(activation_filename):
            os.remove(activation_filename)
        DeconvOutput(projection,
                     constrast).save_as(filename=activation_filename)

        original_filename = save_to_folder + '/Layer{}_Filter{}_Top{}.JPEG'.format(
            layer, f, t)
        if os.path.exists(original_filename):
            os.remove(original_filename)
        DeconvOutput(original_image).save_as(filename=original_filename)
Example #2
0
def project_top_layer_filters(img_id=None, deconv_base_model=None):
    if img_id is None:
        img_id = randint(1, 50000)
    if deconv_base_model is None:
        deconv_base_model = Deconvolution(AlexNet().model)

    path = get_path_from_id(img_id)
    save_to_folder = 'TopFilters'

    projections = []
    box_borders = []
    layer = 5
    for max_filter in get_strongest_filters(img_id, layer, top=3):
        projection = deconv_base_model.project_down(path, layer, max_filter)

        # Increase Contrast
        percentile = 99
        max_val = np.percentile(projection, percentile)
        projection *= (20 / max_val)
        box_borders.append(get_bounding_box_coordinates(projection))
        projections.append(projection)

    superposed_projections = np.maximum.reduce(projections)
    # superposed_projections = sum(projections)
    assert superposed_projections.shape == projections[0].shape

    DeconvOutput(superposed_projections).save_as(
        save_to_folder, '{}_activations.JPEG'.format(img_id))

    original_image = preprocess_image_batch(path)
    original_image = draw_bounding_box(original_image, box_borders)
    DeconvOutput(original_image).save_as(save_to_folder,
                                         '{}.JPEG'.format(img_id))
Example #3
0
def project_complete_image(layer, file_name='Example_JPG/Elephant.jpg'):
    conv_base_model = AlexNet().model

    projection = Deconvolution(conv_base_model).project_down(file_name,
                                                             layer=layer,
                                                             use_bias=True)
    original_image = preprocess_image_batch(file_name)

    activation_filename = 'test.JPEG'
    DeconvOutput(projection).save_as(filename=activation_filename)

    original_filename = 'test_original.JPEG'
    DeconvOutput(original_image).save_as(filename=original_filename)
Example #4
0
def project_multiple_layer_filters(img_id=None, deconv_base_model=None):
    if img_id is None:
        img_id = randint(1, 50000)
    if deconv_base_model is None:
        deconv_base_model = Deconvolution(AlexNet().model)

    path = get_path_from_id(img_id)
    save_to_folder = 'MultipleLayers'

    projections = []
    box_borders = []
    contrast = [None, 1, 3, 7, 13, 22]
    for layer in (5, ):  # 1, 2, 3, 4,
        assert get_strongest_filter(img_id,
                                    layer) == get_strongest_filters(img_id,
                                                                    layer,
                                                                    top=1)
        max_filter = get_strongest_filter(img_id, layer)
        if layer == 1: print(img_id, ': ', max_filter)
        projection = deconv_base_model.project_down(
            path, layer, max_filter)  # 某一层的特征图解卷积后的重构图

        if layer != 1:
            # 提升对比度
            percentile = 99
            # max_val = np.nanargmax(unarranged_array)
            max_val = np.percentile(
                projection, percentile
            )  # 百分位数: 具体参见 stackflow.com/questions/2374640/how-do-i-calculate-percentiles-with-python-numpy
            projection *= (contrast[layer] / max_val)
        else:
            projection *= 0.3

        box_borders.append(get_bounding_box_coordinates(projection))

        # x_diff[layer].append(box_borders[-1][1] - box_borders[-1][0])
        # y_diff[layer].append(box_borders[-1][3] - box_borders[-1][2])

        projections.append(projection)
    superposed_projections = np.maximum.reduce(projections)
    # superposed_projections = sum(projections)
    assert superposed_projections.shape == projections[0].shape
    DeconvOutput(superposed_projections).save_as(
        save_to_folder, '{}_activations.JPEG'.format(img_id))

    original_image = preprocess_image_batch(path)
    original_image = draw_bounding_box(original_image, box_borders)
    DeconvOutput(original_image).save_as(save_to_folder,
                                         '{}.JPEG'.format(img_id))