File size: 6,549 Bytes
6d35aff | 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 | import json
import pandas as pd
from collections import defaultdict
from matplotlib import pyplot as plt
import numpy as np
SPLIT_GENRE = {
"TRAIN": [
"Casual",
"Adventure",
"Action",
"Indie"
],
"VAL": [
"Strategy",
"Education",
"RPG",
"Massively Multiplayer",
"Design & Illustration",
"Animation & Modeling"
],
"TEST": [
"Simulation",
"Sports"
]
}
def parse_img_id(img_id):
img_id = str(img_id)
return int(img_id[:-3]), int(img_id[-3:])
def plot(data, img_name, is_genre=False):
plt.rcParams.update({'font.size': 18})
colors = []
for cat in data.keys():
if cat in SPLIT_GENRE['TRAIN']:
colors.append('tab:blue')
elif cat in SPLIT_GENRE['VAL']:
colors.append('tab:orange')
elif cat in SPLIT_GENRE['TEST']:
colors.append('tab:green')
else:
colors.append('gray')
if is_genre:
plt.figure(figsize=(10, 6))
plt.grid(True, axis='y', zorder=0)
plt.bar(data.keys(), data.values(), color=colors, zorder=3)
plt.xticks(rotation=30, ha='right')
plt.legend(handles=[
plt.Line2D([0], [0], color='tab:blue', lw=10, label='Train'),
plt.Line2D([0], [0], color='tab:orange', lw=10, label='Val'),
plt.Line2D([0], [0], color='tab:green', lw=10, label='Test'),
])
else:
plt.figure(figsize=(15, 6))
plt.grid(True, axis='y', zorder=0)
plt.bar(data.keys(), data.values(), zorder=3)
plt.xticks(rotation=45, ha='right')
plt.gca().yaxis.set_major_locator(plt.MaxNLocator(integer=True))
for i, v in enumerate(data.values()):
plt.text(i, v, str(v), ha='center', va='bottom')
y_max = max(data.values())
plt.ylim(0, 1.1 * y_max)
plt.tight_layout()
plt.savefig(img_name + '.png')
plt.savefig(img_name + '.pdf')
def plot_genre():
df = pd.read_csv('app_genre.csv')
with open('../data/coco_merged/annotations/semantics.json', 'r') as f:
dataset = json.load(f)
app_img_map = {}
for img in dataset['images']:
img_id = img['id']
app_id, _ = parse_img_id(img['id'])
if app_id not in app_img_map:
app_img_map[app_id] = []
app_img_map[app_id].append(img_id)
gnr_app_map = defaultdict(list)
for i in range(len(df)):
app_id = df['id'][i]
tags = df['genre'][i].split(';')
for tag in tags:
gnr_app_map[tag].append(int(app_id))
gnr_app_count = {cat: len(apps) for cat, apps in gnr_app_map.items()}
gnr_img_count = {cat: sum([len(app_img_map[app]) for app in apps]) for cat, apps in gnr_app_map.items()}
gnr_anno_count = {cat: sum([len([anno for anno in dataset['annotations'] if anno['image_id'] in app_img_map[app]]) for app in apps]) for cat, apps in gnr_app_map.items()}
split_genre_order = {genre: i for i, genre in enumerate(SPLIT_GENRE['TRAIN'] + SPLIT_GENRE['VAL'] + SPLIT_GENRE['TEST'])}
gnr_app_count = {cat: gnr_app_count[cat] for cat in sorted(gnr_app_count, key=lambda x: split_genre_order.get(x, float('inf')))}
gnr_img_count = {cat: gnr_img_count[cat] for cat in sorted(gnr_img_count, key=lambda x: split_genre_order.get(x, float('inf')))}
gnr_anno_count = {cat: gnr_anno_count[cat] for cat in sorted(gnr_anno_count, key=lambda x: split_genre_order.get(x, float('inf')))}
plot(gnr_app_count, 'genre_app_count', is_genre=True)
plot(gnr_img_count, 'genre_img_count', is_genre=True)
plot(gnr_anno_count, 'genre_anno_count', is_genre=True)
def plot_cat():
df = pd.read_csv('app_tag.csv')
with open('../data/coco_merged/annotations/semantics.json', 'r') as f:
dataset = json.load(f)
app_img_map = {}
for img in dataset['images']:
img_id = img['id']
app_id, _ = parse_img_id(img['id'])
if app_id not in app_img_map:
app_img_map[app_id] = []
app_img_map[app_id].append(img_id)
cat_app_map = defaultdict(list)
for i in range(len(df)):
app_id = df['id'][i]
tags = df['tag'][i].split(';')
for tag in tags:
cat_app_map[tag].append(int(app_id))
cat_app_map.pop('VR')
cat_app_count = {cat: len(apps) for cat, apps in cat_app_map.items()}
cat_img_count = {cat: sum([len(app_img_map[app]) for app in apps]) for cat, apps in cat_app_map.items()}
cat_anno_count = {cat: sum([len([anno for anno in dataset['annotations'] if anno['image_id'] in app_img_map[app]]) for app in apps]) for cat, apps in cat_app_map.items()}
cat_app_count = {cat: count for cat, count in list(cat_app_count.items())[:30]}
cat_img_count = {cat: count for cat, count in list(cat_img_count.items())[:30]}
cat_anno_count = {cat: count for cat, count in list(cat_anno_count.items())[:30]}
cat_app_count = {cat: count for cat, count in sorted(cat_app_count.items(), key=lambda item: item[1], reverse=True)}
cat_img_count = {cat: count for cat, count in sorted(cat_img_count.items(), key=lambda item: item[1], reverse=True)}
cat_anno_count = {cat: count for cat, count in sorted(cat_anno_count.items(), key=lambda item: item[1], reverse=True)}
plot(cat_app_count, 'tag_app_count')
plot(cat_img_count, 'tag_img_count')
plot(cat_anno_count, 'tag_anno_count')
def plot_cat_anno():
with open('../data/coco_merged/annotations/semantics.json', 'r') as f:
dataset = json.load(f)
cat_catname_map = {}
for cat in dataset['categories']:
cat_catname_map[cat['id']] = cat['name']
cat_anno_map = defaultdict(list)
for anno in dataset['annotations']:
cat_anno_map[cat_catname_map[anno['category_id']]].append(anno)
print(len(cat_anno_map['button']))
cat_anno_map.pop('button')
cat_anno_count = {cat: len(annos) for cat, annos in cat_anno_map.items()}
cat_anno_count = {cat: count for cat, count in sorted(cat_anno_count.items(), key=lambda item: item[1], reverse=True)}
cat_anno_count = {cat: count for cat, count in list(cat_anno_count.items())[:30]}
plot(cat_anno_count, 'ige_cat_anno_count')
plot_genre()
plot_cat()
plot_cat_anno()
|