File size: 21,672 Bytes
032e687 |
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 |
import argparse
import json
import math
import os
import pprint
import string
from collections import Counter
from math import floor
import numpy as np
from tqdm import tqdm
from difflib import SequenceMatcher
from detectron2.data.detection_utils import read_image
def preprocess_object_labels(data, alias_dict={}):
for img in data:
for obj in img["objects"]:
obj["ids"] = [obj["object_id"]]
names = []
for name in obj["names"]:
name = name.lower()
label = sentence_preprocess(name)
if label in alias_dict:
label = alias_dict[label]
names.append(label)
obj["names"] = names
def extract_object_token(data, num_tokens, object_list=[], verbose=True):
"""Builds a set that contains the object names. Filters infrequent tokens."""
token_counter = Counter()
for img in data:
for obj in img["objects"]:
for name in obj["names"]:
if len(name):
pass
else:
continue
if not object_list or not set([x.lower() for x in name.split(",")]).isdisjoint(object_list):
# if not object_list or name in object_list:
token_counter.update([name])
tokens = set()
# pick top N tokens
token_counter_return = {}
for token, count in token_counter.most_common():
tokens.add(token)
token_counter_return[token] = count
if len(tokens) == num_tokens:
break
if verbose:
print(("Keeping %d / %d objects" % (len(tokens), len(token_counter))))
return tokens, token_counter_return
def merge_duplicate_boxes(data):
def IoU(b1, b2):
if b1[2] <= b2[0] or b1[3] <= b2[1] or b1[0] >= b2[2] or b1[1] >= b2[3]:
return 0
b1b2 = np.vstack([b1, b2])
minc = np.min(b1b2, 0)
maxc = np.max(b1b2, 0)
union_area = (maxc[2] - minc[0]) * (maxc[3] - minc[1])
int_area = (minc[2] - maxc[0]) * (minc[3] - maxc[1])
return float(int_area) / float(union_area)
def to_x1y1x2y2(obj):
x1 = obj["x"]
y1 = obj["y"]
x2 = obj["x"] + obj["w"]
y2 = obj["y"] + obj["h"]
return np.array([x1, y1, x2, y2], dtype=np.int32)
def inside(b1, b2):
return b1[0] >= b2[0] and b1[1] >= b2[1] and b1[2] <= b2[2] and b1[3] <= b2[3]
def overlap(obj1, obj2):
b1 = to_x1y1x2y2(obj1)
b2 = to_x1y1x2y2(obj2)
iou = IoU(b1, b2)
if all(b1 == b2) or iou > 0.9: # consider as the same box
return 1
elif (inside(b1, b2) or inside(b2, b1)) and obj1["names"][0] == obj2["names"][
0
]: # same object inside the other
return 2
elif iou > 0.6 and obj1["names"][0] == obj2["names"][0]: # multiple overlapping same object
return 3
else:
return 0 # no overlap
num_merged = {1: 0, 2: 0, 3: 0}
print("merging boxes..")
for img in data:
# mark objects to be merged and save their ids
objs = img["objects"]
num_obj = len(objs)
for i in range(num_obj):
if "M_TYPE" in objs[i]: # has been merged
continue
merged_objs = [] # circular refs, but fine
for j in range(i + 1, num_obj):
if "M_TYPE" in objs[j]: # has been merged
continue
overlap_type = overlap(objs[i], objs[j])
if overlap_type > 0:
objs[j]["M_TYPE"] = overlap_type
merged_objs.append(objs[j])
objs[i]["mobjs"] = merged_objs
# merge boxes
filtered_objs = []
merged_num_obj = 0
for obj in objs:
if "M_TYPE" not in obj:
ids = [obj["object_id"]]
dims = [to_x1y1x2y2(obj)]
prominent_type = 1
for mo in obj["mobjs"]:
ids.append(mo["object_id"])
obj["names"].extend(mo["names"])
dims.append(to_x1y1x2y2(mo))
if mo["M_TYPE"] > prominent_type:
prominent_type = mo["M_TYPE"]
merged_num_obj += len(ids)
obj["ids"] = ids
mdims = np.zeros(4)
if prominent_type > 1: # use extreme
mdims[:2] = np.min(np.vstack(dims)[:, :2], 0)
mdims[2:] = np.max(np.vstack(dims)[:, 2:], 0)
else: # use mean
mdims = np.mean(np.vstack(dims), 0)
obj["x"] = int(mdims[0])
obj["y"] = int(mdims[1])
obj["w"] = int(mdims[2] - mdims[0])
obj["h"] = int(mdims[3] - mdims[1])
num_merged[prominent_type] += len(obj["mobjs"])
obj["mobjs"] = None
obj["names"] = list(set(obj["names"])) # remove duplicates
filtered_objs.append(obj)
else:
assert "mobjs" not in obj
img["objects"] = filtered_objs
assert merged_num_obj == num_obj
print("# merged boxes per merging type:")
print(num_merged)
def build_token_dict(vocab):
"""build bi-directional mapping between index and token"""
token_to_idx, idx_to_token = {}, {}
next_idx = 1
vocab_sorted = sorted(list(vocab)) # make sure it's the same order everytime
for token in vocab_sorted:
token_to_idx[token] = next_idx
idx_to_token[next_idx] = token
next_idx = next_idx + 1
return token_to_idx, idx_to_token
def sentence_preprocess(phrase):
"""preprocess a sentence: lowercase, clean up weird chars, remove punctuation"""
replacements = {
"½": "half",
"—": "-",
"™": "",
"¢": "cent",
"ç": "c",
"û": "u",
"é": "e",
"°": " degree",
"è": "e",
"…": "",
}
# phrase = phrase.encode('utf-8')
phrase = phrase.lstrip(" ").rstrip(" ")
for k, v in replacements.items():
phrase = phrase.replace(k, v)
# return str(phrase).lower().translate(None, string.punctuation).decode('utf-8', 'ignore')
return str(phrase).lower().translate(str.maketrans("", "", string.punctuation))
def make_alias_dict_new(dict_file, dict_file2):
# return {}
alias_list = []
for line in open(dict_file, "r"):
alias = [alia.strip("\n").strip("\r") for alia in line.strip("\n").strip("\r").split(",")]
alias_list.append(alias)
for line in open(dict_file2, "r"):
alias = [alia.strip("\n").strip("\r") for alia in line.strip("\n").strip("\r").split(",")]
alias_list.append(alias)
alias_list_merged = []
merged_id = []
for i in range(len(alias_list)):
if i in merged_id:
continue
merged_id.append(i)
a = alias_list[i]
a_set = set(a)
if len(a) <= 1:
continue
while True:
find_overlap = False
for j in range(len(alias_list)):
if j in merged_id:
continue
b = alias_list[j]
if not a_set.isdisjoint(b):
# print(i, j, a, b)
a.extend(b)
a_set = set(a)
merged_id.append(j)
find_overlap = True
if not find_overlap:
break
# a = list(set(a))
if len(a) > 1:
# alias_list_merged.append(list(set(a)))
alias_list_merged.append(a)
out_dict = {}
for alias in alias_list_merged:
# name = alias[0]
# for alia in alias[1:]:
# match = SequenceMatcher(None, name, alia).find_longest_match()
# name = name[match.a:match.a + match.size]
name = ",".join(alias)
for alia in alias:
out_dict[alia] = name
print("merged token", out_dict)
print("merge token", len(list(out_dict.keys())), "to", len(set(list(out_dict.values()))))
return out_dict
def make_alias_dict(dict_file):
"""create an alias dictionary from a file"""
out_dict = {}
vocab = []
for line in open(dict_file, "r"):
alias = line.strip("\n").strip("\r").split(",")
alias_target = alias[0] if alias[0] not in out_dict else out_dict[alias[0]]
for a in alias:
out_dict[a] = alias_target # use the first term as the aliasing target
vocab.append(alias_target)
print("merge token", len(list(out_dict.keys())), "to", len(set(list(out_dict.values()))))
return out_dict, vocab
def make_list(list_file):
"""create a blacklist list from a file"""
return [l.strip("\n").strip("\r") for line in open(list_file) for l in line.strip("\n").strip("\r").split(",")]
return [line.strip("\n").strip("\r") for line in open(list_file)]
def filter_object_boxes(data, image_data, area_frac_thresh):
"""
filter boxes by a box area-image area ratio threshold
"""
thresh_count = 0
all_count = 0
for i, img in enumerate(data):
filtered_obj = []
area = float(image_data[i]["height"] * image_data[i]["width"])
for obj in img["objects"]:
if float(obj["h"] * obj["w"]) > area * area_frac_thresh:
filtered_obj.append(obj)
thresh_count += 1
all_count += 1
img["objects"] = filtered_obj
print("box threshod: keeping %i/%i boxes" % (thresh_count, all_count))
def filter_by_idx(data, valid_list):
return [data[i] for i in valid_list]
def object(args):
print("start")
pprint.pprint(args)
if args.apply_exif:
print("-" * 60)
print("We will apply exif orientation...")
print("-" * 60)
base_dir = args.path
object_alias_path = os.path.join(base_dir, "annotations", "object_alias.txt")
object_alias_path2 = os.path.join(os.path.dirname(os.path.realpath(__file__)), "VG/1600-400-20/objects_vocab.txt")
object_data_path = os.path.join(base_dir, "annotations", "objects.json")
image_data_path = os.path.join(base_dir, "annotations", "image_data.json")
obj_alias_dict = {}
print("using object alias from %s" % (object_alias_path))
print("using object alias from %s" % (object_alias_path2))
# obj_alias_dict, obj_vocab_list = make_alias_dict(object_alias_path)
obj_alias_dict = make_alias_dict_new(object_alias_path, object_alias_path2)
object_list = []
if len(args.object_list_path) > 0:
print("using object list from %s" % (args.object_list_path))
object_list = make_list(args.object_list_path)
object_list = [x.lower() for x in object_list]
# assert len(object_list) >= args.num_objects
print("number of objects", len(object_list))
exclude_object_list = []
if len(args.exclude_object_list_path) > 0:
print("using exclude object list from %s" % (args.exclude_object_list_path))
exclude_object_list = make_list(args.exclude_object_list_path)
exclude_object_list = [x.lower() for x in exclude_object_list]
print("number of exclude objects", len(exclude_object_list))
# read in the annotation data
print("loading json files..")
print("using object data from %s" % (object_data_path))
object_data = json.load(open(object_data_path))
print("using image data from %s" % (image_data_path))
image_data = json.load(open(image_data_path))
assert len(object_data) == len(image_data)
num_im = len(image_data)
# sanity check
for i in range(num_im):
assert object_data[i]["image_id"] == image_data[i]["image_id"]
print("processing %i images" % num_im)
# preprocess label data
preprocess_object_labels(object_data, alias_dict=obj_alias_dict)
if args.min_box_area_frac > 0:
# filter out invalid small boxes
print("threshold bounding box by %f area fraction" % args.min_box_area_frac)
filter_object_boxes(object_data, image_data, args.min_box_area_frac) # filter by box dimensions
# merge_duplicate_boxes(object_data)
# build vocabulary
object_tokens, object_token_counter = extract_object_token(object_data, args.num_objects, object_list)
label_to_idx, idx_to_label = build_token_dict(object_tokens)
print("object list missing:", list(set(set([x for token in object_tokens for x in token.split(",")]) - set(object_list)).intersection(set(object_list))))
print("object list merged:")
for tokens in object_tokens:
inter = set(tokens.split(",")).intersection(set(object_list))
if len(list(inter)) > 1:
print(tokens)
exclude_object_tokens = []
if len(args.exclude_object_list_path) > 0:
for token in tqdm(object_tokens):
if not set([x.lower() for x in token.split(",")]).isdisjoint(exclude_object_list):
# if token in exclude_object_list:
exclude_object_tokens.append(token)
print("exclude_object_tokens", exclude_object_tokens)
print("exclude_object_tokens", len(exclude_object_tokens))
# print out vocabulary
print("objects: ")
print(list(object_token_counter.items())[:100])
print(list(object_token_counter.items())[-100:])
# Convert image mnetadata
print("converting image info ...")
images = []
image_ids = []
for i in tqdm(range(num_im), mininterval=0.5):
path = image_data[i]["url"]
path = os.path.normpath(path)
path_split = path.split(os.sep)
image_id = image_data[i]["image_id"]
coco_id = image_data[i]["coco_id"]
flickr_id = image_data[i]["flickr_id"]
height = image_data[i]["height"]
width = image_data[i]["width"]
assert image_data[i]["image_id"] == object_data[i]["image_id"]
has_obj = False
for obj in object_data[i]["objects"]:
names = obj["names"]
assert len(names) == 1
name = names[0]
if name not in object_tokens:
continue
if name in exclude_object_tokens:
continue
has_obj = True
break
if not has_obj:
continue
img = {}
img["id"] = image_id
img["file_name"] = os.path.join(path_split[-2], path_split[-1])
img["height"] = height
img["width"] = width
if args.apply_exif:
filename = os.path.join(base_dir, img["file_name"])
image = read_image(filename, format="BGR")
if image.shape[1] != img["width"] or image.shape[0] != img["height"]:
print("before exif correction: ", img)
img["width"], img["height"] = image.shape[1], image.shape[0]
print("after exif correction: ", img)
images.append(img)
image_ids.append(image_id)
# build train/val/test splits
print("build train/val/test splits")
num_im = len(images)
num_im_train = max(int(num_im * 0.7), num_im - 5000)
print("build train split")
images_train = images[:num_im_train]
image_ids_train = image_ids[:num_im_train]
print("build val split")
images_val = images[num_im_train:]
image_ids_val = image_ids[num_im_train:]
# Convert instance annotations
print("converting annotations info ...")
annotations = []
annotations_train = []
annotations_val = []
label_to_synset = {obj: [] for obj in object_tokens}
image_count = {obj: 0 for obj in object_tokens}
instance_count = {obj: 0 for obj in object_tokens}
ann_id = 1
for i in tqdm(range(num_im), mininterval=0.5):
image_id = image_data[i]["image_id"]
if image_id not in image_ids:
continue
assert image_data[i]["image_id"] == object_data[i]["image_id"]
names = []
for obj in object_data[i]["objects"]:
name = obj["names"]
assert len(name) == 1
name = name[0]
if name not in object_tokens:
continue
if name in exclude_object_tokens:
continue
names.append(name)
synsets = obj["synsets"]
object_id = obj["object_id"]
# merged_object_ids = obj['merged_object_ids']
x = obj["x"]
y = obj["y"]
h = obj["h"]
w = obj["w"]
ann = {}
ann["id"] = ann_id
ann_id += 1
ann["image_id"] = image_id
ann["category_id"] = label_to_idx[name]
ann["phrase"] = name.split(",")[0].strip("\n").strip("\r").strip()
ann["isobject"] = 1
# ann["bbox"] = [x, y, x + w, y + h]
ann["bbox"] = [x, y, w, h]
ann["area"] = h * w
ann["iscrowd"] = False
annotations.append(ann)
if image_id in image_ids_train:
annotations_train.append(ann)
elif image_id in image_ids_val:
annotations_val.append(ann)
else:
assert 0
# assert len(synsets) <= 1
# if len(synsets) > 0:
# synset = synsets[0]
# if synset not in label_to_synset[name]:
# label_to_synset[name].append(synset)
for synset in synsets:
if synset not in label_to_synset[name]:
label_to_synset[name].append(synset)
instance_count[name] += 1
for name in list(set(names)):
image_count[name] += 1
print("all images: ", len(images))
print("train images: ", len(images_train))
print("val images: ", len(images_val))
print("all annotations: ", len(annotations))
print("train annotations: ", len(annotations_train))
print("val annotations: ", len(annotations_val))
oi_train = {}
oi_val = {}
oi_all = {}
# Add basic dataset info
print("adding basic dataset info")
oi_train["info"] = {}
oi_val["info"] = {}
oi_all["info"] = {}
# Add license information
print("adding basic license info")
oi_train["licenses"] = []
oi_val["licenses"] = []
oi_all["licenses"] = []
# Convert category information
print("converting category info")
categories = []
for i, name in idx_to_label.items():
cat = {}
cat["id"] = i
cat["name"] = name
# cat["synsets"] = label_to_synset[name]
alias = name.split(",")
name = alias[0].strip("\n").strip("\r").strip()
# for alia in alias[1:]:
# match = SequenceMatcher(None, name, alia).find_longest_match()
# name = name[match.a:match.a + match.size]
cat["name"] = name
categories.append(cat)
oi_train["categories"] = categories
oi_val["categories"] = categories
oi_all["categories"] = categories
# Convert image mnetadata
print("converting image info ...")
oi_train["images"] = images_train
oi_val["images"] = images_val
oi_all["images"] = images
# Convert instance annotations
print("converting annotations ...")
oi_train["annotations"] = annotations_train
oi_val["annotations"] = annotations_val
oi_all["annotations"] = annotations
# Write annotations into .json file
filename = os.path.join(base_dir, "annotations/", f"visualgenome_{len(categories)}_box_train.json")
if exclude_object_list:
filename = os.path.join(base_dir, "annotations/", f"visualgenome_{len(categories)}minus{len(exclude_object_list)}_box_train.json")
print("writing output to {}".format(filename))
json.dump(oi_train, open(filename, "w"))
filename = os.path.join(base_dir, "annotations/", f"visualgenome_{len(categories)}_box_val.json")
if exclude_object_list:
filename = os.path.join(base_dir, "annotations/", f"visualgenome_{len(categories)}minus{len(exclude_object_list)}_box_val.json")
print("writing output to {}".format(filename))
json.dump(oi_val, open(filename, "w"))
filename = os.path.join(base_dir, "annotations/", f"visualgenome_{len(categories)}_box.json")
if exclude_object_list:
filename = os.path.join(base_dir, "annotations/", f"visualgenome_{len(categories)}minus{len(exclude_object_list)}_box.json")
print("writing output to {}".format(filename))
json.dump(oi_all, open(filename, "w"))
filename = os.path.join(base_dir, "annotations/", f"visualgenome_{len(categories)}_box_categories.json")
if exclude_object_list:
filename = os.path.join(base_dir, "annotations/", f"visualgenome_{len(categories)}minus{len(exclude_object_list)}_box_categories.json")
print("writing output to {}".format(filename))
json.dump(categories, open(filename, "w"))
print("Done")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-p", "--path", dest="path", help="path to visual genome data", type=str)
parser.add_argument(
"--apply-exif",
dest="apply_exif",
action="store_true",
help="apply the exif orientation correctly",
)
parser.add_argument("--object_list_path", default="VG/object_list.txt", type=str)
parser.add_argument("--exclude_object_list_path", default="", type=str)
parser.add_argument(
"--num_objects", default=150, type=int, help="set to 0 to disable filtering"
)
parser.add_argument("--min_box_area_frac", default=0.002, type=float)
args = parser.parse_args()
object(args)
|