"""Batch yield prediction from a JSONL file (one sample per line, same format as the single-sample JSON). Example: python scripts/inference_batch_hf.py --hf_model_dir checkpoints \ --input_jsonl examples/sample_batch.jsonl --output_json predictions.json [--output_csv predictions.csv] Each line: {"sample_id", "planting_date", "week", "texts": [...], "images": [{"path", "date"}, ...]} Use --week to predict every sample at the same week. """ import sys from pathlib import Path PROJECT_ROOT = Path(__file__).resolve().parents[1] sys.path.insert(0, str(PROJECT_ROOT)) import argparse import json import pandas as pd import torch from transformers import AutoModel from data.embeddings import ImageEmbedder, TextEmbedder, embed_samples from hf.auto import register_multimodal_autoclass def parse_args(): p = argparse.ArgumentParser() p.add_argument("--hf_model_dir", required=True, help="local checkpoint dir or Hub repo id") p.add_argument("--input_jsonl", required=True) p.add_argument("--output_json", default="inference_predictions.json") p.add_argument("--output_csv", default=None, help="optional: also save as CSV") p.add_argument("--week", type=int, default=None, help="overrides each sample's week (1..22)") p.add_argument("--batch_size", type=int, default=64) return p.parse_args() def main(): a = parse_args() register_multimodal_autoclass() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = AutoModel.from_pretrained(a.hf_model_dir, trust_remote_code=True).to(device).eval() with open(a.input_jsonl) as f: samples = [json.loads(line) for line in f if line.strip()] print(f"{len(samples)} samples") x = embed_samples( samples, TextEmbedder(model.config.text_model, device), ImageEmbedder(model.config.image_model, device), week=a.week, ) # Samples can have different weeks: run each week group as its own batch. preds = [None] * len(samples) with torch.no_grad(): for week in sorted(set(x["week"].tolist())): idx = [i for i, w in enumerate(x["week"]) if w == week] for s in range(0, len(idx), a.batch_size): b = idx[s:s + a.batch_size] out = model( text=torch.from_numpy(x["text"][b]).to(device), image=torch.from_numpy(x["image"][b]).to(device), img_mask=torch.from_numpy(x["img_mask"][b]).to(device), img_age=torch.from_numpy(x["img_age"][b]).to(device), cutoff=week, ) for i, p in zip(b, out.predictions.cpu().tolist()): preds[i] = p results = [] for i, s in enumerate(samples): week = int(x["week"][i]) results.append({ "sample_id": s.get("sample_id", i), "week": week, "has_image_by_week": bool(x["img_mask"][i, week - 1] > 0), "image_age_weeks": int(x["img_age"][i, week - 1]), "yield_pred_bu_per_acre": float(preds[i]), }) with open(a.output_json, "w") as f: json.dump(results, f, indent=2) print(pd.DataFrame(results).head()) print(f"saved {len(results)} predictions to {a.output_json}") if a.output_csv: pd.DataFrame(results).to_csv(a.output_csv, index=False) print(f"saved {len(results)} predictions to {a.output_csv}") if __name__ == "__main__": main()