👨🏿💻👩🏽💻🌈🪴 Want to join the AI Institute for Food Systems team and help lead AgML development? 🪴🌈👩🏼💻👨🏻💻
We're looking to hire a postdoc with both Python library development and ML experience. Send your resume and GitHub profile link to jmearles@ucdavis.edu!
Overview
AgML is a comprehensive library for agricultural machine learning. Currently, AgML provides
access to a wealth of public agricultural datasets for common agricultural deep learning tasks.
AgML supports both the TensorFlow and PyTorch machine learning frameworks.
To browse our latest datasets, view our full set of documentation, and compare model performance on your crops / tasks of interest, please visit project-agml.github.io.
Installation
To install the latest release of AgML, run the following command:
pip install agml
Quick Start
AgML datasets are hosted on the Hugging Face Hub under the Project-AgML
organization. You can start off by using the HuggingFaceDataLoader to download and load a dataset directly into a
native Hugging Face DatasetDict:
from agml.data import HuggingFaceDataLoader
# Load a dataset from the Hub
loader = HuggingFaceDataLoader("Project-AgML/apple_flower_segmentation")
# Load a specific config/subset (e.g. an augmented variant)
loader = HuggingFaceDataLoader("Project-AgML/apple_flower_segmentation", config="augmented")
HuggingFaceDataLoader automatically casts image-like columns (image, mask, and image-typed label columns) to
the Hugging Face Image type for decoded pixel access.
You can split the dataset into train/val/test sets, with optional stratification across one or more columns:
dataset = loader.split(val_size=0.1, test_size=0.1, stratify_cols="label")
# Returns a DatasetDict with 'train', 'val', and 'test' splits
# Access the underlying DatasetDict at any time
dataset = loader.dataset
For any preprocessing, inference, or training beyond loading and splitting, use the
datasets and transformers
libraries directly. Since loader.dataset is a native Hugging Face DatasetDict, it works out of the box with
datasets' map, filter, and with_transform methods for preprocessing, and with transformers' Trainer,
Pipeline, and model classes for training and inference — there's no separate AgML-specific processing API to learn.
from transformers import AutoImageProcessor, AutoModelForImageClassification, Trainer
processor = AutoImageProcessor.from_pretrained("google/vit-base-patch16-224")
model = AutoModelForImageClassification.from_pretrained(
"google/vit-base-patch16-224",
num_labels=dataset["train"].features["label"].num_classes,
)
def preprocess(batch):
batch["pixel_values"] = processor(batch["image"], return_tensors="pt")["pixel_values"]
return batch
dataset = dataset.with_transform(preprocess)
trainer = Trainer(model=model, train_dataset=dataset["train"], eval_dataset=dataset["val"])
trainer.train()
Image-Text-to-Text Datasets
Some AgML datasets (e.g. Project-AgML/AgroOmni) are too large to fit the standard load_dataset()
archive handling, which fully extracts every archive to disk before reading it. These datasets instead store images
as raw relative paths inside zip shards, alongside a single shared path -> shard index, so no extraction step is
needed and no per-archive metadata has to be duplicated. Because of that, they aren't load_dataset()-compatible,
and are loaded with loadImageTextToTextDataset instead:
from agml import loadImageTextToTextDataset
# Whole dataset, split-aware:
ds = loadImageTextToTextDataset(
"Project-AgML/AgroOmni",
token=HF_TOKEN
)
ds["train"][0] # {'id', 'messages', 'raw_metadata', 'images': [PIL.Image, ...]}
# Single split:
train_ds = loadImageTextToTextDataset(
"Project-AgML/AgroOmni",
split="train",
token=HF_TOKEN
)
# Multi-config repo (each config under its own folder): pass `config` to select one.
ds = loadImageTextToTextDataset(
"Project-AgML/MIRAGE",
config="MMST_Standard",
token=HF_TOKEN,
)
loadImageTextToTextDataset(repo_id, config=None, split=None, cache_dir=None, token=None, cache_max_bytes=...) returns a DatasetDict
when split is omitted, or a single Dataset when a split name is given. Splits are auto-discovered from the
metadata parquet filenames (e.g. train-0000-of-0001.parquet becomes train), the same convention load_dataset()
itself uses; if no split can be inferred, every row is placed under a single train split. For repos with multiple
configs, each living under its own folder, config is required, if it's omitted the error message lists the
configs available in that repo.
Image bytes are only read the first time a row is actually accessed (ds["train"][0], a slice, or a batch), never
eagerly over the whole dataset, and repeated access to the same image is cached. The ImageTextToTextShardStore
backing that lazy decoding is attached to the returned dataset as ds.store, in case you want to inspect its cache,
call ds.store.getBytes(path) directly, or free its open shard file handles and cached bytes (ds.store.close()).
The cache is bounded by total size, 256 MiB by default; pass cache_max_bytes= to change it, or 0 to disable it.
If you're using this with a PyTorch DataLoader, prefer num_workers > 0. Each worker process opens its
own shard handles (and keeps its own byte cache), whether workers are forked or spawned.
Agricultural Statistics
agml.ag_stats holds crop statistics, one subpackage per data source. Each source has its own extra, so only the
packages for the sources you use are installed:
| Source |
Module |
Data |
| USDA NASS |
agml.ag_stats.nass |
United States crop statistics at national, state, district, and county level |
| California Crop Yield Benchmark |
agml.ag_stats.calicropyield |
California county crop yield with satellite, ET, climate, and soil layers |
USDA NASS Crop Statistics
agml.ag_stats.nass queries USDA NASS crop statistics (yield, area and production, Census of Agriculture, crop progress,
crop condition, and yield forecasts) at national, state, district, and county level. It needs the ag_stats extra
(pip install agml[ag_stats]). The data is read from the
Project-AgML/usda_nass_crop_statistics
dataset on Hugging Face. Each table is downloaded the first time it is queried and cached after that:
from agml.ag_stats.nass import NASSDataLoader
nass = NASSDataLoader() # or NASSDataLoader("/path/to/local/copy"), or set AGML_NASS_PATH
# Corn grain yield for one county, 2000 to 2024, as a pandas DataFrame.
df = nass.load_yield(crop="corn", utilization="grain", state="IA", county="Story", years=(2000, 2024))
# Weekly planting progress for soybeans in Illinois, as a polars DataFrame.
nass.load_progress(crop="soybeans", state="IL", years=2023, stage="planted", format="polars")
# State-level wheat production for several states, written to a CSV file.
nass.load_area_production(crop="wheat", state=["KS", "ND"], level="state", statistic="production",
format="csv", path="wheat_production.csv")
# Look up names and codes.
nass.catalog.crops("corn")
nass.catalog.counties(state="IA")
# 2020 Census county boundaries, with WKB geometry.
nass.load_boundaries("county")
NASSDataLoader.tables() lists every table. nass.query(table, ...) takes the same filters as the per-table methods:
crop, crop_class, practice, utilization, state, county, district, years, level, where, and
columns. Results come back as pandas (default), polars, arrow, or records, or are written to a csv or
parquet file. Pass revision= (a commit hash) to NASSDataLoader to pin a version of the dataset, and cache_dir=
to choose where downloads are kept.
California Crop Yield Benchmark
agml.ag_stats.calicropyield reads the California Crop Yield Benchmark:
county crop yield (t/ha) for over 100 crops in the 58 California counties (2008-2022), with Cropland Data Layer,
monthly Landsat and OpenET, irrigation, daily DayMet climate, and soil layers for each county. It needs the
calicropyield extra (pip install agml[calicropyield]; the climate and soil layers need zarr 3, so Python 3.11 or
newer). Each file is downloaded from Hugging Face the first time it is read and cached after that:
from agml.ag_stats.calicropyield import CaliCropYieldLoader
cali = CaliCropYieldLoader() # or CaliCropYieldLoader("/path/to/local/copy"), or set AGML_CALICROPYIELD_PATH
# County yield as a pandas DataFrame.
cali.load_yield(county="Yolo", crop="tomatoes", years=(2015, 2022))
# Rasters as xarray DataArrays in EPSG:4326, optionally cropped to a shapely geometry.
cdl = cali.load_cdl("Yolo", 2020) # (y, x) CDL class codes
tomatoes = cali.crop_mask("Yolo", 2020, crops=cali.yield_crop_cdl_codes("TOMATOES"))
landsat = cali.load_landsat("Yolo", 2020, months=[6, 7, 8]) # (month, band, y, x)
et = cali.load_et("Yolo", 2020) # (month, y, x)
# Climate and soil as xarray Datasets.
weather = cali.load_climate("Yolo", years=[2019, 2020], variables=["prcp", "tmax"], monthly=True)
soil = cali.load_soil("Yolo", variables=["soc0_100", "nccpi3all"])
# Fetch files ahead of time, e.g. before working offline.
cali.download("cdl", ["Fresno", "Kern"], years=(2018, 2022))
CaliCropYieldLoader.layers() lists the layers and years(layer) their years. load_boundaries() returns the county
polygons as a GeoDataFrame.
Public Datasets
AgML contains a wide variety of public datasets from various locations across the world:
Use the Dataset Search experience for filtering and previews. For programmatic filtering,
agml.data.public_data_sources(...) supports task and modality filters.
iNatAg and iNatAg-mini
AgML provides an API with direct access to iNatAg (and iNatAg-mini), one of the world's largest collections of agricultural images dedicated for the task of image classification. Collectively, this dataset contains over 4 million images along with detailed species classificaations and enables access to a variety of large-scale agricultural machine learning tasks. You can instantiate the iNatAg (or iNatAg-mini, a smaller variant of iNatAg for smaller-scale applications) dataset as follows:
# To select a collection of scientific family names.
loader = agml.data.AgMLDataLoader.from_parent("iNatAg", filters={"family_name": ["...", "..."]})
# To select common names.
loader = agml.data.AgMLDataLoader.from_parent("iNatAg", filters={"common_name": "..."})
Usage Information
Using Public Agricultural Data
AgML aims to provide easy access to a range of existing public agricultural datasets The core of AgML's public data pipeline is
AgMLDataLoader. You can use the AgMLDataLoader or agml.data.download_public_dataset() to download
the dataset locally from which point it will be automatically loaded from the disk on future runs.
From this point, the data within the loader can be split into train/val/test sets, batched, have augmentations and transforms
applied, and be converted into a training-ready dataset (including batching, tensor conversion, and image formatting).
Annotation Formats
A core aim of AgML is to provide datasets in a standardized format, enabling the synthesizing of multiple datasets
into a single training pipeline. Datasets on the Hugging Face Hub encode annotations as columns on the underlying
Dataset/DatasetDict:
- Image Classification: a
label column of type ClassLabel.
- Object Detection: an
objects column, holding COCO-style bounding boxes (un-normlized [x_min, y_min, width, height]) and corresponding category IDs as a ClassLabel per image.
- Semantic Segmentation: a
mask column, a single-channel (L-mode) image the same size as the corresponding image.
Contributions
We welcome contributions! If you would like to contribute a new feature, fix an issue that you've noticed, or even just mention
a bug or feature that you would like to see implemented, please don't hesitate to use the Issues tab to bring it to our attention.
See the contributing guidelines for more information, or the
guide to contributing leaderboard results if you have benchmark results to add.
Funding
This project is partly funded by the National AI Institute for Food Systems.