Esempio n. 1
0
def pool(theta, size):
    w, h = theta.shape[0]//size, theta.shape[1]//size
    feat = np.zeros((w, h, theta.shape[-1]))
    for x, y in gv.multirange(w, h):
        feat[x,y] = np.apply_over_axes(np.sum, theta[x*size:(x+1)*size, y*size:(y+1)*size], [0, 1])[0,0]

    feat /= np.apply_over_axes(np.sum, feat, [-1]) + 1e-8
    
    return feat
Esempio n. 2
0
def find_bounding_box(im):
    first = True 
    pixels = im.getdata()
    bounding_box = [np.inf, np.inf, -np.inf, -np.inf]
    for x, y in gv.multirange(*im.size):
        p = pixels[y * im.size[0] + x]
        if p[3] > 0:
            bounding_box[0] = min(x, bounding_box[0])
            bounding_box[1] = min(y, bounding_box[1])
            bounding_box[2] = max(x, bounding_box[2])
            bounding_box[3] = max(y, bounding_box[3])
    # Make a bit bigger
    bounding_box = (bounding_box[0]-1, bounding_box[1]-1, bounding_box[2]+1, bounding_box[3]+1)
        
    return tuple(bounding_box)
Esempio n. 3
0
def find_bounding_box(im):
    first = True
    pixels = im.getdata()
    bounding_box = [np.inf, np.inf, -np.inf, -np.inf]
    for x, y in gv.multirange(*im.size):
        p = pixels[y * im.size[0] + x]
        if p[3] > 0:
            bounding_box[0] = min(x, bounding_box[0])
            bounding_box[1] = min(y, bounding_box[1])
            bounding_box[2] = max(x, bounding_box[2])
            bounding_box[3] = max(y, bounding_box[3])
    # Make a bit bigger
    bounding_box = (bounding_box[0] - 1, bounding_box[1] - 1,
                    bounding_box[2] + 1, bounding_box[3] + 1)

    return tuple(bounding_box)
Esempio n. 4
0
        Gs_ok = Gs[:, burnin:].reshape((-1, F))
        Zs_ok = np.exp(Gs_ok)

        # Now use samples to estimate
        G_mu = Gs_ok.mean(0)
        Z_mu = np.exp(G_mu)

        #g_mu = np.exp(G_mu)
        G_Sigma = np.cov(Gs_ok.T) + np.eye(F) * 0.0001

        # Find w through a binary search
        #for f in xrange(10):
        if 0:
            for f in xrange(F):
                print 'Doing', f
                for l0, l1 in gv.multirange(*w.shape[:2]):

                    def fun(w0):
                        return gv.sigmoid(w0 + gv.logit(Zs_ok[..., f])).mean(
                        ) - Xbar[l0, l1, f]

                    w[l0, l1, f] = find_zero(fun, l=-6, u=6, depth=15)

        if 0:
            l0 = l1 = 4
            f = 10

            def fun(w0):
                return gv.sigmoid(w0 +
                                  gv.logit(Zs_ok[..., f])).mean() - Xbar[l0,
                                                                         l1, f]
Esempio n. 5
0
import os
import numpy as np
from pylab import cm
net = pnet.PartsNet.load('parts-net.npy')

p = net._layers[1]._parts[...,0]
shape = p.shape[-2:]

grid = gv.plot.ImageGrid(10, 10, p.shape[1:])

for i in xrange(p.shape[0]):
    grid.set_image(p[i], i//10, i%10, vmin=0, vmax=1, cmap=cm.RdBu_r)

if 0:
    print('shape', shape)
    print( np.asarray(list(gv.multirange(*[2]*np.prod(shape)))).shape)
    types = np.asarray(list(gv.multirange(*[2]*np.prod(shape)))).reshape((-1,) + shape)

    t = p[:,np.newaxis]
    x = types[np.newaxis]

    llh = x * np.log(t) + (1 - x) * np.log(1 - t)

    counts = np.bincount(np.argmax(llh.sum(-1).sum(-1), 0), minlength=p.shape[0])

    print('counts', counts)

    #import pdb; pdb.set_trace()

if args.output is None:
    plt.imshow(grid.image, interpolation='nearest')
def superimposed_model(settings, threading=True):
    num_mixtures = settings['detector']['num_mixtures']

    # Train a mixture model to get a clustering of the angles of the object
    descriptor = gv.load_descriptor(settings)
    detector = gv.BernoulliDetector(num_mixtures, descriptor, settings['detector'])

    files = get_training_files(detector)
    neg_files = sorted(glob.glob(settings['detector']['neg_dir']))

    ag.info("Checkpoint 1")

    testing_type = detector.settings.get('testing_type')

    # Extract clusters (manual or through EM)
    ##############################################################################
    detector, comps = cluster(detector, files)
    each_mix_N = np.bincount(comps, minlength=num_mixtures)

    ##############################################################################

    ag.info("Checkpoint 3")

    ag.info("Checkpoint 4")

    support = detector.support 

    kernels = []

    #ag.info("TODO, quitting")
    #return detector

    # Determine bounding boxes
    ##############################################################################

    psize = settings['detector']['subsample_size']

    bbs = calc_bbs(detector)

    ag.info("Checkpoint 6")

    ag.info("Checkpoint 7")

    bkgs = []
    orig_sizes = []
    new_support = []
    im_size = settings['detector']['image_size']

    ag.info("Checkpoint 8")
    all_negs = []

    ag.info("Checkpoint 9")

    # Retrieve features and support 
    ##############################################################################

    ag.info('Fetching positives again...')
    all_pos_feats = []
    all_neg_feats = []
    alphas = []
    all_alphas = []
    all_binarized_alphas = []


    if settings['detector'].get('superimpose'):
        detector.extra['concentrations'] = []

        argses = [(m, settings, bbs[m], list(np.where(comps == m)[0]), files, neg_files, settings['detector'].get('stand_multiples', 1)) for m in range(detector.num_mixtures)]        
        for mixcomp, neg_feats, pos_feats, alpha_maps, extra in itr.starmap(get_pos_and_neg, argses):
            alpha = np.mean(alpha_maps, axis=0)
            alpha_maps = np.asarray(alpha_maps)
            all_alphas.append(alpha_maps)
            all_binarized_alphas.append(alpha_maps > 0.05)

            alphas.append(alpha)
            all_neg_feats.append(neg_feats)
            all_pos_feats.append(pos_feats)

            detector.extra['concentrations'].append(extra.get('concentrations', {}))

        ag.info('Done.')

        # Setup some places to store things
        if 'weights' not in detector.extra:
            detector.extra['weights'] = [None] * detector.num_mixtures
        if 'sturf' not in detector.extra:
            detector.extra['sturf'] = [{} for _ in xrange(detector.num_mixtures)]

        for m in xrange(detector.num_mixtures):
            detector.extra['sturf'].append(dict())

            obj = all_pos_feats[m].mean(axis=0)
            bkg = all_neg_feats[m].mean(axis=0)
            size = gv.bb.size(bbs[m])

            kernels.append(obj)
            bkgs.append(bkg)
            orig_sizes.append(size)
            new_support.append(alphas[m])

        if 0:
            for m in xrange(detector.num_mixtures):
                obj = all_pos_feats[m].mean(axis=0)
                bkg = all_neg_feats[m].mean(axis=0)
                size = gv.bb.size(bbs[m])

                eps = 0.025
                obj = np.clip(obj, eps, 1 - eps)
                avg = np.clip(avg, eps, 1 - eps)
                #lmb = obj / avg
                #w = np.clip(np.log(obj / avg), -1, 1)
                w = np.log(obj / (1 - obj) * ((1 - avg) / avg))
                #w = np.log(

                #w_avg = np.apply_over_axes(np.sum, w * support[...,np.newaxis], [0, 1]) / support.sum()

                #w -= w_avg * support[...,np.newaxis]

                if 'weights' not in detector.extra:
                    detector.extra['weights'] = []
                detector.extra['weights'].append(w)

                if 'sturf' not in detector.extra:
                    detector.extra['sturf'] = []

                detector.extra['sturf'].append(dict())
                        
                kernels.append(obj)
                bkgs.append(bkg)
                orig_sizes.append(size)
                new_support.append(alphas[m])

        detector.settings['per_mixcomp_bkg'] = True
    else:
        # Get a single background model for this one
        bkg = _get_background_model(settings, neg_files)

        crop_image = detector.settings.get('crop_image')
        import pdb; pdb.set_trace()
        argses = [(m, settings, list(np.where(comps == m)[0]), files, crop_image) for m in range(detector.num_mixtures)]        
        for m, pos_feats in gv.parallel.starmap(get_positives, argses):
            obj = pos_feats.mean(axis=0)
            all_pos_feats.append(pos_feats)

            kernels.append(obj)
            bkgs.append(bkg)
            size = gv.bb.size(bbs[m])

            orig_sizes.append(size)
            support = np.ones(settings['detector']['image_size'])
            new_support.append(support)

        detector.settings['per_mixcomp_bkg'] = True # False 


    # Get weights and support

    for m in xrange(detector.num_mixtures):
        #kern = detector.kernel_templates[m]
        #bkg = detector.fixed_spread_bkg[m]
        obj = all_pos_feats[m].mean(axis=0)
        bkg = all_neg_feats[m].mean(axis=0)

        if detector.eps is None:
            detector.prepare_eps(bkg)

        weights = detector.build_clipped_weights(obj, bkg, detector.eps)

        detector.extra['weights'][m] = weights

        detector.extra['sturf'][m]['support'] = arrange_support(alphas[m], weights.shape, psize)

    # Modify weights

    if not detector.settings.get('plain'):
        for m in xrange(detector.num_mixtures):
            weights = detector.extra['weights'][m] 

            F = detector.num_features
            indices = get_key_points(weights, suppress_radius=detector.settings.get('indices_suppress_radius', 4), even=True)

            L0 = indices.shape[0] // F 
            
            kp_weights = np.zeros((L0, F))

            M = np.zeros(weights.shape, dtype=np.uint8)
            counts = np.zeros(F)
            for index in indices:
                f = index[2]
                M[tuple(index)] = 1
                kp_weights[counts[f],f] = weights[tuple(index)]
                counts[f] += 1

            #theta = np.load('theta3.npy')[1:-1,1:-1]
            #th = theta
            #eth = np.load('empty_theta.npy')

            #support = 1-th[:,:,np.arange(1,F+1),np.arange(F)].mean(-1)
            #offset = gv.sub.subsample_offset_shape(alphas[m].shape, psize)

            support = detector.extra['sturf'][m]['support'] 

            #    def subsample_offset_shape(shape, size):


            pos, neg = all_pos_feats[m].astype(bool), all_neg_feats[m].astype(bool)
            #avg = np.apply_over_axes(

            diff = pos ^ neg
            appeared = pos & ~neg
            disappeared = ~pos & neg

            #bs = (support > 0.5)[np.newaxis,...,np.newaxis]
             

            A = appeared.mean(0) / (0.00001+((1-neg).mean(0)))
            D = disappeared.mean(0) / (0.00001+neg.mean(0))
            #ss = D.mean(-1)[...,np.newaxis]
            ss = support[...,np.newaxis]

            B = (np.apply_over_axes(np.mean, A*ss, [0, 1])).squeeze() / ss.mean()

            def clogit(x):
                return gv.logit(gv.bclip(x, 0.025))

            def find_zero(fun, l, u, depth=30):
                m = np.mean([l, u])
                if depth == 0:
                    return m
                v = fun(m)
                if v > 0:
                    return find_zero(fun, l, m, depth-1)
                else:
                    return find_zero(fun, m, u, depth-1)

            # Find zero-crossing
            #for f in xrange(F):
                

            # Now construct weights from these deltas
            #weights = ((clogit(ss * deltas + A) - clogit(B)))
            #weights = (ss * (clogit(deltas + pos.mean(0)) - clogit(neg.mean(0))))

            
            avg = np.apply_over_axes(np.mean, pos * M * ss, [1, 2]) / (ss * M).mean()

            if 0:
                for l0, l1, f in gv.multirange(*weights.shape):

                    def fun(w):
                        return -(np.clip(pos[:,l0,l1,f].mean(), 0.005, 0.995) - np.mean(expit(w + logit(avg[...,f]))))

                    weights[l0,l1,f] = find_zero(fun, -10, 10)



            if 1:
                # Print these to file
                from matplotlib.pylab import cm
                grid = gv.plot.ImageGrid(detector.num_features, 1, weights.shape[:2], border_color=(0.5, 0.5, 0.5))
                mm = np.fabs(weights).max()
                for f in xrange(detector.num_features):
                    grid.set_image(weights[...,f], f, 0, vmin=-mm, vmax=mm, cmap=cm.RdBu_r)
                fn = os.path.join(os.path.expandvars('$HOME'), 'html', 'plots', 'plot2.png')
                grid.save(fn, scale=10)
                os.chmod(fn, 0644)
                



            #A = appeared.mean(0) / (0.00001+((1-neg).mean(0)))
            #mm = (A * ss).mean() / ss.mean()


            #xx = (bs & pos) | (~bs & appeared)

            #avg = xx.mean(0)
            weights1 = ss*(weights - np.apply_over_axes(np.mean, weights * ss, [0, 1])/ss.mean())
            detector.extra['sturf'][m]['weights1'] = weights1

            eps = 0.025

            avg_pos = (np.apply_over_axes(np.mean, pos * ss, [0, 1, 2]) / ss.mean()).squeeze().clip(eps, 1-eps)
            avg_neg = (np.apply_over_axes(np.mean, neg * ss, [0, 1, 2]) / ss.mean()).squeeze().clip(eps, 1-eps)

            #w_avg = np.apply_over_axes(np.sum, weights * support[...,np.newaxis], [0, 1]) / support.sum()
            #
            #w_avg = (logit(np.apply_over_axes(np.mean, pos, [0, 1, 2])) - \
             #        logit(np.apply_over_axes(np.mean, neg, [0, 1, 2]))).squeeze()
            w_avg = logit(avg_pos) - logit(avg_neg)
            detector.extra['sturf'][m]['wavg'] = w_avg
            detector.extra['sturf'][m]['reweighted'] = (w_avg * support[...,np.newaxis]).squeeze()

            #weights -= w_avg * support[...,np.newaxis]
            #weights *= support[...,np.newaxis] * M
            if 0:
                weights *= support[...,np.newaxis]

                avg_weights = np.apply_over_axes(np.mean, weights, [0, 1]) / M.mean(0).mean(0)

                avg_w = kp_weights.mean(0)

                weights -= avg_w - (-kp_weights.var(0) / 2)

                weights *= support[...,np.newaxis]

                print((weights * M).mean(0))


            #weights = (weights - w_avg) * support[...,np.newaxis]
            #weights -= (w_avg + 0.0) * support[...,np.newaxis]

            weights -= w_avg * support[...,np.newaxis]

            F = detector.num_features

            if 0:
                for f in xrange(F):
                    #zz = np.random.normal(-1.5, size=(1, 1, 50))
                    zz = np.random.normal(-1.5, size=(1, 1, 50)).ravel()

                    betas = np.zeros(len(zz))
                    for i, z in enumerate(zz):
                        def fun(beta):
                            w = weights[...,f] - beta * support 
                            return np.log(1 - expit(w[...,np.newaxis] + z)).mean() - np.log(1 - expit(z))

                        betas[i] = find_zero(fun, -10, 10)

                    
                    if f == 0:
                        np.save('betas.npy', betas)
                    beta0 = betas.mean()
                    print(f, beta0, betas.std())
                    weights[...,f] -= beta0 * support 


            if 1:
                # Print these to file
                from matplotlib.pylab import cm
                grid = gv.plot.ImageGrid(detector.num_features, 2, weights.shape[:2], border_color=(0.5, 0.5, 0.5))
                mm = np.fabs(weights).max()
                for f in xrange(detector.num_features):
                    grid.set_image(weights[...,f], f, 0, vmin=-mm, vmax=mm, cmap=cm.RdBu_r)
                    grid.set_image(M[...,f], f, 1, vmin=0, vmax=1, cmap=cm.RdBu_r)
                fn = os.path.join(os.path.expandvars('$HOME'), 'html', 'plots', 'plot.png')
                grid.save(fn, scale=10)
                os.chmod(fn, 0644)

            ag.info('sum', np.fabs(np.apply_over_axes(np.sum, weights, [0, 1])).sum())

            # Instead, train model rigorously!!
            detector.extra['sturf'][m]['pos'] = all_pos_feats[m]
            detector.extra['sturf'][m]['neg'] = all_neg_feats[m]


            # Averags of all positives
            ff = all_pos_feats[m]
            posavg = np.apply_over_axes(np.sum, all_pos_feats[m] * support[...,np.newaxis], [1, 2]).squeeze() / support.sum() 
            negavg = np.apply_over_axes(np.sum, all_neg_feats[m] * support[...,np.newaxis], [1, 2]).squeeze() / support.sum() 

            S = np.cov(posavg.T)
            Sneg = np.cov(negavg.T)

            detector.extra['sturf'][m]['pavg'] = avg_pos
            detector.extra['sturf'][m]['pos-samples'] = posavg 
            detector.extra['sturf'][m]['S'] = S
            detector.extra['sturf'][m]['Sneg'] = Sneg
            detector.extra['sturf'][m]['navg'] = avg_neg

            Spos = S
            rs = np.random.RandomState(0)
            detector.extra['sturf'][m]['Zs'] = rs.multivariate_normal(avg_neg, Sneg, size=1000).clip(min=0.005, max=0.995)
            detector.extra['sturf'][m]['Zs_pos'] = rs.multivariate_normal(avg_pos, Spos, size=1000).clip(min=0.005, max=0.995)
            detector.extra['sturf'][m]['Zs_pos2'] = rs.multivariate_normal(avg_pos, Spos * 2, size=1000).clip(min=0.005, max=0.995)
            detector.extra['sturf'][m]['Zs_pos10'] = rs.multivariate_normal(avg_pos, Spos * 10, size=1000).clip(min=0.005, max=0.995)
            detector.extra['sturf'][m]['Zs_pos50'] = rs.multivariate_normal(avg_pos, Spos * 50, size=1000).clip(min=0.005, max=0.995)

    #{{{
    if 0:
        argses = [(m, settings, bbs[m], np.where(comps == m)[0], files, neg_files) for m in xrange(detector.num_mixtures)]
        for kern, bkg, orig_size, sup in gv.parallel.starmap(_create_kernel_for_mixcomp, argses):
            kernels.append(kern) 
            bkgs.append(bkg)
            orig_sizes.append(orig_size)
            new_support.append(sup)
                    
            ag.info("Checkpoint 10")

            detector.settings['per_mixcomp_bkg'] = True
    #}}}

    detector.kernel_templates = kernels
    detector.kernel_sizes = orig_sizes
    detector.settings['kernel_ready'] = True
    detector.use_alpha = False
    detector.support = new_support

    # Determine the background
    ag.info("Determining background")

    detector.fixed_bkg = None
    detector.fixed_spread_bkg = bkgs

    detector.settings['bkg_type'] = 'from-file'

    detector._preprocess()
    detector.prepare_eps(detector.fixed_spread_bkg[0])

    # Determine the standardization values
    ag.info("Determining standardization values")

    #fixed_train_mean = np.zeros(detector.num_mixtures)
    #detector.fixed_train_mean = []
    #fixed_train_std = np.ones(detector.num_mixtures)

    # Determine indices for coarse detection sweep
    if INDICES:
        detector.indices = []

        for m in xrange(detector.num_mixtures):
            these_indices = []
            weights = detector.extra['weights'][m]

            ag.info('Indices:', np.prod(weights.shape))

            # If not plain, we need even keypoints
            even = not detector.settings.get('plain')
            indices = get_key_points(weights, suppress_radius=detector.settings.get('indices_suppress_radius', 4), even=even)

            if not detector.settings.get('plain'):
                detector.extra['weights'][m] = weights

            assert len(indices) > 0, "No indices were extracted when keypointing"

            detector.indices.append(indices)
    else:
        detector.indices = None

    if testing_type in ('fixed', 'non-parametric'):
        detector.standardization_info = []
        if testing_type == 'fixed':
            if detector.settings.get('standardize_with_samples'):
                detector.standardization_info = [dict(mean=0, std=1)] * detector.num_mixtures
                info = []
                source = detector.settings.get('standardize_negative_source', 'neg-dir')
                N = detector.settings.get('standardize_num_images', 50)
                if source.startswith('voc-train-non-'):
                    obj_class = source.split('-')[-1] 
                    print('Taking negatives from voc train, without class', obj_class)
                    gen = gv.voc.gen_negative_files(obj_class, 'train')
                    #print('negatives', len([im for im in gen]))
                else:
                    print('Taking negatives from neg_dir')
                    gen = itr.cycle(gv.datasets.ImgFile(path=fn, img_id=os.path.basename(fn)) for fn in neg_files)
                    
                gen = itr.cycle(gen)
                gen = itr.islice(gen, N)
                gens = itr.tee(gen, detector.num_mixtures)

                th = -np.inf
                for m in xrange(detector.num_mixtures):
                    neg_files_segment = gens[m]
                    argses = [(detector, i, fileobj, th, m) for i, fileobj in enumerate(neg_files_segment)] 
                    topsy = list(gv.parallel.starmap_unordered(get_strong_fps_single, argses))
                    confs = np.asarray([bbobj.confidence for topsy_m in topsy for bbobj in topsy_m])

                    info.append(dict(mean=confs.mean(), std=confs.std())) 
                    #for m in xrange(detector.num_mixtures):
                    
                detector.standardization_info = info      

            else:

                argses = [(m, settings, detector.eps, bbs[m], kernels[m], bkgs[m], None, None, None, detector.indices[m] if INDICES else None, 3) for m in xrange(detector.num_mixtures)]

                detector.standardization_info = list(gv.parallel.starmap(_calc_standardization_for_mixcomp, argses))
        else:
            raise Exception("Unknown testing type")


    detector.settings['testing_type'] = testing_type 
    #detector.settings['testing_type'] = 'NEW'

    #detector.

    #
    # Data mine stronger negatives 
    #
    # TODO: Object class must be input
    if 1:
        contest = 'voc'
        obj_class = 'car'
        gen = gv.voc.gen_negative_files(obj_class, 'train')
    else:
        contest = 'custom-tmp-frontbacks'
        obj_class = 'bicycle'
        gen, tot = gv.datasets.load_files(contest, obj_class)

    import heapq
    top_bbs = [[] for k in xrange(detector.num_mixtures)]
    TOP_N = 10000


    if detector.settings.get('cascade'): # New SVM attempt 
        detector.extra['cascade_threshold'] = detector.settings.get('cascade_threshold', 8) 
        COUNT = detector.settings.get('cascade_farming_count', 500)

        args = itr.izip( \
            itr.repeat(detector), 
            xrange(COUNT), 
            itr.islice(gen, COUNT)
        )

        for res in gv.parallel.starmap_unordered(get_strong_fps, args):
            for m in xrange(detector.num_mixtures):
                top_bbs[m].extend(res[m])

        ag.info('- TOPS ------')
        ag.info(map(np.shape, top_bbs) )
        detector.extra['top_bbs_shape'] = map(np.shape, top_bbs) 

        # Save the strong negatives
        detector.extra['negs'] = top_bbs
        
        def phi(X, mixcomp):
            if SVM_INDICES and 0:
                indices = detector.indices2[mixcomp][0]
                return X.ravel()[np.ravel_multi_index(indices.T, X.shape)]
            else:
                #return gv.sub.subsample(X, (2, 2)).ravel()
                return X.ravel()

        all_neg_X0 = []
        for k in xrange(detector.num_mixtures):
            all_neg_X0.append(np.asarray(map(lambda bbobj: phi(bbobj.X, k), top_bbs[k])))

        del top_bbs

        all_pos_X0 = []
        for mixcomp, pos_feats in enumerate(all_pos_feats):
            all_pos_X0.append(np.asarray(map(lambda X: phi(X, mixcomp), pos_feats))) 
        ag.info('Done.')

        detector.extra['poss'] = all_pos_feats

        ag.info('Training SVMs...')
        # Train SVMs
        #from sklearn.svm import LinearSVC
        from sklearn.svm import LinearSVC, SVC
        clfs = []
        detector.indices2 = None # not [] for now 

        #all_neg_X0 = [[bbobj.X for bbobj in top_bbs[m]] for m in xrange(detector.num_mixtures)]

        detector.extra['svms'] = []
        for m in xrange(detector.num_mixtures):
            X = np.concatenate([all_pos_X0[m], all_neg_X0[m]])  
    
            # Flatten
            ag.info(m, ':', X.shape)
            #X = phi(X, k)
            ag.info(m, '>', X.shape)
            y = np.concatenate([np.ones(len(all_pos_feats[m])), np.zeros(len(all_neg_X0[m]))])

            #detector.extra['data_x'].append(X)
            #detector.extra['data_y'].append(y)


            from sklearn import cross_validation as cv

            #C = 5e-8
            C = 1.0

            #clf = LinearSVC(C=C)
            #clf = LinearSVC(C=C)
            clf = SVC(C=C, kernel='linear')
            clf.fit(X, y)

            svm_info = dict(intercept=float(clf.intercept_), weights=clf.coef_)
            detector.extra['svms'].append(svm_info)

            #sh = all_pos_feats[m][0].shape

            # Get most significant coefficients

            #th = smallest_th[k] 
            #th = 0
            #detector.extra['svms'].append(dict(svm=clf, th=th, uses_indices=SVM_INDICES))
        ag.info('Done.')

        # Remove negatives and positives from extra, since it takes space
        if 1:
            del detector.extra['poss']
            del detector.extra['negs']

    ag.info('extra')
    ag.info(detector.extra.keys())
    ag.info('eps', detector.eps)

    #ag.info("THIS IS SO TEMPORARY!!!!!")
    if 'weights' in detector.extra:
        #detector.indices = None

        ag.info(detector.standardization_info)
        #try:
        #    detector.standardization_info[0]['std'] = 1.0
        #except TypeError:
        #    detector.standardization_info = [dict(std=1.0, mean=0.0)]
        ag.info('corner2', detector.extra['weights'][0][0,0,:5])

    return detector 
Esempio n. 7
0
    def train_from_samples(self, patches,patches_original):
        #from pnet.latent_bernoulli_mm import LatentBernoulliMM
        from pnet.bernoullimm import BernoulliMM
        min_prob = self._settings.get('min_prob', 0.01)
        flatpatches = patches.reshape((patches.shape[0], -1))
        
        if 1:
            mm = BernoulliMM(n_components=self._num_parts, n_iter=20, tol=1e-15,n_init=2, random_state=self._settings.get('em_seed',0), min_prob=min_prob, verbose=False)
            mm.fit(flatpatches)
            print(mm.fit(flatpatches))
            #print('AIC', mm.aic(flatpatches))
            #print('BIC', mm.bic(flatpatches))
            
            if 0:
                # Draw samples
                #size = (20, 20)
                import gv
                import os
                N = 10000
                D = np.prod(self._part_shape)
                size = (20, 20)
                grid = gv.plot.ImageGrid(size[0], size[1], self._part_shape)
                samp = mm.sample(n_samples=np.prod(size)).reshape((-1,) + self._part_shape)
                samples = mm.sample(n_samples=N).reshape((-1,) + self._part_shape)
                print('samples', samples.shape)

                types = np.asarray(list(gv.multirange(*[2]*D))).reshape((-1,) + self._part_shape)
                th = np.clip(types, 0.01, 0.99)

                t = th[:,np.newaxis]
                x = samples[np.newaxis]

                llh0 = x * np.log(t) + (1 - x) * np.log(1 - t)
                counts0 = np.bincount(np.argmax(llh0.sum(-1).sum(-1), 0), minlength=th.shape[0])
                
                x1 = patches[np.newaxis,...,0]
                llh1 = x1 * np.log(t) + (1 - x1) * np.log(1 - t)
                counts1 = np.bincount(np.argmax(llh1.sum(-1).sum(-1), 0), minlength=th.shape[0])

                #import pdb; pdb.set_trace()

                w0 = counts0 / counts0.sum()
                w1 = counts1 / counts1.sum()

                print('w0', w0)
                print('w1', w1) 
                #import pdb; pdb.set_trace()


            #import pdb; pdb.set_trace()
            self._parts = mm.means_.reshape((self._num_parts,)+patches.shape[1:])
            self._weights = mm.weights_
        else:
            #mm = ag.stats.BernoulliMixture(self._num_parts, flatpatches, max_iter=2000)
            #mm.run_EM(1e-6, min_probability=min_prob)
            #self._parts = mm.templates.reshape((self._num_parts,)+patches.shape[1:])
            #self._weights = mm.weights
            from pnet.bernoulli import em
            ret = em(flatpatches, self._num_parts,20,numpy_rng=self._settings.get('em_seed',0),verbose=True)
            self._parts = ret[1].reshape((self._num_parts,) + patches.shape[1:])
            self._weights = np.arange(self._num_parts)
        if 0:
            predictedGroups = mm.predict(flatpatches) 
        
        # Calculate entropy of parts
        Hall = (self._parts * np.log(self._parts) + (1 - self._parts) * np.log(1 - self._parts))
        H = -np.apply_over_axes(np.mean, Hall, [1, 2, 3])[:,0,0,0]
        Gs_ok = Gs[:,burnin:].reshape((-1, F))
        Zs_ok = np.exp(Gs_ok)

        # Now use samples to estimate
        G_mu = Gs_ok.mean(0)
        Z_mu = np.exp(G_mu)

        #g_mu = np.exp(G_mu)
        G_Sigma = np.cov(Gs_ok.T) + np.eye(F) * 0.0001

        # Find w through a binary search   
        #for f in xrange(10):
        if 0:
            for f in xrange(F): 
                print 'Doing', f
                for l0, l1 in gv.multirange(*w.shape[:2]):
                    def fun(w0):
                        return gv.sigmoid(w0 + gv.logit(Zs_ok[...,f])).mean() - Xbar[l0,l1,f]

                    w[l0,l1,f] = find_zero(fun, l=-6, u=6, depth=15)

        if 0:
            l0 = l1 = 4
            f = 10

            def fun(w0):
                return gv.sigmoid(w0 + gv.logit(Zs_ok[...,f])).mean() - Xbar[l0,l1,f]

            #import IPython
            #IPython.embed()