---
title: image_classification
canonical_url: "https://www.modelscope.cn/models/tiansz/image_classification"
md_url: "https://www.modelscope.cn/models/tiansz/image_classification.md"
repository: tiansz/image_classification
chinese_name: "图像分类"
last_updated: 2024-07-27
pipeline_tag: image-classification
tasks:
  - image-classification
parameters: 172.2M
tensor_type:
  - F32
library_name:
  - safetensors
  - pytorch
frameworks:
  - Pytorch
downloads: 110
stars: 1
---

# image_classification

> image_classification - tiansz 在 ModelScope 开源的模型。图像分类 1 环境准备 1.1 windows系统 1.1.1 下载项目文件 在 windows 系统中需要安装 git 、git-lfs 。其中 git 是用来克隆较小容量的文件夹，而 git-lfs 则是用来克隆大文件夹

- **Repository**: tiansz/image_classification
- **Tasks**: image-classification
- **Parameters**: 172.2M
- **Downloads**: 110
- **Stars**: 1
- **Last updated**: 2024-07-27

Source: https://www.modelscope.cn/models/tiansz/image_classification

---

# 图像分类
## 1 环境准备
### 1.1 windows系统
#### 1.1.1 下载项目文件
在 windows 系统中需要安装 [git](https://mirrors.tuna.tsinghua.edu.cn/github-release/git-for-windows/git/) 、[git-lfs](https://git-lfs.com/) 。其中 git 是用来克隆较小容量的文件夹，而 git-lfs 则是用来克隆大文件夹

当你安装完以上软件后，你就可以运行如下命令来克隆本项目的代码和模型了：
```bash
git clone https://www.modelscope.cn/tiansz/image_classification.git
```
#### 1.1.2 python 环境
在克隆完仓库后，还需要安装 [miniconda](https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/) 、 [vs code](hhttps://code.visualstudio.com/) 和 [PyCharm](https://www.jetbrains.com.cn/pycharm/download/?section=windows)。其中 miniconda 包含了 conda 包管理工具和 python 编程语言。安装完以上软件后，python 编程环境就搭建完毕了。同时为了加速依赖包的下载，你可以替换 pip 源：
```bash
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
```
#### 1.1.3 pytorch 环境
除了准备 python 编程环境之外，我们还需要构建深度学习环境，首先需要安装[显卡驱动](https://www.nvidia.cn/geforce/drivers/)。安装好显卡驱动后，我们还需要安装 cuda 和 [pytorch](https://pytorch.org/get-started/locally/)，在安装 cuda 前，你需要查看 pytorch 当前支持哪些版本的 cuda，请先安装对应版本的 cuda，之后我推荐你使用 pip 安装 pytorch，这样下载速度会快些，因为我们在前面已经替换了 pip 源。

除了最主要的 pytorch 外，我们还需要安装其他依赖，为了方便起见，我们可以从 `requirements.txt` 中安装：
```bash
pip install -r requirements.txt
```

## 1.2 wsl 和 ubuntu 系统

wsl 是 windows 的 linux 子系统，你可以在 windows 系统的终端运行如下命令来安装 wsl：
```powershell
wsl --install
```

在安装好 wsl 后，我们可以设置 root 用户权限：（在 windows 系统的终端运行）
```powershell
ubuntu.exe config --default-user root
```

打开 wsl 后，你同样需要安装 git、git-lfs、miniconda、显卡驱动、cuda 和 pytorch

#### 1.2.1 下载项目文件
在 linux 系统中，你只需要使用命令安装软件即可，安装 git 的命令如下：
```bash
sudo apt-get update
```
```bash
sudo apt-get upgrade
```
```bash
sudo apt-get install git
```
安装 git-lfs 的命令如下：
```bash
sudo apt-get install git-lfs
```
当你安装完以上软件后，你就可以运行如下命令来克隆本项目的代码和模型了：
```bash
git clone https://www.modelscope.cn/tiansz/image_classification.git
```
#### 1.2.2 python 环境
安装 miniconda 的示例命令如下：（你可以在该[镜像网站](https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/)下载最新的miniconda）
```bash
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/miniconda/Miniconda3-py311_23.11.0-1-Linux-x86_64.sh
```
```bash
bash Miniconda3-py311_23.11.0-1-Linux-x86_64.sh
```
安装完成后还需要启动环境变量：`source ~/.bashrc`

#### 1.2.3 pytorch 环境
在安装 cuda 前，还需要安装 gcc：
```bash
sudo apt install gcc
```


若你的 windows 电脑本机已经安装了显卡驱动，那么 wsl 就不再需要安装显卡驱动了，你可以在 wsl 的终端运行 `nvidia-smi` 检查是否安装成功。若你使用的是物理机的 ubuntu 系统，则可以在 [N卡官网](https://www.nvidia.cn/geforce/drivers/) 下载并安装显卡驱动。

同样的，cuda 和 pytorch 的安装和 windows 系统的类似

例如安装 [cuda 12.1](https://developer.nvidia.com/cuda-12-1-0-download-archive?target_os=Linux&target_arch=x86_64)，请根据系统情况安装对应的版本，推荐下载 run 后缀的文件进行安装。

在安装完成后，你需要将 cuda 写入环境变量，可使用 vim 编辑环境变量：
```bash
vim ~/.bashrc
```
并在文件的末尾处添加如下内容：
```bash
export CUDA_HOME=/usr/local/cuda
export PATH=$PATH:$CUDA_HOME/bin
export LD_LIBRARY_PATH=/usr/local/cuda-12.1/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
```
编辑完成后退出即可，最后你还要激活修改后的环境变量：
```bash
source ~/.bashrc
```
同时你可以使用 `nvcc -V` 来验证 cuda 是否激活成功。若失败你还可以使用 [switch-cuda](https://github.com/phohenecker/switch-cuda) 来配置 cuda 环境

除了最主要的 pytorch 外，我们还需要安装其他依赖，为了方便起见，我们可以从 `requirements.txt` 中安装：
```bash
pip install -r requirements.txt
```
## 2 准备数据集
类似 `dataset/猫狗分类` 路径的数据集一样，你只需将每一类图片放在一个文件夹就行，例如猫的照片一律放置在“猫”文件夹下，狗的照片都放在“狗”的文件夹下。在实际任务中，我建议大家还要创建一个“其他”的文件夹，用于放置既不是猫，也不是狗的照片，这样有助于模型将除了猫和狗之外的图片进行分类，否则我们只能通过置信度来判断其他图片是否应该被分到猫或狗类别

## 3 微调模型
我们需要指定上面创建的数据集路径，也就是 `dataset_path`；同时还要设置模型的训练参数，其中 `batch_size` 指的是模型每次训练的样本数量；而 `num_train_epochs` 则是模型对于我们创建的数据集学习多少遍；最后就是 `finetune_model_path`，该参数表示我们微调好后的图像分类模型路径
```bash
python finetune/finetune_model.py \
--dataset_path dataset/猫狗分类 \
--batch_size 32 \
--num_train_epochs 25 \
--finetune_model_path model/finetuned_model
```


## 4 模型推理
在得到图像分类模型后，我们就可以加载该模型，并进行图像分类任务了：
```bash
python inference/load_finetune_model.py
```

webui 网页展示：
```bash
python inference/webui.py
```

## 5 模型部署

```bash
python server/server.py
```

```bash
python server/client.py
```


## 6 图片朝向检测
尽量确保每个物品或物种出现的频次相近，同时注意是将同一照片翻转后放置在不同朝向的类别中，这样模型才能理解并学习图像的朝向。


批量将图片旋转：
```bash
python process/batch_image_rotation.py \
--source_folder dataset/Image_orientation_detection/does_not_rotate \
--destination_folder dataset/Image_orientation_detection/rotate \
--rotation_angle 90
```

然后微调模型
```bash
python finetune/finetune_model.py \
--dataset_path dataset/Image_orientation_detection \
--batch_size 32 \
--num_train_epochs 25 \
--finetune_model_path model/finetuned_model
```

最后可以在 `inference\load_finetune_model.py` 中进行模型推理，检测模型的效果
