> ## Documentation Index
> Fetch the complete documentation index at: https://docs.monostate.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 视觉任务

> 训练图像分类、检测和 VLM 模型

# 视觉任务

训练用于图像分类、目标检测和视觉-语言任务的模型。

## 图像分类

### 快速开始

```bash theme={null}
aitraining image-classification \
  --model google/vit-base-patch16-224 \
  --data-path ./images/ \
  --project-name image-classifier
```

### Parameters

| Parameter         | Description          | Default                       |
| ----------------- | -------------------- | ----------------------------- |
| `--model`         | Base model           | `google/vit-base-patch16-224` |
| `--data-path`     | Path to image folder | None (required)               |
| `--project-name`  | Output directory     | `project-name`                |
| `--image-column`  | Image column name    | `image`                       |
| `--target-column` | Label column name    | `target`                      |
| `--epochs`        | Training epochs      | `3`                           |
| `--batch-size`    | Batch size           | `8`                           |
| `--lr`            | Learning rate        | `5e-5`                        |

### 数据格式

文件夹结构：

```
images/
├── cat/
│   ├── cat1.jpg
│   └── cat2.jpg
├── dog/
│   ├── dog1.jpg
│   └── dog2.jpg
```

Or CSV with paths:

```csv theme={null}
image,label
/path/to/cat1.jpg,cat
/path/to/dog1.jpg,dog
```

### 示例：自定义分类器

```bash theme={null}
aitraining image-classification \
  --model microsoft/resnet-50 \
  --data-path ./product_images/ \
  --project-name product-classifier \
  --epochs 10 \
  --batch-size 32 \
  --lr 5e-5
```

## 图像回归

用于从图像预测连续值。

### 快速开始

```bash theme={null}
aitraining image-regression \
  --model google/vit-base-patch16-224 \
  --data-path ./images.csv \
  --image-column image \
  --target-column score \
  --project-name quality-scorer
```

### Parameters

| Parameter         | Description         | Default                       |
| ----------------- | ------------------- | ----------------------------- |
| `--model`         | Base model          | `google/vit-base-patch16-224` |
| `--data-path`     | Path to data        | None (required)               |
| `--project-name`  | Output directory    | `project-name`                |
| `--image-column`  | Image column name   | `image`                       |
| `--target-column` | Target value column | `target`                      |
| `--epochs`        | Training epochs     | `3`                           |
| `--batch-size`    | Batch size          | `8`                           |
| `--lr`            | Learning rate       | `5e-5`                        |

### 示例：年龄预测

```bash theme={null}
aitraining image-regression \
  --model microsoft/resnet-50 \
  --data-path ./faces.csv \
  --image-column photo \
  --target-column age \
  --project-name age-predictor \
  --epochs 20
```

## 目标检测

### 快速开始

```bash theme={null}
aitraining object-detection \
  --model facebook/detr-resnet-50 \
  --data-path ./coco_format/ \
  --project-name detector
```

### 数据格式

COCO 风格格式：

```json theme={null}
{
  "image": "image.jpg",
  "objects": {
    "bbox": [[x, y, width, height], ...],
    "categories": [0, 1, ...]
  }
}
```

### Parameters

| Parameter             | Description            | Default   |
| --------------------- | ---------------------- | --------- |
| `--image-column`      | Image column           | `image`   |
| `--objects-column`    | Objects column         | `objects` |
| `--image-square-size` | Square size for images | `600`     |

### 示例：自定义检测

```bash theme={null}
aitraining object-detection \
  --model facebook/detr-resnet-50 \
  --data-path ./annotations/ \
  --project-name custom-detector \
  --epochs 50 \
  --batch-size 8
```

## 视觉-语言模型（VLM）

训练能够理解图像和文本的模型。

### 快速开始

```bash theme={null}
aitraining vlm \
  --model google/paligemma-3b-pt-224 \
  --data-path ./image_captions.jsonl \
  --project-name vlm-model
```

### 数据格式

```json theme={null}
{
  "image": "image.jpg",
  "conversations": [
    {"role": "user", "content": "What's in this image?"},
    {"role": "assistant", "content": "A cat sitting on a couch."}
  ]
}
```

### Parameters

| Parameter                 | Description                                                      | Default                      |
| ------------------------- | ---------------------------------------------------------------- | ---------------------------- |
| `--model`                 | Base model                                                       | `google/paligemma-3b-pt-224` |
| `--image-column`          | Image column                                                     | `image`                      |
| `--text-column`           | Text/answer column                                               | `text`                       |
| `--prompt-text-column`    | Prompt/prefix column                                             | `prompt`                     |
| `--trainer`               | Training mode (`vqa`, `captioning`, `segmentation`, `detection`) | `vqa`                        |
| `--epochs`                | Training epochs                                                  | `3`                          |
| `--batch-size`            | Batch size                                                       | `2`                          |
| `--lr`                    | Learning rate                                                    | `5e-5`                       |
| `--gradient-accumulation` | Gradient accumulation steps                                      | `4`                          |
| `--peft`                  | Enable LoRA                                                      | `False`                      |
| `--lora-r`                | LoRA rank                                                        | `16`                         |
| `--lora-alpha`            | LoRA alpha                                                       | `32`                         |

### 示例：图像字幕

```bash theme={null}
aitraining vlm \
  --model Qwen/Qwen2-VL-2B-Instruct \
  --data-path ./captions.jsonl \
  --project-name captioner \
  --epochs 3 \
  --batch-size 2 \
  --peft \
  --lora-r 16
```

## 常用模型

### 图像分类

| Model                         | Parameters | Best For        |
| ----------------------------- | ---------- | --------------- |
| `google/vit-base-patch16-224` | 86M        | General purpose |
| `microsoft/resnet-50`         | 25M        | Fast inference  |
| `facebook/convnext-base-224`  | 89M        | High accuracy   |

### 目标检测

| Model                      | Parameters | Best For          |
| -------------------------- | ---------- | ----------------- |
| `facebook/detr-resnet-50`  | 41M        | General detection |
| `facebook/detr-resnet-101` | 60M        | Higher accuracy   |

### 视觉-语言

| Model                       | Parameters | Best For     |
| --------------------------- | ---------- | ------------ |
| `Qwen/Qwen2-VL-2B-Instruct` | 2B         | Balanced     |
| `llava-hf/llava-1.5-7b-hf`  | 7B         | High quality |

## GPU 内存提示

* 对于大图像使用较小的批次大小
* 为 VLM 启用梯度检查点
* 使用 LoRA 进行 VLM 训练：

```bash theme={null}
aitraining vlm \
  --model Qwen/Qwen2-VL-2B-Instruct \
  --data-path ./data.jsonl \
  --project-name vlm \
  --peft \
  --lora-r 16 \
  --batch-size 1 \
  --gradient-accumulation 8
```

## 下一步

<CardGroup cols={2}>
  <Card title="文本任务" href="/cli/text-tasks">
    文本分类和 NER
  </Card>

  <Card title="表格数据" href="/cli/tabular-data">
    在结构化数据上训练
  </Card>
</CardGroup>
