Multiple dropout layers in class_head and box_head for fast MCDropout
What is the top-level directory of the model you are using: https://github.com/tensorflow/models/blob/master/research/object_detection/meta_architectures/faster_rcnn_meta_arch.py Have I written custom code: Yes OS Platform and Distribution: ubuntu 16.04 LTS TensorFlow installed from: pip3 TensorFlow version: 1.10 Bazel version: N/A CUDA/cuDNN version: CUDA 9.0/cuDNN 7.1 GPU model and memory: TITAN X (Pascal)/PCIe/SSE2, 32 GiB Exact command to reproduce: N/A
I am using Faster R-CNN Resnet 101.
I added multiple dropout layers to class_head and box_head prediction methods for a faster MC Dropout sampling during evaluation. In model's configuration file I set use_dropout to true, and keep rate to 0.5. For instance, I want to receive 300*50 detections if I add 50 dropout layers. Then I will compute variance of that output.
An example from box_head.py after my change is below. I added a new method called multi_dropout in box_head.py, and I call this new method in the predict method of the same script.
When I start evaluation with ./legacy/eval.py, I receive an error. I think the model can't understand that I am sending a batch of 50 outputs, but considers it as one output since I am using the same image. The problem is with decoding the boxes using anchors in the second stage before NMS.
I wonder if there is a way to overcome this problem with dimensions. I really need to speed up the inference with a trick like this one.
box_head.py
# 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
ERROR:
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].
NOTE: I also tried sending the output to box decoder one by one, but again received an error
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].
Thank you for your post. We noticed you have not filled out the following field in the issue template. Could you update them if they are relevant in your case, or leave them as N/A? Thanks. What is the top-level directory of the model you are using Have I written custom code OS Platform and Distribution TensorFlow installed from TensorFlow version Bazel version CUDA/cuDNN version GPU model and memory Exact command to reproduce
I edited my issue.
Hi There, We are checking to see if you still need help on this, as this seems to be an old issue. Please update this issue with the latest information, code snippet to reproduce your issue and error you are seeing. If we don't hear from you in the next 7 days, this issue will be closed automatically. If you don't need help on this issue any more, please consider closing this.