config.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. """Fast R-CNN config system.
  2. This file specifies default config options for Fast R-CNN. You should not
  3. change values in this file. Instead, you should write a config file (in yaml)
  4. and use cfg_from_file(yaml_file) to load it and override the default options.
  5. Most tools in $ROOT/tools take a --cfg option to specify an override file.
  6. - See tools/{train,test}_net.py for example code that uses cfg_from_file()
  7. - See experiments/cfgs/*.yml for example YAML config override files
  8. """
  9. import os
  10. import os.path as osp
  11. import numpy as np
  12. # `pip install easydict` if you don't have it
  13. from easydict import EasyDict as edict
  14. __C = edict()
  15. # Consumers can get config by:
  16. # from fast_rcnn_config import cfg
  17. cfg = __C
  18. #
  19. # Training options
  20. #
  21. __C.TRAIN = edict()
  22. # Scales to use during training (can list multiple scales)
  23. # Each scale is the pixel size of an image's shortest side
  24. __C.TRAIN.SCALES = (600,)
  25. # Max pixel size of the longest side of a scaled input image
  26. __C.TRAIN.MAX_SIZE = 1000
  27. # Images to use per minibatch
  28. __C.TRAIN.IMS_PER_BATCH = 1
  29. # Minibatch size (number of regions of interest [ROIs])
  30. __C.TRAIN.BATCH_SIZE = 128
  31. # Fraction of minibatch that is labeled foreground (i.e. class > 0)
  32. __C.TRAIN.FG_FRACTION = 0.25
  33. # Overlap threshold for a ROI to be considered foreground (if >= FG_THRESH)
  34. __C.TRAIN.FG_THRESH = 0.5
  35. # Overlap threshold for a ROI to be considered background (class = 0 if
  36. # overlap in [LO, HI))
  37. __C.TRAIN.BG_THRESH_HI = 0.5
  38. __C.TRAIN.BG_THRESH_LO = 0.0
  39. # Use horizontally-flipped images during training?
  40. __C.TRAIN.USE_FLIPPED = True
  41. # Train bounding-box regressors
  42. __C.TRAIN.BBOX_REG = True
  43. # Overlap required between a ROI and ground-truth box in order for that ROI to
  44. # be used as a bounding-box regression training example
  45. __C.TRAIN.BBOX_THRESH = 0.5
  46. # Iterations between snapshots
  47. __C.TRAIN.SNAPSHOT_ITERS = 1000
  48. # solver.prototxt specifies the snapshot path prefix, this adds an optional
  49. # infix to yield the path: <prefix>[_<infix>]_iters_XYZ.caffemodel
  50. __C.TRAIN.SNAPSHOT_INFIX = ''
  51. # Use a prefetch thread in roi_data_layer.layer
  52. # So far I haven't found this useful; likely more engineering work is required
  53. __C.TRAIN.USE_PREFETCH = False
  54. # Normalize the targets (subtract empirical mean, divide by empirical stddev)
  55. __C.TRAIN.BBOX_NORMALIZE_TARGETS = True
  56. # Deprecated (inside weights)
  57. __C.TRAIN.BBOX_INSIDE_WEIGHTS = (1.0, 1.0, 1.0, 1.0)
  58. # Normalize the targets using "precomputed" (or made up) means and stdevs
  59. # (BBOX_NORMALIZE_TARGETS must also be True)
  60. __C.TRAIN.BBOX_NORMALIZE_TARGETS_PRECOMPUTED = True
  61. __C.TRAIN.BBOX_NORMALIZE_MEANS = (0.0, 0.0, 0.0, 0.0)
  62. __C.TRAIN.BBOX_NORMALIZE_STDS = (0.1, 0.1, 0.2, 0.2)
  63. # Train using these proposals
  64. __C.TRAIN.PROPOSAL_METHOD = 'gt'
  65. # Make minibatches from images that have similar aspect ratios (i.e. both
  66. # tall and thin or both short and wide) in order to avoid wasting computation
  67. # on zero-padding.
  68. __C.TRAIN.ASPECT_GROUPING = True
  69. # Use RPN to detect objects
  70. __C.TRAIN.HAS_RPN = True
  71. # IOU >= thresh: positive example
  72. __C.TRAIN.RPN_POSITIVE_OVERLAP = 0.7
  73. # IOU < thresh: negative example
  74. __C.TRAIN.RPN_NEGATIVE_OVERLAP = 0.3
  75. # If an anchor statisfied by positive and negative conditions set to negative
  76. __C.TRAIN.RPN_CLOBBER_POSITIVES = False
  77. # Max number of foreground examples
  78. __C.TRAIN.RPN_FG_FRACTION = 0.5
  79. # Total number of examples
  80. __C.TRAIN.RPN_BATCHSIZE = 256
  81. # NMS threshold used on RPN proposals
  82. __C.TRAIN.RPN_NMS_THRESH = 0.7
  83. # Number of top scoring boxes to keep before apply NMS to RPN proposals
  84. __C.TRAIN.RPN_PRE_NMS_TOP_N = 12000
  85. # Number of top scoring boxes to keep after applying NMS to RPN proposals
  86. __C.TRAIN.RPN_POST_NMS_TOP_N = 2000
  87. # Proposal height and width both need to be greater than RPN_MIN_SIZE (at orig image scale)
  88. __C.TRAIN.RPN_MIN_SIZE = 16
  89. # Deprecated (outside weights)
  90. __C.TRAIN.RPN_BBOX_INSIDE_WEIGHTS = (1.0, 1.0, 1.0, 1.0)
  91. # Give the positive RPN examples weight of p * 1 / {num positives}
  92. # and give negatives a weight of (1 - p)
  93. # Set to -1.0 to use uniform example weighting
  94. __C.TRAIN.RPN_POSITIVE_WEIGHT = -1.0
  95. #
  96. # Testing options
  97. #
  98. __C.TEST = edict()
  99. # Scales to use during testing (can list multiple scales)
  100. # Each scale is the pixel size of an image's shortest side
  101. __C.TEST.SCALES = (600,)
  102. # Max pixel size of the longest side of a scaled input image
  103. __C.TEST.MAX_SIZE = 1000
  104. # Overlap threshold used for non-maximum suppression (suppress boxes with
  105. # IoU >= this threshold)
  106. __C.TEST.NMS = 0.0
  107. # Experimental: treat the (K+1) units in the cls_score layer as linear
  108. # predictors (trained, eg, with one-vs-rest SVMs).
  109. __C.TEST.SVM = False
  110. # Test using bounding-box regressors
  111. __C.TEST.BBOX_REG = True
  112. # Propose boxes
  113. __C.TEST.HAS_RPN = True
  114. # Test using these proposals
  115. __C.TEST.PROPOSAL_METHOD = 'selective_search'
  116. ## NMS threshold used on RPN proposals
  117. __C.TEST.RPN_NMS_THRESH = 0.7
  118. ## Number of top scoring boxes to keep before apply NMS to RPN proposals
  119. __C.TEST.RPN_PRE_NMS_TOP_N = 6000
  120. ## Number of top scoring boxes to keep after applying NMS to RPN proposals
  121. __C.TEST.RPN_POST_NMS_TOP_N = 300
  122. # Proposal height and width both need to be greater than RPN_MIN_SIZE (at orig image scale)
  123. __C.TEST.RPN_MIN_SIZE = 16
  124. #
  125. # MISC
  126. #
  127. # The mapping from image coordinates to feature map coordinates might cause
  128. # some boxes that are distinct in image space to become identical in feature
  129. # coordinates. If DEDUP_BOXES > 0, then DEDUP_BOXES is used as the scale factor
  130. # for identifying duplicate boxes.
  131. # 1/16 is correct for {Alex,Caffe}Net, VGG_CNN_M_1024, and VGG16
  132. __C.DEDUP_BOXES = 1./16.
  133. # Pixel mean values (BGR order) as a (1, 1, 3) array
  134. # We use the same pixel mean for all networks even though it's not exactly what
  135. # they were trained with
  136. __C.PIXEL_MEANS = np.array([[[102.9801, 115.9465, 122.7717]]])
  137. # For reproducibility
  138. __C.RNG_SEED = 3
  139. # A small number that's used many times
  140. __C.EPS = 1e-14
  141. # Root directory of project
  142. __C.ROOT_DIR = osp.abspath(osp.join(osp.dirname(__file__), '..', '..', '..'))
  143. # Data directory
  144. __C.DATA_DIR = osp.abspath(osp.join(__C.ROOT_DIR, 'data'))
  145. # Prototxt directory
  146. __C.PROTOTXT_DIR = osp.abspath(osp.join(__C.ROOT_DIR, 'prototxt'))
  147. # Name (or path to) the matlab executable
  148. __C.MATLAB = 'matlab'
  149. # Place outputs under an experiments directory
  150. __C.EXP_DIR = 'faster_rcnn_end2end'
  151. # Use GPU implementation of non-maximum suppression
  152. __C.USE_GPU_NMS = False
  153. # Default GPU device id
  154. __C.GPU_ID = 0
  155. def get_output_dir(imdb, net=None):
  156. """Return the directory where experimental artifacts are placed.
  157. If the directory does not exist, it is created.
  158. A canonical path is built using the name from an imdb and a network
  159. (if not None).
  160. """
  161. outdir = osp.abspath(osp.join(__C.ROOT_DIR, 'output'))
  162. if net is not None:
  163. outdir = osp.join(outdir, net.name)
  164. if not os.path.exists(outdir):
  165. os.makedirs(outdir)
  166. return outdir
  167. def _merge_a_into_b(a, b):
  168. """Merge config dictionary a into config dictionary b, clobbering the
  169. options in b whenever they are also specified in a.
  170. """
  171. if type(a) is not edict:
  172. return
  173. for k, v in a.iteritems():
  174. # a must specify keys that are in b
  175. if not b.has_key(k):
  176. raise KeyError('{} is not a valid config key'.format(k))
  177. # the types must match, too
  178. old_type = type(b[k])
  179. if old_type is not type(v):
  180. if isinstance(b[k], np.ndarray):
  181. v = np.array(v, dtype=b[k].dtype)
  182. else:
  183. raise ValueError(('Type mismatch ({} vs. {}) '
  184. 'for config key: {}').format(type(b[k]),
  185. type(v), k))
  186. # recursively merge dicts
  187. if type(v) is edict:
  188. try:
  189. _merge_a_into_b(a[k], b[k])
  190. except:
  191. print('Error under config key: {}'.format(k))
  192. raise
  193. else:
  194. b[k] = v
  195. def cfg_from_file(filename):
  196. if not os.path.exists(filename):
  197. return
  198. """Load a config file and merge it into the default options."""
  199. import yaml
  200. with open(filename, 'r') as f:
  201. yaml_cfg = edict(yaml.load(f))
  202. _merge_a_into_b(yaml_cfg, __C)
  203. def cfg_from_list(cfg_list):
  204. """Set config keys via list (e.g., from command line)."""
  205. from ast import literal_eval
  206. assert len(cfg_list) % 2 == 0
  207. for k, v in zip(cfg_list[0::2], cfg_list[1::2]):
  208. key_list = k.split('.')
  209. d = __C
  210. for subkey in key_list[:-1]:
  211. assert d.has_key(subkey)
  212. d = d[subkey]
  213. subkey = key_list[-1]
  214. assert d.has_key(subkey)
  215. try:
  216. value = literal_eval(v)
  217. except:
  218. # handle the case when v is a string literal
  219. value = v
  220. assert type(value) == type(d[subkey]), \
  221. 'type {} does not match original type {}'.format(
  222. type(value), type(d[subkey]))
  223. d[subkey] = value