Exemplo n.º 1
0
def show_probability_chip(
    ibs, aid, species=None, fnum=None, config2_=None, blend=False, **kwargs
):
    """
    TODO: allow species override in controller

    CommandLine:
        python -m wbia.viz.viz_hough --exec-show_probability_chip --cnn --show
        python -m wbia.viz.viz_hough --exec-show_probability_chip --cnn --show --db PZ_Master1
        python -m wbia.viz.viz_hough --exec-show_probability_chip --cnn --show --db PZ_Master1 --aid 9970

    Example:
        >>> # SCRIPT
        >>> from wbia.viz.viz_hough import *  # NOQA
        >>> import wbia
        >>> from wbia.viz import viz_chip
        >>> ibs, aid_list, kwargs, config2_ = viz_chip.testdata_showchip()
        >>> fnum = 1
        >>> species = None
        >>> aid = aid_list[0]
        >>> fig, ax = show_probability_chip(ibs, aid, species, fnum, blend=True, **kwargs)
        >>> ut.show_if_requested()
    """
    fnum = pt.ensure_fnum(fnum)
    title = 'Probability Chip: ' + ', '.join(vh.get_annot_text(ibs, [aid], True))
    hough_cpath = ibs.get_annot_probchip_fpath(aid, config2_=config2_)
    img = vt.imread(hough_cpath)
    if blend:
        chip = ibs.get_annot_chips(aid, config2_=config2_)
        img = vt.blend_images_multiply(chip, vt.resize_mask(img, chip))
    fig, ax = viz_image2.show_image(img, title=title, fnum=fnum, **kwargs)
    return fig, ax
Exemplo n.º 2
0
def draw_demo():
    r"""
    CommandLine:
        python -m plottool.interact_impaint --exec-draw_demo --show

    Example:
        >>> # SCRIPT
        >>> from plottool.interact_impaint import *  # NOQA
        >>> result = draw_demo()
        >>> print(result)
        >>> ut.quit_if_noshow()
        >>> import plottool as pt
        >>> ut.show_if_requested()
    """
    fpath = ut.grab_test_imgpath('zebra.png')
    img = vt.imread(fpath)
    mask = impaint_mask2(img)
    print('mask = %r' % (mask, ))
    print('mask.sum() = %r' % (mask.sum(), ))
    if False:
        plt.imshow(vt.blend_images_multiply(img, mask))
        ax = plt.gca()
        ax.grid(False)
        ax.set_xticks([])
        ax.set_yticks([])
Exemplo n.º 3
0
def draw_demo():
    r"""
    CommandLine:
        python -m plottool.interact_impaint --exec-draw_demo --show

    Example:
        >>> # SCRIPT
        >>> from plottool.interact_impaint import *  # NOQA
        >>> result = draw_demo()
        >>> print(result)
        >>> ut.quit_if_noshow()
        >>> import plottool as pt
        >>> ut.show_if_requested()
    """
    fpath = ut.grab_test_imgpath('zebra.png')
    img = vt.imread(fpath)
    mask = impaint_mask2(img)
    print('mask = %r' % (mask,))
    print('mask.sum() = %r' % (mask.sum(),))
    if False:
        plt.imshow(vt.blend_images_multiply(img, mask))
        ax = plt.gca()
        ax.grid(False)
        ax.set_xticks([])
        ax.set_yticks([])
Exemplo n.º 4
0
def show_probability_chip(ibs, aid, species=None, fnum=None, config2_=None,
                          blend=False, **kwargs):
    """
    TODO: allow species override in controller

    CommandLine:
        python -m ibeis.viz.viz_hough --exec-show_probability_chip --cnn --show
        python -m ibeis.viz.viz_hough --exec-show_probability_chip --cnn --show --db PZ_Master1
        python -m ibeis.viz.viz_hough --exec-show_probability_chip --cnn --show --db PZ_Master1 --aid 9970

    Example:
        >>> # SCRIPT
        >>> from ibeis.viz.viz_hough import *  # NOQA
        >>> import ibeis
        >>> from ibeis.viz import viz_chip
        >>> ibs, aid_list, kwargs, config2_ = viz_chip.testdata_showchip()
        >>> fnum = 1
        >>> species = None
        >>> aid = aid_list[0]
        >>> fig, ax = show_probability_chip(ibs, aid, species, fnum, blend=True, **kwargs)
        >>> ut.show_if_requested()
    """
    fnum = pt.ensure_fnum(fnum)
    title = 'Probability Chip: ' + ', '.join(vh.get_annot_text(ibs, [aid], True))
    hough_cpath = ibs.get_annot_probchip_fpath(aid, config2_=config2_)
    img = vt.imread(hough_cpath)
    if blend:
        chip = ibs.get_annot_chips(aid, config2_=config2_)
        img = vt.blend_images_multiply(chip, vt.resize_mask(img, chip))
    fig, ax = viz_image2.show_image(img, title=title, fnum=fnum, **kwargs)
    return fig, ax
Exemplo n.º 5
0
    def saliency(dream, Xb, yb):
        """
        num = 10
        Xb = model.prepare_data(X_test[0:num])
        yb = y_test[0:num]

        dpath = ''
        dataset = None
        """
        dpath = '.'

        import theano.tensor as T
        import lasagne
        import vtool as vt
        import theano
        model = dream.model

        # Use current weights to find the score of a particular class
        Xb_shared = theano.shared(Xb)
        yb_shared = theano.shared(yb.astype(np.int32))

        # Get the final layer and remove the softmax nonlinearity to access the
        # pre-activation. (Softmax encourages minimization of other classes)
        import copy
        #softmax = copy.copy(model.output_layer)
        #softmax.nonlinearity = lasagne.nonlinearities.identity
        softmax = copy.copy(model.output_layer)

        class_probs = lasagne.layers.get_output(softmax, Xb_shared,
                                                deterministic=True)

        # werid way to index into position of target
        flat_idx = (T.arange(yb_shared.shape[0]) * class_probs.shape[1]) + yb_shared
        class_probs_target = T.flatten(class_probs)[flat_idx]

        # Get derivative of scores for the target class wrt the input
        d_score_wrt_input = theano.grad(class_probs_target.mean(), Xb_shared)
        w = np.array(d_score_wrt_input.eval())
        saliency = w.max(axis=1, keepdims=True)

        outs = saliency.transpose((0, 2, 3, 1))
        X = Xb.transpose((0, 2, 3, 1))

        for count in range(len(Xb)):
            img = X[count]
            y = yb[count]
            out = vt.norm01(outs[count])
            overlay = vt.blend_images_multiply(out, img)

            vt.imwrite(join(dpath, 'out%d_A_image_t=%s.jpg' % (count, y)),
                       vt.rectify_to_uint8(img))
            vt.imwrite(join(dpath, 'out%d_B_heat_t=%s.jpg' % (count, y)),
                       vt.rectify_to_uint8(out))
            vt.imwrite(join(dpath, 'out%d_C_overlay_t=%s.jpg' % (count, y)),
                       vt.rectify_to_uint8(overlay))
Exemplo n.º 6
0
def augment_shadow(Xb, yb=None, rng=np.random, return_shadowmaps=False):
    """
    CommandLine:
        python -m ibeis_cnn.augment --test-augment_shadow --show --db PZ_MTEST

    Example:
        >>> # ENABLE_DOCTEST
        >>> from ibeis_cnn.augment import *  # NOQA
        >>> from ibeis_cnn import ingest_data, utils, draw_results
        >>> Xb_orig, yb_orig, Xb, yb = testdata_augment()
        >>> rng = np.random.RandomState(0)
        >>> Xb, yb, shadows = augment_shadow(Xb, yb, rng=rng, return_shadowmaps=True)
        >>> ut.quit_if_noshow()
        >>> show_augmented_patches(Xb_orig, Xb, yb_orig, yb, shadows)
        >>> ut.show_if_requested()
    """

    import vtool as vt
    # Rotate corresponding patches together
    Xb1, Xb2 = Xb[::2], Xb[1::2]
    perlin_perteb = .5

    def perlin_noise01(size):
        #scale = 128.0
        #scale = 80.0
        scale = size[0] * 1.5
        noise = (vt.perlin_noise(size, scale=scale, rng=rng).astype(np.float32)) / 255.0
        noise = np.transpose(noise[None, :], (1, 2, 0))
        return noise

    num = len(Xb1)

    perlinperterb_flags = rng.uniform(0.0, 1.0, size=num) < perlin_perteb

    #perlin_min = 0.2
    #perlin_max = 0.9
    #perlin_range = perlin_max - perlin_min
    if return_shadowmaps:
        shadows = np.empty(Xb.shape, dtype=Xb.dtype)
        shadows1, shadows2 =  shadows[::2], shadows[1::2]

    for index in np.where(perlinperterb_flags)[0]:
        img1 = Xb1[index]
        img2 = Xb2[index]
        # TODO: TAKE IN NORMALIZED POINTS
        noise1 = perlin_noise01(img1.shape[0:2])
        noise2 = perlin_noise01(img2.shape[0:2])

        #noise1 = np.clip(noise1 / .7, 0, 1)
        #noise2 = np.clip(noise2 / .7, 0, 1)
        noise1 = np.clip((noise1 ** .7 - .15) / .75, .1, 1)
        noise2 = np.clip((noise2 ** .7 - .15) / .75, .1, 1)

        if return_shadowmaps:
            shadows1[index] = noise1
            shadows2[index] = noise2

        #noise1[noise1 > .6] = 1
        #noise2[noise2 > .6] = 1

        #alpha1 = ((rng.rand() * perlin_range) + perlin_min)
        #alpha2 = ((rng.rand() * perlin_range) + perlin_min)
        #Xb1[index] = img1 ** (noise1 * alpha1)
        #Xb2[index] = img2 ** (noise2 * alpha2)
        #alpha1 = alpha2 = .5
        alpha1 = alpha2 = .5
        Xb1[index] = vt.blend_images_multiply(img1, noise1, alpha1)
        Xb2[index] = vt.blend_images_multiply(img2, noise2, alpha2)

        # Linear Burn
        #Xb1[index] = np.clip(img1 + noise1 - 1.0, 0, 1)
        #Xb2[index] = np.clip(img2 + noise2 - 1.0, 0, 1)

        #Xb1[index] = vt.blend_images_average(img1, noise1, alpha1)
        #Xb2[index] = vt.blend_images_average(img2, noise2, alpha2)

        #Xb1[index] = noise1
        #Xb2[index] = noise2
    #with ut.embed_on_exception_context:
    assert Xb.max() <= 1.0, 'max/min = %r, %r' % (Xb.min(), Xb.max())
    assert Xb.min() >= 0.0, 'max/min = %r, %r' % (Xb.min(), Xb.max())
    #bad_xs = np.where((Xb > 1).sum(axis=-1).sum(axis=-1).sum(axis=-1) > 0)[0]
    if return_shadowmaps:
        return Xb, yb, shadows
    return Xb, yb