コード例 #1
0
ファイル: train_util.py プロジェクト: sjjdd/segtran
def visualize_model(net, vis_mode, dataset=None):
    remove_norms(net, 'net')
    input_shape = [1152, 1152, 3]
    rf = PytorchReceptiveField(lambda: net)
    if dataset is None:
        probe_image = get_default_image(input_shape, name='cat')
        probe_mask  = None
    else:
        probe_image_dict = dataset[24]
        probe_image = probe_image_dict['image']
        probe_mask  = probe_image_dict['mask']
        test_patch = F.interpolate(test_patch, size=patch_size,
                                   mode='bilinear', align_corners=False)
                                       
    rf_params = rf.compute(input_shape = input_shape)
    
    '''
    # plot receptive fields
    rf.plot_rf_grids(
        custom_image=cat_image, 
        figsize=(20, 12), 
        layout=(1, 3))
    '''
    for i in range(net.num_vis_layers):
        rf.plot_gradient_at(fm_id=i, point=(16, 16), image=probe_image, figsize=(7, 7))
    plt.show()
コード例 #2
0
def test_example_network():
    input_shape = [120, 120, 3]
    rf = PytorchReceptiveField(model_fn)
    rf_params = rf.compute(input_shape=ImageShape(*input_shape))

    assert_allclose(rf_params[0].rf.size, (6, 6))
    assert_allclose(rf_params[0].rf.stride, (2, 2))

    rs = 6 + (2 + 2 + 1) * 2
    assert_allclose(rf_params[1].rf.size, (rs, rs))
    assert_allclose(rf_params[1].rf.stride, (4, 4))

    rs = 6 + (2 + 2 + 1) * 2 + (2 + 2 + 1) * 4
    assert_allclose(rf_params[2].rf.size, (rs, rs))
    assert_allclose(rf_params[2].rf.stride, (8, 8))

    #rf.plot_gradient_at(fm_id=1, point=(9, 9))
    rf.plot_rf_grids(get_default_image(input_shape, name='cat'), plot_naive_rf=False, figsize=(6, 6))
    plt.show()
コード例 #3
0
def visualize_model(net, vis_mode, vis_layers, patch_size, dataset=None):
    remove_norms(net, 'net')
    input_shape = list(patch_size) + [3]
    rf = PytorchReceptiveField(lambda: net)
    if dataset is None:
        probe_image = get_default_image(input_shape, name='cat')
        probe_mask  = None
    else:
        probe_image_dict = dataset[24]
        probe_image = probe_image_dict['image']
        probe_mask  = probe_image_dict['mask']
        image_path  = probe_image_dict['image_path']
        print(image_path)
        # probe_image: np.array (576, 576, 3)
        # probe_mask:  np.array (3, 576, 576)
        probe_image = cv2.resize(probe_image, patch_size)

    rf_params = rf.compute(input_shape = input_shape)

    '''
    # plot receptive fields
    rf.plot_rf_grids(
        custom_image=cat_image,
        figsize=(20, 12),
        layout=(1, 3))
    '''

    # visualize all layers
    if vis_layers is None:
        vis_layers = list(range(net.num_vis_layers))

    for i in vis_layers:
        feat_size = net.feature_maps[i].shape[2:]
        center = (min(feat_size[0]//2, 128), min(feat_size[1]//2, 128))
        print(net.feature_maps[i].shape, center)
        rf.plot_gradient_at(fm_id=i, point=center, image=probe_image, figsize=(7, 7))

    plt.show()
コード例 #4
0
def get_test_image(shape=(64, 64), tile_factor=0):
    image = get_default_image(shape=shape, tile_factor=tile_factor)
    return image
コード例 #5
0
def plot_receptive_grid(input_shape: GridShape,
                        output_shape: GridShape,
                        rf_params: ReceptiveFieldDescription,
                        custom_image: Optional[np.ndarray] = None,
                        plot_naive_rf: bool = False,
                        axis: Optional[Any] = None,
                        **plot_params) -> None:
    """
    Visualize receptive field grid.

    :param input_shape: an input image shape as an instance of GridShape
    :param output_shape: an output feature map shape
    :param rf_params: an instance of ReceptiveFieldDescription computed for
        this feature map.
    :param custom_image: optional image [height, width, 3] to be plotted as
        a background.
    :param plot_naive_rf: plot naive version of the receptive field. Naive
        version of RF does not take strides, and offsets into considerations,
        it is a simple linear mapping from N points in feature map to pixels
        in the image.
    :param axis: a matplotlib axis object as returned by the e.g. plt.subplot
        function. If not None then axis is used for visualizations otherwise
        default figure is created.
    :param plot_params: additional plot params: figsize=(5, 5)
    """
    if custom_image is None:
        img = get_default_image(shape=ImageShape(input_shape.h, input_shape.w))
    else:
        img = custom_image

    figsize = plot_params.get("figsize", (10, 10))

    # plot image
    if axis is None:
        plt.figure(figsize=figsize)
        axis = plt.subplot(111)

    axis.imshow(img)
    # plot naive receptive field grid
    if plot_naive_rf:
        dw = input_shape.w / output_shape.w
        dh = input_shape.h / output_shape.h
        for i, j in itertools.product(range(output_shape.w),
                                      range(output_shape.h)):
            x0, x1 = i * dw, (i + 1) * dw
            y0, y1 = j * dh, (j + 1) * dh

            axis.add_patch(
                patches.Rectangle(
                    (y0, x0),
                    dh,
                    dw,
                    alpha=0.9,
                    fill=False,
                    edgecolor="gray",
                    linewidth=1,
                ))

    rf_offset = rf_params.offset
    rf_size = rf_params.size
    rf_stride = rf_params.stride

    # map from output grid space to input image
    def map_point(i: int, j: int):
        return np.array(rf_offset) + np.array([i, j]) * np.array(rf_stride)

    # plot RF grid based on rf params
    '''
    points = [
        map_point(i, j)
        for i, j in itertools.product(range(output_shape.w), range(output_shape.h))
    ]

    points = np.array(points)
    axis.scatter(points[:, 1], points[:, 0], marker="o", c=(0.2, 0.9, 0.1, 0.9), s=10)
    '''

    # plot receptive field from corner point
    _plot_rect(
        axis,
        rect=to_rf_rect(rf_offset, rf_size),
        color=(0.9, 0.3, 0.2),
        linewidth=2,
        size=10,
    )
    center_point = map_point(output_shape.w // 2, output_shape.h // 2)
    _plot_rect(
        axis,
        rect=to_rf_rect(GridPoint(center_point[0], center_point[1]), rf_size),
        color=(0.1, 0.3, 0.9),
        linewidth=2,
        size=10,
    )
    last_point = map_point(output_shape.w - 1, output_shape.h - 1)
    _plot_rect(
        axis,
        rect=to_rf_rect(GridPoint(last_point[0], last_point[1]), rf_size),
        color=(0.1, 0.9, 0.3),
        linewidth=2,
        size=10,
    )
    axis.set_aspect("equal")
コード例 #6
0
ファイル: plotting.py プロジェクト: zxyang521/receptivefield
def plot_receptive_grid(input_shape: GridShape,
                        output_shape: GridShape,
                        rf_params: ReceptiveFieldDescription,
                        custom_image: np.ndarray = None,
                        plot_naive_rf: bool = False,
                        **plot_params) -> None:

    if custom_image is None:
        img = get_default_image(shape=ImageShape(input_shape.h, input_shape.w))
    else:
        img = custom_image

    figsize = plot_params.get("figsize", (10, 10))

    # plot image
    plt.figure(figsize=figsize)
    ax = plt.subplot(111)
    plt.imshow(img)

    # plot naive receptive field grid
    if plot_naive_rf:
        dw = input_shape.w / output_shape.w
        dh = input_shape.h / output_shape.h
        for i, j in itertools.product(range(output_shape.w),
                                      range(output_shape.h)):
            x0, x1 = i * dw, (i + 1) * dw
            y0, y1 = j * dh, (j + 1) * dh

            ax.add_patch(
                patches.Rectangle((y0, x0),
                                  dh,
                                  dw,
                                  alpha=0.9,
                                  fill=False,
                                  edgecolor="gray",
                                  linewidth=1))

    rf_offset = rf_params.offset
    rf_size = rf_params.size
    rf_stride = rf_params.stride

    # map from output grid space to input image
    def map_point(i: int, j: int):
        return np.array(rf_offset) + np.array([i, j]) * np.array(rf_stride)

    # plot RF grid based on rf params
    points = [
        map_point(i, j) for i, j in itertools.product(range(output_shape.w),
                                                      range(output_shape.h))
    ]

    points = np.array(points)
    plt.scatter(points[:, 1],
                points[:, 0],
                marker="o",
                c=(0.2, 0.9, 0.1, 0.9),
                s=10)

    # plot receptive field from corner point
    _plot_rect(ax,
               rect=to_rf_rect(rf_offset, rf_size),
               color=(0.9, 0.3, 0.2),
               linewidth=5,
               size=90)
    center_point = map_point(output_shape.w // 2, output_shape.h // 2)
    _plot_rect(ax,
               rect=to_rf_rect(GridPoint(center_point[0], center_point[1]),
                               rf_size),
               color=(0.1, 0.3, 0.9),
               linewidth=5,
               size=90)
    last_point = map_point(output_shape.w - 1, output_shape.h - 1)
    _plot_rect(ax,
               rect=to_rf_rect(GridPoint(last_point[0], last_point[1]),
                               rf_size),
               color=(0.1, 0.9, 0.3),
               linewidth=5,
               size=90)
    ax.set_aspect('equal')