Code Monkey home page Code Monkey logo

pytorch-faster-rcnn's Introduction

pytorch-faster-rcnn

1. Introduction

Pytorch based implementation of faster rcnn framework.For details about faster R-CNN please refer to the paper Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks by Shaoqing Ren, Kaiming He, Ross Girshick, Jian Sun

This detection framework has the following features:

  • It can be run as pure python code, and also pure based on pytorch framework, no need to build
  • It is easily trained by only running a train.py script, just set the data root dir
  • It has many backbone networks. like vgg, resnet-fpn, mobilenet, high resolution net(HRNet)
  • It can be a really detection framework. You only need to change super parameters in config file and get different models to compare different model
  • It's memory-efficient (about 3GB for vgg16)

2. Installation

2.1 Prerequisites

  • Python 2.7 or 3.5
  • Pytorch 1.5.1
  • torchvision 0.6.1
  • numpy 1.15.4
  • Pillow 6.1.0
  • pycocotools 2.0
  • matplotlib 3.0.2
  • tensorboardX 2.0
pip install -r requirements.txt

2.2 Code-Preparing

 git clone https://github.com/AlphaJia/pytorch-faster-rcnn.git

3. Data Preparation

COCO

3.1 Download the training, validation, test data and annotations
 wget http://images.cocodataset.org/zips/train2017.zip  
 wget http://images.cocodataset.org/zips/val2017.zip
 wget http://images.cocodataset.org/zips/test2017.zip
 wget http://images.cocodataset.org/annotations/annotations_trainval2017.zip
3.2 Extract all of these tars into one directory named COCODevKit
 tar xvf train2017.zip
 tar xvf val2017.zip
 tar xvf test2017.zip
 tar xvf annotations_trainval2017.zip
3.3 Data dir should like this
   COCODevKit
       |-- train2017
               |-- [xxxxxxxxxxxx].jpg
       |-- val2017
               |-- [xxxxxxxxxxxx].jpg
       |-- test2017
               |-- [xxxxxxxxxxxx].jpg
       |-- annotations
               |-- instances_train2017.json
               |-- instances_val2017.json
               |-- image_info_test2017.json
3.4 modify data_root_dir cfg item in config/train_config.py with /path/COCODevKit/

4. Train

Modify model_save_dir cfg item in config/train_config.py with your own save path and device_name with your own device

  • Train with mobilenet
    Modify backbone cfg item in config/train_config.py with mobilenet, download pretrained weights here, and set backbone_pretrained_weights in config/train_config.py with downloaded path.
 python train.py
  • Train with resnet-fpn
    Modify backbone cfg item in config/train_config.py with resnet50_fpn, download pretrained weights here, and set backbone_pretrained_weights in config/train_config.py with downloaded path
 python train.py
  • Train with vgg16
    Modify backbone cfg item in config/train_config.py with vgg16
 python train.py
  • Train with HRNet
    Modify backbone cfg item in config/train_config.py with HRNe
 python train.py

Weights and tensorboard log will save in your model_save_path dir
you may refer to config/train_config.py for more argument.
Some Key arguments:
--backbone: feature extraction backbone network
--backbone_pretrained_weights: backbone pretrained weights, None or path
--train_horizon_flip_prob: data horizontal flip probability
--num_class: number of classification, including background
--data_root_dir: COCO dataset root dir
--model_save_dir: training weights save path
--device_name: training device
--num_epochs: training epochs

5. Test

Modify model_weights cfg item in config/test_config.py with your trained weights path and gpu_id with your own cuda device ID.
you may refer to config/test_config.py for more argument.
Some Key arguments:
--model_weights: training save path
--image_path: predicted images
--gpu_id: cuda device gpu ID
--num_classes: number of classification, including background
--data_root_dir: COCO dataset root dir

 python test.py

6. Demo

img

7. Framework Structure

backbone

This module includes backbone feature extraction network

config

This module includes config parameters in training period and testing period

  • test_config: specify config parameters in testing period like model_file, image_path_dir, save_dir, etc.
  • train_config: specify config parameters in training period like backbone network, batch_size, image_path_dir, anchor_size, ect.

dataloader

This module inherits pytorch dataloader classes, dataset IO.You can also generate your own dataset dataloader IO and put it in this module

test

This module includes the utils function test(common called unit test, also called UT)

  • anchor_utils_test: some unit testing for utils/anchor_utils.py

utils

This module includes some utilies for image processing, network architectures building, anchor generating, loss function, etc.

  • anchor_utils: some basic function for building anchors
  • im_utils: some basic function for image processing

pytorch-faster-rcnn's People

Contributors

alphajia avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pytorch-faster-rcnn's Issues

Please help me

when I run a command which Train with mobilenet, I got information like this:
Epoch: [0] [ 2950/19715] eta: 1:36:08.972184 loss_rpn_box_reg: 0.8739 (1.0911) lr: 0.005000 loss: 1.6920 (2.0218) loss_classifier: 0.3552 (0.4206) loss_box_reg: 0.1971 (0.2049) loss_objectness: 0.2848 (0.3052) time: 0.3408 data: 0.0152 max mem: 0

I want to know the number 19715 represent what and can you tell me how to reduce it's value, because I want to finish the train process as soon as possible.

Please help me NameError : name "module" is not defined

image

Hi , i followed the instructions of README.md, prepared the packages and the dataset, and change some paramenters in train_config.py. All in done.
However,when i started to run train.py, errors saying that "module" is not defined. i don not change the code. I am really wonder how to solve the problem.
Thanks for any help!

Model parameter error

Hi,

I downloaded your model fasterrcnn_resnet50_fpn_coco-258fb6c6.pth, but when i try to load this model using model.load_state_dict(checkpoint['model]), it shows "key error 'model'" so i try to print keys in model dictionaries and i didn't find 'model', Is it possible that you put the wrong model?

add more backbone

Hi, thanks for your contribution, would you like to add more backbone like hrnet, some high resolution backbone network
Thanks

rel_codes can not reshape bug

pred_boxes = self.decode_single(rel_codes.reshape(box_sum, -1), concat_boxes)
=>
rel_codes = rel_codes.view(box_sum, -1)
pred_boxes = self.decode_single(rel_codes, concat_boxes)

########################bug ###########################################
the rel_codes = rel_codes.view(box_sum, -1) #reshape fail on fpn resnet-50

########################correct######################################
rel_codes = rel_codes[:box_sum] #this will be correct

The map is vey low

I have been running the model for a long time, but the map is always 0

Error using resnet50-fpn backbone

Hi
I am getting following error if i use resnet50-fpn backbone. mobilenet just works fine. Could you help me.

Traceback (most recent call last):
File "/Users/Mansoor/PycharmProjects/pythonProject/train.py", line 131, in
main()
File "/Users/Mansoor/PycharmProjects/pythonProject/train.py", line 83, in main
print_freq=50, warmup=False)
File "/Users/Mansoor/PycharmProjects/pythonProject/utils/train_utils.py", line 373, in train_one_epoch
loss_dict = model(images, targets)
File "/Users/Mansoor/opt/anaconda3/envs/pythonProject/lib/python3.6/site-packages/torch/nn/modules/module.py", line 550, in call
result = self.forward(*input, **kwargs)
File "/Users/Mansoor/PycharmProjects/pythonProject/utils/faster_rcnn_utils.py", line 85, in forward
proposals, proposal_losses = self.rpn(images, features, targets)
File "/Users/Mansoor/opt/anaconda3/envs/pythonProject/lib/python3.6/site-packages/torch/nn/modules/module.py", line 550, in call
result = self.forward(*input, **kwargs)
File "/Users/Mansoor/PycharmProjects/pythonProject/utils/rpn_utils.py", line 290, in forward
proposals = self.box_coder.decode(pred_bbox_deltas.detach(), anchors)
File "/Users/Mansoor/PycharmProjects/pythonProject/utils/det_utils.py", line 182, in decode
rel_codes.reshape(box_sum, -1), concat_boxes
RuntimeError: shape '[96768, -1]' is invalid for input of size 392832

RuntimeError: shape '[516096, -1]' is invalid for input of size 2095104

I modified the data_root_dir in train_config.py and also placed the data as mentioned in the README.md
I face this error:

/home/dksingh/anaconda3/envs/mmdet220/lib/python3.7/site-packages/torch/nn/functional.py:3000: UserWarning: The default behavior for interpolate/upsample with float scale
_factor changed in 1.6.0 to align with other frameworks/libraries, and uses scale_factor directly, instead of relying on the computed output size. If you wish to keep the
 old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details. 
  warnings.warn("The default behavior for interpolate/upsample with float scale_factor changed "
Traceback (most recent call last):
  File "train.py", line 132, in <module>
    main()
  File "train.py", line 84, in main
    print_freq=50, warmup=False)
  File "/home/dksingh/codes/clones/pytorch-faster-rcnn/utils/train_utils.py", line 373, in train_one_epoch
    loss_dict = model(images, targets)
  File "/home/dksingh/anaconda3/envs/mmdet220/lib/python3.7/site-packages/torch/nn/modules/module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/dksingh/codes/clones/pytorch-faster-rcnn/utils/faster_rcnn_utils.py", line 85, in forward
    proposals, proposal_losses = self.rpn(images, features, targets)
  File "/home/dksingh/anaconda3/envs/mmdet220/lib/python3.7/site-packages/torch/nn/modules/module.py", line 722, in _call_impl
    result = self.forward(*input, **kwargs)
  File "/home/dksingh/codes/clones/pytorch-faster-rcnn/utils/rpn_utils.py", line 290, in forward
    proposals = self.box_coder.decode(pred_bbox_deltas.detach(), anchors)
  File "/home/dksingh/codes/clones/pytorch-faster-rcnn/utils/det_utils.py", line 182, in decode
    rel_codes.reshape(box_sum, -1), concat_boxes
RuntimeError: shape '[516096, -1]' is invalid for input of size 2095104

When I install

ERROR: Invalid requirement: u'torch~=1.5.1+cu101' (from line 1 of requirements.txt)
ERROR: Invalid requirement: u'torchvision~=0.6.1+cu101' (from line 5 of requirements.txt)
What should I do?

Question about the Result

I tried to use VOC dataset, so i have modified the dataloader and the evaluation code with some parts of mmedetction. However, i got really pool result (mAP is about 0.167) after 17 epochs training(about 1 day). So, i'm wondering how many epochs did your coco dataset cost, and what's your final AP result? I would be very grateful if you could give me the answer.

FPN101 error

is this model combination of ResNet101-FPN?
is so then why FPN codes are different than resnet50-fpn?

you got my star

just star it~~~thanks full py source in faster-rcnn, easier to learn

i think anchor has error

in anchor generator

self.sizes = ((64,), (128,), (256,))
self.aspect_ratios = ((0.5,), (1,), (2.0,), (0.5,), (1,), (2.0,), (0.5,), (1,), (2.0,))

cell_anchors = [generate_anchors(sizes, aspect_ratios, dtype, device)
                        for sizes, aspect_ratios in zip(self.sizes, self.aspect_ratios)]

and cell_anchors result is

[tensor([[-45., -23.,  45.,  23.]], device='cuda:2'),
 tensor([[-64., -64.,  64.,  64.]], device='cuda:2'),
 tensor([[ -91., -181.,   91.,  181.]], device='cuda:2')]

anchors count should be 9 per pixel but only 3 base anchor..
i think because cell anchors made by zip(size, ratios), not product

please help me how to train my own data

i find that the COCODevkit dataset is too large for me, and i need to finish the training as soon as possible.
i tried to changed dataset i need to COCO format, and i changed the data_root_dir in my train_confi.py, however ,it can not work. i really wonder how to do so.
appreciate for any avaiable help

wrong out features on cls_logits

self.conv = nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1)

'''background/foreground score'''
self.cls_logits = nn.Conv2d(in_channels, num_anchors, kernel_size=1, stride=1) #wrong

'''bbox regression parameters'''
self.bbox_pred = nn.Conv2d(in_channels, num_anchors * 4, kernel_size=1, stride=1)

###################
self.cls_logits = nn.Conv2d(in_channels, num_anchors * 2, kernel_size=1, stride=1) #correct

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.