Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexHex7 committed Dec 14, 2017
1 parent 4fda7a4 commit ce17db6
Show file tree
Hide file tree
Showing 52 changed files with 2,206 additions and 684 deletions.
100 changes: 0 additions & 100 deletions .gitignore

This file was deleted.

21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

103 changes: 37 additions & 66 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,87 +1,58 @@
# Faster RCNN with PyTorch
**Note:** I re-implemented faster rcnn in this project when I started learning PyTorch. Then I use PyTorch in all of my projects. I still remember it costed one week for me to figure out how to build cuda code as a pytorch layer :).
But actually this is not a good implementation and I didn't achieve the same mIOU as the original caffe code.
# Faster_R-CNN_pytorch
- Modifed based on [**longcw/faster_rcnn_pytorch**](https://github.com/daviddao/spatial-transformer-tensorflow) .
- Implementation of [**Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks**](https://arxiv.org/abs/1506.01497).

**This project is no longer maintained. So I suggest:**
- You can still read and study this code if you want to re-implement faster rcnn by yourself;
- You can use the better PyTorch implementation by [ruotianluo](https://github.com/ruotianluo/pytorch-faster-rcnn) if you want to train faster rcnn with your own data;
## Statement
- Based on **longcw/faster_rcnn_pytorch (python2.7)**, I modified the code to **python3.6** and **numpy 1.13.3**. So the numpy version do not need to change into 1.11.
- Follow **longcw**'s code, I draw some picture to understand the process of Faster R-CNN.

This is a [PyTorch](https://github.com/pytorch/pytorch)
implementation of Faster RCNN.
This project is mainly based on [py-faster-rcnn](https://github.com/rbgirshick/py-faster-rcnn)
and [TFFRCNN](https://github.com/CharlesShang/TFFRCNN).
Thank **longcw**, his code helps me a lot to understand Faster R-CNN. Also the readme files of **longcw** and **ruotianluo** tell me how to build cython models. You can also follow their code and readme file.

For details about R-CNN please refer to the [paper](https://arxiv.org/abs/1506.01497)
Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
by Shaoqing Ren, Kaiming He, Ross Girshick, Jian Sun.
**If there is something I can not write in my repertory, please contact me.**

### Progress
## Environment and Compile
- python 3.6
- pytorch 0.3.0
- numpy 1.13.3
- opencv 3.3.0

- [x] Forward for detecting
- [x] RoI Pooling layer with C extensions on CPU (only forward)
- [x] RoI Pooling layer on GPU (forward and backward)
- [x] Training on VOC2007
- [x] TensroBoard support
- [x] Evaluation

### Installation and demo
0. Install the requirements (you can use pip or [Anaconda](https://www.continuum.io/downloads)):
Follow [**longcw/faster_rcnn_pytorch**](https://github.com/longcw/faster_rcnn_pytorch) and [**ruotianluo/pytorch-faster-rcnn**](https://github.com/ruotianluo/pytorch-faster-rcnn).

1. Install requirements (directly copy from [**longcw/faster_rcnn_pytorch/README.md**](https://raw.githubusercontent.com/longcw/faster_rcnn_pytorch/master/README.md)).

```
conda install pip pyyaml sympy h5py cython numpy scipy
conda install -c menpo opencv3
pip install easydict
```

2. Choose arch of GPU type. (directly copy from[**ruotianluo/pytorch-faster-rcnn/README.md**](https://raw.githubusercontent.com/ruotianluo/pytorch-faster-rcnn/master/README.md))

| GPU model | Architecture |
| ------------- | ------------- |
| TitanX (Maxwell/Pascal) | sm_52 |
| GTX 960M | sm_50 |
| GTX 1080 (Ti) | sm_61 |
| Grid K520 (AWS g2.2xlarge) | sm_30 |
| Tesla K80 (AWS p2.xlarge) | sm_37 |

modify the parameter **arch** in **faster_rcnn_pytorch/faster_rcnn/make.sh**

3. Build Cython models (directly copy from [**longcw/faster_rcnn_pytorch/README.md**](https://raw.githubusercontent.com/longcw/faster_rcnn_pytorch/master/README.md)).


1. Clone the Faster R-CNN repository
```bash
git clone [email protected]:longcw/faster_rcnn_pytorch.git
```

2. Build the Cython modules for nms and the roi_pooling layer
```bash
cd faster_rcnn_pytorch/faster_rcnn
./make.sh
```
3. Download the trained model [VGGnet_fast_rcnn_iter_70000.h5](https://drive.google.com/open?id=0B4pXCfnYmG1WOXdpYVFybWxiZFE)
and set the model path in `demo.py`
3. Run demo `python demo.py`

### Training on Pascal VOC 2007

Follow [this project (TFFRCNN)](https://github.com/CharlesShang/TFFRCNN)
to download and prepare the training, validation, test data
and the VGG16 model pre-trained on ImageNet.

Since the program loading the data in `faster_rcnn_pytorch/data` by default,
you can set the data path as following.
```bash
cd faster_rcnn_pytorch
mkdir data
cd data
ln -s $VOCdevkit VOCdevkit2007
```

Then you can set some hyper-parameters in `train.py` and training parameters in the `.yml` file.

Now I got a 0.661 mAP on VOC07 while the origin paper got a 0.699 mAP.
You may need to tune the loss function defined in `faster_rcnn/faster_rcnn.py` by yourself.

### Training with TensorBoard
With the aid of [Crayon](https://github.com/torrvision/crayon),
we can access the visualisation power of TensorBoard for any
deep learning framework.
## Run
You can follow [**longcw/faster_rcnn_pytorch**](https://github.com/daviddao/spatial-transformer-tensorflow) .

To use the TensorBoard, install Crayon (https://github.com/torrvision/crayon)
and set `use_tensorboard = True` in `faster_rcnn/train.py`.
- **demo.py** You need to download **trained_weights(VGGnet_fast_rcnn_iter_70000.h5)** and load it's weight.
- **train.py** You need to download **VOC dataset** and ** VGG_imagenet_pretrained_weight(VGG_imagenet.npy)**.
- **test.py** YOu need to download **VOC dataset**.

### Evaluation
Set the path of the trained model in `test.py`.
```bash
cd faster_rcnn_pytorch
mkdir output
python test.py
```
## Process of Code
![](pics/process.png)

License: MIT license (MIT)
2 changes: 2 additions & 0 deletions data/VOCdevkit2012/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Download VOC dataset.
The structure of data_dir is data/VOCdevkit2012/VOC2012/[Annotations|ImageSets|...]
1 change: 1 addition & 0 deletions data/pretrained_model/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Download VGG_imagenet.npy
11 changes: 8 additions & 3 deletions demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ def test():
# im_file = '/media/longc/Data/data/2DMOT2015/test/ETH-Crossing/img1/000100.jpg'
image = cv2.imread(im_file)

model_file = '/media/longc/Data/models/VGGnet_fast_rcnn_iter_70000.h5'
model_file = 'models/saved_model3/faster_rcnn_100000.h5'
# model_file = 'models/VGGnet_fast_rcnn_iter_70000.h5'
# model_file = '/media/longc/Data/models/faster_rcnn_pytorch3/faster_rcnn_100000.h5'
# model_file = '/media/longc/Data/models/faster_rcnn_pytorch2/faster_rcnn_2000.h5'
detector = FasterRCNN()
Expand All @@ -27,6 +28,10 @@ def test():
t = Timer()
t.tic()
# image = np.zeros(shape=[600, 800, 3], dtype=np.uint8) + 255

# dets_np (n_box, 4)
# socres_np (n_box, ) [0.9, 0.96, ...]
# classes_np (n_box, ) ['dog', 'human', 'people', ...]
dets, scores, classes = detector.detect(image, 0.7)
runtime = t.toc()
print('total spend: {}s'.format(runtime))
Expand All @@ -38,8 +43,8 @@ def test():
cv2.putText(im2show, '%s: %.3f' % (classes[i], scores[i]), (det[0], det[1] + 15), cv2.FONT_HERSHEY_PLAIN,
1.0, (0, 0, 255), thickness=1)
cv2.imwrite(os.path.join('demo', 'out.jpg'), im2show)
cv2.imshow('demo', im2show)
cv2.waitKey(0)
# cv2.imshow('demo', im2show)
# cv2.waitKey(0)


if __name__ == '__main__':
Expand Down
Binary file modified demo/out.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added faster_rcnn/__pycache__/__init__.cpython-35.pyc
Binary file not shown.
Binary file not shown.
Binary file added faster_rcnn/__pycache__/network.cpython-35.pyc
Binary file not shown.
Loading

0 comments on commit ce17db6

Please sign in to comment.