YOLOv2 in PyTorch

Overview

YOLOv2 in PyTorch

NOTE: This project is no longer maintained and may not compatible with the newest pytorch (after 0.4.0).

This is a PyTorch implementation of YOLOv2. This project is mainly based on darkflow and darknet.

I used a Cython extension for postprocessing and multiprocessing.Pool for image preprocessing. Testing an image in VOC2007 costs about 13~20ms.

For details about YOLO and YOLOv2 please refer to their project page and the paper: YOLO9000: Better, Faster, Stronger by Joseph Redmon and Ali Farhadi.

NOTE 1: This is still an experimental project. VOC07 test mAP is about 0.71 (trained on VOC07+12 trainval, reported by @cory8249). See issue1 and issue23 for more details about training.

NOTE 2: I recommend to write your own dataloader using torch.utils.data.Dataset since multiprocessing.Pool.imap won't stop even there is no enough memory space. An example of dataloader for VOCDataset: issue71.

NOTE 3: Upgrade to PyTorch 0.4: https://github.com/longcw/yolo2-pytorch/issues/59

Installation and demo

  1. Clone this repository

    git clone [email protected]:longcw/yolo2-pytorch.git
  2. Build the reorg layer (tf.extract_image_patches)

    cd yolo2-pytorch
    ./make.sh
  3. Download the trained model yolo-voc.weights.h5 and set the model path in demo.py

  4. Run demo python demo.py.

Training YOLOv2

You can train YOLO2 on any dataset. Here we train it on VOC2007/2012.

  1. Download the training, validation, test data and VOCdevkit

    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

    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

    $VOCdevkit/                           # development kit
    $VOCdevkit/VOCcode/                   # VOC utility code
    $VOCdevkit/VOC2007                    # image sets, annotations, etc.
    # ... and several other directories ...
  4. Since the program loading the data in yolo2-pytorch/data by default, you can set the data path as following.

    cd yolo2-pytorch
    mkdir data
    cd data
    ln -s $VOCdevkit VOCdevkit2007
  5. Download the pretrained darknet19 model and set the path in yolo2-pytorch/cfgs/exps/darknet19_exp1.py.

  6. (optional) Training with TensorBoard.

    To use the TensorBoard, set use_tensorboard = True in yolo2-pytorch/cfgs/config.py and install TensorboardX (https://github.com/lanpa/tensorboard-pytorch). Tensorboard log will be saved in training/runs.

  7. Run the training program: python train.py.

Evaluation

Set the path of the trained_model in yolo2-pytorch/cfgs/config.py.

cd faster_rcnn_pytorch
mkdir output
python test.py

Training on your own data

The forward pass requires that you supply 4 arguments to the network:

  • im_data - image data.
    • This should be in the format C x H x W, where C corresponds to the color channels of the image and H and W are the height and width respectively.
    • Color channels should be in RGB format.
    • Use the imcv2_recolor function provided in utils/im_transform.py to preprocess your image. Also, make sure that images have been resized to 416 x 416 pixels
  • gt_boxes - A list of numpy arrays, where each one is of size N x 4, where N is the number of features in the image. The four values in each row should correspond to x_bottom_left, y_bottom_left, x_top_right, and y_top_right.
  • gt_classes - A list of numpy arrays, where each array contains an integer value corresponding to the class of each bounding box provided in gt_boxes
  • dontcare - a list of lists

License: MIT license (MIT)

Comments
  • cuda runtime error : invalid device function

    cuda runtime error : invalid device function

    Hello, I'm currently working on project using YOLO v2 as a base and am very interested in using your pytorch implementation as a starting point. I've run into a strange issue right from the start however when running the test and demo scripts.

    The error is: RuntimeError: cuda runtime error (8) : invalid device function at /data/users/soumith/builder/wheel/pytorch-src/torch/lib/THC/THCTensorCopy.cu:204

    Oddly, when running the demo script, the first image with the computer appears with detection boxes and the error only happens after hitting the key and trying to move to the next image.

    I've already changed the architecture in make.sh to sm_30 which is what my video card is compatible with. Have you run into this kind of issue before? Perhaps there is another architecture setting I'm missing somewhere or maybe it has to do with my install of pytorch itself...

    Let me know if you have any ideas. Once I get this running I hope to port over your mAP scoring code to pjreddie's implementation and compare scores.

    opened by rdfong 6
  • size_index is missing for preprocess_test function

    size_index is missing for preprocess_test function

    When trying the demo, I found that preprocess_test has two arguments (data, size_index) while in the demo.py file, only one argument is passed yolo_utils.preprocess_test((image, None, cfg.inp_size))[0] , how to fix this ?

    Thanks.

    opened by aelnouby 5
  • Undefined Symbol: PyInt_FromLong

    Undefined Symbol: PyInt_FromLong

    I've written a demo program and have been trying to run this in python3 but keep getting this error:

    ImportError: /home/ubuntu/git/yolo2-pytorch/layers/reorg/_ext/reorg_layer/_reorg_layer.so: undefined symbol: PyInt_FromLong

    I'm assuming it was written in python2, but is there any way to convert this? I've tried but haven't gotten anything to work.

    opened by nottug 4
  • ImportError on _reorg_layer.so

    ImportError on _reorg_layer.so

    Hi,

    Can't seem to resolve another issue on attempting to run demo.py :

    from ._reorg_layer import lib as _lib, ffi as _ffi ImportError: dynamic module does not define init function (init_reorg_layer)

    Seems to be thrown on importing the _reorg_layer.so in the /_ext/reorg_layer directory

    Using Python 2.7 and Pytorch 0.1.10-py27_1cu80

    Also, I'm using Pytorch from within an anaconda environment. Could that be causing problems?

    opened by tymokvo 4
  • next_batch: still epoch xxx start

    next_batch: still epoch xxx start

    When I run python demo.py, I always get the epoch xxx start..., and the memroy up!~ I see the imdb.py , it's confuse to me. Could everyone give me some advice? (I use python3.6)

    opened by AceCoooool 3
  • network output 0 and inf

    network output 0 and inf

    i implemented your code with tensorflow (using pretrained vgg instead of darknet), after training with 100 epochs with VOC2007 (cls_loss use MSE, softmax), its output are all zeros or inf, what problems happened? thanks!

    opened by ghost 2
  • Could not clone the repository

    Could not clone the repository

    Cloning into 'yolo2-pytorch'... Permission denied (publickey). fatal: Could not read from remote repository.

    Please make sure you have the correct access rights and the repository exists.

    Kindly help me !!

    opened by soumenms2015 2
  • No module named _reorg_layer

    No module named _reorg_layer

    yolo2-pytorch/layers/reorg/_ext/reorg_layer/__init__.py", line 3, in <module> from ._reorg_layer import lib as _lib, ffi as _ffi ImportError: No module named _reorg_layer

    opened by skrish13 2
  • error locating .cu files for building reorg layer

    error locating .cu files for building reorg layer

    Hi,

    This is an exciting project! Unfortunately, I've encountered some errors while getting it running locally...

    While trying to build the reorg layer from make.sh (per instructions in the readme), gcc throws the following errors:

    gcc: error: reorg_cuda_kernel.cu: No such file or directory though that file does exist where it is searching for it.

    and

    gcc: error: /home/tyler/yolo2-pytorch/layers/roi_pooling/src/cuda/roi_pooling.cu.o: No such file or directory and indeed this one is not present, though I assume this could be caused by the first failure?

    gcc and nvcc info if that helps

    gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3) Cuda compilation tools, release 8.0, V8.0.44

    Let me know if I there's more info that would be useful.

    opened by tymokvo 2
  • ValueError: zero-size array to reduction operation maximum which has no identity

    ValueError: zero-size array to reduction operation maximum which has no identity

    Hi guys, I trained yolo on my customized VOC dataset. After some steps, I got such error:

    ValueError: zero-size array to reduction operation maximum which has no identity

    How to solve this error? Thank you!

    opened by ToolmanInside 1
  • images, gt_boxes, classes, dontcare, origin_im = next(self.gen)

    images, gt_boxes, classes, dontcare, origin_im = next(self.gen)

    Hi, everybody! Can anyone help me with my problem why it stopped at "images, gt_boxes, classes, dontcare, origin_im = next(self.gen) " in imdb.py/next_batch when I run train.py , or to be exact , it is not 'stopped' , just runing this line and don't continue next line.

    opened by EddieEduardo 1
  • anchors question

    anchors question

    When multi-scale training is applied, the output feature map size also changes, so shouldn't the size of the prior anchor be modified at the same time? But why does anchors remain the same in the code?

    opened by mmpp406 0
  • demo

    demo

    ImportError: /home/zgx/mask_recognition/yolo2-pytorch-master/layers/reorg/_ext/reorg_layer/_reorg_layer.so: undefined symbol: __cudaRegisterFatBinaryEnd

    opened by JiayiXie11 1
  • RuntimeError:CUDNN_Status_Mapping_Errror

    RuntimeError:CUDNN_Status_Mapping_Errror

    I run in multi (2~4) GPUs (Tesla V100), there are something error.

    Found GPU0 V100-SXM2-16GB which requires CUDA_VERSION >= 9000 for optimal performance and fast startup time, but your PyTorch was compiled with CUDA_VERSION 8000. Please install the correct PyTorch binary using instructions from http://pytorch.org warnings.warn(incorrect_binary_warn % (d, name, 9000, CUDA_VERSION)) * ...... ../python3.5/dist-packages/torch/nn/modules/module.py, line 491, in__call__ result = self.forward(*input, **kwargs) ...... *

    RuntimeError:CUDNN_Status_Mapping_Errror

    opened by bheAI 1
Owner
Long Chen
Computer Vision
Long Chen
RealFormer-Pytorch Implementation of RealFormer using pytorch

RealFormer-Pytorch Implementation of RealFormer using pytorch. Includes comparison with classical Transformer on image classification task (ViT) wrt C

Simo Ryu 90 Dec 8, 2022
Generic template to bootstrap your PyTorch project with PyTorch Lightning, Hydra, W&B, and DVC.

NN Template Generic template to bootstrap your PyTorch project. Click on Use this Template and avoid writing boilerplate code for: PyTorch Lightning,

Luca Moschella 520 Dec 30, 2022
A PyTorch Extension: Tools for easy mixed precision and distributed training in Pytorch

This repository holds NVIDIA-maintained utilities to streamline mixed precision and distributed training in Pytorch. Some of the code here will be included in upstream Pytorch eventually. The intention of Apex is to make up-to-date utilities available to users as quickly as possible.

NVIDIA Corporation 6.9k Jan 3, 2023
Objective of the repository is to learn and build machine learning models using Pytorch. 30DaysofML Using Pytorch

30 Days Of Machine Learning Using Pytorch Objective of the repository is to learn and build machine learning models using Pytorch. List of Algorithms

Mayur 119 Nov 24, 2022
Pretrained SOTA Deep Learning models, callbacks and more for research and production with PyTorch Lightning and PyTorch

Pretrained SOTA Deep Learning models, callbacks and more for research and production with PyTorch Lightning and PyTorch

Pytorch Lightning 1.4k Jan 1, 2023
Amazon Forest Computer Vision: Satellite Image tagging code using PyTorch / Keras with lots of PyTorch tricks

Amazon Forest Computer Vision Satellite Image tagging code using PyTorch / Keras Here is a sample of images we had to work with Source: https://www.ka

Mamy Ratsimbazafy 360 Dec 10, 2022
The Incredible PyTorch: a curated list of tutorials, papers, projects, communities and more relating to PyTorch.

This is a curated list of tutorials, projects, libraries, videos, papers, books and anything related to the incredible PyTorch. Feel free to make a pu

Ritchie Ng 9.2k Jan 2, 2023
A PyTorch implementation of the paper Mixup: Beyond Empirical Risk Minimization in PyTorch

Mixup: Beyond Empirical Risk Minimization in PyTorch This is an unofficial PyTorch implementation of mixup: Beyond Empirical Risk Minimization. The co

Harry Yang 121 Dec 17, 2022
A pytorch implementation of Pytorch-Sketch-RNN

Pytorch-Sketch-RNN A pytorch implementation of https://arxiv.org/abs/1704.03477 In order to draw other things than cats, you will find more drawing da

Alexis David Jacq 172 Dec 12, 2022
PyTorch implementation of Advantage async actor-critic Algorithms (A3C) in PyTorch

Advantage async actor-critic Algorithms (A3C) in PyTorch @inproceedings{mnih2016asynchronous, title={Asynchronous methods for deep reinforcement lea

LEI TAI 111 Dec 8, 2022
Amazon Forest Computer Vision: Satellite Image tagging code using PyTorch / Keras with lots of PyTorch tricks

Amazon Forest Computer Vision Satellite Image tagging code using PyTorch / Keras Here is a sample of images we had to work with Source: https://www.ka

Mamy Ratsimbazafy 359 Jan 5, 2023
A bunch of random PyTorch models using PyTorch's C++ frontend

PyTorch Deep Learning Models using the C++ frontend Gettting started Clone the repo 1. https://github.com/mrdvince/pytorchcpp 2. cd fashionmnist or

Vince 0 Jul 13, 2021
PyTorch Autoencoders - Implementing a Variational Autoencoder (VAE) Series in Pytorch.

PyTorch Autoencoders Implementing a Variational Autoencoder (VAE) Series in Pytorch. Inspired by this repository Model List check model paper conferen

Subin An 8 Nov 21, 2022
PyTorch-LIT is the Lite Inference Toolkit (LIT) for PyTorch which focuses on easy and fast inference of large models on end-devices.

PyTorch-LIT PyTorch-LIT is the Lite Inference Toolkit (LIT) for PyTorch which focuses on easy and fast inference of large models on end-devices. With

Amin Rezaei 157 Dec 11, 2022
A general framework for deep learning experiments under PyTorch based on pytorch-lightning

torchx Torchx is a general framework for deep learning experiments under PyTorch based on pytorch-lightning. TODO list gan-like training wrapper text

Yingtian Liu 6 Mar 17, 2022
A PyTorch Extension: Tools for easy mixed precision and distributed training in Pytorch

Introduction This is a Python package available on PyPI for NVIDIA-maintained utilities to streamline mixed precision and distributed training in Pyto

Artit 'Art' Wangperawong 5 Sep 29, 2021
Pytorch-diffusion - A basic PyTorch implementation of 'Denoising Diffusion Probabilistic Models'

PyTorch implementation of 'Denoising Diffusion Probabilistic Models' This reposi

Arthur Juliani 76 Jan 7, 2023
Fang Zhonghao 13 Nov 19, 2022
ALBERT-pytorch-implementation - ALBERT pytorch implementation

ALBERT-pytorch-implementation developing... 모델의 개념이해를 돕기 위한 구현물로 현재 변수명을 상세히 적었고

BG Kim 3 Oct 6, 2022