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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import json
from pathlib import Path
from typing import Any

import numpy as np
import torch
import torch.nn.functional as F
from PIL import Image
from tqdm import tqdm
from transformers import CLIPModel, CLIPProcessor
import os
from dotenv import load_dotenv

# ============================================================
# ์„ค์ •๊ฐ’
# ============================================================

load_dotenv()

# .env ์•ˆ์˜ HF_TOKEN ์ฝ๊ธฐ
hf_token = os.getenv("HF_TOKEN")

# ์ „์ฒด ํด๋ž˜์Šค๋ฅผ ๊ฒ€์ˆ˜ํ•˜๋ ค๋ฉด True
# ํŠน์ • ํด๋ž˜์Šค๋งŒ ๊ฒ€์ˆ˜ํ•˜๋ ค๋ฉด False
CHECK_ALL_CLASSES = True

# ์ „์ฒด ํด๋ž˜์Šค ๊ฒ€์ˆ˜ ์‹œ ๊ธฐ์ค€์ด ๋˜๋Š” raw ๋ฐ์ดํ„ฐ ๋ฃจํŠธ
DATA_RAW_ROOT_DIR = Path("data/raw")

# ํŠน์ • ํด๋ž˜์Šค๋งŒ ๊ฒ€์ˆ˜ํ•  ๋•Œ ์‚ฌ์šฉํ•  ํด๋ž˜์Šค ํด๋” ๊ฒฝ๋กœ
# CHECK_ALL_CLASSES = False ์ผ ๋•Œ๋งŒ ์‚ฌ์šฉ๋จ
TARGET_CLASS_DIR = Path("data/raw")

# ์ž…๋ ฅ JSON ํŒŒ์ผ
INPUT_JSON_PATH = Path("data/annotations/captions_flo_all.json")

# ์ถœ๋ ฅ JSON ํŒŒ์ผ
OUTPUT_JSON_PATH = Path("data/annotations/clip_checked_flo_all.json")

# ์‚ฌ์šฉํ•  CLIP ๋ชจ๋ธ
MODEL_NAME = "openai/clip-vit-base-patch32"

# ํ•œ ๋ฒˆ์— ์ฒ˜๋ฆฌํ•  ์ด๋ฏธ์ง€-์บก์…˜ ์Œ ๊ฐœ์ˆ˜
BATCH_SIZE = 32

# ํ•˜์œ„ ๋ช‡ %๋ฅผ fail / review๋กœ ๋ณผ์ง€
FAIL_BOTTOM_PERCENT = 10
REVIEW_BOTTOM_PERCENT = 20

print("๊ฒฝ๋กœ : " , INPUT_JSON_PATH)

# ============================================================
# JSON ์ž…์ถœ๋ ฅ
# ============================================================

def load_json(path: Path) -> list[dict[str, Any]]:
    with path.open("r", encoding="utf-8") as f:
        data = json.load(f)

    if not isinstance(data, list):
        raise ValueError("์ž…๋ ฅ JSON์€ ๋ฐ˜๋“œ์‹œ ๋ฐฐ์—ด ํ˜•ํƒœ์—ฌ์•ผ ํ•ฉ๋‹ˆ๋‹ค.")

    return data


def save_json(data: list[dict[str, Any]], path: Path) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)

    with path.open("w", encoding="utf-8") as f:
        json.dump(data, f, ensure_ascii=False, indent=4)


# ============================================================
# ํด๋ž˜์Šค / ๊ฒฝ๋กœ ์ฒ˜๋ฆฌ
# ============================================================

def get_target_class_name() -> str:
    """
    TARGET_CLASS_DIR = data/raw/airplane ์ด๋ฉด airplane ๋ฐ˜ํ™˜
    """
    return TARGET_CLASS_DIR.name


def get_class_name_from_image_value(image_value: str) -> str:
    """
    JSON์˜ image ๊ฐ’์ด airplane/hf_airplane_001.jpg ๋ผ๋ฉด airplane ๋ฐ˜ํ™˜
    """
    image_value = image_value.replace("\\", "/")
    image_path = Path(image_value)

    if len(image_path.parts) < 2:
        return ""

    return image_path.parts[0]


def is_target_item(item: dict[str, Any]) -> bool:
    """
    CHECK_ALL_CLASSES = True:
        ๋ชจ๋“  item ์ฒ˜๋ฆฌ

    CHECK_ALL_CLASSES = False:
        TARGET_CLASS_DIR.name๊ณผ JSON image์˜ ์ฒซ ๋ฒˆ์งธ ํด๋”๋ช…์ด ๊ฐ™์€ item๋งŒ ์ฒ˜๋ฆฌ
    """
    if CHECK_ALL_CLASSES:
        return True

    image_value = str(item.get("image", ""))
    image_class_name = get_class_name_from_image_value(image_value)

    return image_class_name == get_target_class_name()


def resolve_image_path(image_value: str) -> Path:
    """
    JSON:
        "image": "airplane/hf_airplane_001.jpg"

    ์ „์ฒด ํด๋ž˜์Šค ๊ฒ€์ˆ˜:
        DATA_RAW_ROOT_DIR / image
        โ†’ data/raw/airplane/hf_airplane_001.jpg

    ํŠน์ • ํด๋ž˜์Šค ๊ฒ€์ˆ˜:
        TARGET_CLASS_DIR / ํŒŒ์ผ๋ช…
        โ†’ data/raw/airplane/hf_airplane_001.jpg
    """
    image_value = image_value.replace("\\", "/")
    image_path = Path(image_value)

    if CHECK_ALL_CLASSES:
        return DATA_RAW_ROOT_DIR / image_path

    return TARGET_CLASS_DIR / image_path.name


def load_image(image_path: Path) -> Image.Image | None:
    try:
        with Image.open(image_path) as img:
            return img.convert("RGB").copy()
    except Exception:
        return None


# ============================================================
# ์บก์…˜ ํŽผ์น˜๊ธฐ
# ============================================================

def flatten_caption_items(data: list[dict[str, Any]]) -> tuple[list[dict[str, Any]], list[dict[str, Any]]]:
    """
    ์ด๋ฏธ์ง€ 1์žฅ์— caption 3๊ฐœ๊ฐ€ ์žˆ์œผ๋ฉด
    ์ด๋ฏธ์ง€-์บก์…˜ ์Œ 3๊ฐœ๋กœ ํŽผ์นœ๋‹ค.
    """
    target_data = []
    flat_items = []

    for item in data:
        if not is_target_item(item):
            continue

        target_item_index = len(target_data)
        target_data.append(item)

        image_value = str(item.get("image", ""))
        captions = item.get("captions", [])

        if not isinstance(captions, list):
            captions = []

        for caption_index, caption in enumerate(captions):
            flat_items.append({
                "item_index": target_item_index,
                "caption_index": caption_index,
                "image": image_value,
                "class": item.get("class", ""),
                "split": item.get("split", ""),
                "caption": str(caption).strip()
            })

    return target_data, flat_items


# ============================================================
# CLIP Score ๊ณ„์‚ฐ
# ============================================================

@torch.no_grad()
def compute_clip_scores(
    flat_items: list[dict[str, Any]],
    model: CLIPModel,
    processor: CLIPProcessor,
    device: torch.device
) -> list[dict[str, Any]]:

    results = []

    for start in tqdm(range(0, len(flat_items), BATCH_SIZE), desc="computing CLIP scores"):
        batch_items = flat_items[start:start + BATCH_SIZE]

        valid_items = []
        images = []
        texts = []

        for item in batch_items:
            image_path = resolve_image_path(item["image"])
            image = load_image(image_path)

            if image is None:
                results.append({
                    **item,
                    "resolved_image_path": str(image_path).replace("\\", "/"),
                    "clip_cosine": None,
                    "clip_score": None,
                    "clip_status": "missing_image",
                    "clip_reason": f"image file could not be opened: {image_path}"
                })
                continue

            caption = item["caption"]

            if not caption:
                results.append({
                    **item,
                    "resolved_image_path": str(image_path).replace("\\", "/"),
                    "clip_cosine": None,
                    "clip_score": None,
                    "clip_status": "empty_caption",
                    "clip_reason": "caption is empty"
                })
                continue

            valid_items.append({
                **item,
                "resolved_image_path": str(image_path).replace("\\", "/")
            })
            images.append(image)
            texts.append(caption)

        if not valid_items:
            continue

        inputs = processor(
            text=texts,
            images=images,
            return_tensors="pt",
            padding=True,
            truncation=True
        )

        inputs = {
            key: value.to(device)
            for key, value in inputs.items()
        }

        outputs = model(
            input_ids=inputs["input_ids"],
            attention_mask=inputs["attention_mask"],
            pixel_values=inputs["pixel_values"]
        )

        image_features = outputs.image_embeds
        text_features = outputs.text_embeds

        image_features = F.normalize(image_features, p=2, dim=1)
        text_features = F.normalize(text_features, p=2, dim=1)

        cosine_scores = (image_features * text_features).sum(dim=1)

        for item, cosine in zip(valid_items, cosine_scores):
            cosine_value = float(cosine.detach().cpu().item())
            clip_score = 2.5 * max(cosine_value, 0.0)

            results.append({
                **item,
                "clip_cosine": round(cosine_value, 6),
                "clip_score": round(clip_score, 6),
                "clip_status": "pending",
                "clip_reason": ""
            })

    return results


# ============================================================
# pass / review / fail ํŒ์ •
# ============================================================

def assign_clip_status(results: list[dict[str, Any]]) -> None:
    valid_scores = [
        result["clip_score"]
        for result in results
        if isinstance(result.get("clip_score"), float)
    ]

    if not valid_scores:
        return

    fail_threshold = np.percentile(valid_scores, FAIL_BOTTOM_PERCENT)
    review_threshold = np.percentile(valid_scores, REVIEW_BOTTOM_PERCENT)

    for result in results:
        clip_score = result.get("clip_score")

        if clip_score is None:
            continue

        if clip_score <= fail_threshold:
            result["clip_status"] = "fail"
            result["clip_reason"] = f"clip score is in the bottom {FAIL_BOTTOM_PERCENT}%"
        elif clip_score <= review_threshold:
            result["clip_status"] = "review"
            result["clip_reason"] = f"clip score is in the bottom {REVIEW_BOTTOM_PERCENT}%"
        else:
            result["clip_status"] = "pass"
            result["clip_reason"] = "clip score is acceptable"


# ============================================================
# ๊ฒฐ๊ณผ๋ฅผ ์›๋ž˜ JSON ๊ตฌ์กฐ์— ๋ถ™์ด๊ธฐ
# ============================================================

def attach_results_to_data(
    target_data: list[dict[str, Any]],
    results: list[dict[str, Any]]
) -> list[dict[str, Any]]:

    for item in target_data:
        item["caption_checks"] = []

    results = sorted(
        results,
        key=lambda x: (x["item_index"], x["caption_index"])
    )

    for result in results:
        item_index = result["item_index"]

        check = {
            "caption_index": result["caption_index"],
            "caption": result["caption"],
            "resolved_image_path": result.get("resolved_image_path"),
            "clip_cosine": result.get("clip_cosine"),
            "clip_score": result.get("clip_score"),
            "clip_status": result.get("clip_status"),
            "clip_reason": result.get("clip_reason", "")
        }

        target_data[item_index]["caption_checks"].append(check)

    return target_data


# ============================================================
# ์š”์•ฝ ์ถœ๋ ฅ
# ============================================================

def print_summary(
    target_data: list[dict[str, Any]],
    flat_items: list[dict[str, Any]],
    results: list[dict[str, Any]]
) -> None:

    status_count = {}
    valid_scores = []

    for result in results:
        status = result.get("clip_status", "unknown")
        status_count[status] = status_count.get(status, 0) + 1

        if isinstance(result.get("clip_score"), float):
            valid_scores.append(result["clip_score"])

    print("\n===== CLIP Score Summary =====")
    print(f"check all classes: {CHECK_ALL_CLASSES}")

    if CHECK_ALL_CLASSES:
        print(f"data raw root dir: {DATA_RAW_ROOT_DIR}")
    else:
        print(f"target class dir: {TARGET_CLASS_DIR}")
        print(f"target class name: {get_target_class_name()}")

    print(f"target images: {len(target_data)}")
    print(f"target image-caption pairs: {len(flat_items)}")
    print(f"status count: {status_count}")

    if valid_scores:
        print(f"min score: {min(valid_scores):.4f}")
        print(f"max score: {max(valid_scores):.4f}")
        print(f"mean score: {np.mean(valid_scores):.4f}")
        print(f"bottom {FAIL_BOTTOM_PERCENT}% threshold: {np.percentile(valid_scores, FAIL_BOTTOM_PERCENT):.4f}")
        print(f"bottom {REVIEW_BOTTOM_PERCENT}% threshold: {np.percentile(valid_scores, REVIEW_BOTTOM_PERCENT):.4f}")


# ============================================================
# ์‹คํ–‰
# ============================================================

def main():
    if not INPUT_JSON_PATH.exists():
        raise FileNotFoundError(f"input file not found: {INPUT_JSON_PATH}")

    if CHECK_ALL_CLASSES:
        if not DATA_RAW_ROOT_DIR.exists():
            raise FileNotFoundError(f"data raw root directory not found: {DATA_RAW_ROOT_DIR}")
    else:
        if not TARGET_CLASS_DIR.exists():
            raise FileNotFoundError(f"target class directory not found: {TARGET_CLASS_DIR}")

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    print(f"device: {device}")

    print(f"loading model: {MODEL_NAME}")
    model = CLIPModel.from_pretrained(MODEL_NAME, token=hf_token).to(device)
    processor = CLIPProcessor.from_pretrained(MODEL_NAME, token=hf_token)
    model.eval()

    data = load_json(INPUT_JSON_PATH)
    target_data, flat_items = flatten_caption_items(data)

    if not target_data:
        raise ValueError("๊ฒ€์ˆ˜ ๋Œ€์ƒ ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค. CHECK_ALL_CLASSES ๋˜๋Š” TARGET_CLASS_DIR ์„ค์ •์„ ํ™•์ธํ•˜์„ธ์š”.")

    results = compute_clip_scores(
        flat_items=flat_items,
        model=model,
        processor=processor,
        device=device
    )

    assign_clip_status(results)

    checked_data = attach_results_to_data(target_data, results)

    save_json(checked_data, OUTPUT_JSON_PATH)

    print_summary(target_data, flat_items, results)
    print(f"\nsaved: {OUTPUT_JSON_PATH}")


if __name__ == "__main__":
    main()