Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Another Anchor Box Question #439

Closed
suchiek opened this issue Apr 29, 2018 · 10 comments
Closed

Another Anchor Box Question #439

suchiek opened this issue Apr 29, 2018 · 10 comments

Comments

@suchiek
Copy link

suchiek commented Apr 29, 2018

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)])
  1. By how to modify anchor box? #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
)

  1. 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.

@suchiek
Copy link
Author

suchiek commented May 2, 2018

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 suchiek closed this as completed May 2, 2018
@elinevandeven
Copy link

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

@de-vri-es
Copy link
Contributor

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

@ycui123
Copy link

ycui123 commented Aug 8, 2018

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

@shivareddy37
Copy link

shivareddy37 commented Aug 21, 2018

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

@ycui123
Copy link

ycui123 commented Aug 22, 2018

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.

@shivareddy37
Copy link

shivareddy37 commented Aug 22, 2018

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

@ycui123
Copy link

ycui123 commented Aug 23, 2018

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.

@IntelligentIndia7
Copy link

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?

@teramike
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants