You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/11/12 03:36:44 UTC

[GitHub] WANG-MengJiao opened a new issue #13219: using C++ interface on CPU to extract feature vector, delete NDArray::WaitAll() generate incorrect result

WANG-MengJiao opened a new issue #13219: using C++  interface on CPU to extract feature vector, delete NDArray::WaitAll() generate incorrect result
URL: https://github.com/apache/incubator-mxnet/issues/13219
 
 
   I am using a c++ api on CPU to extract a 512-dim feature vector from images. The feature vector will be printed out. 
   I encountered with the problem that with/without `NDArray::WaitAll()` generate totally different outputs. Among the results, with `NDArray::WaitAll()` can get correct vector. 
   However, the `NDArray::WaitAll()` is very time consuming. According to the comment in [feature_extract.cpp](https://github.com/apache/incubator-mxnet/blob/master/cpp-package/example/feature_extract/feature_extract.cpp), the `NDArray::WaitAll()` is needed only when copy between GPU and main memory. So I am wondering why the removal of `NDArray::WaitAll()` will cause incorrect output when I run the program on CPU. Is there any other tricks I should do?
   The code I used is as follows:
   ``` cpp
   using namespace std;
   using namespace mxnet::cpp;
   
   Context global_ctx(kCPU,0);
   
   class FeatureExtractor {
    private:
     /*the mean image, get from the pretrained model*/
     NDArray mean_img;
     /*the following two maps store all the paramters need by the model*/
     map<string, NDArray> args_map;
     map<string, NDArray> aux_map;
     Symbol net;
     Executor *executor;
     /*Get the feature layer we want to extract*/
     void GetFeatureSymbol() {
       net = Symbol::Load("/mymodelpath/model-r50-symbol.json")
                 .GetInternals()["fc1_output"];
     }
     /*Fill the trained paramters into the model, a.k.a. net, executor*/
     void LoadParameters() {
       map<string, NDArray> paramters;
       NDArray::Load("/mymodelpath/model-r50-0104.params", 0, &paramters);
       for (const auto &k : paramters) {
         if (k.first.substr(0, 4) == "aux:") {
           auto name = k.first.substr(4, k.first.size() - 4);
           aux_map[name] = k.second;
         }
         if (k.first.substr(0, 4) == "arg:") {
           auto name = k.first.substr(4, k.first.size() - 4);
   	    args_map[name] = k.second;
         }
       }
     }
   
    public:
     FeatureExtractor() {
       GetFeatureSymbol();
       LoadParameters();
     }
   
     void Extract(NDArray data) {
       data -= 127.5;
       data = data * 1.0 / 127.5;
       args_map["data"] = data;
       /*bind the executor*/
       executor = net.SimpleBind(global_ctx, args_map, map<string, NDArray>(), map<string, OpReqType>(), aux_map); 
       executor->Forward(false);    
       NDArray array = executor->outputs[0];
       NDArray::WaitAll();
       /*print out the features*/
       for (int i = 0; i < 512; ++i) {
         cout << array.At(0, i) << ",";
       }
       cout << endl;
     }
   };
   ```
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services