File size: 2,445 Bytes
4191737
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""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()