Download scripts/vqs4k.py from bf0820/VQS-4K: direct link, hf CLI and curl.
- Browser
- Download file 2.97 kB
-
https://huggingface.co/datasets/bf0820/VQS-4K/resolve/main/scripts/vqs4k.py
- Command line
-
hf download hf://datasets/bf0820/VQS-4K/scripts/vqs4k.py
-
curl -L -o vqs4k.py https://huggingface.co/datasets/bf0820/VQS-4K/resolve/main/scripts/vqs4k.py
2.97 kB
| #!/usr/bin/env python3 | |
| """A PyTorch Dataset for VQS-4K. | |
| from vqs4k import VQS4K | |
| ds = VQS4K("VQS-4K", split="test") # a folder made by download.py | |
| s = ds[0] | |
| s["query_image"], s["query_mask"] # PIL: the external visual query | |
| s["frames"], s["masks"] # lists over the search video | |
| s["present"] # bool per frame: is the target visible? | |
| `masks[i]` is None exactly where `present[i]` is False. That is ground truth, not missing | |
| data: the correct prediction on those frames is an empty mask. | |
| Needs: pip install torch pillow | |
| """ | |
| from pathlib import Path | |
| from PIL import Image | |
| from torch.utils.data import Dataset | |
| class VQS4K(Dataset): | |
| def __init__(self, root, split="all", protocol="tiou", transform=None): | |
| self.root = Path(root) | |
| self.transform = transform | |
| if split == "all": | |
| self.seqs = sorted(p.name for p in (self.root / "images").iterdir() if p.is_dir()) | |
| else: | |
| suffix = "" if protocol == "tiou" else "_by_class" | |
| listing = self.root / "splits" / f"{split}_t-IoU{suffix}.txt" | |
| names = [l.strip() for l in listing.read_text().splitlines() if l.strip()] | |
| # keep only what is actually on disk, so partial downloads still work | |
| self.seqs = [s for s in names if (self.root / "images" / s).is_dir()] | |
| if not self.seqs: | |
| raise RuntimeError(f"no sequences found under {self.root}") | |
| def __len__(self): | |
| return len(self.seqs) | |
| def __getitem__(self, i): | |
| seq = self.seqs[i] | |
| img_dir = self.root / "images" / seq | |
| msk_dir = self.root / "masks" / seq | |
| # 00000 is the query; everything after it is the search video | |
| indices = sorted(int(p.stem) for p in img_dir.glob("*.jpg")) | |
| search = [n for n in indices if n != 0] | |
| frames, masks, present = [], [], [] | |
| for n in search: | |
| frames.append(Image.open(img_dir / f"{n:05d}.jpg").convert("RGB")) | |
| mp = msk_dir / f"{n:05d}.png" | |
| hit = mp.exists() | |
| present.append(hit) | |
| masks.append(Image.open(mp).convert("L") if hit else None) | |
| sample = { | |
| "seq_id": seq, | |
| "category_id": seq.rsplit("-set_", 1)[0], | |
| "query_image": Image.open(img_dir / "00000.jpg").convert("RGB"), | |
| "query_mask": Image.open(msk_dir / "00000.png").convert("L"), | |
| "frames": frames, | |
| "masks": masks, | |
| "present": present, | |
| "frame_indices": search, | |
| } | |
| return self.transform(sample) if self.transform else sample | |
| if __name__ == "__main__": | |
| import sys | |
| ds = VQS4K(sys.argv[1] if len(sys.argv) > 1 else "VQS-4K") | |
| s = ds[0] | |
| print(f"{len(ds)} sequences; first = {s['seq_id']}") | |
| print(f" query {s['query_image'].size}, {len(s['frames'])} frames, " | |
| f"{sum(s['present'])} with the target visible") | |