mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

SSD support for arbitrary shapes at prediction (without resize)

Open mmmikael opened this issue 9 years ago • 17 comments

The single short detector is trained on fixed resolution images 300x300 or 512x512 but since the network doesn't include flattening or fully connected layers, it should be possible to do inference on arbitrary sizes without rescaling the input.

I was able to do that with the Tensorflow implementation but not with the MXNet one here. It doesn't crash and it looks like it detects the correct objects but the results are off: screen shot 2017-06-02 at 11 07 35 pm

My code change:

--- a/example/ssd/detect/detector.py
+++ b/example/ssd/detect/detector.py
@@ -35,8 +35,10 @@ class Detector(object):
         if symbol is None:
             symbol = load_symbol
         self.mod = mx.mod.Module(symbol, label_names=None, context=ctx)
+
+        data_shape = (720, 1280)
         self.data_shape = data_shape
-        self.mod.bind(data_shapes=[('data', (batch_size, 3, data_shape, data_shape))])
+        self.mod.bind(data_shapes=[('data', (batch_size, 3, data_shape[0], data_shape[1]))])
         self.mod.set_params(args, auxs)
         self.data_shape = data_shape
         self.mean_pixels = mean_pixels

Original image: http://data.1freewallpapers.com/download/cows-on-the-meadow-1280x720.jpg

cc @zhreshold

mmmikael avatar Jun 02 '17 21:06 mmmikael

FYI, this is the result obtained with Tensorflow's implementation: 000000

mmmikael avatar Jun 02 '17 21:06 mmmikael

I would check the scaling of predictions. Looks like a display error

piiswrong avatar Jun 02 '17 22:06 piiswrong

There is probably a scaling issue somewhere but the predictions coming out self.mod.predict are already cropped somehow:

[[ 9.          0.99957496  1.          1.          1.          1.        ]
 [ 9.          0.99943036  1.          1.          1.          1.        ]
 [ 9.          0.99912649  1.          1.          1.          1.        ]
 ..., 
 [ 9.          0.15663669  1.          1.          1.          1.        ]
 [ 9.          0.15572004  1.          1.          1.          1.        ]
 [ 9.          0.15441671  1.          1.          1.          1.        ]]

mmmikael avatar Jun 03 '17 08:06 mmmikael

Ok, I found where the clipping was done: mx.contrib.symbol.MultiBoxDetection has an optional parameters clip. After setting it to False and scaling the detected rectangles correctly, I get the right results*:

screen shot 2017-06-03 at 10 18 51 am

Do you think it would be worth adding an option to the SSD example to do inference on original image sizes? I can prepare a pull request for that.

* I'm not sure what to think about the 3 false positives at the bottom.

mmmikael avatar Jun 03 '17 08:06 mmmikael

@mmmikael What is the predictions without clipping? I suppose they are beyond 1s? If you can fix it, you are very welcome to do so, thanks.

zhreshold avatar Jun 05 '17 01:06 zhreshold

@mmmikael What is the predictions without clipping? I suppose they are beyond 1s?

Yes, they are beyond 1s. To get back to image coordinates, the scaling is not anymore image width and height but the training size (300 or 512). It seems like something should be changed in MultiBoxDetection. Any idea?

mmmikael avatar Jun 05 '17 06:06 mmmikael

If you want clip to work for original images, pass in clip range instead of true/false, and preserve the trained scaling (300, 512). The modification in MultiBoxDetection layer should be easy. @mmmikael

zhreshold avatar Jun 07 '17 17:06 zhreshold

This helps me and I think that people want SSD to predict variant input size in practice. If it's convenient I hope you can add this feature in your implementation. @zhreshold

chinakook avatar Oct 13 '17 11:10 chinakook

This will be solved in the new gluon object detection example that I'm working on.

zhreshold avatar Oct 16 '17 05:10 zhreshold

@zhreshold Thanks.

chinakook avatar Oct 16 '17 05:10 chinakook

I've tested variant input size for mxnet-ssd and I found that we just need fix a absolute anchor size(but not a relative anchor size, i.e. sizes in multibox_layer) for variant input size images to get the right result. @zhreshold

chinakook avatar Oct 21 '17 02:10 chinakook

@chinakook There's not too much work to enable variant input size, however, it will break compatibility to the previous versions. That's the major reason I am reluctant to do so. If you are interested, we can set up a new branch on this fix, and release it in a proper time.

zhreshold avatar Oct 21 '17 02:10 zhreshold

Yes, I know why's that and your viewpoint indeed. Maybe we can just write something about variant input size in README.md to issue this. @zhreshold

chinakook avatar Oct 21 '17 02:10 chinakook

@apache/mxnet-committers: This issue has been inactive for the past 90 days. It has no label and needs triage.

For general "how-to" questions, our user forum (and Chinese version) is a good place to get help.

szha avatar Jan 20 '18 12:01 szha

Can you please add the label 'Feature request' and 'Example' to this issue and remove 'Needs Triage'

mbaijal avatar Mar 20 '18 21:03 mbaijal

@sandeep-krishnamurthy can we close out this issue actually? looks to have been addressed.

sojiadeshina avatar Jul 30 '18 23:07 sojiadeshina

Checkout our new gluon-cv examples: https://gluon-cv.mxnet.io/build/examples_detection/demo_ssd.html#sphx-glr-build-examples-detection-demo-ssd-py

which fully support arbitrary shape images. I am closing this now.

zhreshold avatar Jul 30 '18 23:07 zhreshold