class_head和box_head中有多个滤除层,可实现快速MCDropout

时间:2019-02-08 16:15:21

标签: tensorflow object-detection object-detection-api dropout non-maximum-suppression

我正在使用Tensorflow API的Faster R-CNN Resnet 101。

我为class_head和box_head预测方法添加了多个滤除层,以实现更快的MC滤除采样。例如,如果添加50个滤除层,我希望接收300 * 50个检测。然后,我将计算输出的方差。

下面是我的更改后来自box_head.py的示例。

当我开始评估时,我收到一个错误。我认为该模型无法理解我正在发送一批50个输出,但是由于我使用的是同一张图片,因此将其视为一个输出。问题在于在NMS之前的第二阶段使用锚点对盒子进行解码。

我想知道是否有办法解决这个问题。我真的需要用这样的技巧来加快推理速度。

# New method 
  def multi_dropout(self, features, keep_prob=0.5, is_training=True, n_samples=10):

   output = []

   for dropout_index in range(n_samples):
     output.append(slim.dropout(
         features,
         keep_prob=keep_prob,
         is_training=is_training))
   stacked = tf.stack(output)
   return stacked

 def predict(self, features, num_predictions_per_location=1):
   """Predicts boxes.

   Args:
     features: A float tensor of shape [batch_size, height, width,
       channels] containing features for a batch of images.
     num_predictions_per_location: Int containing number of predictions per
       location.

   Returns:
     box_encodings: A float tensor of shape
       [batch_size, 1, num_classes, code_size] representing the location of the
       objects.

   Raises:
     ValueError: If num_predictions_per_location is not 1.
   """
   if num_predictions_per_location != 1:
     raise ValueError('Only num_predictions_per_location=1 is supported')
   spatial_averaged_roi_pooled_features = tf.reduce_mean(
       features, [1, 2], keep_dims=True, name='AvgPool')
   flattened_roi_pooled_features = slim.flatten(
       spatial_averaged_roi_pooled_features)
   if self._use_dropout:
     # New controls 
     if self._is_training:
         flattened_roi_pooled_features = slim.dropout(
             flattened_roi_pooled_features,
             keep_prob=self._dropout_keep_prob,
             is_training=self._is_training)

      else:
         flattened_roi_pooled_features = self.multi_dropout(features=flattened_roi_pooled_features, keep_prob=self._dropout_keep_prob, is_training=self._is_training, n_samples=50)


   number_of_boxes = 1
   if not self._share_box_across_classes:
     number_of_boxes = self._num_classes

   with slim.arg_scope(self._fc_hyperparams_fn()):
     box_encodings = slim.fully_connected(
         flattened_roi_pooled_features,
         number_of_boxes * self._box_code_size,
         activation_fn=None,
         scope='BoxEncodingPredictor')
   box_encodings = tf.reshape(box_encodings,
                              [-1, 1, number_of_boxes, self._box_code_size])
   return box_encodings

错误:

Traceback (most recent call last):
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1576, in _create_c_op
    c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimensions must be equal, but are 45000 and 900 for 'SecondStagePostprocessor/Decode/mul' (op: 'Mul') with input shapes: [45000], [900].

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./legacy/eval.py", line 141, in <module>
    tf.app.run()
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 125, in run
    _sys.exit(main(argv))
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/python/util/deprecation.py", line 272, in new_func
    return func(*args, **kwargs)
  File "./legacy/eval.py", line 138, in main
    graph_hook_fn=graph_rewriter_fn)
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/models/research/object_detection/legacy/evaluator.py", line 187, in evaluate
    ignore_groundtruth=eval_config.ignore_groundtruth)
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/models/research/object_detection/legacy/evaluator.py", line 79, in _extract_predictions_and_losses
    detections = model.postprocess(prediction_dict, true_image_shapes)
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/models/research/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 1175, in postprocess
    mask_predictions=mask_predictions)
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/models/research/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 1595, in _postprocess_box_classifier
    refined_box_encodings_batch, proposal_boxes)
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/models/research/object_detection/meta_architectures/faster_rcnn_meta_arch.py", line 1665, in _batch_decode_boxes
    tiled_anchors_boxlist)
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/models/research/object_detection/core/box_coder.py", line 86, in decode
    return self._decode(rel_codes, anchors)
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/models/research/object_detection/box_coders/faster_rcnn_box_coder.py", line 111, in _decode
    w = tf.exp(tw) * wa
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/python/ops/math_ops.py", line 850, in binary_op_wrapper
    return func(x, y, name=name)
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/python/ops/math_ops.py", line 1094, in _mul_dispatch
    return gen_math_ops.mul(x, y, name=name)
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/python/ops/gen_math_ops.py", line 4936, in mul
    "Mul", x=x, y=y, name=name)
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/python/util/deprecation.py", line 454, in new_func
    return func(*args, **kwargs)
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 3155, in create_op
    op_def=op_def)
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1731, in __init__
    control_input_ops)
  File "/home/a21/.virtualenvs/tf/lib/python3.5/site-packages/tensorflow/python/framework/ops.py", line 1579, in _create_c_op
    raise ValueError(str(e))
ValueError: Dimensions must be equal, but are 45000 and 900 for 'SecondStagePostprocessor/Decode/mul' (op: 'Mul') with input shapes: [45000], [900].

我也尝试将输出一一发送到盒式解码器,但再次收到错误 ValueError: Shape must be rank 3 but is rank 4 for 'BatchMultiClassNonMaxSuppression/map/while/Slice' (op: 'Slice') with input shapes: [1,1,1,4], [3], [3].

0 个答案:

没有答案