multimodal-yield-estimation / scripts /inference_single_hf.py
Sarikaa-Sridhar's picture
multi-modal yield estimation code base
4191737 verified
Raw History Blame Contribute Delete
2.45 kB
"""Predict yield for one plot from weekly captions + UAS images.
Example:
python scripts/inference_single_hf.py --hf_model_dir checkpoints \
--sample_json examples/sample_input.json --week 12 --output_json prediction.json
The JSON holds planting_date, week, texts (one caption per week after planting)
and images (path + date). Text and image embeddings are computed here.
"""
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 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("--sample_json", required=True)
p.add_argument("--week", type=int, default=None, help="overrides the sample's week (1..22)")
p.add_argument("--output_json", default="inference_prediction.json")
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.sample_json) as f:
sample = json.load(f)
x = embed_samples(
[sample],
TextEmbedder(model.config.text_model, device),
ImageEmbedder(model.config.image_model, device),
week=a.week,
)
week = int(x["week"][0])
with torch.no_grad():
out = model(
text=torch.from_numpy(x["text"]).to(device),
image=torch.from_numpy(x["image"]).to(device),
img_mask=torch.from_numpy(x["img_mask"]).to(device),
img_age=torch.from_numpy(x["img_age"]).to(device),
cutoff=week,
)
result = {
"sample_id": sample.get("sample_id"),
"week": week,
"has_image_by_week": bool(x["img_mask"][0, week - 1] > 0),
"image_age_weeks": int(x["img_age"][0, week - 1]),
"yield_pred_bu_per_acre": float(out.predictions[0]),
}
print(json.dumps(result, indent=2))
with open(a.output_json, "w") as f:
json.dump(result, f, indent=2)
print(f"saved prediction to {a.output_json}")
if __name__ == "__main__":
main()