# FSOD-Code **Repository Path**: gongshuicheng/fsod-code ## Basic Information - **Project Name**: FSOD-Code - **Description**: To detect object on few-shot dataset - **Primary Language**: Unknown - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 1 - **Created**: 2022-10-15 - **Last Updated**: 2022-10-15 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 小样本目标检测 (FsDet) FsDet包含ICML 2020论文的官方小样本目标检测实现 [简单小样本目标检测](https://arxiv.org/abs/2003.06957): ![TFA Figure](https://user-images.githubusercontent.com/7898443/76520006-698cc200-6438-11ea-864f-fd30b3d50cea.png) 除了以前的论文中所使用的基准外,我们在PASCAL VOC、COCO和LVIS这三个数据集上引入了新的基准。我们对多组小样本训练示例进行多次实验,并将基类和新类的评估结果写在报告中。这些在 [Data Preparation](#data-preparation)中有更详细的描述。 我们还为本文中的两阶段微调方法( TFA )提供了基准结果和预训练模型。在TFA中,我们首先在数据丰富的基类上训练整个目标检测器,然后只在一个小的平衡训练集中微调检测器的最后一层。请参阅 [Models](#models) 了解我们提供的模型,并参阅 [Getting Started](#getting-started) 了解有关训练和评估的说明。 FsDet具有良好的模块化,因此可以方便地添加自己的数据集和模型。这个存储库的目标是为可用于未来研究的小样本目标检测提供一个通用框架。 ```angular2html @article{wang2020few, title={Frustratingly Simple Few-Shot Object Detection}, author={Wang, Xin and Huang, Thomas E. and Darrell, Trevor and Gonzalez, Joseph E and Yu, Fisher} booktitle = {International Conference on Machine Learning (ICML)}, month = {July}, year = {2020} } ``` ## Updates - [Few-Shot Object Detection (FsDet)](#few-shot-object-detection-fsdet) - [Updates](#updates) - [Table of Contents](#table-of-contents) - [Installation](#installation) - [Code Structure](#code-structure) - [Data Preparation](#data-preparation) - [Models](#models) - [Getting Started](#getting-started) - [Inference Demo with Pre-trained Models](#inference-demo-with-pre-trained-models) - [Training & Evaluation in Command Line](#training--evaluation-in-command-line) - [Multiple Runs](#multiple-runs) ## Installation 环境要求: * Linux 环境 Python 版本 >= 3.7 * [PyTorch](https://pytorch.org/get-started/locally/) 版本 >= 1.5 * 安装匹配 PyTorch 版本的 [torchvision](https://github.com/pytorch/vision/) 库 * GCC 版本>= 4.9 **构建 FsDet** * 创建一个虚拟环境 ```angular2html python3 -m venv fsdet source fsdet/bin/activate ``` 也可以使用 `conda`来创建一个新的环境。 ```angular2html conda create --name fsdet conda activate fsdet ``` * 安装PyTorch。您可以根据您的机器选择PyTorch和torchvision版本。只需确保PyTorch版本与预先构建的Detectron2版本匹配即可。Pytorch V1.5.0的示例: ```angular2html pip install torch==1.5.0 torchvision==0.6.0 ``` * 安装 Detectron2 ```angular2html python3 -m pip install -e ./detectron2/ ``` * 安装其他必要的扩展包. ```angular2html python3 -m pip install -r requirements.txt ``` 也可通过运行命令行安装(会自动安装Detectron2和其他requirements) ``` bash setup.sh ``` ## Code Structure - **configs**: 配置文件 - **datasets**: 数据集文件 [Data Preparation](#data-preparation) - **fsdet** - **checkpoint**: 检查点代码 - **config**: 配置代码和默认配置 - **engine**: 包含训练和评估的循环和挂钩 - **layers**: 模型中神经网络不同层的实现 - **modeling**: 模型的代码,包括Backbone,RPN等 - **tools** - **train_net.py**: 训练脚本 - **test_net.py**: 测试脚本 - **ckpt_surgery.py**: 权重合并脚本 - **run_experiments.py**: 进行c-way k-shot实验 - **aggregate_seeds.py**: 集成多Seeds集的实验结果 ## Data Preparation 我们在三个公开数据集及一个自定义数据集(金相数据)上评估我们的模型: - [PASCAL VOC](http://host.robots.ox.ac.uk/pascal/VOC/): 我们使用PASCAL VOC 2007 + 2012的train / val集进行训练,使用PASCAL VOC 2007的test集进行评估。我们将20个对象类随机拆分为15个基类和5个新类,并考虑3个随机拆分。这些拆分可以在 [fsdet/data/builtin_meta.py](fsdet/data/builtin_meta.py)中找到。 - [COCO](http://cocodataset.org/): 我们使用COCO 2014并从val集中提取5千张图像用于评估,其余的用于训练。我们使用与PASCAL VOC相同的20个对象类作为新类,其余的作为基类。 - [LVIS](https://www.lvisdataset.org/): 我们将频繁类和常见类作为基类,稀有类作为新类。 - 工业焊缝缺陷金相数据集(PSCAL VOC文件结构):由于只有两类缺陷,种类较少,且原始数据很少,因此此处采用泊松融合进行数据增强,后直接作为基类样本进行训练。 若由于工业缺陷样本太少,正常样本较多,可考虑进行泊松融合合成新的样本,从而扩充缺陷样本的数量,详情见 [PossionFusion/README.md](PossionFusion/README.md) . 对于Fsdet,训练前需要进行数据集注册操作: ​ a: 若数据集为PASCAL_VOC,COCO,LVIS则无需额外注册,代码中有相关注册代码,运行即可。 ​ b: 若数据集为自己定义的普通数据集,则在训练前需要进行数据注册,请参阅[CUSTOM.md](docs/CUSTOM.md)说明。 ## Models 我们在 [MODEL_ZOO.md](docs/MODEL_ZOO.md)中提供了一组可供下载的基准结果和预训练模型。 ## Getting Started ### Inference Demo with Pre-trained Models 1. 从 [model zoo](fsdet/model_zoo/model_zoo.py)中选择一个模型及其配置文件,例如 `COCO-detection/faster_rcnn_R_101_FPN_ft_all_1shot.yaml`。 2. 我们提供 `demo.py`文件 ,它能够运行内置的标准模型。用它运行: ``` python3 -m demo.demo \ --config-file configs/COCO-detection/faster_rcnn_R_101_FPN_ft_all_1shot.yaml \ --input input1.jpg input2.jpg \ --output output \ --opts MODEL.WEIGHTS fsdet://coco/tfa_cos_1shot/model_final.pth\ --confidence-threshold 0.5 或着直接再demo.sh文件中进行参数修改,然后直接运行命令:bash demo.sh 即可 注意:要保证config-file与MODEL.WEIGHT是同属于一个模型 ``` 配置是为了训练,因此需要将 `MODEL.WEIGHTS` 指定给model zoo的模型进行评估。该命令将对图片进行推理将结果保存在output文件中。 有关命令行参数的详细信息,请参阅 `demo.py -h` 或查看其源代码以了解其作用。一些常见的论点是: * 运行 __on your webcam__, 要用 `--webcam`替代 `--input files` . * 运行 __on a video__, 要用 `--video-input video.mp4`替代`--input files` . * 运行 __on cpu__, 在 `--opts` 后添加 `MODEL.DEVICE cpu` (此处device默认为npu) * 若要将输出保存到目录(图像类)或文件(webcam或视频类),请使用 `--output`。 ### Training & Evaluation in Command Line 运行以下代码进行训练 ```angular2html python3 -m tools.train_net \ --config-file configs/PascalVOC-detection/split1/faster_rcnn_R_101_FPN_base1.yaml 或者在train.sh中进行参数配置,然后直接运行命令:bash train.sh ``` 运行以下代码来评估训练好的模型 ```angular2html python3 -m tools.test_net \ --config-file configs/PascalVOC-detection/split1/faster_rcnn_R_101_FPN_ft_all1_1shot.yaml \ --eval-only 或者在test.sh中进行参数配置,然后直接运行命令:bash test.sh ``` 更多关于TFA训练流程的详细说明,见 [TRAIN_INST.md](docs/TRAIN_INST.md)。 ### Multiple Runs 为了便于多次运行的训练和评估,我们在 `tools/`.中提供了几个有用的脚本。 您可以使用 `tools/run_experiments.py` 进行培训和评估。例如,运行以下代码在所有样本上对PascalVOC的第一次分割的30个种子进行实验: ```angular2html python3 -m tools.run_experiments \ --shots 1 2 3 5 10 --seeds 0 30 --split 1 或者在run_experiments.sh中进行参数配置,然后直接运行命令:bash run_experiments.sh ``` 在训练和评估之后,您可以使用 `tools/aggregate_seeds.py` 将所有seed的结果进行汇集,以获得一组数据。运行以下代码来聚合上述命令的3 shot结果: ```angular2html python3 -m tools.aggregate_seeds --shots 3 --seeds 30 --split 1 \ --print --plot 或者在run_aggregate_seeds.sh中进行参数配置,然后直接运行命令行:bash run_aggregate_seeds.sh ``` ## Reference: code:[ucbdrive/few-shot-object-detection: Implementations of few-shot object detection benchmarks (github.com)](https://github.com/ucbdrive/few-shot-object-detection) paper:[Frustratingly Simple Few-Shot Object Detection](https://arxiv.org/abs/2003.06957)