def main(): # load from downloads with open('../data/annotations/downloads/info.json') as f: full_data_list = json.load(f) with open('../data/annotations/downloads/val.jsonl') as f: val = f.read().splitlines() # make infomation of data list data_list = { 'train': full_data_list['train'], 'val': [], 'test_cls': full_data_list['val'], 'test_det': full_data_list['val'], } with open(settings.DATA_LIST, 'w') as f: json.dump(data_list, f, indent=2) # copy training set shutil.copy('../data/annotations/downloads/train.jsonl', settings.TRAIN) # create empty validation set with open(settings.VAL, 'w') as f: pass # create testing set for classification with open(settings.TEST_CLASSIFICATION, 'w') as f, open(settings.TEST_CLASSIFICATION_GT, 'w') as fgt: for line in val: anno = json.loads(line.strip()) proposals = [] gt = [] for char in anno_tools.each_char(anno): if not char['is_chinese']: continue proposals.append({ 'adjusted_bbox': char['adjusted_bbox'], 'polygon': char['polygon'] }) gt.append({ 'text': char['text'], 'attributes': char['attributes'], 'size': char['adjusted_bbox'][-2:] }) anno.pop('annotations') anno.pop('ignore') anno['proposals'] = proposals f.write(common_tools.to_jsonl(anno)) f.write('\n') anno.pop('proposals') anno['ground_truth'] = gt fgt.write(common_tools.to_jsonl(anno)) fgt.write('\n') # create testing set for detection shutil.copy('../data/annotations/downloads/val.jsonl', settings.TEST_DETECTION_GT)
def write(nms_sorted, file_path): with open(settings.CATES) as f: cates = json.load(f) with open(settings.DATA_LIST) as f: data_list = json.load(f) test_det = data_list['test_det'] with open(file_path, 'w') as f: for o in test_det: image_id = o['image_id'] detections = nms_sorted[image_id] detections.sort( key=lambda o: (-o['prob'], o['cate_id'], o['bbox'])) f.write(common_tools.to_jsonl({ 'image_id': image_id, 'detections': [{ 'text': cates[dt['cate_id']]['text'], 'bbox': dt['bbox'], 'score': dt['prob'], } for dt in detections if dt['cate_id'] < settings.NUM_CHAR_CATES][:settings.MAX_DET_PER_IMAGE], 'proposals': [{ # for printtext 'text': '', 'bbox': dt['bbox'], 'score': dt['prob'], } for dt in detections if dt['cate_id'] == settings.NUM_CHAR_CATES][:settings.MAX_DET_PER_IMAGE], })) f.write('\n')
def write(nms_sorted, file_path): with open(settings.CATES) as f: cates = json.load(f) testPath_list = glob.glob(os.path.join(settings.TEST_IMAGE_DIR, '*.jpg')) testPath_list += glob.glob(os.path.join(settings.TEST_IMAGE_DIR, '*.png')) with open(file_path, 'w') as f: for testPath in testPath_list: fileName = os.path.basename(testPath) image_id = os.path.splitext(fileName)[0] detections = nms_sorted[image_id] detections.sort( key=lambda o: (-o['prob'], o['cate_id'], o['bbox'])) f.write(common_tools.to_jsonl({ 'file_name': fileName, 'image_id': image_id, 'detections': [{ 'text': cates[dt['cate_id']]['text'], 'bbox': dt['bbox'], 'score': dt['prob'], } for dt in detections if dt['cate_id'] < settings.NUM_CHAR_CATES][:settings.MAX_DET_PER_IMAGE], 'proposals': [{ # for printtext 'text': '', 'bbox': dt['bbox'], 'score': dt['prob'], } for dt in detections if dt['cate_id'] == settings.NUM_CHAR_CATES][:settings.MAX_DET_PER_IMAGE], })) f.write('\n')
def qt_print_text(in_file_name, out_file_name, obj): if not os.path.isfile(settings.PRINTTEXT_EXEC): compile() args = [settings.PRINTTEXT_EXEC, in_file_name, out_file_name] print(*args) p = subprocess.Popen(args, stdin=subprocess.PIPE) p.communicate('{}\n'.format(common_tools.to_jsonl(obj)).encode()) p.wait() assert 0 == p.returncode
def main(model_name, max_prediction): assert six.PY3 with open(settings.CATES) as f: cates = json.load(f) cfg_model = list(filter(lambda o: o['model_name'] == model_name, eval.cfgs))[0] eval_dir = cfg_model['eval_dir'] with open(eval_dir, 'rb') as f: data = cPickle.load(f) for k in sorted(data.keys()): if k != 'logits': print(k, data[k]) logits = data['logits'] assert settings.NUM_CHAR_CATES + 1 == logits.shape[1] logits = logits[:, :settings.NUM_CHAR_CATES] explogits = np.exp(np.minimum(logits, 70)) expsums = np.sum(explogits, axis=1) expsums.shape = (logits.shape[0], 1) expsums = np.repeat(expsums, settings.NUM_CHAR_CATES, axis=1) probs = explogits / expsums argsorts = np.argsort(-logits, axis=1) lo = 0 pred_file_name = os.path.join(settings.PRODUCTS_ROOT, 'predictions_{}.jsonl'.format(model_name)) with open(settings.TEST_CLASSIFICATION) as f, open(pred_file_name, 'w') as fout: for line in f: image_info = json.loads(line.strip()) n = len(image_info['proposals']) predictions = [] probabilities = [] for i in range(n): pred = argsorts[lo][:max_prediction] prob = probs[lo][pred].tolist() pred = list(map(lambda i: cates[i]['text'], pred.tolist())) predictions.append(pred) probabilities.append(prob) lo += 1 fout.write( common_tools.to_jsonl({ 'predictions': predictions, 'probabilities': probabilities, })) fout.write('\n') assert lo == logits.shape[0]
def main(): allowed_filename = get_allowed_filename() basename2cnt = dict() imgid2anno = dict() with codecs.open(settings.OVERALL_ANNOTATIONS, 'r', 'utf-8') as f: for line in f: image_anno = json.loads(line.strip()) image_id = image_anno['image_id'] if image_id[:3] == '204': logo_bbox = [810., 1800., 460., 100.] image_anno['ignore'].append({ 'bbox': logo_bbox, 'polygon': bbox2poly(logo_bbox), }) else: logo_bbox = None for i, blk in enumerate(image_anno['annotations']): image_anno['annotations'][i] = list( filter( is_legal_char( (image_anno['height'], image_anno['width'], 3), logo_bbox), blk)) image_anno['annotations'] = list( filter(lambda a: a, image_anno['annotations'])) direction = image_id[0] onew = image_id[1:] oold = mapper.new2old[onew] complete_basename = '{}.{}'.format(oold, direction) if image_id[ 1:3] != '04' and complete_basename not in allowed_filename: continue is_chinese = 0 is_not_chinese = 0 for char in anno_tools.each_char(image_anno): if char['is_chinese']: is_chinese += 1 else: is_not_chinese += 1 if 0 == is_chinese: continue basename2cnt[complete_basename] = [is_chinese, is_not_chinese] imgid2anno[image_id] = image_anno streets = [] current_street = [] def feature(basename): assert 25 == len(basename) cityid = basename[:4] streetid = basename[4:8] dateid = basename[8:14] timeid = basename[14:23] direction = basename[-1] return (cityid, streetid, dateid, timeid, direction) def like(n1, n2): f1, f2 = feature(n1), feature(n2) return (f1[0], f1[1], f1[2]) == (f2[0], f2[1], f2[2]) for i, basename in enumerate(sorted(basename2cnt.keys())): if i == 0 or like(last_basename, basename): current_street.append(basename) else: streets.append(current_street) current_street = [basename] last_basename = basename streets.append(current_street) def count_chinese(a): return sum([basename2cnt[basename][0] for basename in a]) np.random.seed(0) train = [] tests = [[], [], []] requires = [101000, 101000, 50500] basenames = sorted(mapper.old2new.keys()) for i, st in enumerate(streets): ntest = 100 nval = 50 ntrainval = 1200 ninterval = 5 if ntest * 2 + ninterval + ntrainval < len(st): while 0 < len(st): j = np.random.randint(0, 2) tests[j] += st[0:ntest] tests[1 - j] += st[ntest:ntest * 2] lastname = st[ntest * 2 - 1].split('.')[0] lastpos = bisect.bisect(basenames, lastname) - 1 assert basenames[lastpos] == lastname st = st[ntest * 2:] j = 0 while True: name = st[j].split('.')[0] pos = bisect.bisect(basenames, name) - 1 assert basenames[pos] == name if pos - lastpos <= ninterval: j += 1 else: break st = st[j:] hi = ntrainval if len(st) < ntrainval * (math.sqrt(5) + 1) / 2 + ntest * 2 + ninterval * 2: hi = len(st) j = np.random.randint(0, 2) train += st[j * nval:hi - nval + j * nval] tests[2] += st[0:j * nval] + st[hi - nval + j * nval:hi] lastname = st[hi - 1].split('.')[0] st = st[hi:] if 0 < len(st): lastpos = bisect.bisect(basenames, lastname) - 1 j = 0 while True: name = st[j].split('.')[0] pos = bisect.bisect(basenames, name) - 1 assert (basenames[pos] == name) if pos - lastpos <= ninterval: j += 1 else: break st = st[j:] streets[i] = [] counts = [count_chinese(test) for test in tests] while (np.array(counts) < np.array(requires)).any(): i = np.random.randint(0, len(streets)) j = np.random.randint(0, len(requires)) if counts[j] >= requires[j]: continue st = streets[i] if 300 < len(st): continue tests[j] += streets.pop(i) counts[j] = count_chinese(tests[j]) for st in streets: train += st print('train', len(train), count_chinese(train)) print('val', len(tests[2]), count_chinese(tests[2])) print('test_1', len(tests[0]), count_chinese(tests[0])) print('test_2', len(tests[1]), count_chinese(tests[1])) def basename2imgid(bn): a, direction = bn.split('.') return '{}{}'.format(direction, mapper.old2new[a]) train = sorted(map(basename2imgid, train)) val = sorted(map(basename2imgid, tests[2])) test_1 = sorted(map(basename2imgid, tests[0])) test_2 = sorted(map(basename2imgid, tests[1])) def toinfo(anno): keys = list(anno.keys()) keys.remove('annotations') keys.remove('ignore') return {k: anno[k] for k in keys} with open(settings.DATA_LIST, 'w') as f: def g(ds): return [toinfo(imgid2anno[imgid]) for imgid in ds] r = { 'train': g(train), 'val': g(val), 'test_cls': g(test_1), 'test_det': g(test_2) } json.dump(r, f, ensure_ascii=True, allow_nan=False, indent=2, sort_keys=True) with open(settings.TRAIN, 'w') as f: for imgid in train: f.write(common_tools.to_jsonl(imgid2anno[imgid])) f.write('\n') with open(settings.VAL, 'w') as f: for imgid in val: f.write(common_tools.to_jsonl(imgid2anno[imgid])) f.write('\n') with open(settings.TEST_CLASSIFICATION, 'w') as f, open(settings.TEST_CLASSIFICATION_GT, 'w') as fgt: for imgid in test_1: anno = imgid2anno[imgid] proposals = [] gt = [] for char in anno_tools.each_char(anno): if not char['is_chinese']: continue proposals.append({ 'adjusted_bbox': char['adjusted_bbox'], 'polygon': char['polygon'] }) gt.append({ 'text': char['text'], 'attributes': char['attributes'], 'size': char['adjusted_bbox'][-2:] }) anno.pop('annotations') anno.pop('ignore') anno['proposals'] = proposals f.write(common_tools.to_jsonl(anno)) f.write('\n') anno.pop('proposals') anno['ground_truth'] = gt fgt.write(common_tools.to_jsonl(anno)) fgt.write('\n') with open(settings.TEST_DETECTION_GT, 'w') as f: for imgid in test_2: f.write(common_tools.to_jsonl(imgid2anno[imgid])) f.write('\n')