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 2019/06/17 03:01:14 UTC

[GitHub] [incubator-mxnet] ciyongch commented on a change in pull request #15164: [C++] Improve inference script to support benchmark on Imagenet

ciyongch commented on a change in pull request #15164: [C++] Improve inference script to support benchmark on Imagenet
URL: https://github.com/apache/incubator-mxnet/pull/15164#discussion_r294122792
 
 

 ##########
 File path: cpp-package/example/inference/imagenet_inference.cpp
 ##########
 @@ -0,0 +1,597 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+/*
+ * This example demonstrates image classification workflow with pre-trained models using MXNet C++ API.
+ * The example performs following tasks.
+ * 1. Load the pre-trained model.
+ * 2. Load the parameters of pre-trained model.
+ * 3. Load the inference dataset and create a new ImageRecordIter.
+ * 4. Run the forward pass and obtain throughput & accuracy.
+ */
+
+#include <sys/stat.h>
+#include <sys/time.h>
+#include <cstdlib>
+#include <fstream>
+#include <iostream>
+#include <map>
+#include <string>
+#include <vector>
+#include <random>
+#include <type_traits>
+#include <opencv2/opencv.hpp>
+#include "mxnet/c_api.h"
+#include "mxnet/tuple.h"
+#include "mxnet-cpp/MxNetCpp.h"
+#include "mxnet-cpp/initializer.h"
+
+using namespace mxnet::cpp;
+
+double get_msec() {
+    struct timeval time;
+    gettimeofday(&time, NULL);
+    return 1e+3 * time.tv_sec + 1e-3 * time.tv_usec;
+}
+
+// define the data type for NDArray, aliged with the definition in mshadow/base.h
+enum TypeFlag {
+  kFloat32 = 0,
+  kFloat64 = 1,
+  kFloat16 = 2,
+  kUint8 = 3,
+  kInt32 = 4,
+  kInt8  = 5,
+  kInt64 = 6,
+};
+
+/*
+ * class Predictor
+ *
+ * This class encapsulates the functionality to load the model, prepare dataset and run the forward pass.
+ */
+
+class Predictor {
+ public:
+    Predictor() {}
+    Predictor(const std::string& model_json_file,
+              const std::string& model_params_file,
+              const Shape& input_shape,
+              bool use_gpu,
+              const std::string& dataset,
+              const int data_nthreads,
+              const std::string& data_layer_type,
+              const std::vector<float>& rgb_mean,
+              const std::vector<float>& rgb_std,
+              int shuffle_chunk_seed,
+              int seed, bool benchmark);
+    void BenchmarkScore(int num_inference_batches);
+    void Score(int num_skipped_batches, int num_inference_batches);
+    ~Predictor();
+
+ private:
+    bool CreateImageRecordIter();
+    bool AdvanceDataIter(int skipped_batches);
+    void LoadModel(const std::string& model_json_file);
+    void LoadParameters(const std::string& model_parameters_file);
+    void InitParameters();
+
+    inline bool FileExists(const std::string& name) {
+      struct stat buffer;
+      return (stat(name.c_str(), &buffer) == 0);
+    }
+    int GetDataLayerType();
+
+    NDArray mean_img;
+    std::map<std::string, NDArray> args_map;
+    std::map<std::string, NDArray> aux_map;
+    Symbol net;
+    Executor *executor;
+    Shape input_shape;
 
 Review comment:
   Add suffix `_` to these private vars.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to 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