mnist-vqa / analys_data.py
star092304's picture
up README
2df9a7a verified
Raw
History Blame Contribute Delete
2.89 kB
import json
import os
from collections import Counter
# Get list of 22 labels from standard ANSWER_VOCAB
ANSWER_VOCAB = (
[str(d) for d in range(10)]
+ [
"top-left", "top-center", "top-right",
"center-left", "center", "center-right",
"bottom-left", "bottom-center", "bottom-right",
]
+ ["yes", "no", "empty"]
)
def analyze_split(split_name, dataset_path):
if not os.path.exists(dataset_path):
print(f"[-] Skipping {split_name.upper()}: File not found {dataset_path}")
return
with open(dataset_path, 'r', encoding='utf-8') as f:
data = json.load(f)
total = len(data)
print(f"\n{'='*50}")
print(f"📊 Analyzing {split_name.upper()} split ({total:,} questions)")
print(f"{'='*50}")
if total == 0:
print("Empty dataset!")
return
# Calculate counts for all 22 labels
counts = Counter(item.get('answer') for item in data)
# Print results in the original order of ANSWER_VOCAB
print(f"{'Label (Answer)':<18} | {'Count':<10} | {'Ratio (%)'}")
print("-" * 50)
for ans in ANSWER_VOCAB:
cnt = counts.get(ans, 0)
pct = (cnt / total) * 100
# Highlight yes/no case to easily see balancing effect after fix
if ans in ['yes', 'no']:
print(f"{ans:<18} | {cnt:<10,} | {pct:6.2f}% <---")
else:
print(f"{ans:<18} | {cnt:<10,} | {pct:6.2f}%")
# Calculate overview by label group
digit_cnt = sum(counts.get(str(i), 0) for i in range(10))
pos_cnt = sum(counts.get(pos, 0) for pos in [
"top-left", "top-center", "top-right",
"center-left", "center", "center-right",
"bottom-left", "bottom-center", "bottom-right"
])
yes_cnt = counts.get("yes", 0)
no_cnt = counts.get("no", 0)
yes_no_cnt = yes_cnt + no_cnt
empty_cnt = counts.get("empty", 0)
print("-" * 50)
print("📈 OVERVIEW BY LABEL GROUP:")
print(f"- Digits Group (0-9) : {digit_cnt:<7,} ({digit_cnt/total*100:5.2f}%)")
print(f"- Position Group : {pos_cnt:<7,} ({pos_cnt/total*100:5.2f}%)")
print(f"- Yes/No Group : {yes_no_cnt:<7,} ({yes_no_cnt/total*100:5.2f}%)")
if yes_no_cnt > 0:
print(f" + Yes/No Ratio : {yes_cnt/yes_no_cnt*100:.1f}% Yes / {no_cnt/yes_no_cnt*100:.1f}% No")
print(f"- Empty Group : {empty_cnt:<7,} ({empty_cnt/total*100:5.2f}%)")
if __name__ == "__main__":
# Root directory containing generated datasets
base_dir = "mnist_vqa_dataset"
print("\n🚀 STARTING MNIST-VQA-V4 DATA STATISTICAL ANALYSIS")
analyze_split("train", os.path.join(base_dir, "train", "dataset.json"))
analyze_split("val", os.path.join(base_dir, "val", "dataset.json"))
analyze_split("test", os.path.join(base_dir, "test", "dataset.json"))
print(f"\n{'='*50}")
print("Done!")