|
|
import json |
|
|
from collections import defaultdict |
|
|
import numpy as np |
|
|
|
|
|
TRAIN_JSON_PATH = "./data/miko/train.json" |
|
|
ACTION_STATS_PATH = "./data/action_statistics.json" |
|
|
|
|
|
|
|
|
MIN_TOTAL_LEN = 1.0 |
|
|
MAX_WEIGHT = 0.05 |
|
|
|
|
|
|
|
|
with open(TRAIN_JSON_PATH, "r") as f: |
|
|
dataset = json.load(f) |
|
|
|
|
|
|
|
|
try: |
|
|
with open(ACTION_STATS_PATH, "r") as f: |
|
|
existing_stats = json.load(f) |
|
|
except FileNotFoundError: |
|
|
existing_stats = {} |
|
|
|
|
|
|
|
|
merged_stats = defaultdict(lambda: {"total_len": 0.0, "total_weight": 1}) |
|
|
|
|
|
|
|
|
for act, stats in existing_stats.items(): |
|
|
merged_stats[act]["total_len"] = stats.get("total_len", 0.0) |
|
|
merged_stats[act]["total_weight"] = 1 |
|
|
|
|
|
|
|
|
for entry in dataset: |
|
|
labels = entry.get("frame_ann", {}).get("labels", []) |
|
|
for label in labels: |
|
|
start = label.get("start_t") |
|
|
end = label.get("end_t") |
|
|
act_cats = label.get("act_cat", []) |
|
|
if start is None or end is None: |
|
|
continue |
|
|
duration = max(0.01, end - start) |
|
|
for act in act_cats: |
|
|
merged_stats[act]["total_len"] += duration |
|
|
|
|
|
|
|
|
cleaned_stats = {} |
|
|
for act, stats in merged_stats.items(): |
|
|
total_len = max(MIN_TOTAL_LEN, stats["total_len"]) |
|
|
if np.isfinite(total_len): |
|
|
raw_weight = 1.0 / total_len |
|
|
weight = min(raw_weight, MAX_WEIGHT) |
|
|
cleaned_stats[act] = { |
|
|
"total_len": total_len, |
|
|
"total_weight": 1, |
|
|
"weight": weight |
|
|
} |
|
|
else: |
|
|
print(f"⚠️ Skipping act_cat '{act}' due to invalid total_len={stats['total_len']}") |
|
|
|
|
|
|
|
|
total_weight_sum = sum(stats["weight"] for stats in cleaned_stats.values()) |
|
|
if total_weight_sum == 0: |
|
|
raise ValueError("❌ All weights are zero. Check act_cat labels or durations.") |
|
|
|
|
|
for stats in cleaned_stats.values(): |
|
|
stats["weight"] /= total_weight_sum |
|
|
|
|
|
|
|
|
with open(ACTION_STATS_PATH, "w") as f: |
|
|
json.dump(cleaned_stats, f, indent=2) |
|
|
|
|
|
print(f"✅ Saved {len(cleaned_stats)} act_cat entries to {ACTION_STATS_PATH}") |
|
|
print(f"🎯 Final normalized weight sum: {sum(stats['weight'] for stats in cleaned_stats.values()):.4f}") |
|
|
|