Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

Conversation

@skylook
Copy link
Contributor

@skylook skylook commented Dec 10, 2015

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

@pertusa
Copy link

pertusa commented Dec 10, 2015

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?

borrame

@piiswrong
Copy link
Contributor

Thanks! This helps a lot!
A minor suggestion: could you add your build command at the top?
you may find this https://github.com/pertusa/mxnet_predict_cc/blob/master/Makefile useful

@piiswrong
Copy link
Contributor

@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!

@skylook
Copy link
Contributor Author

skylook commented Dec 11, 2015

I have added a Makefile and README file for help, and move every thing to an independent folder.
Thanks to @pertusa & @piiswrong

@pertusa
Copy link

pertusa commented Dec 11, 2015

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?

@skylook
Copy link
Contributor Author

skylook commented Dec 11, 2015

Yes, I met the same problem as @piiswrong , the predict result of c_api seems not very stable.
Even most of the time it shows the same result with the same image, but sometimes it shows differently.
I do not know if it is a bug or just my miss unstanding with the c api.

@pertusa
Copy link

pertusa commented Dec 11, 2015

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.

@zhangchen-qinyinghua
Copy link
Contributor

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.

@pertusa
Copy link

pertusa commented Dec 11, 2015

Good point @zhangchen-qinyinghua, I'll also check this

Copy link
Member

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

Copy link
Member

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

@tqchen
Copy link
Member

tqchen commented Dec 11, 2015

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 lint job in main Makefile, which can help auto checks the code style

Thanks!

@pertusa
Copy link

pertusa commented Dec 11, 2015

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.

@piiswrong
Copy link
Contributor

@pertusa Are you sure it's not BBBBBB...,GGGGGGGG....,RRRRRRRR......

@pertusa
Copy link

pertusa commented Dec 12, 2015

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

@skylook
Copy link
Contributor Author

skylook commented Dec 13, 2015

Some small modifications:

  1. Updated the code according to Google C Style and @tqchen 's suggestions.
  2. Fix the image loading error and add synset result showing as @pertusa .

@pertusa
Copy link

pertusa commented Dec 14, 2015

Good work Skylook, thanks!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No space before bracket

@tqchen
Copy link
Member

tqchen commented Dec 14, 2015

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/cpp

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

@tqchen
Copy link
Member

tqchen commented Dec 16, 2015

@skylook can you update the code accordingly?

@caprice-j
Copy link

@skylook Hi, Thanks for improving my code in #531.
May I add my name after you to the header of image-classification-predict.cpp?

@skylook
Copy link
Contributor Author

skylook commented Dec 17, 2015

I am working on it, sorry for being too busy :) @tqchen

@skylook
Copy link
Contributor Author

skylook commented Dec 17, 2015

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

@skylook skylook force-pushed the master branch 2 times, most recently from 74adf98 to ecbae55 Compare December 17, 2015 09:49
@skylook
Copy link
Contributor Author

skylook commented Dec 17, 2015

Hi, I finally have time to do these following changes:

  1. Change code style to pass cpplint check.
  2. Update README file to add more details of building and usage of the code.
  3. Combine multiple commits into one.

Hopefully this patch is ready to be merged :) @tqchen

@piiswrong
Copy link
Contributor

Looks good to me. @tqchen

tqchen added a commit that referenced this pull request Dec 18, 2015
Add predict example for C++
@tqchen tqchen merged commit ea453a5 into apache:master Dec 18, 2015
@tqchen
Copy link
Member

tqchen commented Dec 18, 2015

Great Job! Thanks for the contribution and make mxnet better for everyone!

@caprice-j
Copy link

@skylook Thanks for your kindness.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants