endernewton / tf-faster-rcnn

Tensorflow Faster RCNN for Object Detection
https://arxiv.org/pdf/1702.02138.pdf
MIT License
3.65k stars 1.57k forks source link

how to change the feat_stride? #383

Open ReneWu1117 opened 6 years ago

ReneWu1117 commented 6 years ago

I want to change feat_stride from 16 to 8, so that model can be more sensitive to small object. And I change: self._feat_stride = [16, ] to self._feat_stride = [8, ] But that's not enough, I get error as follow:

2018-09-01 18:46:33.846993: W tensorflow/core/framework/op_kernel.cc:1263] Invalid argument: exceptions.ValueError: cannot reshape array of size 116400 into shape (1,38,49,16)
Traceback (most recent call last):

  File "/home/rene/.local/lib/python2.7/site-packages/tensorflow/python/ops/script_ops.py", line 206, in __call__
    ret = func(*args)

  File "/home/rene/code/tf-faster-rcnn/tools/../lib/layer_utils/anchor_target_layer.py", line 136, in anchor_target_layer
    labels = labels.reshape((1, height, width, A)).transpose(0, 3, 1, 2)

ValueError: cannot reshape array of size 116400 into shape (1,38,49,16)

Traceback (most recent call last):
  File "/home/rene/code/tf-faster-rcnn/tools/trainval_net.py", line 159, in <module>
    max_iters=args.max_iters)
  File "/home/rene/code/tf-faster-rcnn/tools/../lib/model/train_val.py", line 404, in train_net
    sw.train_model(sess, max_iters)
  File "/home/rene/code/tf-faster-rcnn/tools/../lib/model/train_val.py", line 300, in train_model
    self.net.train_step_with_summary(sess, blobs, train_op)
  File "/home/rene/code/tf-faster-rcnn/tools/../lib/nets/network.py", line 686, in train_step_with_summary
    feed_dict=feed_dict)
  File "/home/rene/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 877, in run
    run_metadata_ptr)
  File "/home/rene/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1100, in _run
    feed_dict_tensor, options, run_metadata)
  File "/home/rene/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1272, in _do_run
    run_metadata)
  File "/home/rene/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py", line 1291, in _do_call
    raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [29792] vs. [116400]
     [[Node: resnet_v1_101_3/rois/Mul_2 = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](resnet_v1_101_3/rois/strided_slice_7, resnet_v1_101_3/rois/add)]]

Caused by op u'resnet_v1_101_3/rois/Mul_2', defined at:
  File "/home/rene/code/tf-faster-rcnn/tools/trainval_net.py", line 159, in <module>
    max_iters=args.max_iters)
  File "/home/rene/code/tf-faster-rcnn/tools/../lib/model/train_val.py", line 404, in train_net
    sw.train_model(sess, max_iters)
  File "/home/rene/code/tf-faster-rcnn/tools/../lib/model/train_val.py", line 260, in train_model
    lr, train_op = self.construct_graph(sess)
  File "/home/rene/code/tf-faster-rcnn/tools/../lib/model/train_val.py", line 129, in construct_graph
    anchor_ratios=cfg.ANCHOR_RATIOS)
  File "/home/rene/code/tf-faster-rcnn/tools/../lib/nets/network.py", line 568, in create_architecture
    rois, cls_prob, bbox_pred = self._build_network(training)
  File "/home/rene/code/tf-faster-rcnn/tools/../lib/nets/network.py", line 280, in _build_network
    rois = self._region_proposal(net_conv, is_training, initializer)
  File "/home/rene/code/tf-faster-rcnn/tools/../lib/nets/network.py", line 453, in _region_proposal
    rois, roi_scores = self._proposal_layer(rpn_cls_prob, rpn_bbox_pred, "rois")
  File "/home/rene/code/tf-faster-rcnn/tools/../lib/nets/network.py", line 126, in _proposal_layer
    self._num_anchors
  File "/home/rene/code/tf-faster-rcnn/tools/../lib/layer_utils/proposal_layer.py", line 85, in proposal_layer_tf
    proposals = bbox_transform_inv_tf(anchors, rpn_bbox_pred)
  File "/home/rene/code/tf-faster-rcnn/tools/../lib/model/bbox_transform.py", line 141, in bbox_transform_inv_tf
    pred_ctr_x = tf.add(tf.multiply(dx, widths), ctr_x)
  File "/home/rene/.local/lib/python2.7/site-packages/tensorflow/python/ops/math_ops.py", line 203, in multiply
    return gen_math_ops.mul(x, y, name)
  File "/home/rene/.local/lib/python2.7/site-packages/tensorflow/python/ops/gen_math_ops.py", line 4936, in mul
    "Mul", x=x, y=y, name=name)
  File "/home/rene/.local/lib/python2.7/site-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/home/rene/.local/lib/python2.7/site-packages/tensorflow/python/util/deprecation.py", line 454, in new_func
    return func(*args, **kwargs)
  File "/home/rene/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 3155, in create_op
    op_def=op_def)
  File "/home/rene/.local/lib/python2.7/site-packages/tensorflow/python/framework/ops.py", line 1717, in __init__
    self._traceback = tf_stack.extract_stack()

InvalidArgumentError (see above for traceback): Incompatible shapes: [29792] vs. [116400]
     [[Node: resnet_v1_101_3/rois/Mul_2 = Mul[T=DT_FLOAT, _device="/job:localhost/replica:0/task:0/device:GPU:0"](resnet_v1_101_3/rois/strided_slice_7, resnet_v1_101_3/rois/add)]]

I found the old version had __C.DEDUP_BOXES, but now it seem to be abandoned. So how can I change the the feat_stride now? Any suggestions will be helpful.

rnsandeep commented 6 years ago

You need to also change lib/layer_utils/snippets.py which has functions generate_anchors_pre_tf and generate_anchors_pre which calls the function generate_anchors without the feat_stride argument. Please change that and add feat_stride[0] as the first argument in calling function generate_anchors. Without this the feat_stride defaults to 16. Hope it might work for you now.

ReneWu1117 commented 6 years ago

@rnsandeep Thanks for your advice. I know your mean, the basesize of anchor better match the feat_stride. In fact, I ignored to note that I changed the basesize manually. So there must be other reasons. Thanks anyway!

rnsandeep commented 6 years ago

I have got the same problem, so i am using previous layer features instead of the layer you use generally in image_to_head, making the feature dimension double the size. In that case it's working but i am yet to see the accuracy.

rnsandeep commented 6 years ago

I have change the feature stride to 8 and added an deconv layer after net_conv ( image_to_head) to double the size of feature map. The original feature map was intended only for feature stride of 16. In order to use 8 you have to double the feature map size. Instead of removing the net_conv layer you can add deconv layer to make it work.

ReneWu1117 commented 6 years ago

@rnsandeep Thanks! I use resnet, so I change _build_base function in resnet_v1.py: net = slim.max_pool2d(net, [3, 3], stride=2, padding='VALID', scope='pool1') to net = slim.max_pool2d(net, [3, 3], stride=1, padding='VALID', scope='pool1') which also doubles the feature map size, and the code run without errors. But I am not sure whether or not it is a good solution.

rnsandeep commented 6 years ago

@ReneWu1117 I would suggest to add this after net_conv instead of doing it the build_base function which are mostly initial layers of networks. I have added slim.layers.conv2d_transpose(1024, [4,4], [2,2], scope='deconv') after net_conv and it didn't reduce the accuracy.

zqdeepbluesky commented 5 years ago

@rnsandeep hi, can you show me how to add deconv layer after net_conv? thanks so much.

Hajarat commented 4 years ago

Hey @rnsandeep, I'm trying to run the code on Mammogram images, which require high sensitivity to small objects, which is why I want the stride to be 2 if possible. Does that mean I should add one more layer that 8x the output feature map?