File size: 3,127 Bytes
d0e7c02
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Evaluation sets outside the training data.

  screenspot   ScreenSpot (bevaya/ScreenSpot, test, 1,272), shuffled with seed 0: clicks and element boxes
  coco17       COCO val2017 photos (RefCOCO is all train2014): objects prompted by category name, for
               categories with exactly one instance in the image, at least 1% of it, polygon masks only
  fashion      Fashionpedia val: one garment per image, by name (a new domain; boxes only)

Point COCO17_DIR at a folder with annotations/instances_val2017.json and val2017/
(recipes/fetch_coco17.sh).
"""

import json, os, random

COCO17 = os.environ.get("COCO17_DIR", "/workspace/data/coco17")
MIN_AREA = 0.01
GARMENTS = 27  # Fashionpedia classes 0-26 are whole garments and accessories; the rest are parts


def screenspot(n=0):
    from datasets import load_dataset
    ds = load_dataset("bevaya/ScreenSpot", split="test").shuffle(seed=0)
    ds = ds.select(range(n)) if n else ds
    return [dict(image=r["image"], instruction=r["instruction"], box=list(r["bbox"]), norm=True,
                 group=(r["data_source"], r["data_type"])) for r in ds]


def coco17(n, seed=0):
    d = json.load(open(f"{COCO17}/annotations/instances_val2017.json"))
    cats = {c["id"]: c["name"] for c in d["categories"]}
    imgs = {i["id"]: i for i in d["images"]}
    by = {}
    for a in d["annotations"]:
        by.setdefault((a["image_id"], a["category_id"]), []).append(a)
    out = []
    for (iid, cid), anns in sorted(by.items()):
        if len(anns) != 1:
            continue
        a, im = anns[0], imgs[iid]
        W, H = im["width"], im["height"]
        if a["iscrowd"] or not isinstance(a["segmentation"], list) or a["area"] < MIN_AREA * W * H:
            continue
        x, y, w, h = a["bbox"]
        out.append(dict(image=f"{COCO17}/val2017/{im['file_name']}", size=(W, H), sentence=cats[cid],
                        instruction=cats[cid], polys=a["segmentation"], bbox=[x, y, x + w, y + h],
                        box=[x / W, y / H, (x + w) / W, (y + h) / H], norm=True, ref_id=a["id"], source="coco17"))
    random.Random(seed).shuffle(out)
    return out[:n] if n else out


def fashion(n, seed=0):
    from datasets import load_dataset
    ds = load_dataset("detection-datasets/fashionpedia", split="val")
    names = ds.features["objects"]["category"].feature.names
    idx = list(range(len(ds)))
    random.Random(seed).shuffle(idx)
    out = []
    for i in idx:
        r = ds[i]
        W, H, o = r["width"], r["height"], r["objects"]
        counts = {}
        for c in o["category"]:
            counts[c] = counts.get(c, 0) + 1
        for c, (x1, y1, x2, y2) in zip(o["category"], o["bbox"]):  # x1, y1, x2, y2 in pixels
            if c < GARMENTS and counts[c] == 1 and (x2 - x1) * (y2 - y1) >= MIN_AREA * W * H:
                out.append(dict(image=r["image"], size=(W, H), instruction=f"the {names[c].split(',')[0]}",
                                box=[x1 / W, y1 / H, x2 / W, y2 / H], norm=True, source="fashionpedia"))
                break  # one object per image
        if n and len(out) >= n:
            break
    return out