import json, random, collections, urllib.request, os d = json.load(open("instances_val2017.json")) cats = {c["id"]: c["name"] for c in d["categories"]} imgs = {i["id"]: i for i in d["images"]} by = collections.defaultdict(list) for a in d["annotations"]: if a["iscrowd"]: continue by[a["image_id"]].append(a) rng = random.Random(0); cand = [] for iid, anns in sorted(by.items()): W, H = imgs[iid]["width"], imgs[iid]["height"] cnt = collections.Counter(a["category_id"] for a in anns) cid, n = cnt.most_common(1)[0] g = [a for a in anns if a["category_id"] == cid] if not (1 <= n <= 8): continue if any(a["bbox"][2] * a["bbox"][3] < 32 * 32 for a in g): continue boxes = [[a["bbox"][0]/W*1000, a["bbox"][1]/H*1000, (a["bbox"][0]+a["bbox"][2])/W*1000, (a["bbox"][1]+a["bbox"][3])/H*1000] for a in g] cand.append({"id": iid, "file": "img/%012d.jpg" % iid, "cat": cats[cid], "n_gt": n, "boxes": boxes, "w": W, "h": H}) rng.shuffle(cand); sel = cand[:100] for s in sel: if not os.path.exists(s["file"]): urllib.request.urlretrieve("http://images.cocodataset.org/val2017/%012d.jpg" % s["id"], s["file"]) json.dump(sel, open("eval_set.json", "w")) print("candidates", len(cand), "selected", len(sel), "mean GT/img", sum(s["n_gt"] for s in sel)/len(sel)) print(collections.Counter(s["cat"] for s in sel).most_common(8))