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 02:53:21 UTC

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

ZhennanQin 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_r294121844
 
 

 ##########
 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;
+    Context global_ctx = Context::cpu();
+
+    MXDataIter *val_iter;
+    bool use_gpu_;
+    std::string dataset_;
+    int data_nthreads_;
+    std::string data_layer_type_;
+    std::vector<float> rgb_mean_;
+    std::vector<float> rgb_std_;
+    int shuffle_chunk_seed_;
+    int seed_;
+    bool benchmark_;
+};
+
+
+/*
+ * The constructor takes following parameters as input:
+ * 1. model_json_file:  The model in json formatted file.
+ * 2. model_params_file: File containing model parameters
+ * 3. input_shape: Shape of input data to the model. Since this class will be running one inference at a time,
+ *                 the input shape is required to be in format Shape(1, number_of_channels, height, width)
+ *                 The input image will be resized to (height x width) size before running the inference.
+ * 4. use_gpu: determine if run inference on GPU
+ * 5. dataset: data file (.rec) to be used for inference
+ * 6. data_nthreads: number of threads for data loading
+ * 7. data_layer_type: data type for data layer
+ * 8. rgb_mean: mean value to be subtracted on R/G/B channel
+ * 9. rgb_std: standard deviation on R/G/B channel
+ * 10. shuffle_chunk_seed: shuffling chunk seed
+ * 11. seed: shuffling seed
+ * 12. benchmark: use dummy data for inference
+ *
+ * The constructor will:
+ *  1. Create ImageRecordIter based on the given dataset file.
+ *  2. Load the model and parameter files.
+ *  3. Infer and construct NDArrays according to the input argument and create an executor.
+ */
+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)
+    : input_shape(input_shape),
+      use_gpu_(use_gpu),
+      dataset_(dataset),
+      data_nthreads_(data_nthreads),
+      data_layer_type_(data_layer_type),
+      rgb_mean_(rgb_mean),
+      rgb_std_(rgb_std),
+      shuffle_chunk_seed_(shuffle_chunk_seed),
+      seed_(seed),
+      benchmark_(benchmark) {
+  if (use_gpu) {
+    global_ctx = Context::gpu();
+  }
+
+  // initilize data iterator
+  if (!benchmark_ && !CreateImageRecordIter()) {
+    LG << "Error: failed to create ImageRecordIter";
+    throw std::runtime_error("ImageRecordIter cannot be created");
+  }
+
+  // Load the model
+  LoadModel(model_json_file);
+  // Initilize the parameters
+  // benchmark=false, load parameters from file
+  // benchmark=true, randomly initialize parameters
+  if (!benchmark_) {
+    LoadParameters(model_params_file);
+  } else {
+    InitParameters();
+  }
+
+  int dtype = GetDataLayerType();
+  if (dtype == -1) {
+    throw std::runtime_error("Unsupported data layer type...");
+  }
+  args_map["data"] = NDArray(input_shape, global_ctx, false, dtype);
+  Shape label_shape(input_shape[0]);
+  args_map["softmax_label"] = NDArray(label_shape, global_ctx, false);
+  std::vector<NDArray> arg_arrays;
+  std::vector<NDArray> grad_arrays;
+  std::vector<OpReqType> grad_reqs;
+  std::vector<NDArray> aux_arrays;
+
+  // infer and create ndarrays according to the given input ndarrays.
+  net.InferExecutorArrays(global_ctx, &arg_arrays, &grad_arrays, &grad_reqs, &aux_arrays, args_map,
+                      std::map<std::string, NDArray>(), std::map<std::string, OpReqType>(),
+                      aux_map);
+  for (auto& i : grad_reqs) i = OpReqType::kNullOp;
+
+  // Create an executor after binding the model to input parameters.
+  executor = new Executor(net, global_ctx, arg_arrays, grad_arrays, grad_reqs, aux_arrays);
+}
+
+/*
+ * The following function is used to get the data layer type for input data
+ */
+int Predictor::GetDataLayerType() {
+  int ret_type = -1;
+  if (data_layer_type_ == "float32") {
+    ret_type = kFloat32;
+  } else if (data_layer_type_ == "int8") {
+    ret_type = kInt8;
+  } else if (data_layer_type_ == "uint8") {
+    ret_type = kUint8;
+  } else {
+    LG << "Unsupported data layer type " << data_layer_type_ << "..."
+       << "Please use one of {float32, int8, uint8}";
+  }
+  return ret_type;
+}
+
+/*
+ * create a new ImageRecordIter according to the given parameters
+ */
+bool Predictor::CreateImageRecordIter() {
+  val_iter = new MXDataIter("ImageRecordIter");
+  if (!FileExists(dataset_)) {
+    LG << "Error: " << dataset_ << " must be provided";
+    return false;
+  }
+
+  std::vector<index_t> shape_vec {input_shape[1], input_shape[2], input_shape[3]};
+  mxnet::TShape data_shape(shape_vec.begin(), shape_vec.end());
+
+  // set image record parser parameters
+  val_iter->SetParam("path_imgrec", dataset_);
+  val_iter->SetParam("label_width", 1);
+  val_iter->SetParam("data_shape", data_shape);
+  val_iter->SetParam("preprocess_threads", data_nthreads_);
+  val_iter->SetParam("shuffle_chunk_seed", shuffle_chunk_seed_);
+
+  // set Batch parameters
+  val_iter->SetParam("batch_size", input_shape[0]);
+
+  // image record parameters
+  val_iter->SetParam("shuffle", true);
+  val_iter->SetParam("seed", seed_);
+
+  // set normalize parameters
+  val_iter->SetParam("mean_r", rgb_mean_[0]);
+  val_iter->SetParam("mean_g", rgb_mean_[1]);
+  val_iter->SetParam("mean_b", rgb_mean_[2]);
+  val_iter->SetParam("std_r", rgb_std_[0]);
+  val_iter->SetParam("std_g", rgb_std_[1]);
+  val_iter->SetParam("std_b", rgb_std_[2]);
+
+  // set prefetcher parameters
+  if (use_gpu_) {
+    val_iter->SetParam("ctx", "gpu");
+  } else {
+    val_iter->SetParam("ctx", "cpu");
+  }
+  val_iter->SetParam("dtype", data_layer_type_);
+
+  val_iter->CreateDataIter();
+  return true;
+}
+
+/*
+ * The following function loads the model from json file.
+ */
+void Predictor::LoadModel(const std::string& model_json_file) {
+  if (!FileExists(model_json_file)) {
+    LG << "Model file " << model_json_file << " does not exist";
+    throw std::runtime_error("Model file does not exist");
+  }
+  LG << "Loading the model from " << model_json_file << std::endl;
+  net = Symbol::Load(model_json_file);
+}
+
+
+/*
+ * The following function loads the model parameters.
+ */
+void Predictor::LoadParameters(const std::string& model_parameters_file) {
+  if (!FileExists(model_parameters_file)) {
+    LG << "Parameter file " << model_parameters_file << " does not exist";
+    throw std::runtime_error("Model parameters does not exist");
+  }
+  LG << "Loading the model parameters from " << model_parameters_file << std::endl;
+  std::map<std::string, NDArray> parameters;
+  NDArray::Load(model_parameters_file, 0, &parameters);
+  for (const auto &k : parameters) {
+    if (k.first.substr(0, 4) == "aux:") {
+      auto name = k.first.substr(4, k.first.size() - 4);
+      aux_map[name] = k.second.Copy(global_ctx);
+    }
+    if (k.first.substr(0, 4) == "arg:") {
+      auto name = k.first.substr(4, k.first.size() - 4);
+      args_map[name] = k.second.Copy(global_ctx);
+    }
+  }
+  /*WaitAll is need when we copy data between GPU and the main memory*/
+  NDArray::WaitAll();
+}
+
+/*
+ * The following function randomly initializes the parameters when benchmark_ is true.
+ */
+void Predictor::InitParameters() {
+  std::vector<mx_uint> data_shape;
+  for (index_t i=0; i < input_shape.ndim(); i++) {
 
 Review comment:
   code style

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