age-gender-detection.cpp
Go to the documentation of this file.
1 // Copyright (C) 2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
7 #include <easylogging++.h>
8 
9 
10 using namespace InferenceEngine;
11 
12 
14 {
15  age_gender_detection::age_gender_detection(
16  const std::string &pathToModel,
17  bool isAsync,
18  int maxBatch, bool isBatchDynamic,
19  bool doRawOutputMessages
20  )
21  : base_detection( "age/gender", pathToModel, maxBatch, isBatchDynamic, isAsync, doRawOutputMessages)
22  , _n_enqued_frames(0)
23  {
24  }
25 
26 
28  {
29  if( !_n_enqued_frames )
30  return;
31  _n_enqued_frames = 0;
33  }
34 
35 
36  void age_gender_detection::enqueue( const cv::Mat &face )
37  {
38  if( !enabled() )
39  return;
40  if( !_request )
41  _request = net.CreateInferRequestPtr();
42 
43  Blob::Ptr inputBlob = _request->GetBlob( input );
44  matU8ToBlob<uint8_t>( face, inputBlob, _n_enqued_frames );
45 
47  }
48 
49 
51  {
52  Blob::Ptr genderBlob = _request->GetBlob( outputGender );
53  Blob::Ptr ageBlob = _request->GetBlob( outputAge );
54 
56  ageBlob->buffer().as<float*>()[idx] * 100,
57  genderBlob->buffer().as<float*>()[idx * 2 + 1]
58  };
60  LOG(DEBUG) << "element" << idx << ", male prob = " << r.maleProb << ", age = " << r.age;
61 
62  return r;
63  }
64 
65 
67  {
68  LOG(INFO) << "Loading " << topoName << " model from: " << pathToModel;
69 
70  CNNNetReader netReader;
71  netReader.ReadNetwork(pathToModel);
72  netReader.getNetwork().setBatchSize(maxBatch);
73 
74  // Extract model name and load its weights
75  std::string binFileName = remove_ext( pathToModel ) + ".bin";
76  netReader.ReadWeights( binFileName );
77 
78  // Age/Gender Recognition network should have one input and two outputs
79 
80  LOG(DEBUG) << "Checking Age/Gender Recognition network inputs";
81  InputsDataMap inputInfo(netReader.getNetwork().getInputsInfo());
82  if (inputInfo.size() != 1)
83  throw std::logic_error("Age/Gender Recognition network should have only one input");
84  InputInfo::Ptr& inputInfoFirst = inputInfo.begin()->second;
85  inputInfoFirst->setPrecision(Precision::U8);
86  input = inputInfo.begin()->first;
87 
88  LOG(DEBUG) << "Checking Age/Gender Recognition network outputs";
89  OutputsDataMap outputInfo(netReader.getNetwork().getOutputsInfo());
90  if (outputInfo.size() != 2)
91  throw std::logic_error("Age/Gender Recognition network should have two output layers");
92  auto it = outputInfo.begin();
93 
94  DataPtr ptrAgeOutput = (it++)->second;
95  DataPtr ptrGenderOutput = (it++)->second;
96 
97  if (!ptrAgeOutput)
98  throw std::logic_error("Age output data pointer is not valid");
99  if (!ptrGenderOutput)
100  throw std::logic_error("Gender output data pointer is not valid");
101 
102  auto genderCreatorLayer = ptrGenderOutput->getCreatorLayer().lock();
103  auto ageCreatorLayer = ptrAgeOutput->getCreatorLayer().lock();
104 
105  if (!ageCreatorLayer)
106  throw std::logic_error("Age creator layer pointer is not valid");
107  if (!genderCreatorLayer)
108  throw std::logic_error("Gender creator layer pointer is not valid");
109 
110  // if gender output is convolution, it can be swapped with age
111  if (genderCreatorLayer->type == "Convolution")
112  std::swap(ptrAgeOutput, ptrGenderOutput);
113 
114  if (ptrAgeOutput->getCreatorLayer().lock()->type != "Convolution")
115  throw std::logic_error("In Age/Gender Recognition network, age layer (" + ageCreatorLayer->name +
116  ") should be a Convolution, but was: " + ageCreatorLayer->type);
117 
118  if (ptrGenderOutput->getCreatorLayer().lock()->type != "SoftMax")
119  throw std::logic_error("In Age/Gender Recognition network, gender layer (" + genderCreatorLayer->name +
120  ") should be a SoftMax, but was: " + genderCreatorLayer->type);
121  if( doRawOutputMessages )
122  {
123  LOG(DEBUG) << "Age layer: " << ageCreatorLayer->name;
124  LOG(DEBUG) << "Gender layer: " << genderCreatorLayer->name;
125  }
126 
127  outputAge = ptrAgeOutput->getName();
128  outputGender = ptrGenderOutput->getName();
129 
130  _enabled = true;
131  return netReader.getNetwork();
132  }
133 }
InferenceEngine::InferRequest::Ptr _request
std::string remove_ext(const std::string &filepath)
InferenceEngine::ExecutableNetwork net
LOG(INFO)<< "Log message to default logger"
GLsizei const GLchar *const * string
GLdouble GLdouble r
void swap(nlohmann::json &j1, nlohmann::json &j2) noexcept(is_nothrow_move_constructible< nlohmann::json >::value andis_nothrow_move_assignable< nlohmann::json >::value)
exchanges the values of two JSON objects
Definition: json.hpp:12141
static auto it
GLenum GLenum GLenum input
Definition: glext.h:10805
#define INFO(msg)
Definition: catch.hpp:17429
InferenceEngine::CNNNetwork read_network() override
GLenum GLuint GLint GLenum face
Definition: glext.h:3377


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:45:06