-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Add predict example for C++ #891
Conversation
|
Thanks!, I was working on the same this morning, here is the alternative code just in case that you find it useful: https://github.com/pertusa/mxnet_predict_cc. My results are very similar to yours (although I wasn't cropping the image), but after some tests I've found that there is a problem on both implementations. The output is not even similar to that obtained with the corresponding python "mxnet_predict_example.py" for some images, like the one attached. Could you please check it? |
|
Thanks! This helps a lot! |
|
@pertusa I looked at your code. It pretty good, especially the make file. After we merge this you are welcome to add improvements to it. Thanks for you interest! |
|
I have added a Makefile and README file for help, and move every thing to an independent folder. |
|
Thanks @piiswrong , I'll contribute once merged. I've tested the @skylook code, and I get a similar output than with my version, but still very different from the python results. Classifying the vulture image (from my last message) with the ImageNet1K model, I get only a clear peak at the class 904 (window). However, the python script returns a kite as first option, and then some birds. I'm not sure if it a problem of the Makefile (some missing library?), but I've also tried with static linking (updated Makefile in my repo) and this issue remains. Could you try to classify this image and check that the output is consistent? |
|
Yes, I met the same problem as @piiswrong , the predict result of c_api seems not very stable. |
|
Usually non-deterministic results (different outputs from the same input) are due to linkage issues, typically missing libraries. For instance, when using MKL in C++ it is necessary to add many extra flags and libs. So maybe the Makefile is wrong. I can't check it this week, but I'll try to do it ASAP. |
|
When I used Theano, some layers with random noise, such as the dropout layer, will make the result non-deterministic. So make sure it is not the reason of your problem. |
|
Good point @zhangchen-qinyinghua, I'll also check this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please change to Google C++ style PrintOutputResult
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you do not modify data, try pas const std::vector<float>& data
|
Thanks for the good job, can you please make a pass on code style to make it consistent with rest of the project ? i.e. Google's C style. Check the Thanks! |
|
I've found the problem of the inconsistent output. OpenCV data array is stored as BGR,BGR,BGR, whereas MXPredSetInput expects the array as RRRRRR...,GGGGGGG...,BBBBBBB.... I've updated my code in https://github.com/pertusa/mxnet_predict_cc accordingly and now it works. @tqchen, could you please change your loop in getMeanFile? About the non-deterministic output, dropout layers give some random values as @zhangchen-qinyinghua commented. This is not a serious issue as the expected results are usually quite similar but some frameworks like Caffe have some layers that can be discarded int the TEST phase (usually dropouts). I wonder if there is something similar in MXNet, but if not it could be a good feature. |
|
@pertusa Are you sure it's not BBBBBB...,GGGGGGGG....,RRRRRRRR...... |
|
Yes, I've checked it printing the data array received in MXPredSetInput() of c_predict_api.cc. With the python call from (mxnet_predict_example.py) and a red image, the first elements in the array are the ones with the highest values, so it's RRR...GGG...BBB... |
|
Good work Skylook, thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No space before bracket
|
Thanks for being patient on this and great contribution. Can you please do a lint on your file? like dmlc-core/script/lint.py cpp mxnet cpp example/cppYou can add this path to https://github.com/dmlc/mxnet/blob/master/Makefile#L145 so it is automatically checked by travis This should detect most of common things linter complained on, I commented a few. This is just to make sure the project have consistent programming style :) After this is done, please
Then it is good to be merged. |
|
@skylook can you update the code accordingly? |
|
I am working on it, sorry for being too busy :) @tqchen |
|
I got some sample code from my workmates and didnot notice your github. I have not yet finished but certainly I will add all your names above when I finished :) @caprice-j |
74adf98 to
ecbae55
Compare
|
Hi, I finally have time to do these following changes:
Hopefully this patch is ready to be merged :) @tqchen |
|
Looks good to me. @tqchen |
|
Great Job! Thanks for the contribution and make mxnet better for everyone! |
|
@skylook Thanks for your kindness. |

This is a simple predictor which shows how to use c api for image classfication
It uses opencv for image reading