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

Converted MobileNet-SSD Producing Irregular Bounding Boxes #279

Open
AdamWP opened this issue Feb 3, 2019 · 11 comments
Open

Converted MobileNet-SSD Producing Irregular Bounding Boxes #279

AdamWP opened this issue Feb 3, 2019 · 11 comments

Comments

@AdamWP
Copy link

AdamWP commented Feb 3, 2019

I have retrained MobileNetSSD V1 and V2 models with my own dataset. They both work perfectly with tensorflow and the converted TFLite models work perfect with MLKit. However the same models converted to CoreML are a bit wonky. They both detect objects with correct bounding boxes at first, but then start producing overlapping bounding boxes that are not the correct size. One box is half the height and twice the width that it should be, and the other is twice the height and half the width that it should be. Confidences are .9 to 1

The models were converted as per the instructions found here https://github.com/tf-coreml/tf-coreml/blob/master/examples/ssd_example.ipynb with these settings:

coreml_model = tfcoreml.convert(
tf_model_path=frozen_model_file,
mlmodel_path=coreml_model_file,
input_name_shape_dict=input_tensor_shapes,
image_input_names="Preprocessor/sub:0",
output_feature_names=output_tensor_names,
image_scale=2./255.,
red_bias=-1.0,
green_bias=-1.0,
blue_bias=-1.0
)

Any idea what could be causing this?

@aseemw
Copy link
Collaborator

aseemw commented Feb 3, 2019

Hard to say without more details.
Did you check with a quick python script, when the same bumpy array is fed into the .pb and .mlmodels, do they produce similar numpy arrays as outputs ?

@AdamWP
Copy link
Author

AdamWP commented Feb 5, 2019

The mlmodel won't accept the numpy array. I get NSLocalizedDescription = "The model expects input feature Preprocessor__sub__0 to be an image, but the input is of type 5.";

However if I input an image instead of a numpy array and compare it to the TF outputs I get:
Max relative error on box encoding: 0.000113
Max relative error on scores: 0.000055

@aseemw
Copy link
Collaborator

aseemw commented Feb 5, 2019

The errors seem quite small!

@AdamWP
Copy link
Author

AdamWP commented Feb 5, 2019

I agree. If I hold my phone still the boxes are ok. Unfortunately as soon as the phone is moved the bounding boxes are terrible. It works ok with the ssd_mobilenet_v2_coco model, but terrible with my re-trained model. I'm guessing there may be some kind of loss during the conversion. I'm going to add some more epochs to the training and see if it improves things.

@mabrowning
Copy link

We have the exact same issue after using (slightly modified) https://github.com/vonholst/SSDMobileNet_CoreML. I suspect that the anchors hardcoded here don't all quite match up with what the network was trained on. NMS then fails because the 2 "crossed" boxes don't actually have that much intersection.

As per OP, some image/frames seem to work perfectly, but other slightly offset ones show 2 divergent boxes for each detection.

I'm investigating the process outlined in https://github.com/hollance/coreml-survival-guide/tree/master/MobileNetV2%2BSSDLite https://machinethink.net/blog/mobilenet-ssdlite-coreml/, which moves anchor/box extraction and NMS into CoreML itself.

@lordrebel
Copy link

lordrebel commented Mar 5, 2019

@mabrowning could u pls share the code for how to move anchor/box extraction to CoreML ? I am using a ssd mobilenet V1, is there any difference in anchor/box extraction ?

@mabrowning
Copy link

mabrowning commented Mar 5, 2019 via email

@lordrebel
Copy link

lordrebel commented Mar 15, 2019

Thanks!! @mabrowning , I compared the inference result of tensorflow model with coreml model I has converted, I found that coreml model lost some boxes (about 30% ), did you meet this problem ? @mabrowning @aseemw @miaout17

@mabrowning
Copy link

mabrowning commented Mar 15, 2019 via email

@lordrebel
Copy link

yes ,both iou threshold and confidence thershold are same as TF model. and i compare the model structure using Netron. It looks quite different.@mabrowning

@Mstronach
Copy link
Collaborator

Is there an update on this issue, please let us know if you're still experiencing this! Thanks

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

5 participants