Skip to content

Commit 8d448ba

Browse files
committed
Refactor datasets package (empty __init__.py)
1 parent 95918a5 commit 8d448ba

File tree

7 files changed

+24
-55
lines changed

7 files changed

+24
-55
lines changed

lib/datasets/__init__.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,3 @@
44
# Licensed under The MIT License [see LICENSE for details]
55
# Written by Ross Girshick
66
# --------------------------------------------------------
7-
8-
from .imdb import imdb
9-
from .pascal_voc import pascal_voc
10-
from . import factory
11-
12-
import os.path as osp
13-
ROOT_DIR = osp.join(osp.dirname(__file__), '..', '..')
14-
15-
# We assume your matlab binary is in your path and called `matlab'.
16-
# If either is not true, just add it to your path and alias it as matlab, or
17-
# you could change this file.
18-
MATLAB = 'matlab'
19-
20-
# http://stackoverflow.com/questions/377017/test-if-executable-exists-in-python
21-
def _which(program):
22-
import os
23-
def is_exe(fpath):
24-
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
25-
26-
fpath, fname = os.path.split(program)
27-
if fpath:
28-
if is_exe(program):
29-
return program
30-
else:
31-
for path in os.environ["PATH"].split(os.pathsep):
32-
path = path.strip('"')
33-
exe_file = os.path.join(path, program)
34-
if is_exe(exe_file):
35-
return exe_file
36-
37-
return None
38-
39-
if _which(MATLAB) is None:
40-
msg = ("MATLAB command '{}' not found. "
41-
"Please add '{}' to your PATH.").format(MATLAB, MATLAB)
42-
raise EnvironmentError(msg)

lib/datasets/factory.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
__sets = {}
1111

12-
import datasets.pascal_voc
12+
from datasets.pascal_voc import pascal_voc
1313
import numpy as np
1414

1515
def _selective_search_IJCV_top_k(split, year, top_k):
1616
"""Return an imdb that uses the top k proposals from the selective search
1717
IJCV code.
1818
"""
19-
imdb = datasets.pascal_voc(split, year)
19+
imdb = pascal_voc(split, year)
2020
imdb.roidb_handler = imdb.selective_search_IJCV_roidb
2121
imdb.config['top_k'] = top_k
2222
return imdb
@@ -26,7 +26,7 @@ def _selective_search_IJCV_top_k(split, year, top_k):
2626
for split in ['train', 'val', 'trainval', 'test']:
2727
name = 'voc_{}_{}'.format(year, split)
2828
__sets[name] = (lambda split=split, year=year:
29-
datasets.pascal_voc(split, year))
29+
pascal_voc(split, year))
3030

3131
# Set up voc_<year>_<split>_top_<k> using selective search "quality" mode
3232
# but only returning the first k boxes

lib/datasets/imdb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from utils.cython_bbox import bbox_overlaps
1212
import numpy as np
1313
import scipy.sparse
14-
import datasets
14+
from fast_rcnn.config import cfg
1515

1616
class imdb(object):
1717
"""Image database."""
@@ -69,7 +69,7 @@ def roidb(self):
6969

7070
@property
7171
def cache_path(self):
72-
cache_path = osp.abspath(osp.join(datasets.ROOT_DIR, 'data', 'cache'))
72+
cache_path = osp.abspath(osp.join(cfg.DATA_DIR, 'cache'))
7373
if not os.path.exists(cache_path):
7474
os.makedirs(cache_path)
7575
return cache_path

lib/datasets/pascal_voc.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,20 @@
55
# Written by Ross Girshick
66
# --------------------------------------------------------
77

8-
import datasets
9-
import datasets.pascal_voc
108
import os
11-
import datasets.imdb
9+
from datasets.imdb import imdb
1210
import xml.dom.minidom as minidom
1311
import numpy as np
1412
import scipy.sparse
1513
import scipy.io as sio
1614
import utils.cython_bbox
1715
import cPickle
1816
import subprocess
17+
from fast_rcnn.config import cfg
1918

20-
class pascal_voc(datasets.imdb):
19+
class pascal_voc(imdb):
2120
def __init__(self, image_set, year, devkit_path=None):
22-
datasets.imdb.__init__(self, 'voc_' + year + '_' + image_set)
21+
imdb.__init__(self, 'voc_' + year + '_' + image_set)
2322
self._year = year
2423
self._image_set = image_set
2524
self._devkit_path = self._get_default_path() if devkit_path is None \
@@ -83,7 +82,7 @@ def _get_default_path(self):
8382
"""
8483
Return the default path where PASCAL VOC is expected to be installed.
8584
"""
86-
return os.path.join(datasets.ROOT_DIR, 'data', 'VOCdevkit' + self._year)
85+
return os.path.join(cfg.DATA_DIR, 'VOCdevkit' + self._year)
8786

8887
def gt_roidb(self):
8988
"""
@@ -125,7 +124,7 @@ def selective_search_roidb(self):
125124
if int(self._year) == 2007 or self._image_set != 'test':
126125
gt_roidb = self.gt_roidb()
127126
ss_roidb = self._load_selective_search_roidb(gt_roidb)
128-
roidb = datasets.imdb.merge_roidbs(gt_roidb, ss_roidb)
127+
roidb = imdb.merge_roidbs(gt_roidb, ss_roidb)
129128
else:
130129
roidb = self._load_selective_search_roidb(None)
131130
with open(cache_file, 'wb') as fid:
@@ -138,7 +137,7 @@ def rpn_roidb(self):
138137
if int(self._year) == 2007 or self._image_set != 'test':
139138
gt_roidb = self.gt_roidb()
140139
rpn_roidb = self._load_rpn_roidb(gt_roidb)
141-
roidb = datasets.imdb.merge_roidbs(gt_roidb, rpn_roidb)
140+
roidb = imdb.merge_roidbs(gt_roidb, rpn_roidb)
142141
else:
143142
roidb = self._load_rpn_roidb(None)
144143

@@ -154,7 +153,7 @@ def _load_rpn_roidb(self, gt_roidb):
154153
return self.create_roidb_from_box_list(box_list, gt_roidb)
155154

156155
def _load_selective_search_roidb(self, gt_roidb):
157-
filename = os.path.abspath(os.path.join(self.cache_path, '..',
156+
filename = os.path.abspath(os.path.join(cfg.DATA_DIR,
158157
'selective_search_data',
159158
self.name + '.mat'))
160159
assert os.path.exists(filename), \
@@ -245,10 +244,10 @@ def _write_voc_results_file(self, all_boxes):
245244
def _do_matlab_eval(self, comp_id, output_dir='output'):
246245
rm_results = self.config['cleanup']
247246

248-
path = os.path.join(os.path.dirname(__file__),
247+
path = os.path.join(cfg.ROOT_DIR, 'lib', 'datasets',
249248
'VOCdevkit-matlab-wrapper')
250249
cmd = 'cd {} && '.format(path)
251-
cmd += '{:s} -nodisplay -nodesktop '.format(datasets.MATLAB)
250+
cmd += '{:s} -nodisplay -nodesktop '.format(cfg.MATLAB)
252251
cmd += '-r "dbstop if error; '
253252
cmd += 'voc_eval(\'{:s}\',\'{:s}\',\'{:s}\',\'{:s}\',{:d}); quit;"' \
254253
.format(self._devkit_path, comp_id,
@@ -269,6 +268,7 @@ def competition_mode(self, on):
269268
self.config['cleanup'] = True
270269

271270
if __name__ == '__main__':
272-
d = datasets.pascal_voc('trainval', '2007')
271+
from datasets.pascal_voc import pascal_voc
272+
d = pascal_voc('trainval', '2007')
273273
res = d.roidb
274274
from IPython import embed; embed()

lib/fast_rcnn/config.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@
189189
# Root directory of project
190190
__C.ROOT_DIR = osp.abspath(osp.join(osp.dirname(__file__), '..', '..'))
191191

192+
# Data directory
193+
__C.DATA_DIR = osp.abspath(osp.join(__C.ROOT_DIR, 'data'))
194+
195+
# Name (or path to) the matlab executable
196+
__C.MATLAB = 'matlab'
197+
192198
# Place outputs under an experiments directory
193199
__C.EXP_DIR = 'default'
194200

tools/train_faster_rcnn_alt_opt.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
from fast_rcnn.config import cfg, cfg_from_file, cfg_from_list, get_output_dir
1919
from datasets.factory import get_imdb
2020
from rpn.generate import imdb_proposals
21-
import datasets.imdb
2221
import argparse
2322
import pprint
2423
import numpy as np

tools/train_net.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def get_roidb(imdb_name):
7171
if len(roidbs) > 1:
7272
for r in roidbs[1:]:
7373
roidb.extend(r)
74-
imdb = datasets.imdb(imdb_names)
74+
imdb = datasets.imdb.imdb(imdb_names)
7575
else:
7676
imdb = get_imdb(imdb_names)
7777
return imdb, roidb

0 commit comments

Comments
 (0)