# A Simple and Fast Implementation of Faster R-CNN
## 1. Introduction
**[Update:]** I've further simplified the code to pytorch 1.5, torchvision 0.6, and replace the customized ops roipool and nms with the one from torchvision. if you want the old version code, please checkout branch [v1.0](https://github.com/chenyuntc/simple-faster-rcnn-pytorch/tree/v1.0)
This project is a **Simplified** Faster R-CNN implementation based on [chainercv](https://github.com/chainer/chainercv) and other [projects](#acknowledgement) . I hope it can serve as an start code for those who want to know the detail of Faster R-CNN. It aims to:
- Simplify the code (*Simple is better than complex*)
- Make the code more straightforward (*Flat is better than nested*)
- Match the performance reported in [origin paper](https://arxiv.org/abs/1506.01497) (*Speed Counts and mAP Matters*)
And it has the following features:
- It can be run as pure Python code, no more build affair.
- It's a minimal implemention in around 2000 lines valid code with a lot of comment and instruction.(thanks to chainercv's excellent documentation)
- It achieves higher mAP than the origin implementation (0.712 VS 0.699)
- It achieve speed compariable with other implementation (6fps and 14fps for train and test in TITAN XP)
- It's memory-efficient (about 3GB for vgg16)
![img](imgs/faster-speed.jpg)
## 2. Performance
### 2.1 mAP
VGG16 train on `trainval` and test on `test` split.
**Note**: the training shows great randomness, you may need a bit of luck and more epoches of training to reach the highest mAP. However, it should be easy to surpass the lower bound.
| Implementation | mAP |
| :--------------------------------------: | :---------: |
| [origin paper](https://arxiv.org/abs/1506.01497) | 0.699 |
| train with caffe pretrained model | 0.700-0.712 |
| train with torchvision pretrained model | 0.685-0.701 |
| model converted from [chainercv](https://github.com/chainer/chainercv/tree/master/examples/faster_rcnn) (reported 0.706) | 0.7053 |
### 2.2 Speed
| Implementation | GPU | Inference | Trainining |
| :--------------------------------------: | :------: | :-------: | :--------: |
| [origin paper](https://arxiv.org/abs/1506.01497) | K40 | 5 fps | NA |
| This[1] | TITAN Xp | 14-15 fps | 6 fps |
| [pytorch-faster-rcnn](https://github.com/ruotianluo/pytorch-faster-rcnn) | TITAN Xp | 15-17fps | 6fps |
[1]: make sure you install cupy correctly and only one program run on the GPU. The training speed is sensitive to your gpu status. see [troubleshooting](troubleshooting) for more info. Morever it's slow in the start of the program -- it need time to warm up.
It could be faster by removing visualization, logging, averaging loss etc.
## 3. Install dependencies
Here is an example of create environ **from scratch** with `anaconda`
```sh
# create conda env
conda create --name simp python=3.7
conda activate simp
# install pytorch
conda install pytorch torchvision cudatoolkit=10.2 -c pytorch
# install other dependancy
pip install visdom scikit-image tqdm fire ipdb pprint matplotlib torchnet
# start visdom
nohup python -m visdom.server &
```
If you don't use anaconda, then:
- install PyTorch with GPU (code are GPU-only), refer to [official website](http://pytorch.org)
- install other dependencies: `pip install visdom scikit-image tqdm fire ipdb pprint matplotlib torchnet`
- start visdom for visualization
```Bash
nohup python -m visdom.server &
```
## 4. Demo
Download pretrained model from [Google Drive](https://drive.google.com/open?id=1cQ27LIn-Rig4-Uayzy_gH5-cW-NRGVzY) or [Baidu Netdisk( passwd: scxn)](https://pan.baidu.com/s/1o87RuXW)
See [demo.ipynb](https://github.com/chenyuntc/simple-faster-rcnn-pytorch/blob/master/demo.ipynb) for more detail.
## 5. Train
### 5.1 Prepare data
#### Pascal VOC2007
1. Download the training, validation, test data and VOCdevkit
```Bash
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtrainval_06-Nov-2007.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCtest_06-Nov-2007.tar
wget http://host.robots.ox.ac.uk/pascal/VOC/voc2007/VOCdevkit_08-Jun-2007.tar
```
2. Extract all of these tars into one directory named `VOCdevkit`
```Bash
tar xvf VOCtrainval_06-Nov-2007.tar
tar xvf VOCtest_06-Nov-2007.tar
tar xvf VOCdevkit_08-Jun-2007.tar
```
3. It should have this basic structure
```Bash
$VOCdevkit/ # development kit
$VOCdevkit/VOCcode/ # VOC utility code
$VOCdevkit/VOC2007 # image sets, annotations, etc.
# ... and several other directories ...
```
4. modify `voc_data_dir` cfg item in `utils/config.py`, or pass it to program using argument like `--voc-data-dir=/path/to/VOCdevkit/VOC2007/` .
### 5.2 [Optional]Prepare caffe-pretrained vgg16
If you want to use caffe-pretrain model as initial weight, you can run below to get vgg16 weights converted from caffe, which is the same as the origin paper use.
````Bash
python misc/convert_caffe_pretrain.py
````
This scripts would download pretrained model and converted it to the format compatible with torchvision. If you are in China and can not download the pretrain model, you may refer to [this issue](https://github.com/chenyuntc/simple-faster-rcnn-pytorch/issues/63)
Then you could specify where caffe-pretraind model `vgg16_caffe.pth` stored in `utils/config.py` by setting `caffe_pretrain_path`. The default path is ok.
If you want to use pretrained model from torchvision, you may skip this step.
**NOTE**, caffe pretrained model has shown slight better performance.
**NOTE**: caffe model require images in BGR 0-255, while torchvision model requires images in RGB and 0-1. See `data/dataset.py`for more detail.
### 5.3 begin training
```bash
python train.py train --env='fasterrcnn' --plot-every=100
```
you may refer to `utils/config.py` for more argument.
Some Key arguments:
- `--caffe-pretrain=False`: use pretrain model from caffe or torchvision (Default: torchvison)
- `--plot-every=n`: visualize prediction, loss etc every `n` batches.
- `--env`: visdom env for visualization
- `--voc_data_dir`: where the VOC data stored
- `--use-drop`: use dropout in RoI head, default False
- `--use-Adam`: use Adam instead of SGD, default SGD. (You need set a very low `lr` for Adam)
- `--load-path`: pretrained model path, default `None`, if it's specified, it would be loaded.
you may open browser, visit `http://<ip>:8097` and see the visualization of training procedure as below:
![visdom](imgs/visdom-fasterrcnn.png)
## Troubleshooting
- dataloader: `received 0 items of ancdata`
see [discussion](https://github.com/pytorch/pytorch/issues/973#issuecomment-346405667), It's alreadly fixed in [train.py](https://github.com/chenyuntc/simple-faster-rcnn-pytorch/blob/master/train.py#L17-L22). So I think you are free from this problem.
- Windows support
I don't have windows machine with GPU to debug and test it. It's welcome if anyone could make a pull request and test it.
## Acknowledgement
This work builds on many excellent works, which include:
- [Yusuke Niitani's ChainerCV](https://github.com/chainer/chainercv) (mainly)
- [Ruotian Luo's pytorch-faster-rcnn](https://github.com/ruotianluo/pytorch-faster-rcnn) which based on [Xinlei Chen's tf-faster-rcnn](https://github.com/endernewton/tf-faster-rcnn)
- [faster-rcnn.pytorch by Jianwei Yang and Jiasen Lu](https://github.com/jwyang/faster-rcnn.pytorch).It mainly refer to [longcw's faster_rcnn_pytorch](https://github.com/longcw/faster_rcnn_pytorch)
- All the above Repositories have referred to [py-faster-rcnn
没有合适的资源?快使用搜索试试~ 我知道了~
Faster R-CNN 的简单快速实现
共55个文件
py:20个
sample:13个
head:4个
0 下载量 74 浏览量
2024-03-24
19:08:55
上传
评论
收藏 3.69MB ZIP 举报
温馨提示
该项目是基于chainercv等项目的简化Faster R-CNN 实现。我希望它可以作为那些想了解 Faster R-CNN 细节的人的入门代码。其目的是: 简化代码(简单胜于复杂) 让代码更简单(扁平比嵌套更好) 与原始论文中报告的性能相匹配(速度计数和 mAP 问题) 并且它具有以下特点: 它可以作为纯 Python 代码运行,不再需要构建事务。 这是一个大约 2000 行有效代码的最小实现,带有大量注释和说明。(感谢 chainercv 的优秀文档) 它实现了比原始实现更高的 mAP (0.712 VS 0.699) 它实现了与其他实现相当的速度(TITAN XP 中的训练和测试为 6fps 和 14fps) 内存效率高(vgg16 约为 3GB)
资源推荐
资源详情
资源评论
收起资源包目录
Faster R-CNN 的简单快速实现.zip (55个子文件)
Faster R-CNN 的简单快速实现
misc
train_fast.py 4KB
demo.jpg 120KB
convert_caffe_pretrain.py 750B
.gitattributes 19B
data
__init__.py 0B
util.py 10KB
dataset.py 4KB
voc_dataset.py 6KB
LICENSE 2KB
trainer.py 9KB
utils
__init__.py 614B
array_tool.py 647B
eval_tool.py 13KB
vis_tool.py 8KB
config.py 2KB
.git
index 3KB
HEAD 23B
refs
heads
master 41B
tags
remotes
origin
HEAD 32B
objects
pack
pack-851599446ca7e07d85114d1234f16f956324a503.pack 2.14MB
pack-851599446ca7e07d85114d1234f16f956324a503.idx 16KB
info
description 73B
packed-refs 423B
info
exclude 240B
logs
HEAD 195B
refs
heads
master 195B
remotes
origin
HEAD 195B
hooks
post-update.sample 189B
prepare-commit-msg.sample 1KB
commit-msg.sample 896B
pre-receive.sample 544B
update.sample 4KB
pre-commit.sample 2KB
pre-rebase.sample 5KB
applypatch-msg.sample 478B
fsmonitor-watchman.sample 5KB
push-to-checkout.sample 3KB
pre-applypatch.sample 424B
pre-push.sample 1KB
pre-merge-commit.sample 416B
config 304B
demo.ipynb 686KB
model
__init__.py 53B
faster_rcnn.py 11KB
faster_rcnn_vgg16.py 5KB
utils
__init__.py 0B
bbox_tools.py 9KB
creator_tool.py 17KB
region_proposal_network.py 8KB
requirements.txt 126B
train.py 5KB
README.MD 8KB
imgs
faster-speed.jpg 113KB
visdom-fasterrcnn.png 462KB
model_all.png 330KB
共 55 条
- 1
资源评论
Omer_
- 粉丝: 2498
- 资源: 31
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功