def map_object(object_map, obj): """ Use object ids as hashes to `visual_genome.models.Object` instances. If item not in table, create new `Object`. Used when building scene graphs from json. """ oid = obj['object_id'] obj['id'] = oid del obj['object_id'] if oid in object_map: object_ = object_map[oid] else: if 'attributes' in obj: attrs = obj['attributes'] del obj['attributes'] else: attrs = [] if 'w' in obj: obj['width'] = obj['w'] obj['height'] = obj['h'] del obj['w'], obj['h'] object_ = Object(**obj) object_.attributes = attrs object_map[oid] = object_ return object_map, object_
def map_object(object_map, obj): """ Use object ids as hashes to `visual_genome.models.Object` instances. If item not in table, create new `Object`. Used when building scene graphs from json. """ oid = ("gw_{}".format(obj["object_id"]) if obj.get("guesswhat", False) else obj["object_id"]) obj["id"] = oid del obj["object_id"] if oid in object_map: object_ = object_map[oid] else: if "attributes" in obj: attrs = obj["attributes"] del obj["attributes"] else: attrs = [] if "abstract_attributes" in obj: abs_attrs = obj["abstract_attributes"] del obj["abstract_attributes"] else: abs_attrs = [] if "situated_attributes" in obj: sit_attrs = obj["situated_attributes"] del obj["situated_attributes"] else: sit_attrs = [] if "w" in obj: obj["width"] = obj["w"] obj["height"] = obj["h"] del obj["w"], obj["h"] if "guesswhat" in obj: obj["guesswhat"] = True else: obj["guesswhat"] = False object_ = Object(**obj) object_.attributes = attrs object_.abstract_attributes = abs_attrs object_.situated_attributes = sit_attrs object_map[oid] = object_ return object_map, object_
def parse_graph_local(data, image, verbose=False): """ Modified version of `utils.ParseGraph`. """ global count_skips objects = [] object_map = {} relationships = [] attributes = [] for obj in data['objects']: object_map, o_ = map_object(object_map, obj) objects.append(o_) for rel in data['relationships']: if rel['subject_id'] in object_map and rel['object_id'] in object_map: object_map, s = map_object(object_map, {'object_id': rel['subject_id']}) v = rel['predicate'] object_map, o = map_object(object_map, {'object_id': rel['object_id']}) rid = rel['relationship_id'] relationships.append(Relationship(rid, s, v, o, rel['synsets'])) else: # Skip this relationship if we don't have the subject and object in # the object_map for this scene graph. Some data is missing in this # way. count_skips[0] += 1 if 'attributes' in data: for attr in data['attributes']: a = attr['attribute'] if a['object_id'] in object_map: if 'attributes' in a: attributes.append( Attribute( attr['attribute_id'], Object(a['object_id'], a['x'], a['y'], a['w'], a['h'], a['names'], a['synsets']), a['attributes'], a['synsets'])) else: attributes.append( Attribute( attr['attribute_id'], Object(a['object_id'], a['x'], a['y'], a['w'], a['h'], a['names'], a['synsets']), None, a['synsets'])) else: count_skips[1] += 1 if verbose: print('Skipped {} rels, {} attrs total'.format(*count_skips)) return Graph(image, objects, relationships, attributes)
def map_object(object_map, obj): """ Use object ids as hashes to `visual_genome.models.Object` instances. If item not in table, create new `Object`. Used when building scene graphs from json. """ oid = "gw_{}".format(obj['object_id']) if obj.get("guesswhat", False) else obj["object_id"] obj['id'] = oid del obj['object_id'] if oid in object_map: object_ = object_map[oid] else: if 'attributes' in obj: attrs = obj['attributes'] del obj['attributes'] else: attrs = [] if 'abstract_attributes' in obj: abs_attrs = obj['abstract_attributes'] del obj['abstract_attributes'] else: abs_attrs = [] if 'situated_attributes' in obj: sit_attrs = obj['situated_attributes'] del obj['situated_attributes'] else: sit_attrs = [] if 'w' in obj: obj['width'] = obj['w'] obj['height'] = obj['h'] del obj['w'], obj['h'] if 'guesswhat' in obj: obj['guesswhat'] = True else: obj['guesswhat'] = False object_ = Object(**obj) object_.attributes = attrs object_.abstract_attributes = abs_attrs object_.situated_attributes = sit_attrs object_map[oid] = object_ return object_map, object_
def parse_graph_VRD(d): image = Image(d['photo_id'], d['filename'], d['width'], d['height'], '', '') id2obj = {} objs = [] rels = [] atrs = [] for i, o in enumerate(d['objects']): b = o['bbox'] obj = Object(i, b['x'], b['y'], b['w'], b['h'], o['names'], []) id2obj[i] = obj objs.append(obj) for j, a in enumerate(o['attributes']): atrs.append(Attribute(j, obj, a['attribute'], [])) for i, r in enumerate(d['relationships']): s = id2obj[r['objects'][0]] o = id2obj[r['objects'][1]] v = r['relationship'] rels.append(Relationship(i, s, v, o, [])) return Graph(image, objs, rels, atrs)
def parse_graph(data, image): """ Helper to parse a Graph object from API data. """ objects = [] object_map = {} relationships = [] attributes = [] # Create the Objects for obj in data['bounding_boxes']: names = [] synsets = [] for bbx_obj in obj['boxed_objects']: names.append(bbx_obj['name']) synsets.append(parse_synset(bbx_obj['object_canon'])) object_ = Object(obj['id'], obj['x'], obj['y'], obj[ 'width'], obj['height'], names, synsets) object_map[obj['id']] = object_ objects.append(object_) # Create the Relationships for rel in data['relationships']: relationships.append(Relationship(rel['id'], object_map[rel['subject']], rel['predicate'], object_map[rel['object']], parse_synset( rel['relationship_canon']))) # Create the Attributes for atr in data['attributes']: attributes.append(Attribute(atr['id'], object_map[atr['subject']], atr['attribute'], parse_synset(atr['attribute_canon']))) return Graph(image, objects, relationships, attributes)
def parse_graph_VRD(d): image = Image(d["photo_id"], d["filename"], d["width"], d["height"], "", "") id2obj = {} objs = [] rels = [] atrs = [] for i, o in enumerate(d["objects"]): b = o["bbox"] obj = Object(i, b["x"], b["y"], b["w"], b["h"], o["names"], []) id2obj[i] = obj objs.append(obj) for j, a in enumerate(o["attributes"]): atrs.append(Attribute(j, obj, a["attribute"], [])) for i, r in enumerate(d["relationships"]): s = id2obj[r["objects"][0]] o = id2obj[r["objects"][1]] v = r["relationship"] rels.append(Relationship(i, s, v, o, [])) return Graph(image, objs, rels, atrs)
def parse_graph_local(data, image, verbose=False): """ Modified version of `utils.ParseGraph`. """ global count_skips objects = [] object_map = {} relationships = [] attributes = [] for obj in data["objects"]: object_map, o_ = map_object(object_map, obj) objects.append(o_) for rel in data["relationships"]: if rel["subject_id"] in object_map and rel["object_id"] in object_map: object_map, s = map_object(object_map, {"object_id": rel["subject_id"]}) v = rel["predicate"] object_map, o = map_object(object_map, {"object_id": rel["object_id"]}) rid = rel["relationship_id"] relationships.append(Relationship(rid, s, v, o, rel["synsets"])) else: # Skip this relationship if we don't have the subject and object in # the object_map for this scene graph. Some data is missing in this # way. count_skips[0] += 1 if "attributes" in data: for attr in data["attributes"]: a = attr["attribute"] if a["object_id"] in object_map: attributes.append( Attribute( attr["attribute_id"], Object( a["object_id"], a["x"], a["y"], a["w"], a["h"], a["names"], a["synsets"], ), a["attributes"], a["synsets"], )) else: count_skips[1] += 1 if verbose: print("Skipped {} rels, {} attrs total".format(*count_skips)) return Graph(image, objects, relationships, attributes)
def parse_object_data(data): """ Helper to parse object data. """ objects = [] if len(data) == 0: return None if 'object_id' in data[0]: object_id_key = 'object_id' else: object_id_key = 'id' for info in data: objects.append(Object(info[object_id_key], info['x'], info['y'], info['w'], info['h'], info['names'], info['synsets'])) return objects
def parse_graph(data, image): """ Helper to parse a Graph object from API data. """ objects = [] object_map = {} relationships = [] attributes = [] # Create the Objects for obj in data["bounding_boxes"]: names = [] synsets = [] for bbx_obj in obj["boxed_objects"]: names.append(bbx_obj["name"]) synsets.append(parse_synset(bbx_obj["object_canon"])) object_ = Object( obj["id"], obj["x"], obj["y"], obj["width"], obj["height"], names, synsets, ) object_map[obj["id"]] = object_ objects.append(object_) # Create the Relationships for rel in data["relationships"]: relationships.append( Relationship( rel["id"], object_map[rel["subject"]], rel["predicate"], object_map[rel["object"]], parse_synset(rel["relationship_canon"]), )) # Create the Attributes for atr in data["attributes"]: attributes.append( Attribute( atr["id"], object_map[atr["subject"]], atr["attribute"], parse_synset(atr["attribute_canon"]), )) return Graph(image, objects, relationships, attributes)