sps-challenge / src /moiqa /common.py
0x-YuAN's picture
Upload MoIQA challenge submission package
afafc78 verified
Raw History Blame Contribute Delete
1.76 kB
"""Image manifest loading and submission formatting."""
import os
from pathlib import Path
import pandas as pd
ROOT = Path(__file__).resolve().parents[2]
DATA = ROOT / 'data'
os.environ.setdefault('OMP_NUM_THREADS', '12')
def img_path(p) -> str:
"""Images may be stored as lossless WebP (pixel-identical) to save disk; resolve the actual file."""
p = Path(p)
return str(p) if p.exists() else str(p.with_suffix('.webp'))
SUBSCORES = ['vqa', 'cap', 'yon', 'mcq', 'det', 'seg', 'ret']
def load_all():
"""Read labeled training metadata; raw images are required only for feature extraction."""
if not (DATA / 'all.csv').exists():
return pd.DataFrame(columns=['index', 'ref', 'path', 'ref_path'])
frame = pd.read_csv(DATA / 'all.csv')
for c in ('path', 'ref_path'):
if c in frame:
frame[c] = frame[c].map(lambda p: str(ROOT / p) if not Path(p).is_absolute() else p)
return frame
def load_split(split):
"""Read the current inference image manifest."""
if not (DATA / f'{split}.csv').exists():
return pd.DataFrame(columns=['index', 'ref', 'path', 'ref_path'])
frame = pd.read_csv(DATA / f'{split}.csv')
for c in ('path', 'ref_path'):
if c in frame:
frame[c] = frame[c].map(lambda p: str(ROOT / p) if not Path(p).is_absolute() else p)
return frame
def write_submission(df: pd.DataFrame, path) -> None:
"""df with columns index, quality -> zip containing submission.tsv."""
import zipfile
path = Path(path)
path.parent.mkdir(parents=True, exist_ok=True)
tsv = df[['index', 'quality']].to_csv(sep='\t', index=False, float_format='%.6f')
with zipfile.ZipFile(path, 'w', zipfile.ZIP_DEFLATED) as z:
z.writestr('submission.tsv', tsv)