Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
YangYuqi317 authored and YangYuqi317 committed Jan 14, 2023
1 parent 95e8469 commit d37e010
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 11 deletions.
6 changes: 5 additions & 1 deletion README.md
Expand Up @@ -151,4 +151,8 @@ We used fgvclib to replicate the state-of-the-art model, and the following table
Thanks for your attention! If you have any suggestion or question, you can leave a message here or contact us directly:

- changdongliang@bupt.edu.cn
- mazhanyu@bupt.edu.cns
- mazhanyu@bupt.edu.cn

## Others
In addition to fgvclib, we have developed a practical mini program on fine-grained visual classification, which can be accessed by scanning the two-dimensional code below.

59 changes: 56 additions & 3 deletions docs/en/1_exist_data_model.md
Expand Up @@ -2,11 +2,64 @@

To evaluate a model’s accuracy, one usually tests the model on some standard datasets. FGVCLib supports the public datasets including CUB_200_2011. This section will show how to test existing models on supported datasets.

## Prepare datasets
The basic steps are as below:
1.Prepare the dataset
2.Prepare a config
3.Train, test models on the dataset

The public datasets CUB_200_2011 is available from offical websites or mirrors. It is recommended to download and extract the dataset somewhere outside the project directory and symlink the dataset root to `/data/dataset` as below.
## The existing models

If your folder structure is different, you may need to change the corresponding paths in config files `./configs`.
We provide a variety of existing methods, they are `baseline_resnet50`, `MCL`, `PMG`, `PMG_v2`, `API-Net`, `CAL`, `PIM`, `TransFG`.

In the future we will continue to reproduce new methods and add them into FGVCLib.

## Prepare the dataset
We provide the CUB-200-2011, and we split the dataset into train and test folder.

e.g., CUB-200-2011 dataset
```
-/birds/train
└─── 001.Black_footed_Albatross
└─── Black_Footed_Albatross_0001_796111.jpg
└─── ...
└─── 002.Laysan_Albatross
└─── 003.Sooty_Albatross
└─── ...
-/birds/test
└─── ...
```

If you have prepared the dataset, you can skip the step1.

**step1**: open the "/fgvclib/datasets/cub.py", and modify the `class CUB_200_2011: __init__ : download:bool=False` to `class CUB_200_2011: __init__ : download:bool=True`
```note
The parameter 'download' controls whether the dataset is downloaded. Directly downloading CUB dataset by setting download=True. Default is False.
```

**step2**: open the "/configs/xxx/xxx.yml", and replace the `DATASET-ROOT` with your own path.

## Train

**step1**: open the "/configs/xxx/xxx.yml", and replace the `WEIGHT-SAVE_DIR` with your own path.
**step2**: open the "/configs/xxx/xxx.yml", and check the configs about the model. You can change the configs by yourself.
**stpe3**: execute main program to train.

```python
python main.py --config configs/resnet/resnet50.yml
```
There are several arguments to control the program.
- '--config': the path of configuration file.
- '--task': train or predict. The default is **train**.
- '--device': two choices are cuda and cpu. The default is **cuda**.
- '--world-size': the number of distributed processes. The default is 4.
- '--dist-url': url used to set up distributed training. The default is 'env://'.

If you want to run it on cpu, you should execute the following:
```python
python main.py --config configs/resnet/resnet50.yml --device cpu
```

## Test
```python
python main.py --config configs/resnet/resnet50.yml --task predict
```
95 changes: 94 additions & 1 deletion docs/en/2_exist_data_new_model.md
@@ -1 +1,94 @@
# 2: Train with the new models and satndard datastes
# 2: Train with the new models and satndard datastes
We disassemble the model into backbone, encoder, classifier and other basic structures, and then combine them to build the complete method. In the FGVCLib, we have provide the basic structures and reproduce the state-of-art model. We are committed to providing you with a custom structure, using the disassembled modules to reassemble a new model.

The basic steps are as below:
1.Prepare the dataset
2.Prepare you own customized model
3.Prepare a config
4.Train, test and predict models on the dataset

## Prepare the dataset
You need to change the corresponding dataset paths in the config files. And you need to split the dataset into train and test folder.

e.g., CUB-200-2011 dataset
```
-/birds/train
└─── 001.Black_footed_Albatross
└─── Black_Footed_Albatross_0001_796111.jpg
└─── ...
└─── 002.Laysan_Albatross
└─── 003.Sooty_Albatross
└─── ...
-/birds/test
└─── ...
```

## Prepare your own customized model
The second step is to use your own module or training setting. Assume that we want to add a new encoder `xxx`.

### 1.Define a new encoder(e.g. xxx)
Firstly we create a new file `fgvclib/model/encoders/xxx.py`.

```python
import torch
import torch.nn as nn
import torch.nn.functional as F

from fgvclib.models.encoders import encoder

class xxx(nn.Module):
def __init__(self)
pass

def forward(self,inputs):
pass

def xxx(cfg:dict):
pass
```

### 2.Import the module
You can import the encoder in other parts
```python
from .xxx import xxx
```

## Prepare a config
The third step is to prepare a config for your own training setting. In "configs/xxx.yml", you should prepare a complete config file.
Refer to the existing config file, you can create a new config file.
Take the new encoder xxx as an example:
```python
MODEL:
NAME: "ResNet50"
CLASS_NUM: 200
CRITERIONS:
- name: "cross_entropy_loss"
args: []
w: 1.0
BACKBONE:
NAME: "resnet50"
ARGS:
- pretrained: True
- del_keys: []
ENCODER:
NAME: "xxx"
NECKS:
NAME: ~
HEADS:
NAME: "classifier_1fc"
ARGS:
- in_dim:
- 2048
```

## Train a new model
To train a model with the new config, you can simply run
```python
python main.py --configs/xxx.yml --task train
```

## Test a new model
To train a model with the new config, you can simply run
```python
python main.py --configs/xxx.yml --task predict
```
64 changes: 59 additions & 5 deletions docs/zh_CN/1_exist_data_model.md
@@ -1,10 +1,64 @@
## 1: Train with the existing models and satndard datastes
# 1:在标准数据集上训练已有模型

To evaluate a model’s accuracy, one usually tests the model on some standard datasets. FGVCLib supports the public datasets including CUB_200_2011. This section will show how to test existing models on supported datasets.
为了评估模型的准确性,人们通常在一些标准数据集上测试模型。FGVCLib支持包括CUB_200_2011在内的公共数据集。本节将展示如何在受支持的数据集上测试现有模型。

## Prepare datasets
基本步骤如下:
1.准备标准数据集
2.准备配置文件
3.在标准数据集上对模型进行训练、测试和预测

The public datasets CUB_200_2011 is available from offical websites or mirrors. It is recommended to download and extract the dataset somewhere outside the project directory and symlink the dataset root to `/data/dataset` as below.
## 已有模型
我们提供了多种已有的方法,它们分别是:`baseline_resnet50`, `MCL`, `PMG`, `PMG_v2`, `API-Net`, `CAL`, `PIM`, `TransFG`

If your folder structure is different, you may need to change the corresponding paths in config files `./configs`.
今后我们将会继续复现更多新的方法并将它们更新至FGVCLib中。

## 准备标准数据集
我们提供了CUB-200-2011,我们将数据集分为训练文件夹和测试文件夹。

例如,CUB-200-2011数据集:
```
-/birds/train
└─── 001.Black_footed_Albatross
└─── Black_Footed_Albatross_0001_796111.jpg
└─── ...
└─── 002.Laysan_Albatross
└─── 003.Sooty_Albatross
└─── ...
-/birds/test
└─── ...
```

如果你已经准备好数据集了,你可以跳过下面的第一步。

**第一步**:打开"/fgvclib/datasets/cub.py",将`class CUB_200_2011: __init__ : download:bool=False`修改为`class CUB_200_2011: __init__ : download:bool=True`
```note
参数“download”控制是否下载数据集。通过设置download=True直接下载CUB数据集。默认为False。
```

**第二步**:打开"/configs/xxx/xxx.yml",将`DATASET-ROOT`替换为你自己的路径。

## 训练模型
**第一步**:打开"/configs/xxx/xxx.yml",将`WEIGHT-SAVE_DIR`替换为你自己的路径。
**第二步**:打开"/configs/xxx/xxx.yml",检查模型的配置,你可以自己修改这些配置。
**第三步**:执行主程序main.py进行训练

```python
python main.py --config configs/resnet/resnet50.yml
```

这里存在几类参数控制着程序的运行配置:
- '--config':配置文件路径。
- '--task': 默认为**train**
- '--device':两种选择是cuda和cpu。默认为**cuda**
- '--world-size':分布式进程的数量。默认值是4。
- '--dist-url':Url用于设置分布式培训。默认值是'env://'。

如果你想在cpu上运行它,你应该执行下面的:
```python
python main.py --config configs/resnet/resnet50.yml --device cpu
```

## 测试模型
```python
python main.py --config configs/resnet/resnet50.yml --task predict
```
96 changes: 95 additions & 1 deletion docs/zh_CN/2_exist_data_new_model.md
@@ -1 +1,95 @@
## 2: Train with the new models and satndard datastes
## 2: 在标准数据集上训练自定义模型

我们将模型分解为骨干、编码器、分类器等基本结构,然后将它们组合起来构建完整的方法。在FGVCLib中,我们提供了基本结构并复现了最先进的模型。我们致力于为您提供自定义结构,使用分解后的模块重新组装成新的模型。

主要的步骤如下:
1.准备数据集
2.准备自定义模型
3.准备配置文件
4.在标准数据集上进行训练、测试和推理

## 准备数据集
你需要在配置文件中修改对应的数据集路径。你需要将数据集分成训练集和测试集两个文件夹。

例如,CUB-200-2011数据集:
```
-/birds/train
└─── 001.Black_footed_Albatross
└─── Black_Footed_Albatross_0001_796111.jpg
└─── ...
└─── 002.Laysan_Albatross
└─── 003.Sooty_Albatross
└─── ...
-/birds/test
└─── ...
```

## 准备自定义模型
第二步时使用已有的模块和新的模块构建自定义模型,假设我们想添加一个新的编码器`xxx`

### 1.定义一个新的编码器(以xxx为例)
首先建立新文件`fgvclib/model/encoders/xxx.py`

```python
import torch
import torch.nn as nn
import torch.nn.functional as F

from fgvclib.models.encoders import encoder

class xxx(nn.Module):
def __init__(self)
pass

def forward(self,inputs):
pass

def xxx(cfg:dict):
pass
```

### 2.导入模块
你可以在其他需要的地方导入该编码器
```python
from .xxx import xxx
```

## 准备配置文件
第三步是为你自己的训练设置准备一个配置文件。在"configs/xxx.yml"中,你可以根据已有的配置文件,新建立配置。

以新编码器xxx为例:
```python
MODEL:
NAME: "ResNet50"
CLASS_NUM: 200
CRITERIONS:
- name: "cross_entropy_loss"
args: []
w: 1.0
BACKBONE:
NAME: "resnet50"
ARGS:
- pretrained: True
- del_keys: []
ENCODER:
NAME: "xxx"
NECKS:
NAME: ~
HEADS:
NAME: "classifier_1fc"
ARGS:
- in_dim:
- 2048
```

## 训练新模型
为了能够使用新增配置来训练模型,你可以运行如下命令:
```python
python main.py --configs/xxx.yml --task train
```

## 测试新模型
为了能够测试训练好的模型,你可以运行如下命令:
```python
python main.py --configs/xxx.yml --task predict
```

0 comments on commit d37e010

Please sign in to comment.