Initial public project cleanup
This commit is contained in:
commit
2034b04519
95 files changed
+16583
No files matched your search
@@ -0,0 +1,24 @@
|
||||
# Data and model files
|
||||
|
||||
This directory is reserved for local datasets, checkpoints, exported models, and caches.
|
||||
|
||||
Large artifacts are intentionally ignored by git:
|
||||
|
||||
- `processed/`: train/validation image folders
|
||||
- `raw/`: raw source images
|
||||
- `models/`: `.pth`, `.pt`, `.onnx` checkpoints and exported models
|
||||
- `cache/`: generated caches
|
||||
|
||||
Expected processed dataset layout:
|
||||
|
||||
```text
|
||||
processed/
|
||||
├── train/
|
||||
│ ├── class_0/
|
||||
│ ├── class_1/
|
||||
│ └── ...
|
||||
└── val/
|
||||
├── class_0/
|
||||
├── class_1/
|
||||
└── ...
|
||||
```
|
||||
+327
@@ -0,0 +1,327 @@
|
||||
# 数据集准备说明
|
||||
|
||||
## 数据集要求
|
||||
|
||||
### 1. 图像采集
|
||||
|
||||
#### 1.1 采集设备
|
||||
- 相机/手机摄像头
|
||||
- 固定拍摄距离和角度
|
||||
- 保持光照条件一致
|
||||
|
||||
#### 1.2 图像要求
|
||||
- **格式**: JPG, PNG
|
||||
- **分辨率**: 建议 ≥ 224x224 (会自动resize)
|
||||
- **数量**: 每类至少50张,推荐100+张
|
||||
- **质量**: 清晰、无模糊
|
||||
|
||||
### 1.3 类别定义
|
||||
根据光瓣数量分类:
|
||||
- class_0: 0个光瓣
|
||||
- class_1: 1个光瓣
|
||||
- class_2: 2个光瓣
|
||||
- ...
|
||||
- class_9: 9个光瓣
|
||||
|
||||
---
|
||||
|
||||
## 数据集组织
|
||||
|
||||
### 2.1 目录结构
|
||||
|
||||
```
|
||||
data/
|
||||
├── raw/ # 原始采集的图像
|
||||
│ ├── class_0/
|
||||
│ │ ├── img_001.jpg
|
||||
│ │ ├── img_002.jpg
|
||||
│ │ └── ...
|
||||
│ ├── class_1/
|
||||
│ │ ├── img_001.jpg
|
||||
│ │ └── ...
|
||||
│ └── ...
|
||||
│
|
||||
└── processed/ # 分割后的训练/验证集
|
||||
├── train/
|
||||
│ ├── class_0/
|
||||
│ ├── class_1/
|
||||
│ └── ...
|
||||
└── val/
|
||||
├── class_0/
|
||||
├── class_1/
|
||||
└── ...
|
||||
```
|
||||
|
||||
### 2.2 创建数据集结构
|
||||
|
||||
```bash
|
||||
# 方法1: 使用脚本创建
|
||||
cd scripts
|
||||
python prepare_dataset.py --mode create --output ../data/processed --num_classes 10
|
||||
|
||||
# 方法2: 手动创建
|
||||
mkdir -p data/raw/class_{0..9}
|
||||
mkdir -p data/processed/train/class_{0..9}
|
||||
mkdir -p data/processed/val/class_{0..9}
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 数据采集指南
|
||||
|
||||
### 3.1 拍摄建议
|
||||
|
||||
**环境设置**:
|
||||
- 使用固定背景(黑色或白色)
|
||||
- 保持光照稳定
|
||||
- 避免反光和阴影
|
||||
|
||||
**拍摄角度**:
|
||||
- 正面拍摄
|
||||
- 保持相机与目标垂直
|
||||
- 距离固定(建议30-50cm)
|
||||
|
||||
**多样性**:
|
||||
- 不同亮度的光
|
||||
- 不同大小的光瓣
|
||||
- 轻微的角度变化(±15°)
|
||||
|
||||
### 3.2 数据增强
|
||||
|
||||
训练时会自动应用以下增强:
|
||||
- 随机水平翻转
|
||||
- 随机旋转(±15°)
|
||||
- 颜色抖动(亮度、对比度、饱和度)
|
||||
|
||||
因此采集时不需要手动增强。
|
||||
|
||||
---
|
||||
|
||||
## 数据集分割
|
||||
|
||||
### 4.1 自动分割
|
||||
|
||||
```bash
|
||||
# 将raw目录的图像按8:2分割为训练集和验证集
|
||||
cd scripts
|
||||
python prepare_dataset.py \
|
||||
--mode split \
|
||||
--source ../data/raw \
|
||||
--output ../data/processed \
|
||||
--train_ratio 0.8
|
||||
```
|
||||
|
||||
### 4.2 手动分割
|
||||
|
||||
如果需要手动控制:
|
||||
1. 将80%的图像放入 `processed/train/class_X/`
|
||||
2. 将20%的图像放入 `processed/val/class_X/`
|
||||
|
||||
---
|
||||
|
||||
## 数据集质量检查
|
||||
|
||||
### 5.1 检查脚本
|
||||
|
||||
```python
|
||||
# check_dataset.py
|
||||
import os
|
||||
from collections import Counter
|
||||
|
||||
def check_dataset(data_dir):
|
||||
"""检查数据集"""
|
||||
stats = {}
|
||||
|
||||
for split in ['train', 'val']:
|
||||
split_dir = os.path.join(data_dir, split)
|
||||
if not os.path.exists(split_dir):
|
||||
print(f"警告: {split_dir} 不存在")
|
||||
continue
|
||||
|
||||
class_counts = {}
|
||||
for class_name in os.listdir(split_dir):
|
||||
class_path = os.path.join(split_dir, class_name)
|
||||
if os.path.isdir(class_path):
|
||||
images = [f for f in os.listdir(class_path)
|
||||
if f.lower().endswith(('.png', '.jpg', '.jpeg'))]
|
||||
class_counts[class_name] = len(images)
|
||||
|
||||
stats[split] = class_counts
|
||||
|
||||
print(f"\n{split.upper()}集:")
|
||||
for class_name, count in sorted(class_counts.items()):
|
||||
print(f" {class_name}: {count} 张")
|
||||
print(f" 总计: {sum(class_counts.values())} 张")
|
||||
|
||||
return stats
|
||||
|
||||
if __name__ == '__main__':
|
||||
check_dataset('../data/processed')
|
||||
```
|
||||
|
||||
运行检查:
|
||||
```bash
|
||||
python check_dataset.py
|
||||
```
|
||||
|
||||
### 5.2 质量标准
|
||||
|
||||
✅ **合格标准**:
|
||||
- 每类至少50张训练图像
|
||||
- 每类至少10张验证图像
|
||||
- 类别分布相对均衡(最大差异不超过2倍)
|
||||
- 图像清晰可辨
|
||||
|
||||
⚠️ **需要改进**:
|
||||
- 某些类别图像过少(<30张)
|
||||
- 类别分布严重不均(差异>3倍)
|
||||
- 图像质量差(模糊、过暗)
|
||||
|
||||
---
|
||||
|
||||
## 示例数据集
|
||||
|
||||
### 6.1 使用公开数据集
|
||||
|
||||
如果暂时没有实际数据,可以使用类似的数据集进行测试:
|
||||
|
||||
**CIFAR-10** (用于快速验证):
|
||||
```python
|
||||
# download_cifar10.py
|
||||
from torchvision import datasets, transforms
|
||||
|
||||
# 下载CIFAR-10
|
||||
train_dataset = datasets.CIFAR10(
|
||||
root='../data/cifar10',
|
||||
train=True,
|
||||
download=True
|
||||
)
|
||||
|
||||
val_dataset = datasets.CIFAR10(
|
||||
root='../data/cifar10',
|
||||
train=False,
|
||||
download=True
|
||||
)
|
||||
|
||||
print(f"训练集: {len(train_dataset)} 张")
|
||||
print(f"验证集: {len(val_dataset)} 张")
|
||||
```
|
||||
|
||||
**自定义光图像数据集**:
|
||||
- 使用图像生成工具创建合成数据
|
||||
- 或从网络搜集类似图像
|
||||
|
||||
### 6.2 合成数据生成
|
||||
|
||||
```python
|
||||
# generate_synthetic_data.py
|
||||
import cv2
|
||||
import numpy as np
|
||||
import os
|
||||
|
||||
def generate_light_image(num_petals, size=224):
|
||||
"""生成光瓣图像"""
|
||||
img = np.zeros((size, size, 3), dtype=np.uint8)
|
||||
center = (size // 2, size // 2)
|
||||
|
||||
# 绘制光瓣
|
||||
angle_step = 360 / max(num_petals, 1) if num_petals > 0 else 0
|
||||
|
||||
for i in range(num_petals):
|
||||
angle = i * angle_step
|
||||
rad = np.radians(angle)
|
||||
|
||||
# 光瓣位置
|
||||
x = int(center[0] + 60 * np.cos(rad))
|
||||
y = int(center[1] + 60 * np.sin(rad))
|
||||
|
||||
# 绘制光瓣(椭圆)
|
||||
cv2.ellipse(img, (x, y), (20, 30), angle, 0, 360,
|
||||
(255, 255, 200), -1)
|
||||
|
||||
# 添加光晕
|
||||
cv2.circle(img, (x, y), 25, (255, 255, 150), -1)
|
||||
cv2.circle(img, (x, y), 30, (200, 200, 100), 2)
|
||||
|
||||
# 添加噪声
|
||||
noise = np.random.randint(0, 30, img.shape, dtype=np.uint8)
|
||||
img = cv2.add(img, noise)
|
||||
|
||||
return img
|
||||
|
||||
# 生成数据集
|
||||
output_dir = '../data/synthetic'
|
||||
for class_id in range(10):
|
||||
class_dir = os.path.join(output_dir, f'class_{class_id}')
|
||||
os.makedirs(class_dir, exist_ok=True)
|
||||
|
||||
for i in range(100):
|
||||
img = generate_light_image(class_id)
|
||||
cv2.imwrite(os.path.join(class_dir, f'img_{i:03d}.jpg'), img)
|
||||
|
||||
print(f'生成 class_{class_id}: 100 张')
|
||||
|
||||
print('合成数据集生成完成!')
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 数据标注
|
||||
|
||||
### 7.1 标注工具
|
||||
|
||||
如果需要手动标注:
|
||||
- **LabelImg**: 图像分类标注
|
||||
- **CVAT**: 在线标注工具
|
||||
- **Roboflow**: 数据集管理平台
|
||||
|
||||
### 7.2 标注流程
|
||||
|
||||
1. 采集原始图像
|
||||
2. 使用标注工具分类
|
||||
3. 导出为目录结构
|
||||
4. 检查标注质量
|
||||
5. 分割训练/验证集
|
||||
|
||||
---
|
||||
|
||||
## 常见问题
|
||||
|
||||
### Q1: 数据集太小怎么办?
|
||||
A:
|
||||
- 使用数据增强
|
||||
- 使用预训练模型(迁移学习)
|
||||
- 采集更多数据
|
||||
|
||||
### Q2: 类别不平衡怎么办?
|
||||
A:
|
||||
- 对少数类进行过采样
|
||||
- 使用加权损失函数
|
||||
- 采集更多少数类数据
|
||||
|
||||
### Q3: 图像质量不一致?
|
||||
A:
|
||||
- 统一采集条件
|
||||
- 使用图像预处理
|
||||
- 剔除低质量图像
|
||||
|
||||
### Q4: 如何验证数据集质量?
|
||||
A:
|
||||
- 运行 `check_dataset.py`
|
||||
- 可视化部分样本
|
||||
- 训练简单模型测试
|
||||
|
||||
---
|
||||
|
||||
## 下一步
|
||||
|
||||
数据集准备完成后:
|
||||
|
||||
1. ✅ 检查数据集结构和数量
|
||||
2. ✅ 运行质量检查脚本
|
||||
3. ➡️ 开始模型训练 (参考 `docs/快速开始.md`)
|
||||
|
||||
```bash
|
||||
cd pc_training
|
||||
python train.py --data_dir ../data/processed --pretrained --epochs 50
|
||||
```
|
||||
Reference in new issue
Block a user