Text Classification
Transformers
Safetensors
yield-weather-soil
crop-yield
multi-temporal
regression
yield-estimation
custom_code
Instructions to use ICICLE-AI/yield-estimation with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use ICICLE-AI/yield-estimation with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="ICICLE-AI/yield-estimation", trust_remote_code=True)# Load model directly from transformers import AutoModelForSequenceClassification model = AutoModelForSequenceClassification.from_pretrained("ICICLE-AI/yield-estimation", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
File size: 17,384 Bytes
fb4abf6 ae0939c fb4abf6 afd70e2 fb4abf6 afd70e2 ae0939c fb4abf6 abcf12b fb4abf6 ae0939c fb4abf6 abcf12b fb4abf6 ae0939c abcf12b fb4abf6 abcf12b ae0939c fb4abf6 abcf12b ae0939c fb4abf6 abcf12b fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c abcf12b ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 abcf12b fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c abcf12b fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 abcf12b fb4abf6 abcf12b ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c 78253cb ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c 78253cb ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c 78253cb ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 abcf12b fb4abf6 ae0939c fb4abf6 ae0939c fb4abf6 ae0939c | 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 | ---
pipeline_tag: text-classification
library_name: transformers
tags:
- transformers
- crop-yield
- multi-temporal
- regression
- yield-estimation
- yield-weather-soil
license: mit
---
# Yield Estimation Transformer
A Hugging Face custom pipeline using Transformers model for county-level corn yield estimation using multi-temporal weather observations and static soil properties.
The model combines weekly weather time-series with static soil features to estimate corn yield in bushels per acre (`bu/acre`). It is packaged for inference using Hugging Face Transformers and has been tested for deployment through FlexServ.
The Hugging Face `text-classification` task is used as the FlexServ-compatible serving interface. The underlying model performs regression, and the returned `score` represents predicted corn yield in `bu/acre`.
The `training_code` directory composes the source code for data preparation, model training, evaluation, and inference.
### Tags
**Tags:** Digital-Agriculture, Foundation-AI
- In-season Crop Yield Estimation
- Digital Agriculture
- Transformers
- Multi-Temporal Modeling
- Regression
- Hugging Face Transformers
- FlexServ
For guidance on what to include in Tutorials, How-To Guides, Explanation, and Reference, see [DiΓ‘taxis](https://diataxis.fr/).
### License
[](https://opensource.org/licenses/MIT)
This project is released under the MIT License. The full text is available in [LICENSE](LICENSE).
## References
### USA County Level Crop Yield Dataset
This model uses the USA County Level Crop Yield Dataset.
```bibtex
@article{Khaki2020CNNRNN,
author = {Khaki, Saeed and Wang, Liang and Archontoulis, Sotirios V.},
title = {A CNN-RNN Framework for Crop Yield Prediction},
journal = {Frontiers in Plant Science},
volume = {10},
pages = {1750},
year = {2020},
doi = {10.3389/fpls.2019.01750},
publisher = {Frontiers Media SA}
}
```
### FlexServ
The model is packaged and validated for deployment with FlexServ.
FlexServ documentation: https://zhangwei217245.github.io/FlexServ/
## Acknowledgements
This work was developed as part of the ICICLE AI Institute.
*National Science Foundation (NSF) funded AI institute for Intelligent Cyberinfrastructure with Computational Learning in the Environment (ICICLE) (OAC 2112606)*
## Issue reporting
Contact:
For questions or support:
Sarikaa Sridhar: sridhar.86@buckeyemail.osu.edu
---
# Tutorials
### Overview
The Yield Estimation Transformer is a pretrained model for in-season county-level corn yield estimation. It combines temporal weekly weather observations with static soil properties and produces a scalar yield prediction in bushels per acre.
The model accepts six weekly weather variables:
- `prcp`
- `srad`
- `swe`
- `tmax`
- `tmin`
- `vp`
It also uses 66 static soil features defined in `config.json`.
The model supports prediction cutoffs at:
```text
20, 24, 28, 32, 36, 40, 44, 48, 52
```
A cutoff determines how many weeks of weather information are available to the model. A cutoff of `52` represents full-season inference.
For deployment through FlexServ, the model uses the Hugging Face `text-classification` pipeline as its serving interface. This is an interface choice for inference compatibility; the underlying prediction task remains regression.
---
# How-To Guides
### Problem Description
The model estimates county-level corn yield from weather and soil information.
The pretrained architecture expects structured numerical inputs rather than natural-language text. To make the model deployable through FlexServ's supported pipeline tasks, the model is exposed through the Hugging Face `text-classification` interface.
The structured yield input is serialized as a JSON string. The custom tokenizer parses this string and converts the weather, soil, crop, and cutoff information into the tensors expected by the pretrained model.
The resulting inference path is:
```text
JSON-formatted input string
β
YieldTokenizer
β
weather + soil + crop + cutoff tensors
β
Yield Estimation Transformer
β
scalar yield prediction
β
YIELD_BU_ACRE score
```
The `score` returned by the pipeline is therefore a yield estimate in `bu/acre`, not a classification probability.
### Getting Started
The repository contains the files required for standalone Hugging Face and FlexServ inference:
```text
.
βββ README.md
βββ LICENSE
βββ component-info.yaml
βββ training_code/
βββ config.json
βββ configuration_yield.py
βββ model.safetensors
βββ modeling_yield.py
βββ requirements.txt
βββ sample_input_weekly.json
βββ tokenization_yield.py
βββ tokenizer_config.json
βββ yield_transformer.py
```
A complete inference example is provided in:
```text
sample_input_weekly.json
```
Because the repository provides custom model configuration, tokenizer, and architecture code, Hugging Face loading requires:
```python
trust_remote_code=True
```
### Usage
#### Local Hugging Face Inference
Load the model through the Hugging Face `text-classification` pipeline:
```python
import json
from transformers import pipeline
pipe = pipeline(
"text-classification",
model="ICICLE-AI/yield-estimation",
tokenizer="ICICLE-AI/yield-estimation",
trust_remote_code=True,
)
with open("sample_input_weekly.json") as f:
sample = json.load(f)
prediction = pipe(json.dumps(sample))
print(prediction)
```
Example output:
```python
[
{
"label": "YIELD_BU_ACRE",
"score": 165.1769561767578
}
]
```
The `score` is the predicted corn yield in bushels per acre.
#### Input Format
The structured input contains:
```json
{
"crop": "corn",
"weather_format": "weekly",
"cutoff": 52,
"weather": {
"prcp": ["52 weekly values"],
"srad": ["52 weekly values"],
"swe": ["52 weekly values"],
"tmax": ["52 weekly values"],
"tmin": ["52 weekly values"],
"vp": ["52 weekly values"]
},
"soil": {
"bdod_mean_0-5cm": 0.0,
"...": "remaining soil features"
}
}
```
The complete set of 66 soil variables and their expected ordering are stored in `config.json`.
The tokenizer:
1. parses the JSON-formatted string,
2. validates the expected input fields,
3. constructs the weather, soil, crop, and cutoff tensors.
The Hugging Face pipeline then passes these tensors to the pretrained model for inference.
#### FlexServ Inference
The model has been tested for inference through FlexServ using:
```text
Task: text-classification
Model: ICICLE-AI/yield-estimation
```
FlexServ's `inputs` field expects a string. Therefore, the structured yield input must be supplied as a **JSON-formatted string**, rather than directly as a nested JSON object.
Conceptually, a FlexServ request has the following form:
```json
{
"task": "text-classification",
"inputs": "{\"crop\":\"corn\",\"weather_format\":\"weekly\",\"cutoff\":52,\"weather\":{...},\"soil\":{...}}",
"parameters": {},
"model": "ICICLE-AI/yield-estimation"
}
```
A successful response has the form:
```json
[
{
"label": "YIELD_BU_ACRE",
"score": 165.1769561767578
}
]
```
The returned `score` is the estimated yield in `bu/acre`.
#### Validation
The packaged model can be validated locally against the included sample:
```bash
python - <<'PY'
import json
from transformers import pipeline
with open("sample_input_weekly.json") as f:
sample = json.load(f)
pipe = pipeline(
"text-classification",
model=".",
tokenizer=".",
trust_remote_code=True,
)
print(pipe(json.dumps(sample)))
PY
```
Expected output for the included sample is approximately:
```text
[{'label': 'YIELD_BU_ACRE', 'score': 165.1769561767578}]
```
### Training own model
#### Installation
Clone the model repository:
```bash
git clone https://huggingface.co/ICICLE-AI/yield-estimation
cd yield-estimation/training_code
```
Create and activate a Python environment:
```bash
conda create -n yield_hf python=3.10
conda activate yield_hf
```
Install the required dependencies:
```bash
pip install -r requirements.txt
```
#### Prerequisites
- Python 3.10+
- PyTorch
- Hugging Face Transformers
- Dependencies listed in `requirements.txt`
For GPU training, a CUDA-compatible PyTorch environment is recommended.
The included SLURM script can be used on a compatible HPC system.
#### Problem Description
The objective is to estimate county-level corn yield from weather and soil information.
Each sample contains two primary sources of information:
1. multi-temporal weather observations;
2. static soil properties.
The weather input is represented as:
```text
[K, W]
```
where:
- `K` is the number of temporal observations;
- `W = 6` is the number of weather variables.
The static soil input is represented as:
```text
[S]
```
where:
```text
S = 66
```
The model learns a regression function of the form:
```text
weather + soil + crop information
β
transformer model
β
predicted corn yield
```
The predicted value is expressed in bushels per acre (`bu/acre`).
The model supports multi-temporal estimation by evaluating the weather sequence at different seasonal cutoffs.
#### Repository Structure
The final repository is organized as follows:
```text
.
βββ README.md
βββ requirements.txt
βββ training.slurm
β
βββ checkpoints/
β βββ config.json
β βββ metrics.json
β βββ model.safetensors
β
βββ config/
β βββ __init__.py
β βββ config.py
β
βββ data/
β βββ __init__.py
β βββ dataset.py
β βββ preprocessing.py
β
βββ examples/
β βββ sample_input_weekly.json
β
βββ hf/
β βββ __init__.py
β βββ auto.py
β βββ configuration_yield.py
β βββ modeling_yield.py
β
βββ models/
β βββ __init__.py
β βββ unimodal_ws_crossattn.py
β
βββ scripts/
β βββ __init__.py
β βββ prepare_cornbelt.py
β βββ train_hf.py
β βββ evaluate_hf.py
β βββ inference_hf.py
β
βββ training/
βββ __init__.py
βββ engine.py
```
The major components are:
- `data/` β dataset loading and preprocessing
- `models/` β core neural network architecture
- `training/` β training and evaluation utilities
- `hf/` β Hugging Face AutoClass-compatible regression wrapper used by the training repository
- `scripts/` β data preparation, training, evaluation, and inference entry points
- `checkpoints/` β final trained checkpoint and configuration
- `examples/` β example structured model input
- `training.slurm` β example HPC training job
#### Data Preparation
The USA County Level Crop Yield public dataset is used for training, validation and testing. The data preparation workflow is implemented in:
```text
scripts/prepare_cornbelt.py
```
After preparation, the expected dataset structure is:
```text
data/
βββ cornbelt/
βββ train.h5
βββ val.h5
βββ test.h5
```
The model uses the following six weather variables:
```text
prcp
srad
swe
tmax
tmin
vp
```
The 66 soil variables used by the final checkpoint are recorded in the model configuration.
#### Training
The primary training entry point is:
```text
scripts/train_hf.py
```
The final model uses multi-cutoff training with:
```text
20,24,28,32,36,40,44,48,52
```
An example training command is:
```bash
python scripts/train_hf.py \
--train_file data/cornbelt/train.h5 \
--val_file data/cornbelt/val.h5 \
--test_file data/cornbelt/test.h5 \
--weather_vars prcp,srad,swe,tmax,tmin,vp \
--soil_vars bdod_mean_0-5cm,bdod_mean_5-15cm,bdod_mean_15-30cm,bdod_mean_30-60cm,bdod_mean_60-100cm,bdod_mean_100-200cm,cec_mean_0-5cm,cec_mean_5-15cm,cec_mean_15-30cm,cec_mean_30-60cm,cec_mean_60-100cm,cec_mean_100-200cm,cfvo_mean_0-5cm,cfvo_mean_5-15cm,cfvo_mean_15-30cm,cfvo_mean_30-60cm,cfvo_mean_60-100cm,cfvo_mean_100-200cm,clay_mean_0-5cm,clay_mean_5-15cm,clay_mean_15-30cm,clay_mean_30-60cm,clay_mean_60-100cm,clay_mean_100-200cm,nitrogen_mean_0-5cm,nitrogen_mean_5-15cm,nitrogen_mean_15-30cm,nitrogen_mean_30-60cm,nitrogen_mean_60-100cm,nitrogen_mean_100-200cm,ocd_mean_0-5cm,ocd_mean_5-15cm,ocd_mean_15-30cm,ocd_mean_30-60cm,ocd_mean_60-100cm,ocd_mean_100-200cm,ocs_mean_0-5cm,ocs_mean_5-15cm,ocs_mean_15-30cm,ocs_mean_30-60cm,ocs_mean_60-100cm,ocs_mean_100-200cm,phh2o_mean_0-5cm,phh2o_mean_5-15cm,phh2o_mean_15-30cm,phh2o_mean_30-60cm,phh2o_mean_60-100cm,phh2o_mean_100-200cm,sand_mean_0-5cm,sand_mean_5-15cm,sand_mean_15-30cm,sand_mean_30-60cm,sand_mean_60-100cm,sand_mean_100-200cm,silt_mean_0-5cm,silt_mean_5-15cm,silt_mean_15-30cm,silt_mean_30-60cm,silt_mean_60-100cm,silt_mean_100-200cm,soc_mean_0-5cm,soc_mean_5-15cm,soc_mean_15-30cm,soc_mean_30-60cm,soc_mean_60-100cm,soc_mean_100-200cm \
--crop corn \
--time_agg weekly \
--train_cutoffs 20,24,28,32,36,40,44,48,52 \
--eval_cutoffs 20,24,28,32,36,40,44,48,52 \
--epochs 30 \
--lr 3e-5 \
--batch_size 32 \
--out_dir checkpoints
```
The final checkpoint is stored in:
```text
checkpoints/
```
The checkpoint includes:
```text
config.json
model.safetensors
metrics.json
```
#### Training on Your Own Data
The training pipeline can also be used to train a new yield estimation model on a compatible dataset.
Prepare the dataset in the HDF5 format expected by `YieldDataset` and provide separate training, validation, and test files.
The weather and soil variables supplied to the training command must correspond to the variables available in the prepared dataset.
A general training command is:
```bash
python scripts/train_hf.py \
--train_file <path/to/train.h5> \
--val_file <path/to/val.h5> \
--test_file <path/to/test.h5> \
--weather_vars <comma-separated-weather-variables> \
--soil_vars <comma-separated-soil-variables> \
--crop <crop-name> \
--time_agg weekly \
--train_cutoffs <comma-separated-training-cutoffs> \
--eval_cutoffs <comma-separated-evaluation-cutoffs> \
--epochs <number-of-epochs> \
--lr <learning-rate> \
--batch_size <batch-size> \
--out_dir <output-directory>
```
#### SLURM Training
An example SLURM job is provided in:
```text
training.slurm
```
Submit it using:
```bash
sbatch training.slurm
```
#### Evaluation
The trained checkpoint can be evaluated using:
```text
scripts/evaluate_hf.py
```
For the final multi-cutoff model:
```bash
python scripts/evaluate_hf.py \
--hf_model_dir checkpoints \
--test_file data/cornbelt/test.h5 \
--cutoffs 20,24,28,32,36,40,44,48,52 \
--batch_size 64 \
--output_csv checkpoints/test_predictions.csv \
--metrics_json checkpoints/test_metrics.json
```
Evaluation is performed independently at the configured seasonal cutoffs.
The evaluation process:
1. loads the final trained checkpoint;
2. loads the test dataset;
3. applies the normalization statistics stored in the checkpoint configuration;
4. performs inference at the requested cutoffs;
5. computes evaluation metrics;
6. save predictions and metrics to disk.
#### Inference
Inference using the trained checkpoint is implemented in:
```text
scripts/inference_hf.py
```
An example structured input is provided in:
```text
examples/sample_input_weekly.json
```
The sample follows the general structure:
```json
{
"crop": "corn",
"weather_format": "weekly",
"cutoff": 52,
"weather": {
"prcp": [],
"srad": [],
"swe": [],
"tmax": [],
"tmin": [],
"vp": []
},
"soil": {
"bdod_mean_0-5cm": 0.0
}
}
```
The complete sample file contains the required weather sequence and soil variables.
Run single-sample inference with:
```bash
python scripts/inference_hf.py \
--hf_model_dir checkpoints \
--single_sample_json examples/sample_input_weekly.json \
--cutoff 52 \
--output_csv inference_prediction.csv
```
The output contains the predicted yield for the requested cutoff.
For example:
```text
sample_idx,cutoff,y_pred
0,52,<predicted_yield>
```
---
# Explanation
### Features
- **Transformer-Based Yield Estimation:** Uses a transformer architecture to model temporal weather information for corn yield prediction.
- **Weather and Soil Integration:** Combines six weekly weather variables with 66 static soil properties.
- **Multi-Temporal Inference:** Supports yield estimation at multiple seasonal cutoffs from week 20 through week 52.
- **Automatic Preprocessing:** The custom tokenizer converts JSON-formatted structured inputs into the tensors expected by the pretrained model.
- **Automatic Normalization:** Weather and soil features are normalized using statistics stored with the model configuration.
- **Regression Output:** Produces a scalar corn yield estimate in bushels per acre.
- **Hugging Face Integration:** Uses the standard Transformers pipeline interface with repository-provided model and tokenizer code.
- **FlexServ Deployment:** Uses the supported `text-classification` task to expose the regression model as a FlexServ inference service.
- **CPU and GPU Support:** Supports PyTorch inference on CPU and compatible CUDA GPUs.
- **Safetensors Weights:** Model weights are distributed using the Safetensors format. |