anirudh2290 commented on a change in pull request #16654: Multithreaded Inference Support URL: https://github.com/apache/incubator-mxnet/pull/16654#discussion_r366074512
########## File path: example/multi_threaded_inference/multi_threaded_inference.cc ########## @@ -0,0 +1,351 @@ +/* + * 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. + */ + +/*! + * Copyright (c) 2017 by Contributors + * \file multi_threaded_inference.cc + * \brief Multi Threaded inference example with CachedOp +*/ + +#include <cstdio> +#include <cstdlib> +#include <iostream> +#include <fstream> +#include <thread> +#include <iomanip> +#include <chrono> +#include <mxnet/ndarray.h> +#include <opencv2/opencv.hpp> +#include <mxnet/c_predict_api.h> +#include "mxnet-cpp/MxNetCpp.h" + +const mx_float DEFAULT_MEAN = 117.0; + + +// Code to load image, PrintOutput results, helper functions for the same obtained from: +// https://github.com/apache/incubator-mxnet/blob/master/example/image-classification/predict-cpp/ + +static std::string trim(const std::string &input) { + auto not_space = [](int ch) { return !std::isspace(ch); }; + auto output = input; + output.erase(output.begin(), + std::find_if(output.begin(), output.end(), not_space)); + output.erase(std::find_if(output.rbegin(), output.rend(), not_space).base(), + output.end()); + return output; +} + +std::vector<std::string> LoadSynset(const std::string& synset_file) { + std::ifstream fi(synset_file.c_str()); + + if (!fi.is_open()) { + std::cerr << "Error opening synset file " << synset_file << std::endl; + assert(false); + } + + std::vector<std::string> output; + + std::string synset, lemma; + while (fi >> synset) { + getline(fi, lemma); + output.push_back(lemma); + } + + fi.close(); + + return output; +} + +void PrintOutputResult(const float* data, size_t size, const std::vector<std::string>& synset) { + if (size != synset.size()) { + std::cerr << "Result data and synset size do not match!" << std::endl; + } + + float best_accuracy = 0.0; + std::size_t best_idx = 0; + + for (std::size_t i = 0; i < size; ++i) { + if (data[i] > best_accuracy) { + best_accuracy = data[i]; + best_idx = i; + } + } + + std::cout << "Best Result: " << trim(synset[best_idx]) << " (id=" << best_idx << ", " << + "accuracy=" << std::setprecision(8) << best_accuracy << ")" << std::endl; +} + + +// Read Image data into a float array +void GetImageFile(const std::string &image_file, mx_float *image_data, + int channels, cv::Size resize_size) { + // Read all kinds of file into a BGR color 3 channels image + cv::Mat im_ori = cv::imread(image_file, cv::IMREAD_COLOR); + + if (im_ori.empty()) { + std::cerr << "Can't open the image. Plase check " << image_file << ". \n"; + assert(false); + } + + cv::Mat im; + resize(im_ori, im, resize_size); + + int size = im.rows * im.cols * channels; + + mx_float* ptr_image_r = image_data; + mx_float* ptr_image_g = image_data + size / 3; + mx_float* ptr_image_b = image_data + size / 3 * 2; + + float mean_b, mean_g, mean_r; + mean_b = mean_g = mean_r = DEFAULT_MEAN; + + for (int i = 0; i < im.rows; ++i) { + auto data = im.ptr<uchar>(i); + for (int j = 0; j < im.cols; j++) { + if (channels > 1) { + *ptr_image_b++ = static_cast<mx_float>(*data++) - mean_b; + *ptr_image_g++ = static_cast<mx_float>(*data++) - mean_g; + } + } + *ptr_image_r++ = static_cast<mx_float>(*data++) - mean_r; + } +} + +void prepare_input_data(const mxnet::cpp::Shape& shape, const mxnet::cpp::Context& ctx, + int num_threads, + std::vector<mxnet::cpp::NDArray>* data_arr, + bool random_uniform = false) { + for (size_t i = 0; i < num_threads; ++i) { + data_arr->emplace_back(shape, ctx, false, 0); + int begin = i * 100; + int end = begin + 100; + if (random_uniform) { + mxnet::cpp::Operator("_random_uniform")(begin, end) + .Invoke((*data_arr)[i]); + } + mxnet::cpp::NDArray::WaitAll(); + } +} + +// Run inference on a model +void run_inference(const std::string& model_name, const std::vector<mxnet::cpp::NDArray>& input_arrs, + std::vector<mxnet::NDArray*> *output_mx_arr, + int num_inf_per_thread = 1, bool random_sleep = false, + int num_threads = 1, bool static_alloc = false, + bool static_shape = false, + bool is_gpu = false) { + LOG(INFO) << "Running inference for " + model_name + + " num_threads: " + std::to_string(num_threads) + + " num_inf_per_thread: " + std::to_string(num_inf_per_thread) + + " random_sleep: " + std::to_string(random_sleep) + + " static_alloc: " + std::to_string(static_alloc) + + " static_shape: " + std::to_string(static_shape); + std::string json_file = model_name + "-symbol.json"; + std::string param_file = model_name + "-0000.params"; + auto out = mxnet::cpp::Symbol::Load(json_file); + std::string static_alloc_str = static_alloc ? "true" : "false"; + std::string static_shape_str = static_shape ? "true" : "false"; + + // Prepare context +# if MXNET_USE_CUDA == 1 + mxnet::Context backend_ctx; Review comment: frontend version of the context data structure. provides getters and setters for dev_type and dev_id. ---------------------------------------------------------------- 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: [email protected] With regards, Apache Git Services
