Keras-retinanet: Another Anchor Box Question

Created on 29 Apr 2018  路  10Comments  路  Source: fizyr/keras-retinanet

Hi,

I read through all the questions regarding modifying anchor boxes. I have a long narrow object along with several others that I am trying to detect. So far, the model is doing well on all except the long narrow one. I am trying to modify the anchor boxes to get it to detect this object.

Based on past discussions, it appears that there a few places where modifications need to be made.

1) We need to modify ratios and scales in keras_retinanet/utils/anchor.py

**def generate_anchors(base_size=16, ratios=None, scales=None):

       if ratios is None:
         ratios = np.array([0.5, 1, 2])

      if scales is None:
         scales = np.array([2 ** 0, 2 ** (1.0 / 3.0), 2 ** (2.0 / 3.0)])

2) By #421, change the default anchor parameter values in keras_retinanet/models/retinanet.py to match the ones that were used above:

anchor_parameters = keras_retinanet.models.retinanet.AnchorParameters(
sizes = [32, 64, 128, 256, 512],
strides = [8, 16, 32, 64, 128],
ratios = np.array([0.5, 1, 2], keras.backend.floatx()), # Change this for your ratios
scales = np.array([2 * 0, 2 * (1.0 / 3.0), 2 ** (2.0 / 3.0)], keras.backend.floatx()), # Change this for your scales
)

3) pass the above into the model_with_weights function in keras_retinanet/utils/train.py

model = model_with_weights(backbone_retinanet(num_classes, backbone=backbone, modifier=modifier, anchor_parameters=anchor_parameters), weights=weights, skip_mismatch=True)

After doing the above, I am still getting an error message:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes: [1,120087,4] vs. [1,320232,4]

Please let me know if I missed something.

thanks for all your responses and help.

Most helpful comment

Thought I would post this since the following steps worked for me:

1) In keras_retinanet/utils/anchors.py:
change ratios and scales in ----- def generate anchors() and in def anchors_for_shape()

2) In keras_retinanet/models/retinanet.py:
change ratios and scales in ----- anchor parameters.default
change num_anchors in def retinanet() to the value you get when you multiply your specified
(ratios * scales)

3) pass the above into the model_with_weights function in keras_retinanet/bin/train.py:
model = model_with_weights(backbone_retinanet(num_classes, backbone=backbone, modifier=modifier, anchor_parameters=anchor_parameters), weights=weights, skip_mismatch=True)

After doing all of the above it worked and results improved significantly.

Thanks for your site!

All 10 comments

Thought I would post this since the following steps worked for me:

1) In keras_retinanet/utils/anchors.py:
change ratios and scales in ----- def generate anchors() and in def anchors_for_shape()

2) In keras_retinanet/models/retinanet.py:
change ratios and scales in ----- anchor parameters.default
change num_anchors in def retinanet() to the value you get when you multiply your specified
(ratios * scales)

3) pass the above into the model_with_weights function in keras_retinanet/bin/train.py:
model = model_with_weights(backbone_retinanet(num_classes, backbone=backbone, modifier=modifier, anchor_parameters=anchor_parameters), weights=weights, skip_mismatch=True)

After doing all of the above it worked and results improved significantly.

Thanks for your site!

@suchiek i get the error NameError: name 'backbone' is not defined after doing step 3. How did you resolve this?

@elinevandeven: That was probably fixed yesterday by #479.

@suchiek Hi, did you add more ratios and scales or just change the value?

All the mentioned changes above are for what version of keras_retiananet? @suchiek

Hi @shivareddy37,

For the latest retinanet, I think you dont need to apply step 3, I tried with step 1 and 2, and it worked.

I have another question the scales and ratios are based on your annotations or image size . You you mind sharing the values you used ? @suchiek

Hi @shivareddy37, you are @ the wrong person lol.

Anyways, it's based on the size of my objects. Because some of my objects are very elongated. So I add ratios up to 14:1. But I didn't change scales. And it seems to work.

Hey @suchiek @ycui123 , I am trying to detect objects like floor mat, cable wire which are long and thin. My image size is 960 x 540. How can I calculate ratios and scales for these kind of objects. Can you help me with the same? Can you suggest some values? and any other parameter I should change?

hey @IntelligentIndia7 or anyone who can shine some light! I was wondering, for the latest retinanet version, is there anything else to do to change the anchor ratios more than editing the default anchor params at keras_retinanet/utils/anchors.py ? I was changing the anchor size and ratios and got a tensorflow.python.framework.errors_impl.InvalidArgumentError: Incompatible shapes error when predicting (not during training) . Any help would be appreciated! Changing the anchor sizes and ratios thought the config.ini file option produced the same error message.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lugi7 picture lugi7  路  3Comments

mayur-who picture mayur-who  路  5Comments

sumeetssaurav picture sumeetssaurav  路  4Comments

Doodle1106 picture Doodle1106  路  3Comments

CedarYang picture CedarYang  路  5Comments