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|
+
- cnn
|
| 6 |
+
- image-classification
|
| 7 |
+
- pytorch
|
| 8 |
+
- computer-vision
|
| 9 |
+
- digit-recognition
|
| 10 |
+
license: mit
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# 郭靖 MNIST CNN Model
|
| 14 |
+
|
| 15 |
+
## 模型描述
|
| 16 |
+
|
| 17 |
+
这是一个基于CNN的MNIST手写数字识别模型,名为"郭靖"。模型使用PyTorch实现,并与Hugging Face Transformers生态系统完全兼容。
|
| 18 |
+
|
| 19 |
+
## 模型架构
|
| 20 |
+
|
| 21 |
+
- **卷积层**: 2个卷积块,每个块包含2个卷积层 + 批归一化 + ReLU + 最大池化 + Dropout
|
| 22 |
+
- **全连接层**: 2个全连接层(带批归一化和Dropout)
|
| 23 |
+
- **输出层**: 10个类别(数字0-9)
|
| 24 |
+
- **参数量**: 约168万可训练参数
|
| 25 |
+
|
| 26 |
+
## 训练详情
|
| 27 |
+
|
| 28 |
+
- **数据集**: MNIST手写数字数据集
|
| 29 |
+
- **优化器**: Adam
|
| 30 |
+
- **损失函数**: 交叉熵损失
|
| 31 |
+
- **学习率调度**: ReduceLROnPlateau
|
| 32 |
+
- **准确率**: 在测试集上达到 >98% 的准确率
|
| 33 |
+
|
| 34 |
+
## 使用方法
|
| 35 |
+
|
| 36 |
+
### 基本使用
|
| 37 |
+
```python
|
| 38 |
+
from transformers import AutoModelForImageClassification, AutoImageProcessor
|
| 39 |
+
from PIL import Image
|
| 40 |
+
import torch
|
| 41 |
+
|
| 42 |
+
# 加载模型和处理器
|
| 43 |
+
model = AutoModelForImageClassification.from_pretrained("Akimotorakiyu/GuoJing-model")
|
| 44 |
+
processor = AutoImageProcessor.from_pretrained("Akimotorakiyu/GuoJing-model")
|
| 45 |
+
|
| 46 |
+
# 加载并预处理图像
|
| 47 |
+
image = Image.open("path/to/your/image.png").convert("L") # 转换为灰度图
|
| 48 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 49 |
+
|
| 50 |
+
# 推理
|
| 51 |
+
with torch.no_grad():
|
| 52 |
+
outputs = model(**inputs)
|
| 53 |
+
predicted_class = outputs.logits.argmax(-1).item()
|
| 54 |
+
print(f"Predicted digit: {predicted_class}")
|
| 55 |
+
```
|
| 56 |
+
|
| 57 |
+
### 批量推理
|
| 58 |
+
```python
|
| 59 |
+
import torch
|
| 60 |
+
from torchvision import datasets, transforms
|
| 61 |
+
from transformers import AutoModelForImageClassification
|
| 62 |
+
|
| 63 |
+
# 加载模型
|
| 64 |
+
model = AutoModelForImageClassification.from_pretrained("Akimotorakiyu/GuoJing-model")
|
| 65 |
+
model.eval()
|
| 66 |
+
|
| 67 |
+
# 数据预处理
|
| 68 |
+
transform = transforms.Compose([
|
| 69 |
+
transforms.ToTensor(),
|
| 70 |
+
transforms.Normalize((0.1307,), (0.3081,))
|
| 71 |
+
])
|
| 72 |
+
|
| 73 |
+
# 加载测试数据
|
| 74 |
+
test_dataset = datasets.MNIST(root='./data', train=False, download=True, transform=transform)
|
| 75 |
+
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=32)
|
| 76 |
+
|
| 77 |
+
# 评估
|
| 78 |
+
correct = 0
|
| 79 |
+
total = 0
|
| 80 |
+
with torch.no_grad():
|
| 81 |
+
for images, labels in test_loader:
|
| 82 |
+
outputs = model(pixel_values=images)
|
| 83 |
+
_, predicted = torch.max(outputs.logits, 1)
|
| 84 |
+
total += labels.size(0)
|
| 85 |
+
correct += (predicted == labels).sum().item()
|
| 86 |
+
|
| 87 |
+
print(f'Test Accuracy: {100 * correct / total:.2f}%')
|
| 88 |
+
```
|
| 89 |
+
|
| 90 |
+
## 模型配置
|
| 91 |
+
|
| 92 |
+
```python
|
| 93 |
+
{
|
| 94 |
+
"conv_channels": [32, 64],
|
| 95 |
+
"conv_kernel_size": 3,
|
| 96 |
+
"conv_padding": 1,
|
| 97 |
+
"pool_kernel_size": 2,
|
| 98 |
+
"pool_stride": 2,
|
| 99 |
+
"conv_dropout": 0.25,
|
| 100 |
+
"fc_dropout": 0.5,
|
| 101 |
+
"hidden_size": 512,
|
| 102 |
+
"input_channels": 1,
|
| 103 |
+
"num_classes": 10,
|
| 104 |
+
"image_size": 28,
|
| 105 |
+
"normalize_mean": 0.1307,
|
| 106 |
+
"normalize_std": 0.3081
|
| 107 |
+
}
|
| 108 |
+
```
|
| 109 |
+
|
| 110 |
+
## 训练数据预处理
|
| 111 |
+
|
| 112 |
+
- `ToTensor()`: 将PIL图像转换为张量
|
| 113 |
+
- `Normalize((0.1307,), (0.3081,))`: MNIST标准化值
|
| 114 |
+
|
| 115 |
+
## 性能指标
|
| 116 |
+
|
| 117 |
+
- **训练准确率**: >99%
|
| 118 |
+
- **测试准确率**: >98%
|
| 119 |
+
- **推理速度**: <1ms per image (on GPU)
|
| 120 |
+
|
| 121 |
+
## 模型文件说明
|
| 122 |
+
|
| 123 |
+
- `pytorch_model.bin`: 训练好的模型权重
|
| 124 |
+
- `config.json`: 模型配置文件
|
| 125 |
+
- `preprocessor_config.json`: 图像预处理配置
|
| 126 |
+
|
| 127 |
+
## 技术特点
|
| 128 |
+
|
| 129 |
+
- ✅ Transformers生态系统兼容
|
| 130 |
+
- ✅ AutoClass支持
|
| 131 |
+
- ✅ 批量推理优化
|
| 132 |
+
- ✅ GPU/CPU双支持
|
| 133 |
+
- ✅ 内存效率优化
|
| 134 |
+
|
| 135 |
+
## 限制和注意事项
|
| 136 |
+
|
| 137 |
+
1. 模型专门针对MNIST数据集训练,主要用于手写数字识别
|
| 138 |
+
2. 输入图像应为28x28像素的灰度图像
|
| 139 |
+
3. 对于其他类型的图像识别任务,需要重新训练
|
| 140 |
+
|
| 141 |
+
## 许可证
|
| 142 |
+
|
| 143 |
+
MIT License
|
| 144 |
+
|
| 145 |
+
## 作者
|
| 146 |
+
|
| 147 |
+
Akimotorakiyu
|
| 148 |
+
|
| 149 |
+
---
|
| 150 |
+
|
| 151 |
+
**注意**: 这是一个名为"郭靖"的MNIST分类模型,以纪念这位武侠小说中的传奇人物,象征着模型的稳健和可靠。
|