Skyterra作物分类器MCP服务器
使用TerraTorch和NASA-IBM的Prithvi基础模型进行多时相作物分类的模型上下文协议(MCP)服务器。
概述
该MCP服务器为AI代理提供了使用多时相数据训练的深度学习模型将卫星图像分类为13种不同作物类型的能力。该系统使用Prithvi-EO-v2-300地理空间基础模型和UperNet解码器进行语义分割。
作物类别
该模型将像素分为13种裁剪类型:
- 自然植被
- 森林
- 玉米
- 大豆
- 湿地
- 发达/贫瘠
- 开放水域
- 冬小麦
- 苜蓿
- 休耕/闲置耕地
- 棉花
- 高粱
- 其他
输入要求
- 格式:带空间参考的GeoTIFF
- 乐队:总共18个波段(6个光谱波段×3个时间观测)
- 光谱:蓝色、绿色、红色、近红外_箭头、SWIR_1、SWIR_2 - 时间:早季、中季、晚季
- 决心:30米(HLS/Landsat兼容)
- 瓷砖尺寸:支持任何大小(224×224平铺推理)
输出格式
- 分类光栅 (
*_pred.tif):带类ID(0-12)的单波段GeoTIFF - 可视化 (
*_visualization.png):带分类叠加的RGB图像
安装
先决条件
- Python 3.10或更高版本
- 支持CUDA的GPU(推荐)或CPU回退
uv包管理器(在此处安装)
使用uv进行安装
- 克隆或下载此存储库:
cd skyterra-crop-classifier-mcp- 创建虚拟环境并安装依赖关系:
uv venv
source .venv/bin/activate # On Windows: .venv\Scripts\activate
uv pip install -e .安装TerraTorch
TerraTorch在PyPI上不可用,必须从GitHub安装:
uv pip install git+https://github.com/IBM/terratorch.gitGPU支持
对于CUDA支持,请确保PyTorch与CUDA一起安装:
uv pip install torch --index-url https://download.pytorch.org/whl/cu124配置
MCP服务器配置
服务器需要两个主要参数:
- 检查点路径:已训练模型检查点的路径(
.ckpt文件) - 配置路径 (可选):YAML配置文件的路径
适用于克劳德桌面
添加到您的Claude Desktop配置文件中:
macOS: ~/Library/Application Support/Claude/claude_desktop_config.json
视窗: %APPDATA%/Claude/claude_desktop_config.json
Linux: ~/.config/Claude/claude_desktop_config.json
{
"mcpServers": {
"skyterra-crop-classifier": {
"command": "uv",
"args": [
"--directory",
"/absolute/path/to/skyterra-crop-classifier-mcp",
"run",
"skyterra-crop-mcp",
"--checkpoint",
"/absolute/path/to/checkpoint.ckpt"
]
}
}
}使用可选配置文件
{
"mcpServers": {
"skyterra-crop-classifier": {
"command": "uv",
"args": [
"--directory",
"/absolute/path/to/skyterra-crop-classifier-mcp",
"run",
"skyterra-crop-mcp",
"--checkpoint",
"/absolute/path/to/checkpoint.ckpt",
"--config",
"/absolute/path/to/config.yaml"
]
}
}
}用法
可用工具
MCP服务器公开了三个工具:
1. classify_crop_image
对单个多时相卫星图像进行分类。
参数:
image_path(必填):输入GeoTIFF文件的路径output_dir(可选):输出目录(默认:./output)visualize(可选):创建可视化(默认值:true)
对克劳德的请求示例:
Please classify the crop image at /path/to/image.tif and save the results to /path/to/output退货:
- 带有作物类别ID的GeoTIFF分类
- 统计数据(主要类别、类别分布、像素数)
- 可选可视化PNG
2. classify_crop_directory
对目录中的所有TIFF文件进行批量分类。
参数:
input_dir(必填):包含输入TIFF文件的目录output_dir(可选):输出目录(默认:./output)visualize(可选):创建可视化(默认值:false)
对克劳德的请求示例:
Please classify all crop images in /path/to/images and save to /path/to/output退货:
- 已处理文件列表
- 失败文件列表(如有)
- 处理摘要
3. get_crop_classes
获取有关13种作物类别和型号的详细信息。
参数: 无
对克劳德的请求示例:
What crop classes can you classify?退货:
- 13种作物类别及其描述列表
- 型号信息(主干、解码器、频带、分辨率)
工作流示例
单幅图像分类
User: I have a satellite image at /data/field_2024.tif. Can you classify the crops?
Claude: [Uses classify_crop_image tool]
User: What's the dominant crop type?
Claude: According to the classification, the dominant crop is Corn, covering 45.3%
of the area (12,456 pixels). Soybeans are the second most common at 32.1%.批处理
User: Please classify all images in /data/fields_2024/ and save to /data/results/
Claude: [Uses classify_crop_directory tool]
I've processed 15 images successfully:
- field_001.tif → field_001_pred.tif
- field_002.tif → field_002_pred.tif
...
All results saved to /data/results/班级信息
User: What crop types can you detect?
Claude: [Uses get_crop_classes tool]
I can classify 13 different crop types:
0. Natural Vegetation
1. Forest
2. Corn
3. Soybeans
...模型信息
建筑
- 骨干:Prithvi-EO-v2-300,带时间位置编码
- 解码器:具有256个通道的UperNet
- 输入:6个光谱带×3个时间帧=18个通道
- 输出:13个作物类(语义分割)
- 推断:采用224×224瓷砖进行瓷砖处理,50%重叠
演出
- 图形处理器:每224×224块约2-5秒(取决于GPU)
- 中央处理器:每224×224图块约10-30秒(回退模式)
- 记忆:~4-6GB GPU VRAM或~8GB RAM(CPU模式)
设备支持
服务器会自动检测并使用:
- CUDA GPU(如果可用)(推荐)
- 如果未检测到GPU,则CPU回退
加载模型时,您可以在服务器日志中检查正在使用的设备。
故障排除
模型加载错误
错误: Checkpoint file not found
- 验证检查点路径是否绝对正确
- 检查文件权限
错误: CUDA out of memory
- 尝试较小的图像
- 通过删除CUDA使用CPU模式
- 减少批量处理
输入文件错误
错误: Expected 18 bands, got X
- 验证输入具有正确的频带结构(6个频谱×3个时间)
- 检查乐队订购
错误: No TIFF files found
- 检查目录路径是否正确
- 验证文件是否具有
.tif或.tiff扩展
安装问题
错误: ModuleNotFoundError: No module named 'terratorch'
uv pip install git+https://github.com/IBM/terratorch.git错误: torch not compiled with CUDA
uv pip install torch --index-url https://download.pytorch.org/whl/cu124发展
项目结构
skyterra-crop-classifier-mcp/
├── pyproject.toml # Project configuration
├── README.md # This file
├── src/
│ └── skyterra_crop_mcp/
│ ├── __init__.py # Package exports
│ ├── inference.py # Inference logic
│ └── server.py # MCP server implementation
└── examples/
└── config.json # Example MCP configuration本地测试
直接运行服务器进行测试:
python -m skyterra_crop_mcp.server \
--checkpoint /path/to/checkpoint.ckpt \
--config /path/to/config.yaml添加自定义配置
创建一个包含模型参数的YAML配置文件:
bands: ["BLUE", "GREEN", "RED", "NIR_NARROW", "SWIR_1", "SWIR_2"]
num_frames: 3
backbone: "prithvi_eo_v2_300_tl"
decoder: "UperNetDecoder"
neck_indices: [5, 11, 17, 23]
num_classes: 13
class_weights: [0.386, 0.661, 0.548, ...] # 13 values许可证
该项目基于TerraTorch和Prithvi模型构建。请参阅各自的许可证:
引用
如果您在研究中使用此工具,请引用Prithvi和TerraTorch项目:
@article{prithvi2024,
title={Prithvi-EO 2.0: A Foundation Model for Earth Observation},
author={IBM and NASA},
year={2024}
}支持
对于问题和疑问:
- TerraTorch:https://github.com/IBM/terratorch/issues
- MCP协议:https://modelcontextprotocol.io
更新日志
v0.1.0(2024)
- 初始版本
- 三个MCP工具:classfy_crop_image、classify_crop_directory、get_rop_classes
- CPU回退支持
- 自动设备检测
- 13作物分类
