Datasets:
File size: 4,381 Bytes
6fb625d | 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 | import pandas as pd
import json
import os
import random
from rich.progress import track
# For all the models we have pristine data for
MODELS_PRISTINE = [
"TUTUS-TS-P_llava_next_pristine.csv",
"TUTUS-TS-P_llava_onevision_pristine.csv",
"TUTUS-TS-P_llava_onevision_smol_pristine.csv",
"TUTUS-TS-P_qwen2vl_pristine.csv",
"TUTUS-TS-P_qwen2vl_smol_pristine.csv",
"TUTUS-TS-P_qwen2_5vl_pristine.csv",
"TUTUS-TS-P_qwen2_5vl_smol_pristine.csv",
]
def stats_plot():
import json
import pandas as pd
import scienceplots
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use("science")
with open("TUTUS-TS-P_leakage_statistics.json", "r") as f:
dj = json.load(f)
df = pd.DataFrame(dj).T.reset_index().rename(columns={'index': 'model'})
# Melt for seaborn
df_melted = df.melt(id_vars='model', var_name='metric', value_name='count')
# Plot
plt.figure(figsize=(6, 4))
sns.barplot(
data=df_melted,
x='model',
y='count',
hue='metric',
#palette='muted'
)
# Styling
plt.title('Leakage Comparison per Model', fontsize=14, weight='bold')
plt.xticks(rotation=45, ha='right')
plt.xlabel('Model')
plt.ylabel('Count')
plt.grid(axis='y', linestyle='--', alpha=0.7)
plt.legend(title='Metric')
plt.tight_layout()
plt.savefig("TUTUS-TS-P_leakage_statistics_plot.pdf")
def main():
leaked_images_intersection = None
leakage_statistics = {}
for subset in MODELS_PRISTINE:
print(f"Processing subset: {subset}")
model_name = subset.replace("TUTUS-TS-P_", "").replace("_pristine.csv", "")
df = pd.read_csv(subset)
df = df.drop_duplicates(subset=["image_path"])
df = df[df.apply(lambda row: row["name"] in row["leaking"], axis=1)]
if not leaked_images_intersection:
leaked_images_intersection = set(df['image_path'].tolist())
else:
leaked_images_intersection = leaked_images_intersection.intersection(set(df['image_path'].tolist()))
tot_identities_leaked = len(set(df['name'].tolist()))
tot_images_leaked = len(df)
leakage_statistics[model_name] = {
"tot_identities_leaked": tot_identities_leaked,
"tot_images_leaked": tot_images_leaked
}
# After the pass on all models, compute intersection stats
output_path = "TUTUS-TS-P_leakage_statistics.json"
leaked_names_intersection = set([fp.split("/")[-2] for fp in leaked_images_intersection])
train_names = set(os.listdir("../train"))
leaked_names_intersection_base_num = len(leaked_names_intersection.intersection(train_names))
leaked_names_intersection_novel_num = len(leaked_names_intersection) - leaked_names_intersection_base_num
leakage_statistics["intersection"] = {
"tot_identities_leaked_base": leaked_names_intersection_base_num,
"tot_identities_leaked_novel": leaked_names_intersection_novel_num,
"tot_images_leaked": len(leaked_images_intersection)
}
with open(output_path, "w") as f:
json.dump(leakage_statistics, f, indent=4)
leaked_images_intersection_subset = random.sample(list(leaked_images_intersection), k=300)
# for subset in track(MODELS_PRISTINE):
# print(f"Processing again to extract dev-subset: {subset}")
# model_name = subset.replace("TUTUS-TS-P_", "").replace("_pristine.csv", "")
# df = pd.read_csv(subset)
# df = df.drop_duplicates(subset=["image_path"])
# df = df[df.apply(lambda row: row["name"] in row["leaking"], axis=1)]
# # I have a list leaked_images_intersection and want to keep only the rows
# # of df that are in leaked_images_intersection
# df = df[df['image_path'].isin(leaked_images_intersection_subset)]
# df.to_csv(f"TUTUS-DEV-{model_name}_pristine.csv", index=False)
df = pd.read_csv("TUTUS-TS-P.csv")[["idx", "ds_name", "name", "image_path", "leaking_prompt", "support_prompt"]]
df = df.drop_duplicates(subset=["image_path"])
# Keep only name/image_id
df = df[df['image_path'].isin("/".join(sample.split("/")[-2:]) for sample in leaked_images_intersection_subset)]
df.to_csv(f"TUTUS-DEV.csv", index=False)
if __name__ == "__main__":
main()
|