File size: 1,367 Bytes
a5ec84d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Assemble the full dataset + vocab into one zip for Colab (upload to Drive).
The zip unpacks to a single CaptionDataset root: Images/ + dataset_flickr8k.json + vocab.json.
"""

from __future__ import annotations

import json
import zipfile
from pathlib import Path

from capit.config import config


def make_train_zip(out_path: Path | None = None) -> Path:
    out = Path(out_path) if out_path else config.data_root / "flickr8k_colab.zip"
    for required in (config.images_dir, config.karpathy_json, config.vocab_path):
        if not required.exists():
            raise FileNotFoundError(f"{required} missing — run the Stage 0.2 download and build_vocab first")

    jpgs = sorted(config.images_dir.glob("*.jpg"))
    expected = len(json.loads(config.karpathy_json.read_text())["images"])
    if len(jpgs) < expected:
        raise ValueError(f"{len(jpgs)} images in {config.images_dir}, expected >= {expected} (partial download?)")
    with zipfile.ZipFile(out, "w", zipfile.ZIP_STORED) as zf:  # jpgs already compressed
        for jpg in jpgs:
            zf.write(jpg, f"Images/{jpg.name}")
        zf.write(config.karpathy_json, "dataset_flickr8k.json")
        zf.write(config.vocab_path, "vocab.json")

    print(f"wrote {out} ({len(jpgs)} images + dataset_flickr8k.json + vocab.json)")
    return out


if __name__ == "__main__":
    make_train_zip()