Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SqueezeDet object detector reimplemented in Theano/Lasagne #110

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions papers/squeezedet/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Parts of the code in Squeezedet_Theano are derived from the
following version of SqueezeDet by Bichen Wu:

Author : Bichen Wu
Date : 22 November 2017
Source : https://github.com/BichenWuUCB/squeezeDet/
Commit : e7c0860 (Wed Nov 22 15:47:02 2017 -0800)

Copyright for portions of the derived code in Squeezedet_Theano are
held by Bichen Wu, 2016-2107. All other code in Squeezedet_Theano is
copyright Corvidim, 2017. This code is published in accordance with
and retains the same license as SqueezeDet by Bichen Wu.

BSD 2-Clause License

Copyright (c) 2016-2017, Bichen Wu
Copyright (c) 2017, Corvidim
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
82 changes: 82 additions & 0 deletions papers/squeezedet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# SqueezeDet_Theano

This is a reimplementation of Bichen Wu's SqueezeDet [1],[2] for
Theano [3] and Lasagne [4] by
[@corvidim](https://github.com/corvidim). The version contained in
this repo corresponds to a snapshot of
[squeezedet_theano](https://github.com/corvidim/squeezedet_theano)
0.1.0.

SqueezeDet is particularly well-suited to low-power,
low-memory-consumption applications. In particular, the trained
weights take up less than 8Mb!

This release includes:

* Network weights trained on the KITTI dataset (`data/squeezedet_kitti.pkl.gz`, <8MB).

* The detector ("prediction graph" and "interpretation graph"
of the original TensorFlow implementation); training is not yet
supported.


References:

- [1] https://arxiv.org/abs/1612.01051
- [2] https://github.com/BichenWuUCB/squeezeDet
- [3] http://deeplearning.net/software/theano/
- [4] http://lasagne.readthedocs.io/en/latest/

## Setup

Requirements:

- Python 2.7+ (Python 3 experimental support; tested under Python 3.6.3)
- Theano 0.9+
- Lasagne 0.2+
- Numpy, PIL, Matplotlib

Sample docker files for Ubuntu installation:

- `./setup/sqzdet_theano_dockerfile.txt` (Python 2, Theano 0.10)
- `./setup/sqzdet_theano_0.9_dockerfile.txt` (Python 2, Theano 0.9)
- `./setup/sqzdet_theano_py3k_dockerfile.txt` (Python 3, Theano 0.10)


To run detection on a default image (`data/sample.png` from the original SqueezeDet demo):

```
THEANO_FLAGS='floatX=float32' python src/sqz_det_thn.py --network_weights=data/squeezedet_kitti.pkl.gz
```

Use the `--img_in` flag to specify an image file, for example:

```
THEANO_FLAGS='floatX=float32' python src/sqz_det_thn.py --network_weights=data/squeezedet_kitti.pkl.gz --img_in=data/Boston_01.png
```

or specify a comma-separated list of images (all of which must be the same size), for example:

```
THEANO_FLAGS='floatX=float32' python src/sqz_det_thn.py --network_weights=data/squeezedet_kitti.pkl.gz --img_in=data/sample.png,data/Boston_00.png
```

Output visualization(s) will be saved in PDF format to the current
directory (e.g., `./out_sample.pdf`) and displayed (if running
interactively) as shown below. To specify a different directory for
output use the `--out_dir` flag.

## Output visualizations of sample input images
---
![output from sample.png](data/output/out_sample.png)

---

![output from Boston_00.png](data/output/out_Boston_00.png)

---

![output from Boston_01.png](data/output/out_Boston_01.png)

---

Binary file added papers/squeezedet/data/Boston_00.png
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 papers/squeezedet/data/Boston_01.png
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 papers/squeezedet/data/output/out_Boston_00.png
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 papers/squeezedet/data/output/out_Boston_01.png
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 papers/squeezedet/data/output/out_sample.png
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 papers/squeezedet/data/sample.png
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 papers/squeezedet/data/squeezedet_kitti.pkl.gz
Binary file not shown.
37 changes: 37 additions & 0 deletions papers/squeezedet/setup/sqzdet_theano_0.9_dockerfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#Ubuntu CUDA Py2 docker file for sqzdet_theano under Theano 0.9
# nvidia-docker build -t sqzdet_theano_0.9 . -- file sqzdet_theano_0.9.txt

FROM nvidia/cuda:8.0-cudnn5-devel-ubuntu16.04

RUN apt-get update && apt-get install -y \
apt-utils \
git \
wget

RUN wget --no-check-certificate https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh -O miniconda2.sh

RUN chmod +x miniconda2.sh && bash ./miniconda2.sh -b -p /opt/miniconda2

ENV PATH /opt/miniconda2/bin:$PATH

RUN conda update conda

# as of 20171226: matplotlib=2.1.1 numpy=1.13.3 pillow=4.3.0
RUN conda install \
matplotlib \
numpy \
pillow \
mkl-service

#NB: we require Lasagne>=0.2, still "bleeding edge" (0.2.dev1 as of 20171225)
RUN pip install \
numpy \
easydict \
future \
Theano==0.9 \
--upgrade https://github.com/Lasagne/Lasagne/archive/master.zip

# to avoid RuntimeError('To use MKL 2018 with Theano...')
ENV MKL_THREADING_LAYER GNU

# nvidia-docker run --rm -it --name st_0 -v /home/ubuntu:/home/ubuntu sqzdet_theano_0.9
36 changes: 36 additions & 0 deletions papers/squeezedet/setup/sqzdet_theano_dockerfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#Ubuntu CUDA Python2 docker file for sqzdet_theano under Theano 1.0
# nvidia-docker build -t sqzdet_theano . --file sqzdet_theano.txt

FROM nvidia/cuda:8.0-cudnn6-devel-ubuntu16.04

RUN apt-get update && apt-get install -y \
apt-utils \
git \
wget

RUN wget --no-check-certificate https://repo.continuum.io/miniconda/Miniconda2-latest-Linux-x86_64.sh -O miniconda2.sh

RUN chmod +x miniconda2.sh && bash ./miniconda2.sh -b -p /opt/miniconda2

ENV PATH /opt/miniconda2/bin:$PATH

RUN conda update conda

# as of 20171226: matplotlib=2.1.1 numpy=1.13.3 pillow=4.3.0
RUN conda install \
matplotlib \
numpy \
pillow \
mkl-service

# as of 20171226, bleeding-edge installs below: theano=1.0.1+unknown, lasagne=0.2.dev1
RUN pip install \
easydict \
future \
--upgrade https://github.com/Theano/Theano/archive/master.zip \
--upgrade https://github.com/Lasagne/Lasagne/archive/master.zip

#to avoid RuntimeError('To use MKL 2018 with Theano...')
ENV MKL_THREADING_LAYER GNU

# nvidia-docker run --rm -it --name st_1 -v /home/ubuntu:/home/ubuntu sqzdet_theano
36 changes: 36 additions & 0 deletions papers/squeezedet/setup/sqzdet_theano_py3k_dockerfile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#Ubuntu CUDA Python3 docker file for sqzdet_theano under Theano 1.0
# nvidia-docker build -t sqzdet_theano_py3k . --file sqzdet_theano_py3k.txt

FROM nvidia/cuda:8.0-cudnn6-devel-ubuntu16.04

RUN apt-get update && apt-get install -y \
apt-utils \
git \
wget

RUN wget --no-check-certificate https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh -O miniconda3.sh

RUN chmod +x miniconda3.sh && bash ./miniconda3.sh -b -p /opt/miniconda3

ENV PATH /opt/miniconda3/bin:$PATH

RUN conda update conda

# as of 20171226: matplotlib=2.1.1 numpy=1.13.3 pillow=4.3.0
RUN conda install \
matplotlib \
numpy \
pillow \
mkl-service

# as of 20171226, bleeding-edge installs below: theano=1.0.1+unknown, lasagne=0.2.dev1
RUN pip install \
easydict \
future \
--upgrade https://github.com/Theano/Theano/archive/master.zip \
--upgrade https://github.com/Lasagne/Lasagne/archive/master.zip

#to avoid RuntimeError('To use MKL 2018 with Theano...')
ENV MKL_THREADING_LAYER GNU

# nvidia-docker run --rm -it --name st_3k -v /home/ubuntu:/home/ubuntu sqzdet_theano_py3k
142 changes: 142 additions & 0 deletions papers/squeezedet/src/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# Author: Bichen Wu ([email protected]) 08/25/2016

"""Base Model configurations"""

import os
import os.path as osp
import numpy as np
from easydict import EasyDict as edict

def base_model_config(dataset='PASCAL_VOC'):
assert dataset.upper()=='PASCAL_VOC' or dataset.upper()=='KITTI', \
'Currently only support PASCAL_VOC or KITTI dataset'

cfg = edict()

# Dataset used to train/val/test model. Now support PASCAL_VOC or KITTI
cfg.DATASET = dataset.upper()

if cfg.DATASET == 'PASCAL_VOC':
# object categories to classify
cfg.CLASS_NAMES = ('aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus',
'car', 'cat', 'chair', 'cow', 'diningtable', 'dog',
'horse', 'motorbike', 'person', 'pottedplant', 'sheep',
'sofa', 'train', 'tvmonitor')
elif cfg.DATASET == 'KITTI':
cfg.CLASS_NAMES = ('car', 'pedestrian', 'cyclist')

# number of categories to classify
cfg.CLASSES = len(cfg.CLASS_NAMES)

# ROI pooling output width
cfg.GRID_POOL_WIDTH = 7

# ROI pooling output height
cfg.GRID_POOL_HEIGHT = 7

# parameter used in leaky ReLU
cfg.LEAKY_COEF = 0.1

# Probability to keep a node in dropout
cfg.KEEP_PROB = 0.5

# image width
cfg.IMAGE_WIDTH = 224

# image height
cfg.IMAGE_HEIGHT = 224

# anchor box, array of [cx, cy, w, h]. To be defined later
cfg.ANCHOR_BOX = []

# number of anchor boxes
cfg.ANCHORS = len(cfg.ANCHOR_BOX)

# number of anchor boxes per grid
cfg.ANCHOR_PER_GRID = -1

# batch size
cfg.BATCH_SIZE = 20

# Only keep boxes with probability higher than this threshold
cfg.PROB_THRESH = 0.005

# Only plot boxes with probability higher than this threshold
cfg.PLOT_PROB_THRESH = 0.5

# Bounding boxes with IOU larger than this are going to be removed
cfg.NMS_THRESH = 0.2

# Pixel mean values (BGR order) as a (1, 1, 3) array. Below is the BGR mean
# of VGG16
cfg.BGR_MEANS = np.array([[[103.939, 116.779, 123.68]]])

# loss coefficient for confidence regression
cfg.LOSS_COEF_CONF = 1.0

# loss coefficient for classification regression
cfg.LOSS_COEF_CLASS = 1.0

# loss coefficient for bounding box regression
cfg.LOSS_COEF_BBOX = 10.0

# reduce step size after this many steps
cfg.DECAY_STEPS = 10000

# multiply the learning rate by this factor
cfg.LR_DECAY_FACTOR = 0.1

# learning rate
cfg.LEARNING_RATE = 0.005

# momentum
cfg.MOMENTUM = 0.9

# weight decay
cfg.WEIGHT_DECAY = 0.0005

# wether to load pre-trained model
cfg.LOAD_PRETRAINED_MODEL = True

# path to load the pre-trained model
cfg.PRETRAINED_MODEL_PATH = ''

# print log to console in debug mode
cfg.DEBUG_MODE = False

# a small value used to prevent numerical instability
cfg.EPSILON = 1e-16

# threshold for safe exponential operation
cfg.EXP_THRESH=1.0

# gradients with norm larger than this is going to be clipped.
cfg.MAX_GRAD_NORM = 10.0

# Whether to do data augmentation
cfg.DATA_AUGMENTATION = False

# The range to randomly shift the image widht
cfg.DRIFT_X = 0

# The range to randomly shift the image height
cfg.DRIFT_Y = 0

# Whether to exclude images harder than hard-category. Only useful for KITTI
# dataset.
cfg.EXCLUDE_HARD_EXAMPLES = True

# small value used in batch normalization to prevent dividing by 0. The
# default value here is the same with caffe's default value.
cfg.BATCH_NORM_EPSILON = 1e-5

# number of threads to fetch data
cfg.NUM_THREAD = 4

# capacity for FIFOQueue
cfg.QUEUE_CAPACITY = 100

# indicate if the model is in training mode
cfg.IS_TRAINING = False

return cfg
Loading