def process(options, collection, annotationName, pos_num): assert(annotationName.endswith('.txt')) rootpath = options.rootpath pos_bag_num = options.pos_bag_num neg_bag_num = options.neg_bag_num neg_pos_ratio = options.neg_pos_ratio annotationNameStr = annotationName[:-4] + ('.random%d' % pos_num) + '.%d' + ('.npr%d' % neg_pos_ratio) + '.%d.txt' concepts = readConcepts(collection, annotationName, rootpath=rootpath) skip = 0 newAnnotationNames = [None] * (pos_bag_num * neg_bag_num) for idxp in range(pos_bag_num): for idxn in range(neg_bag_num): anno_idx = idxp * neg_bag_num + idxn newAnnotationNames[anno_idx] = annotationNameStr % (idxp, idxn) resultfile = os.path.join(rootpath,collection,'Annotations',newAnnotationNames[anno_idx]) if checkToSkip(resultfile, options.overwrite): skip += 1 continue writeConcepts(concepts,resultfile) first,second,last = annotationNameStr.split('%d') scriptfile = os.path.join(rootpath,collection,'annotationfiles',first + '0-%d'%(pos_bag_num-1) + second + '0-%d'%(neg_bag_num-1) + last) makedirsforfile(scriptfile) fout = open(scriptfile,'w') fout.write('\n'.join(newAnnotationNames) + '\n') fout.close() if len(newAnnotationNames) == skip: return 0 for concept in concepts: names,labels = readAnnotationsFrom(collection, annotationName, concept, skip_0=True, rootpath=rootpath) positivePool = [x[0] for x in zip(names,labels) if x[1]>0] negativePool = [x[0] for x in zip(names,labels) if x[1]<0] for idxp in range(pos_bag_num): if len(positivePool) > pos_num: positiveBag = random.sample(positivePool, pos_num) else: positiveBag = positivePool for idxn in range(neg_bag_num): anno_idx = idxp * neg_bag_num + idxn newAnnotationName = newAnnotationNames[anno_idx] resultfile = os.path.join(rootpath,collection,'Annotations','Image',newAnnotationName,'%s.txt'%concept) if checkToSkip(resultfile, options.overwrite): continue real_neg_num = max(len(positiveBag) * neg_pos_ratio, 1000) real_neg_num = min(len(negativePool), real_neg_num) negativeBag = random.sample(negativePool, real_neg_num) assert(len(set(positiveBag).intersection(set(negativeBag))) == 0) printStatus(INFO, "anno(%s,%d) %d pos %d neg -> %s" % (concept,anno_idx,len(positiveBag),len(negativeBag),resultfile)) writeAnnotations(positiveBag + negativeBag, [1]*len(positiveBag) + [-1]*len(negativeBag), resultfile)
concepts = readConcepts(collection, annotationName) annotationNameStr = generate_new_annotation_template(cmdOpts) nr_skipped = 0 newAnnotationNames = [None] * (nr_pos_bags * nr_neg_bags) for idxp in range(nr_pos_bags): for idxn in range(nr_neg_bags): anno_idx = idxp * nr_neg_bags + idxn newAnnotationNames[anno_idx] = annotationNameStr % (idxp, idxn) resultfile = os.path.join(rootpath, collection, 'Annotations', newAnnotationNames[anno_idx]) if checkToSkip(resultfile, overwrite): nr_skipped += 1 continue writeConcepts(concepts, resultfile) first, second, last = annotationNameStr.split('%d') scriptfile = os.path.join( rootpath, collection, 'annotationfiles', first + '0-%d' % (nr_pos_bags - 1) + second + '0-%d' % (nr_neg_bags - 1) + last) makedirsforfile(scriptfile) fout = open(scriptfile, 'w') fout.write('\n'.join(newAnnotationNames) + '\n') fout.close() if nr_skipped == (nr_pos_bags * nr_neg_bags): sys.exit(0) if select_pos == 'random': pe = PositiveEngine(collection)
concepts = readConcepts(collection, annotationName) annotationNameStr = generate_new_annotation_template(cmdOpts) nr_skipped = 0 newAnnotationNames = [None] * (nr_pos_bags * nr_neg_bags) for idxp in range(nr_pos_bags): for idxn in range(nr_neg_bags): anno_idx = idxp * nr_neg_bags + idxn newAnnotationNames[anno_idx] = annotationNameStr % (idxp, idxn) resultfile = os.path.join(rootpath,collection,'Annotations',newAnnotationNames[anno_idx]) if checkToSkip(resultfile,overwrite): nr_skipped += 1 continue writeConcepts(concepts,resultfile) first,second,last = annotationNameStr.split('%d') scriptfile = os.path.join(rootpath,collection,'annotationfiles',first + '0-%d'%(nr_pos_bags-1) + second + '0-%d'%(nr_neg_bags-1) + last) makedirsforfile(scriptfile) fout = open(scriptfile,'w') fout.write('\n'.join(newAnnotationNames) + '\n') fout.close() if nr_skipped == (nr_pos_bags * nr_neg_bags): sys.exit(0) if select_pos == 'random': pe = PositiveEngine(collection) else:
def process(options, collection, annotationName, pos_num): assert (annotationName.endswith('.txt')) rootpath = options.rootpath pos_bag_num = options.pos_bag_num neg_bag_num = options.neg_bag_num neg_pos_ratio = options.neg_pos_ratio annotationNameStr = annotationName[:-4] + ( '.random%d' % pos_num) + '.%d' + ('.npr%d' % neg_pos_ratio) + '.%d.txt' concepts = readConcepts(collection, annotationName, rootpath=rootpath) skip = 0 newAnnotationNames = [None] * (pos_bag_num * neg_bag_num) for idxp in range(pos_bag_num): for idxn in range(neg_bag_num): anno_idx = idxp * neg_bag_num + idxn newAnnotationNames[anno_idx] = annotationNameStr % (idxp, idxn) resultfile = os.path.join(rootpath, collection, 'Annotations', newAnnotationNames[anno_idx]) if checkToSkip(resultfile, options.overwrite): skip += 1 continue writeConcepts(concepts, resultfile) first, second, last = annotationNameStr.split('%d') scriptfile = os.path.join( rootpath, collection, 'annotationfiles', first + '0-%d' % (pos_bag_num - 1) + second + '0-%d' % (neg_bag_num - 1) + last) makedirsforfile(scriptfile) fout = open(scriptfile, 'w') fout.write('\n'.join(newAnnotationNames) + '\n') fout.close() if len(newAnnotationNames) == skip: return 0 for concept in concepts: names, labels = readAnnotationsFrom(collection, annotationName, concept, skip_0=True, rootpath=rootpath) positivePool = [x[0] for x in zip(names, labels) if x[1] > 0] negativePool = [x[0] for x in zip(names, labels) if x[1] < 0] for idxp in range(pos_bag_num): if len(positivePool) > pos_num: positiveBag = random.sample(positivePool, pos_num) else: positiveBag = positivePool for idxn in range(neg_bag_num): anno_idx = idxp * neg_bag_num + idxn newAnnotationName = newAnnotationNames[anno_idx] resultfile = os.path.join(rootpath, collection, 'Annotations', 'Image', newAnnotationName, '%s.txt' % concept) if checkToSkip(resultfile, options.overwrite): continue real_neg_num = max(len(positiveBag) * neg_pos_ratio, 1000) real_neg_num = min(len(negativePool), real_neg_num) negativeBag = random.sample(negativePool, real_neg_num) assert (len(set(positiveBag).intersection( set(negativeBag))) == 0) printStatus( INFO, "anno(%s,%d) %d pos %d neg -> %s" % (concept, anno_idx, len(positiveBag), len(negativeBag), resultfile)) writeAnnotations(positiveBag + negativeBag, [1] * len(positiveBag) + [-1] * len(negativeBag), resultfile)