File size: 2,777 Bytes
b09c7b1 |
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 |
from itertools import combinations, permutations
from pathlib import Path
from typing import List
import random
SAVE_PATH = "prompts.txt"
MINI_SAVE_PATH = "mini_prompts.txt"
# COCO Classes, from https://docs.ultralytics.com/datasets/detect/coco/#dataset-yaml
OBJECT_PATH = Path(__file__).parent / "coco_classes.txt"
MAX_IMG_OBJECTS = 3
MAX_OBJ_QUANTITY = 5
def get_objects() -> List[str]:
with open(OBJECT_PATH) as f:
objects = [o.strip('\n') for o in f.readlines()]
return objects
def get_plural_suffix(object: str) -> str:
suffix = ""
if object == "person":
object = "people"
elif object.endswith("s") or object.endswith("sh") or object.endswith("ch"):
suffix = "es"
elif object.endswith("y"):
suffix = "ies"
object = object[:-1]
else:
suffix = "s"
object += suffix
return object
def gen_prompt_quantifier_helper(object: str, n: int) -> str:
"""
object: str, ex: 'person', 'car', 'dog', etc.
returns: list of object strings with quantifier prefix and pluralization
"""
if n > 1:
object = get_plural_suffix(object)
return f"{n} {object}"
def gen_single_object_prompts(objects: List[str]) -> List[str]:
prompts = []
for obj in objects:
prompts.extend(gen_prompt_quantifier_helper(obj))
return prompts
def generate_prompts(objects):
"""
Generates a list of prompts and saves them for model evaluation
e.g.
1 person
2 people
3 people
1 person and 1 car
2 people and 1 car
3 people and 1 car
1 person and 2 cars
2 people and 2 cars
3 people and 2 cars
...
"""
prompts = set()
for num_img_objects in range(1, MAX_IMG_OBJECTS + 1):
for combo in combinations(objects, num_img_objects):
for count_permute in permutations(list(range(1, MAX_OBJ_QUANTITY + 1)) * num_img_objects, num_img_objects):
prompt = ""
for i, obj in enumerate(combo):
if i > 0:
prompt += " and "
prompt += gen_prompt_quantifier_helper(obj, count_permute[i])
prompts.add(prompt)
return prompts
def save_prompts(prompts: List[str], save_path: str = SAVE_PATH):
with open(save_path, 'w') as f:
for prompt in prompts:
f.write(prompt + '\n')
if __name__ == "__main__":
objects = get_objects()
prompts = sorted(list(generate_prompts(objects)))
save_prompts(prompts)
# mini_objects = random.sample(objects, 10)
# prompts = sorted(list(generate_prompts(mini_objects)))
# save_prompts(prompts, MINI_SAVE_PATH)
|