Exemplo n.º 1
0
def process(options, collection, annotationName):
    rootpath = options.rootpath
    overwrite = options.overwrite
    neg_filter = options.neg_filter
    
    concepts = readConcepts(collection, annotationName, rootpath)
    newAnnotationName = annotationName[:-4] + 'social.txt'
    ne = STRING_TO_NEGATIVE_ENGINE[neg_filter](collection, rootpath)

    newConcepts = []
    for concept in concepts:
        resultfile = os.path.join(rootpath, collection, 'Annotations', 'Image', newAnnotationName, '%s.txt'%concept)
        if checkToSkip(resultfile, overwrite):
            newConcepts.append(concept)
            continue

        try:
            pos_set = readLabeledImageSet(collection, concept, tpp='lemm', rootpath=rootpath)
        except:
            pos_set = None 
        if not pos_set:
            printStatus(INFO, '*** %s has not labeled examples, will be ignored ***' % concept)
            continue
        neg_set = ne.sample(concept, int(1e8))
        assert(len(set(pos_set).intersection(set(neg_set))) == 0)
        newlabels = [1] * len(pos_set) + [-1] * len(neg_set)
        newnames = pos_set + neg_set
        printStatus(INFO, "anno(%s) %d pos %d neg -> %s" % (concept,len(pos_set),len(neg_set),resultfile))
        writeAnnotations(newnames, newlabels, resultfile)
        newConcepts.append(concept)

    writeConceptsTo(newConcepts, collection, newAnnotationName, rootpath)
Exemplo n.º 2
0
    def GET(self):
        input = web.input(query=None)
        resp = {
            'status': 0,
            'hits': 0,
            'random': [],
            'tagrel': [],
            'metric': metric,
            'perf': 0
        }

        if input.query:
            resp['status'] = 1
            resp['query'] = input.query
            query = input.query.lower()

            if query.isdigit():  # request to view a specific image
                resp['hits'] = 1
                resp['tagrel'] = [{'id': query}]
                return render.index(resp)

            try:
                names, labels = readAnnotationsFrom(collection, annotationName,
                                                    query)
                name2label = dict(zip(names, labels))
            except Exception, e:
                name2label = {}

            content = []
            try:
                if input.tagrel == '0':
                    labeled = readLabeledImageSet(collection,
                                                  query,
                                                  rootpath=rootpath)
                    ranklist = [(x, 0) for x in labeled]
                else:
                    simfile = os.path.join(simdir, '%s.txt' % query)
                    ranklist = readRankingResults(simfile)
                resp['hits'] = len(ranklist)
                for name, score in ranklist:
                    color = 'Chartreuse' if name2label.get(name,
                                                           0) > 0 else 'red'
                    color = 'white' if name not in name2label else color
                    res = {'id': name, 'color': color}
                    content.append(res)
                resp['perf'] = 0 if not name2label else scorer.score(
                    [name2label[x[0]] for x in ranklist if x[0] in name2label])
                resp['tagrel'] = content[:max_hits]
            except:
                None
Exemplo n.º 3
0
def process(options, testCollection, annotationName, tagvotefile):
    rootpath = options.rootpath
    tpp = options.tpp
    tagged = options.tagged
    overwrite = options.overwrite

    resultdir = generate_result_dir(options, testCollection, tagvotefile)
    
    concepts = readConcepts(testCollection, annotationName, rootpath)
    todo = []
    for concept in concepts:
        resfile = os.path.join(resultdir, '%s.txt'%concept)
        if checkToSkip(resfile, overwrite):
            continue
        todo.append(concept)

    if not todo:
        print ('nothing to do')
        return 0

    nr_of_concepts = len(todo)
    labeled_set = [None] * nr_of_concepts
    if tagged:
        for i in range(nr_of_concepts):
            labeled_set[i] = set(readLabeledImageSet(testCollection, todo[i], tpp, rootpath))
        
    concept2index = dict(zip(todo, range(nr_of_concepts)))
    ranklists = [[] for i in range(nr_of_concepts)]

    for line in open(tagvotefile):
        elems = line.strip().split()
        imageid = elems[0]
        del elems[0]
        assert(len(elems)%2==0)

        for i in range(0, len(elems), 2):
            tag = elems[i]
            c = concept2index.get(tag, -1)
            if c >= 0:
                if tagged and imageid not in labeled_set[c]:
                    continue
                score = float(elems[i+1])
                ranklists[c].append((imageid,score))

    for i in range(nr_of_concepts):
        concept = todo[i]
        resfile = os.path.join(resultdir, '%s.txt'%concept)
        ranklist = sorted(ranklists[i], key=lambda v:(v[1], v[0]), reverse=True)
        print ('%s %d -> %s' % (concept, len(ranklist), resfile))
        writeRankingResults(ranklist, resfile)
Exemplo n.º 4
0
    def GET(self):
        input = web.input(query=None)
        resp = {'status':0, 'hits':0, 'random':[], 'tagrel':[], 'metric':metric, 'perf':0}

        if input.query:
            resp['status'] = 1
            resp['query'] = input.query
            query = input.query.lower()

            if query.isdigit(): # request to view a specific image
                resp['hits'] = 1
                resp['tagrel'] = [{'id':query}]
                return  render.index(resp)
            
            try:
                names,labels = readAnnotationsFrom(collection, annotationName, query)
                name2label = dict(zip(names,labels))
            except Exception, e:
                name2label = {}

            content = []
            try:
                if input.tagrel == '0':
                    labeled = readLabeledImageSet(collection, query, rootpath=rootpath)
                    ranklist = [(x,0) for x in labeled]
                else:
                    simfile = os.path.join(simdir, '%s.txt' % query)
                    ranklist = readRankingResults(simfile)
                resp['hits'] = len(ranklist)
                for name,score in ranklist:
                    color = 'Chartreuse' if name2label.get(name,0)>0 else 'red'
                    color = 'white' if name not in name2label else color
                    res = {'id':name, 'color':color}
                    content.append(res)
                resp['perf'] = 0 if not name2label else scorer.score([name2label[x[0]] for x in ranklist if x[0] in name2label])
                resp['tagrel'] = content[:max_hits]
            except:
                None