📝 添加模型文档
Browse files
README.md
CHANGED
|
@@ -1,26 +1,85 @@
|
|
| 1 |
-
|
| 2 |
---
|
| 3 |
license: mit
|
| 4 |
tags:
|
| 5 |
- pytorch
|
| 6 |
- image-classification
|
| 7 |
- cats-dogs
|
|
|
|
| 8 |
---
|
| 9 |
|
| 10 |
-
#
|
| 11 |
|
| 12 |
这是一个简单的猫狗分类模型,使用ResNet18架构训练。
|
| 13 |
|
| 14 |
-
## 模型
|
| 15 |
-
- 架构:
|
| 16 |
-
-
|
| 17 |
-
-
|
|
|
|
|
|
|
| 18 |
|
| 19 |
## 使用方法
|
|
|
|
|
|
|
| 20 |
```python
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
```
|
| 24 |
|
| 25 |
## 训练数据
|
| 26 |
-
使用标准的猫狗图像数据集进行训练
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
license: mit
|
| 3 |
tags:
|
| 4 |
- pytorch
|
| 5 |
- image-classification
|
| 6 |
- cats-dogs
|
| 7 |
+
- resnet18
|
| 8 |
---
|
| 9 |
|
| 10 |
+
# Cat Dog Classifier
|
| 11 |
|
| 12 |
这是一个简单的猫狗分类模型,使用ResNet18架构训练。
|
| 13 |
|
| 14 |
+
## 模型信息
|
| 15 |
+
- **架构**: ResNet18 (预训练)
|
| 16 |
+
- **输入**: 224x224 RGB图像
|
| 17 |
+
- **输出**: 猫/狗二分类
|
| 18 |
+
- **训练框架**: PyTorch
|
| 19 |
+
- **训练环境**: GitHub Actions
|
| 20 |
|
| 21 |
## 使用方法
|
| 22 |
+
|
| 23 |
+
### 方法1:直接加载PyTorch模型
|
| 24 |
```python
|
| 25 |
+
import torch
|
| 26 |
+
from torchvision import transforms
|
| 27 |
+
from PIL import Image
|
| 28 |
+
|
| 29 |
+
# 加载模型
|
| 30 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 31 |
+
model = torch.load("best_model.pth", map_location=device)
|
| 32 |
+
model.eval()
|
| 33 |
+
|
| 34 |
+
# 预测
|
| 35 |
+
transform = transforms.Compose([
|
| 36 |
+
transforms.Resize((224, 224)),
|
| 37 |
+
transforms.ToTensor(),
|
| 38 |
+
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
|
| 39 |
+
])
|
| 40 |
+
|
| 41 |
+
image = Image.open("your_image.jpg")
|
| 42 |
+
input_tensor = transform(image).unsqueeze(0).to(device)
|
| 43 |
+
|
| 44 |
+
with torch.no_grad():
|
| 45 |
+
output = model(input_tensor)
|
| 46 |
+
probabilities = torch.nn.functional.softmax(output, dim=1)
|
| 47 |
+
predicted_class = "cat" if probabilities[0][0] > 0.5 else "dog"
|
| 48 |
+
confidence = max(probabilities[0][0].item(), probabilities[0][1].item()) * 100
|
| 49 |
+
|
| 50 |
+
print(f"预测结果: {predicted_class}")
|
| 51 |
+
print(f"置信度: {confidence:.2f}%")
|
| 52 |
+
```
|
| 53 |
+
|
| 54 |
+
### 方法2:使用Hugging Face Hub
|
| 55 |
+
```python
|
| 56 |
+
from huggingface_hub import hf_hub_download
|
| 57 |
+
import torch
|
| 58 |
+
|
| 59 |
+
# 下载模型
|
| 60 |
+
model_path = hf_hub_download(repo_id="wzx952/cat-dog-classifier", filename="best_model.pth")
|
| 61 |
+
model = torch.load(model_path)
|
| 62 |
+
model.eval()
|
| 63 |
```
|
| 64 |
|
| 65 |
## 训练数据
|
| 66 |
+
- 使用标准的猫狗图像数据集进行训练
|
| 67 |
+
- 训练集:10张猫图片 + 10张狗图片(演示用)
|
| 68 |
+
- 验证集:5张猫图片 + 5张狗图片(演示用)
|
| 69 |
+
|
| 70 |
+
## 训练参数
|
| 71 |
+
- **优化器**: Adam (lr=0.001)
|
| 72 |
+
- **损失函数**: 交叉熵损失
|
| 73 |
+
- **训练轮数**: 5
|
| 74 |
+
- **批次大小**: 32
|
| 75 |
+
|
| 76 |
+
## 项目信息
|
| 77 |
+
- **GitHub仓库**: https://github.com/Rhythmzd/ml-project-huggingface
|
| 78 |
+
- **CI/CD**: GitHub Actions自动训练和部署
|
| 79 |
+
- **Hugging Face**: 自动上传模型和文档
|
| 80 |
+
|
| 81 |
+
## 演示说明
|
| 82 |
+
⚠️ **注意**: 这是一个演示项目,使用随机生成的虚拟数据进行训练。实际使用时需要替换为真实的猫狗图像数据集。
|
| 83 |
+
|
| 84 |
+
## 许可证
|
| 85 |
+
MIT License
|