24 #include <boost/algorithm/string.hpp> 25 #include <boost/lexical_cast.hpp> 36 const std::string& cnn_type,
37 const std::string& graph_file_path,
38 const std::string& category_file_path,
39 const int network_dimension,
40 const std::vector<float>& mean,
46 device_index_(device_index),
47 log_level_(log_level),
49 network_dimension_(network_dimension),
69 assert(
graph_->getHandle() !=
nullptr);
72 uint16_t* probabilities;
74 int ret = mvncGetResult(
graph_->getHandle(),
reinterpret_cast<void**
>(&probabilities),
77 std::vector<uint16_t> result_vector(reinterpret_cast<uint16_t*>(probabilities),
78 reinterpret_cast<uint16_t*>(probabilities) + length);
79 ItemsPtr items = std::make_shared<Items>();
80 for (
size_t index = 0; index < length / 2; ++index)
84 fp32 = _cvtsh_ss(probabilities[index]);
91 items->push_back(item);
94 auto cmp = [](
const Item & a,
const Item & b)
98 std::sort(items->begin(), items->end(), cmp);
100 if (!
result_->getClassificationResult()->items.empty())
102 result_->getClassificationResult()->items.clear();
105 for (
auto i : *items)
107 result_->setClassificationResult(i);
108 if (static_cast<int>(
result_->getClassificationResult()->items.size()) ==
top_n_)
119 std::string debug_info =
graph_->getDebugInfo();
130 assert(
graph_->getHandle() !=
nullptr);
135 int ret = mvncGetResult(
graph_->getHandle(),
136 reinterpret_cast<void**
>(&result),
141 std::vector<uint16_t> result16_vector(reinterpret_cast<uint16_t*>(result),
142 reinterpret_cast<uint16_t*>(result) + length / 2);
143 std::vector<float> result32_vector;
145 for (
auto fp16 : result16_vector)
149 fp32 = _cvtsh_ss(fp16);
153 result32_vector.push_back(fp32);
161 else if (!
cnn_type_.compare(
"mobilenetssd"))
175 std::string debug_info =
graph_->getDebugInfo();
178 catch (std::exception& e)
189 assert(
graph_->getHandle() !=
nullptr);
190 int ret = mvncLoadTensor(
graph_->getHandle(),
199 return result_->getClassificationResult();
204 return result_->getDetectionResult();
228 boost::trim_right(first);
230 if (boost::iequals(first,
"classes"))
242 std::vector<std::string>& lines)
244 std::stringstream ss(content);
247 while (std::getline(ss, line,
'\n'))
249 lines.push_back(line);
255 std::ifstream in(filename.c_str(), std::ios::in | std::ios::binary);
263 in.seekg(0, std::ios::end);
264 content.reserve(in.tellg());
265 in.seekg(0, std::ios::beg);
266 content.assign(std::istreambuf_iterator<char>(in),
267 std::istreambuf_iterator<char>());
const std::string cnn_type_
std::shared_ptr< DetectionResult > DetectionResultPtr
const std::vector< float > mean_
static void fp16tofp32(float *__restrict out, uint16_t in)
void loadGraph(const std::string &graph_file_path)
static void tryToThrowMvncException(int code)
const char * what() const noexcept
void loadCategories(const std::string &category_file_path)
static std::string getFileContent(const std::string &filename)
DetectionResultPtr getDetectionResult()
static void splitIntoLines(const std::string &content, std::vector< std::string > &lines)
std::vector< std::string > categories_
NCS(int device_index, Device::LogLevel log_level, const std::string &cnn_type, const std::string &graph_file_path, const std::string &category_file_path, const int network_dimension, const std::vector< float > &mean, const float &scale, const int &top_n)
std::shared_ptr< Items > ItemsPtr
std::shared_ptr< ClassificationResult > ClassificationResultPtr
const int network_dimension_
ClassificationResultPtr getClassificationResult()
void loadTensor(const cv::Mat &image)
#define ROS_ERROR_STREAM(args)
const Device::LogLevel log_level_