Skip to content

Commit

Permalink
reorg code
Browse files Browse the repository at this point in the history
  • Loading branch information
rbgirshick committed Apr 22, 2015
1 parent caa4a46 commit a8a0f55
Show file tree
Hide file tree
Showing 28 changed files with 106 additions and 69 deletions.
File renamed without changes.
42 changes: 42 additions & 0 deletions lib/datasets/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------

from .imdb import imdb
from .pascal_voc import pascal_voc
from . import factory

import os.path as osp
ROOT_DIR = osp.join(osp.dirname(__file__), '..', '..')

# We assume your matlab binary is in your path and called `matlab'.
# If either is not true, just add it to your path and alias it as matlab, or
# you could change this file.
MATLAB = 'matlab'

# http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
def _which(program):
import os
def is_exe(fpath):
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)

fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
path = path.strip('"')
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file

return None

if _which(MATLAB) is None:
msg = ("MATLAB command '{}' not found. "
"Please add '{}' to your PATH.").format(MATLAB, MATLAB)
raise EnvironmentError(msg)
File renamed without changes.
4 changes: 2 additions & 2 deletions src/datasets/imdb.py → lib/datasets/imdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import utils.cython_bbox
import numpy as np
import scipy.sparse
from fast_rcnn_config import cfg
import datasets

class imdb(object):
def __init__(self, name):
Expand Down Expand Up @@ -62,7 +62,7 @@ def roidb(self):

@property
def cache_path(self):
return os.path.join(cfg.ROOT_DIR, 'data', 'cache')
return os.path.join(datasets.ROOT_DIR, 'data', 'cache')

@property
def num_images(self):
Expand Down
6 changes: 3 additions & 3 deletions src/datasets/pascal_voc.py → lib/datasets/pascal_voc.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# Written by Ross Girshick
# --------------------------------------------------------

import datasets
import datasets.pascal_voc
import os
import datasets.imdb
Expand All @@ -15,7 +16,6 @@
import utils.cython_bbox
import cPickle
import subprocess
from fast_rcnn_config import cfg

class pascal_voc(datasets.imdb):
def __init__(self, image_set, year, devkit_path=None):
Expand Down Expand Up @@ -81,7 +81,7 @@ def _get_default_path(self):
"""
Return the default path where PASCAL VOC is expected to be installed.
"""
return os.path.join(cfg.ROOT_DIR, 'data', 'VOCdevkit' + self._year)
return os.path.join(datasets.ROOT_DIR, 'data', 'VOCdevkit' + self._year)

def gt_roidb(self):
"""
Expand Down Expand Up @@ -261,7 +261,7 @@ def _do_matlab_eval(self, comp_id, output_dir='output'):
path = os.path.join(os.path.dirname(__file__),
'VOCdevkit-matlab-wrapper')
cmd = 'cd {} && '.format(path)
cmd += '{:s} -nodisplay -nodesktop '.format(cfg.MATLAB)
cmd += '{:s} -nodisplay -nodesktop '.format(datasets.MATLAB)
cmd += '-r "dbstop if error; '
cmd += 'voc_eval(\'{:s}\',\'{:s}\',\'{:s}\',\'{:s}\',{:d}); quit;"' \
.format(self._devkit_path, comp_id,
Expand Down
5 changes: 3 additions & 2 deletions src/datasets/__init__.py → lib/fast_rcnn/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
# Written by Ross Girshick
# --------------------------------------------------------

from .imdb import imdb
from .pascal_voc import pascal_voc
from . import config
from . import train
from . import test
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# --------------------------------------------------------

import numpy as np
from fast_rcnn_config import cfg
from fast_rcnn.config import cfg
import utils.cython_bbox

def _compute_targets(rois, overlaps, labels):
Expand Down
12 changes: 1 addition & 11 deletions src/fast_rcnn_config.py → lib/fast_rcnn/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,10 @@

import os
import os.path as osp
import sys
import numpy as np
# `pip install easydict` if you don't have it
from easydict import EasyDict as edict

# Add caffe to PYTHONPATH
caffe_path = osp.abspath(osp.join(osp.dirname(__file__), '..',
'caffe-fast-rcnn', 'python'))
sys.path.insert(0, caffe_path)

__C = edict()
# Consumers can get config by:
# from fast_rcnn_config import cfg
Expand Down Expand Up @@ -126,15 +120,11 @@
__C.EPS = 1e-14

# Root directory of project
__C.ROOT_DIR = osp.abspath(osp.join(osp.dirname(__file__), '..'))
__C.ROOT_DIR = osp.join(osp.dirname(__file__), '..', '..')

# Place outputs under an experiments directory
__C.EXP_DIR = 'default'

# The shell command to start matlab
# If `matlab` is in your path, then this default should work
__C.MATLAB = 'matlab'

def get_output_path(imdb, net):
path = os.path.join(__C.ROOT_DIR, 'output', __C.EXP_DIR, imdb.name)
if net is None:
Expand Down
2 changes: 1 addition & 1 deletion src/finetuning.py → lib/fast_rcnn/finetuning.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import numpy as np
import cv2
import matplotlib.pyplot as plt
from fast_rcnn_config import cfg
from fast_rcnn.config import cfg
import utils.blob

def get_minibatch(roidb):
Expand Down
2 changes: 1 addition & 1 deletion src/fast_rcnn_test.py → lib/fast_rcnn/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Written by Ross Girshick
# --------------------------------------------------------

from fast_rcnn_config import cfg, get_output_path
from fast_rcnn.config import cfg, get_output_path
import argparse
from utils.timer import Timer
import numpy as np
Expand Down
2 changes: 1 addition & 1 deletion src/fast_rcnn_train.py → lib/fast_rcnn/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# Written by Ross Girshick
# --------------------------------------------------------

from fast_rcnn_config import cfg, get_output_path
from fast_rcnn.config import cfg, get_output_path
import numpy as np
import cv2
import caffe
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions tools/_init_paths.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------

import os.path as osp
import sys

this_dir = osp.dirname(__file__)

# Add caffe to PYTHONPATH
caffe_path = osp.join(this_dir, '..', 'caffe-fast-rcnn', 'python')

if caffe_path not in sys.path:
sys.path.insert(0, caffe_path)

lib_path = osp.join(this_dir, '..', 'lib')

if lib_path not in sys.path:
sys.path.insert(0, lib_path)
10 changes: 3 additions & 7 deletions tools/compress_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,17 @@
# Written by Ross Girshick
# --------------------------------------------------------

import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__),
'..', 'src')))

import fast_rcnn_config
import _init_paths
import caffe
import argparse
import numpy as np
import os, sys

def parse_args():
"""
Parse input arguments
"""
parser = argparse.ArgumentParser(description='Compress a fast R-CNN net')
parser = argparse.ArgumentParser(description='Compress a Fast R-CNN network')
parser.add_argument('--def', dest='prototxt',
help='prototxt file defining the uncompressed network',
default=None, type=str)
Expand Down
13 changes: 5 additions & 8 deletions tools/extra/reval.py → tools/reval.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
#!/usr/bin/env python

import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__),
'..', 'src')))

import _init_paths
import fast_rcnn as frc
from fast_rcnn.config import cfg
from datasets.factory import get_imdb
import cPickle
import fast_rcnn_test
from fast_rcnn_config import cfg
import os, sys

def main(imdb_name, output_dir):
imdb = get_imdb(imdb_name)
Expand All @@ -18,7 +15,7 @@ def main(imdb_name, output_dir):
dets = cPickle.load(f)

print 'Applying NMS to all detections'
nms_dets = fast_rcnn_test.apply_nms(dets, cfg.TEST.NMS)
nms_dets = frc.test.apply_nms(dets, cfg.TEST.NMS)

print 'Evaluating detections'
imdb.evaluate_detections(nms_dets, output_dir)
Expand Down
18 changes: 7 additions & 11 deletions tools/test_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,20 @@
# Written by Ross Girshick
# --------------------------------------------------------

import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__),
'..', 'src')))

from fast_rcnn_config import cfg, cfg_from_file
import fast_rcnn_test
import _init_paths
import fast_rcnn as frc
from fast_rcnn.config import cfg, cfg_from_file
from datasets.factory import get_imdb
import caffe
import argparse
import pprint
import time
import time, os, sys

def parse_args():
"""
Parse input arguments
"""
parser = argparse.ArgumentParser(description='Test a fast R-CNN')
parser = argparse.ArgumentParser(description='Test a Fast R-CNN network')
parser.add_argument('--gpu', dest='gpu_id', help='GPU id to use',
default=0, type=int)
parser.add_argument('--def', dest='prototxt',
Expand Down Expand Up @@ -61,7 +57,7 @@ def parse_args():
if args.cfg_file is not None:
cfg_from_file(args.cfg_file)

print('Using fast_rcnn_config:')
print('Using config:')
pprint.pprint(cfg)

while not os.path.exists(args.caffemodel) and args.wait:
Expand All @@ -80,4 +76,4 @@ def parse_args():
if 'cleanup' in imdb.config:
imdb.config['cleanup'] = False

fast_rcnn_test.test_net(net, imdb)
frc.test.test_net(net, imdb)
21 changes: 9 additions & 12 deletions tools/train_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,21 @@
# Written by Ross Girshick
# --------------------------------------------------------

import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__),
'..', 'src')))

from fast_rcnn_config import cfg, cfg_from_file
import fast_rcnn_train
import _init_paths
import fast_rcnn as frc
from fast_rcnn.config import cfg, cfg_from_file
from datasets.factory import get_imdb
import caffe
import argparse
import pprint
import numpy as np
import sys

def parse_args():
"""
Parse input arguments
"""
parser = argparse.ArgumentParser(description='Train a fast R-CNN')
parser = argparse.ArgumentParser(description='Train a Fast R-CNN network')
parser.add_argument('--gpu', dest='gpu_id', help='GPU device id to use [0]',
default=0, type=int)
parser.add_argument('--solver', dest='solver', help='solver prototxt',
Expand Down Expand Up @@ -57,7 +54,7 @@ def parse_args():
if args.cfg_file is not None:
cfg_from_file(args.cfg_file)

print('Using fast_rcnn_config:')
print('Using config:')
pprint.pprint(cfg)

# fix the random seed for reproducibility
Expand All @@ -71,6 +68,6 @@ def parse_args():
imdb_train = get_imdb(args.imdb_name)
print 'Loaded dataset `{:s}` for training'.format(imdb_train.name)

fast_rcnn_train.train_net(args.solver, imdb_train,
pretrained_model=args.pretrained_model,
max_iters=args.max_iters)
frc.train.train_net(args.solver, imdb_train,
pretrained_model=args.pretrained_model,
max_iters=args.max_iters)
14 changes: 5 additions & 9 deletions tools/extra/train_svms.py → tools/train_svms.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,18 @@
# Written by Ross Girshick
# --------------------------------------------------------

import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__),
'..', '..', 'src')))

from fast_rcnn_config import cfg, cfg_from_file
from fast_rcnn_test import im_detect
import fast_rcnn_train
import _init_paths
from fast_rcnn.config import cfg, cfg_from_file
from datasets.factory import get_imdb
from fast_rcnn.test import im_detect
from utils.timer import Timer
import caffe
import argparse
import pprint
import numpy as np
import cv2
from sklearn import svm
import os, sys

class SVMTrainer(object):
def __init__(self, net, imdb):
Expand Down Expand Up @@ -280,7 +276,7 @@ def parse_args():
if args.cfg_file is not None:
cfg_from_file(args.cfg_file)

print('Using fast_rcnn_config:')
print('Using config:')
pprint.pprint(cfg)

# fix the random seed for reproducibility
Expand Down

0 comments on commit a8a0f55

Please sign in to comment.