Exemplo n.º 1
0
def getScoreForHashVal((path_to_db, hash_table, hash_val, class_idx, video_idx,
                        class_idx_gt)):
    mani = Tube_Manipulator(path_to_db)
    mani.openSession()
    toSelect = (Tube.class_idx_pascal, Tube.video_id)
    criterion = (TubeHash.hash_table == hash_table,
                 TubeHash.hash_val == hash_val)
    vals = mani.selectMix(toSelect=toSelect, criterion=criterion)
    vals = np.array(vals)

    if not hasattr(class_idx, '__iter__'):
        class_idx = [class_idx]

    total_count_total = vals.shape[0]
    video_count_total = sum(
        np.logical_and(vals[:, 0] == class_idx_gt, vals[:, 1] == video_idx))

    scores = []
    for class_idx_curr in class_idx:
        class_count = sum(vals[:, 0] == class_idx_curr)
        if class_idx_curr == class_idx_gt:
            video_count = video_count_total
        else:
            video_count = 0

        class_count = class_count - video_count
        total_count = total_count_total - video_count_total
        score = class_count / float(total_count)
        scores.append(score)

    mani.closeSession()
    return scores
Exemplo n.º 2
0
def getScoreForHashVal((path_to_db,hash_table,hash_val,class_idx,video_idx,class_idx_gt)):
    mani=Tube_Manipulator(path_to_db);
    mani.openSession();
    toSelect=(Tube.class_idx_pascal,Tube.video_id)
    criterion=(TubeHash.hash_table==hash_table,TubeHash.hash_val==hash_val);
    vals=mani.selectMix(toSelect=toSelect,criterion=criterion);
    vals=np.array(vals);

    if not hasattr(class_idx,'__iter__'):
        class_idx=[class_idx];
    
    total_count_total=vals.shape[0];
    video_count_total=sum(np.logical_and(vals[:,0]==class_idx_gt,vals[:,1]==video_idx));

    scores=[];
    for class_idx_curr in class_idx:
        class_count=sum(vals[:,0]==class_idx_curr);
        if class_idx_curr==class_idx_gt:
            video_count=video_count_total;
        else:
            video_count=0;

        class_count=class_count-video_count;
        total_count=total_count_total-video_count_total;
        score=class_count/float(total_count);
        scores.append(score);

    mani.closeSession();
    return scores;
Exemplo n.º 3
0
def script_saveHashAnalysisImages(params):
    path_to_db = params.path_to_db
    class_labels_map = params.class_labels_map
    percents = params.percents
    out_file_class_pre = params.out_file_class_pre
    out_file_hash_simple = params.out_file_hash_simple
    out_file_hash_byClass = params.out_file_hash_byClass
    hashtable = params.hashtable
    inc = params.inc
    dtype = params.dtype
    # in_file = params.in_file;

    if not os.path.exists(out_file_class_pre + '.npz'):
        mani = Tube_Manipulator(path_to_db)

        mani.openSession()
        ids = mani.selectMix((Tube.class_idx_pascal, TubeHash.hash_val),
                             (TubeHash.hash_table == hashtable, ))
        mani.closeSession()

        ids = np.array(ids, dtype=dtype)
        np.savez(out_file_class_pre, ids)

    ids = np.load(out_file_class_pre + '.npz')['arr_0']
    # ids=np.load(in_file)['arr_0'];

    counts_all, class_ids_breakdown = getClassIdsCount(ids[:, 0], ids[:, 1])
    ranks = getDiscriminativeScore(counts_all)

    sort_idx = np.argsort(ranks)
    counts_all = [counts_all[idx] for idx in sort_idx]
    class_ids_breakdown = [class_ids_breakdown[idx] for idx in sort_idx]
    im_simple = getHashAnalysisIm(counts_all,
                                  class_ids_breakdown,
                                  inc=inc,
                                  colorByClass=False)
    im_byClass = getHashAnalysisIm(counts_all,
                                   class_ids_breakdown,
                                   inc=inc,
                                   colorByClass=True)

    visualize.saveMatAsImage(im_simple, out_file_hash_simple)
    visualize.saveMatAsImage(im_byClass, out_file_hash_byClass)

    counts_all_ravel = np.array([c for counts in counts_all for c in counts])
    class_ids_breakdown_ravel = np.array(
        [c for class_ids in class_ids_breakdown for c in class_ids])
    class_id_pascal, class_idx_pascal = zip(*class_labels_map)

    for class_id_idx, class_id in enumerate(class_idx_pascal):
        frequency = counts_all_ravel[class_ids_breakdown_ravel == class_id]
        out_file = out_file_class_pre + '_' + class_id_pascal[
            class_id_idx] + '.png'
        title = class_id_pascal[class_id_idx] + ' ' + str(class_id)
        cum_freq, idx_perc = getCumulativeInfo(frequency, percents)
        savePerClassCumulativeGraph(cum_freq / float(cum_freq[-1]), idx_perc,
                                    percents, out_file, title)
Exemplo n.º 4
0
def script_saveHashAnalysisImages(params):
    path_to_db = params.path_to_db;
    class_labels_map = params.class_labels_map;
    percents = params.percents;
    out_file_class_pre = params.out_file_class_pre;
    out_file_hash_simple = params.out_file_hash_simple;
    out_file_hash_byClass = params.out_file_hash_byClass;
    hashtable = params.hashtable;
    inc = params.inc;
    dtype = params.dtype;
    # in_file = params.in_file;

    if not os.path.exists(out_file_class_pre+'.npz'):
        mani=Tube_Manipulator(path_to_db);

        mani.openSession();
        ids=mani.selectMix((Tube.class_idx_pascal,TubeHash.hash_val),(TubeHash.hash_table==hashtable,));
        mani.closeSession();
        
        ids=np.array(ids,dtype=dtype);
        np.savez(out_file_class_pre,ids);

    ids=np.load(out_file_class_pre+'.npz')['arr_0'];
    # ids=np.load(in_file)['arr_0'];
    
    counts_all,class_ids_breakdown = getClassIdsCount(ids[:,0],ids[:,1]);
    ranks = getDiscriminativeScore(counts_all);

    sort_idx=np.argsort(ranks);
    counts_all=[counts_all[idx] for idx in sort_idx];
    class_ids_breakdown=[class_ids_breakdown[idx] for idx in sort_idx];
    im_simple = getHashAnalysisIm(counts_all,class_ids_breakdown,inc=inc,colorByClass=False);
    im_byClass = getHashAnalysisIm(counts_all,class_ids_breakdown,inc=inc,colorByClass=True);

    visualize.saveMatAsImage(im_simple,out_file_hash_simple)
    visualize.saveMatAsImage(im_byClass,out_file_hash_byClass)

    counts_all_ravel=np.array([c for counts in counts_all for c in counts]);
    class_ids_breakdown_ravel=np.array([c for class_ids in class_ids_breakdown for c in class_ids]);
    class_id_pascal,class_idx_pascal = zip(*class_labels_map);

    for class_id_idx,class_id in enumerate(class_idx_pascal):
        frequency = counts_all_ravel[class_ids_breakdown_ravel==class_id]
        out_file=out_file_class_pre+'_'+class_id_pascal[class_id_idx]+'.png'
        title=class_id_pascal[class_id_idx]+' '+str(class_id)        
        cum_freq,idx_perc=getCumulativeInfo(frequency,percents)
        savePerClassCumulativeGraph(cum_freq/float(cum_freq[-1]),idx_perc,percents,out_file,title)
Exemplo n.º 5
0
def getHashBinClassBreakdowns((hash_table,hash_val,path_to_db,out_file,idx)):
    print idx
    mani=Tube_Manipulator(path_to_db)
    mani.openSession();
    toSelect=(Tube.class_idx_pascal,Tube.video_id,Tube.shot_id,Tube.tube_id);
    criterion=(TubeHash.hash_table==hash_table,TubeHash.hash_val==hash_val);
    vals=mani.selectMix(toSelect,criterion=criterion,distinct=True);
    mani.closeSession();
    vals=np.array(vals);
    class_idx_db=vals[:,0];
    ids_db=vals[:,1:];

    column_names=['video','shot','tube'];
    counts=getClassCountsByIdType(class_idx_db,ids_db,column_names)
    # for k in counts.keys():
    #     for k2 in counts[k].keys():
    #         print k,k2,counts[k][k2];

    # return counts
    pickle.dump(counts,open(out_file,'wb'));
Exemplo n.º 6
0
def getHashBinClassBreakdowns((hash_table, hash_val, path_to_db, out_file,
                               idx)):
    print idx
    mani = Tube_Manipulator(path_to_db)
    mani.openSession()
    toSelect = (Tube.class_idx_pascal, Tube.video_id, Tube.shot_id,
                Tube.tube_id)
    criterion = (TubeHash.hash_table == hash_table,
                 TubeHash.hash_val == hash_val)
    vals = mani.selectMix(toSelect, criterion=criterion, distinct=True)
    mani.closeSession()
    vals = np.array(vals)
    class_idx_db = vals[:, 0]
    ids_db = vals[:, 1:]

    column_names = ['video', 'shot', 'tube']
    counts = getClassCountsByIdType(class_idx_db, ids_db, column_names)
    # for k in counts.keys():
    #     for k2 in counts[k].keys():
    #         print k,k2,counts[k][k2];

    # return counts
    pickle.dump(counts, open(out_file, 'wb'))
Exemplo n.º 7
0
def script_saveNpzScorePerShot_normalized(params):
    path_to_db = params['path_to_db']
    file_binCounts = params['file_binCounts']
    class_idx =  params['class_idx']
    video_id =  params['video_id']
    shot_id =  params['shot_id']
    out_file_scores =  params['out_file_scores']
    num_hash_tables = params['num_hash_tables']
    total_counts = params['total_class_counts']
    
    class_idx_assume = params.get('class_idx_assume',None);
    if class_idx_assume is None:
        class_idx_assume=class_idx;

    print params['idx']

    mani=Tube_Manipulator(path_to_db);

    mani.openSession();
    toSelect=(Tube.tube_id,Tube.deep_features_idx,TubeHash.hash_table,TubeHash.hash_val);
    criterion=(Tube.video_id==video_id,Tube.class_idx_pascal==class_idx,Tube.shot_id==shot_id);
    vals=mani.selectMix(toSelect,criterion);
    total_frames = getShotFrameCount(mani,class_idx,video_id,shot_id);
    mani.closeSession();

    hash_count_keys,hash_counts=pickle.load(open(file_binCounts,'rb'));

    hash_info=[tuple(r) for r in vals[:,2:]];
    hash_counts = dict(Counter(hash_info))
    # total_counts=np.sum(hash_counts,axis=0);

    scores_all={};
    vals=np.array(vals)
    tube_ids_uni=np.unique(vals[:,0]);

    for tube_id in tube_ids_uni:
        vals_rel=vals[vals[:,0]==tube_id,1:];
        deep_features_idx_uni = np.unique(vals_rel[:,0]);
        scores_tube=np.empty((len(deep_features_idx_uni),num_hash_tables));
        scores_tube[:]=np.nan;

        for deep_features_idx in deep_features_idx_uni:
            hash_info=vals_rel[vals_rel[:,0]==deep_features_idx,1:];
            
            assert len(hash_info)==num_hash_tables

            scores=[];
            
            for hash_info_curr in hash_info:
                idx_curr=hash_count_keys.index(tuple(hash_info_curr));
                counts_curr=hash_counts[idx_curr,:];
                deno=counts_curr/total_counts.astype(dtype=float);
                numo=deno[class_idx_assume];
                deno=sum(deno);
                score_curr=numo/float(deno);
                scores.append(score_curr);
            
            scores_tube[deep_features_idx,:]=scores;

        scores_all[tube_id]=scores_tube;

    for tube_id in scores_all:
        tube_scores=scores_all[tube_id];
        assert np.sum(np.isnan(tube_scores))==0;

    pickle.dump(scores_all,open(out_file_scores,'wb'));
Exemplo n.º 8
0
def script_saveNpzScorePerShot(params):
    path_to_db = params['path_to_db']
    total_class_counts = params['total_class_counts']
    class_idx =  params['class_idx']
    video_id =  params['video_id']
    shot_id =  params['shot_id']
    out_file_scores =  params['out_file_scores']
    path_to_hash = params['path_to_hash']
    num_hash_tables = params['num_hash_tables']
    
    class_idx_assume = params.get('class_idx_assume',None);
    if class_idx_assume is None:
        class_idx_assume=class_idx;

    print params['idx']

    # print 'getting vals and frame count from db',
    # t=time.time();
    mani=Tube_Manipulator(path_to_db);
    mani.openSession();
    toSelect=(Tube.deep_features_path,Tube.tube_id,Tube.deep_features_idx,TubeHash.hash_table,TubeHash.hash_val);
    criterion=(Tube.video_id==video_id,Tube.class_idx_pascal==class_idx,Tube.shot_id==shot_id);
    vals=mani.selectMix(toSelect,criterion);
    total_frames = getShotFrameCount(mani,class_idx,video_id,shot_id);
    mani.closeSession();
    # print time.time()-t

    # print 'getting hash_counts',
    # t=time.time();
    hash_info=[(tuple_curr[3],tuple_curr[4]) for tuple_curr in vals];
    hash_counts = dict(Counter(hash_info))
    # print time.time()-t

    # print 'getting hash_bin_scores',
    # t=time.time();
    if class_idx_assume ==class_idx:
        total_class_count=total_class_counts[class_idx_assume]-total_frames;
    else:
        total_class_count=total_class_counts[class_idx_assume];

    hash_bin_scores={};
    
    for idx,k in enumerate(hash_counts.keys()):
        in_file=str(k[0])+'_'+str(k[1])+'_counts.p'
        class_id_counts=pickle.load(open(os.path.join(path_to_hash,in_file),'rb'));
        
        if class_idx_assume ==class_idx:
            hash_bin_count=class_id_counts.get(class_idx_assume,0)-hash_counts[k];
        else:
            hash_bin_count=class_id_counts.get(class_idx_assume,0)

        hash_bin_scores[k]=hash_bin_count/float(total_class_count);
    # print time.time()-t;

    # print 'getting tube_scores_all',
    # t=time.time(); 
    vals_org=np.array(vals);
    deep_features_paths=vals_org[:,0];
    vals=np.array(vals_org[:,1:],dtype=int);
    # Tube.tube_id,Tube.deep_features_idx,TubeHash.hash_table,TubeHash.hash_val
    tube_ids=np.unique(vals[:,0])    
    # deep_features_idx=np.unique(vals[:,1]);
    tube_scores_all={};
    for tube_id in tube_ids:
        # print tube_id,len(deep_features_idx),len(np.unique(vals[vals[:,0]==tube_id,1]));
        deep_features_idx=np.unique(vals[vals[:,0]==tube_id,1])
        tube_scores_all[tube_id]=getTubeScoresMat(tube_id,vals,hash_bin_scores,deep_features_idx,num_hash_tables);
    # print time.time()-t;

    # for tube_id in tube_scores_all:
    #     tube_scores=tube_scores_all[tube_id];
    #     print tube_id,np.sum(np.isnan(tube_scores)),tube_scores.shape
    #     # print tube_scores[0,:]
    #     assert np.sum(np.isnan(tube_scores))==0;

    # out_file_temp='/disk2/temp/temp.p';
    # np.savez_compressed(out_file_scores,tube_scores_all.values(),tube_scores_all.keys())
    pickle.dump(tube_scores_all,open(out_file_scores,'wb'));
Exemplo n.º 9
0
def script_saveNpzScorePerShot(params):
    path_to_db = params['path_to_db']
    total_class_counts = params['total_class_counts']
    class_idx = params['class_idx']
    video_id = params['video_id']
    shot_id = params['shot_id']
    out_file_scores = params['out_file_scores']
    path_to_hash = params['path_to_hash']
    num_hash_tables = params['num_hash_tables']

    class_idx_assume = params.get('class_idx_assume', None)
    if class_idx_assume is None:
        class_idx_assume = class_idx

    print params['idx']

    # print 'getting vals and frame count from db',
    # t=time.time();
    mani = Tube_Manipulator(path_to_db)
    mani.openSession()
    toSelect = (Tube.deep_features_path, Tube.tube_id, Tube.deep_features_idx,
                TubeHash.hash_table, TubeHash.hash_val)
    criterion = (Tube.video_id == video_id, Tube.class_idx_pascal == class_idx,
                 Tube.shot_id == shot_id)
    vals = mani.selectMix(toSelect, criterion)
    total_frames = getShotFrameCount(mani, class_idx, video_id, shot_id)
    mani.closeSession()
    # print time.time()-t

    # print 'getting hash_counts',
    # t=time.time();
    hash_info = [(tuple_curr[3], tuple_curr[4]) for tuple_curr in vals]
    hash_counts = dict(Counter(hash_info))
    # print time.time()-t

    # print 'getting hash_bin_scores',
    # t=time.time();
    if class_idx_assume == class_idx:
        total_class_count = total_class_counts[class_idx_assume] - total_frames
    else:
        total_class_count = total_class_counts[class_idx_assume]

    hash_bin_scores = {}

    for idx, k in enumerate(hash_counts.keys()):
        in_file = str(k[0]) + '_' + str(k[1]) + '_counts.p'
        class_id_counts = pickle.load(
            open(os.path.join(path_to_hash, in_file), 'rb'))

        if class_idx_assume == class_idx:
            hash_bin_count = class_id_counts.get(class_idx_assume,
                                                 0) - hash_counts[k]
        else:
            hash_bin_count = class_id_counts.get(class_idx_assume, 0)

        hash_bin_scores[k] = hash_bin_count / float(total_class_count)
    # print time.time()-t;

    # print 'getting tube_scores_all',
    # t=time.time();
    vals_org = np.array(vals)
    deep_features_paths = vals_org[:, 0]
    vals = np.array(vals_org[:, 1:], dtype=int)
    # Tube.tube_id,Tube.deep_features_idx,TubeHash.hash_table,TubeHash.hash_val
    tube_ids = np.unique(vals[:, 0])
    # deep_features_idx=np.unique(vals[:,1]);
    tube_scores_all = {}
    for tube_id in tube_ids:
        # print tube_id,len(deep_features_idx),len(np.unique(vals[vals[:,0]==tube_id,1]));
        deep_features_idx = np.unique(vals[vals[:, 0] == tube_id, 1])
        tube_scores_all[tube_id] = getTubeScoresMat(tube_id, vals,
                                                    hash_bin_scores,
                                                    deep_features_idx,
                                                    num_hash_tables)
    # print time.time()-t;

    # for tube_id in tube_scores_all:
    #     tube_scores=tube_scores_all[tube_id];
    #     print tube_id,np.sum(np.isnan(tube_scores)),tube_scores.shape
    #     # print tube_scores[0,:]
    #     assert np.sum(np.isnan(tube_scores))==0;

    # out_file_temp='/disk2/temp/temp.p';
    # np.savez_compressed(out_file_scores,tube_scores_all.values(),tube_scores_all.keys())
    pickle.dump(tube_scores_all, open(out_file_scores, 'wb'))
Exemplo n.º 10
0
def script_saveNpzScorePerShot_normalized(params):
    path_to_db = params['path_to_db']
    file_binCounts = params['file_binCounts']
    class_idx = params['class_idx']
    video_id = params['video_id']
    shot_id = params['shot_id']
    out_file_scores = params['out_file_scores']
    num_hash_tables = params['num_hash_tables']
    total_counts = params['total_class_counts']

    class_idx_assume = params.get('class_idx_assume', None)
    if class_idx_assume is None:
        class_idx_assume = class_idx

    print params['idx']

    mani = Tube_Manipulator(path_to_db)

    mani.openSession()
    toSelect = (Tube.tube_id, Tube.deep_features_idx, TubeHash.hash_table,
                TubeHash.hash_val)
    criterion = (Tube.video_id == video_id, Tube.class_idx_pascal == class_idx,
                 Tube.shot_id == shot_id)
    vals = mani.selectMix(toSelect, criterion)
    total_frames = getShotFrameCount(mani, class_idx, video_id, shot_id)
    mani.closeSession()

    hash_count_keys, hash_counts = pickle.load(open(file_binCounts, 'rb'))

    hash_info = [tuple(r) for r in vals[:, 2:]]
    hash_counts = dict(Counter(hash_info))
    # total_counts=np.sum(hash_counts,axis=0);

    scores_all = {}
    vals = np.array(vals)
    tube_ids_uni = np.unique(vals[:, 0])

    for tube_id in tube_ids_uni:
        vals_rel = vals[vals[:, 0] == tube_id, 1:]
        deep_features_idx_uni = np.unique(vals_rel[:, 0])
        scores_tube = np.empty((len(deep_features_idx_uni), num_hash_tables))
        scores_tube[:] = np.nan

        for deep_features_idx in deep_features_idx_uni:
            hash_info = vals_rel[vals_rel[:, 0] == deep_features_idx, 1:]

            assert len(hash_info) == num_hash_tables

            scores = []

            for hash_info_curr in hash_info:
                idx_curr = hash_count_keys.index(tuple(hash_info_curr))
                counts_curr = hash_counts[idx_curr, :]
                deno = counts_curr / total_counts.astype(dtype=float)
                numo = deno[class_idx_assume]
                deno = sum(deno)
                score_curr = numo / float(deno)
                scores.append(score_curr)

            scores_tube[deep_features_idx, :] = scores

        scores_all[tube_id] = scores_tube

    for tube_id in scores_all:
        tube_scores = scores_all[tube_id]
        assert np.sum(np.isnan(tube_scores)) == 0

    pickle.dump(scores_all, open(out_file_scores, 'wb'))