\brief Generate a network definition for a given model \param[in] model Model options for this network \param[in,out] network Network storing the parsed results \param[in,out] err Error stream \param[out] vcPluginLibrariesUsed If not nullptr, will be populated with paths to VC plugin libraries required by the parsed network. \return Parser The parser used to initialize the network and that holds
| 237 | //! \see Parser::operator bool() |
| 238 | //! |
| 239 | Parser modelToNetwork(ModelOptions const& model, BuildOptions const& build, nvinfer1::INetworkDefinition& network, |
| 240 | std::ostream& err, std::vector<std::string>* vcPluginLibrariesUsed) |
| 241 | { |
| 242 | sample::gLogInfo << "Start parsing network model." << std::endl; |
| 243 | auto const tBegin = std::chrono::high_resolution_clock::now(); |
| 244 | |
| 245 | Parser parser; |
| 246 | std::string const& modelName = model.baseModel.model; |
| 247 | switch (model.baseModel.format) |
| 248 | { |
| 249 | case ModelFormat::kCAFFE: |
| 250 | { |
| 251 | using namespace nvcaffeparser1; |
| 252 | parser.caffeParser.reset(sampleCreateCaffeParser()); |
| 253 | CaffeBufferShutter bufferShutter; |
| 254 | auto const* const blobNameToTensor = parser.caffeParser->parse( |
| 255 | model.prototxt.c_str(), modelName.empty() ? nullptr : modelName.c_str(), network, DataType::kFLOAT); |
| 256 | if (!blobNameToTensor) |
| 257 | { |
| 258 | err << "Failed to parse caffe model or prototxt, tensors blob not found" << std::endl; |
| 259 | parser.caffeParser.reset(); |
| 260 | break; |
| 261 | } |
| 262 | |
| 263 | for (auto const& s : model.outputs) |
| 264 | { |
| 265 | if (blobNameToTensor->find(s.c_str()) == nullptr) |
| 266 | { |
| 267 | err << "Could not find output blob " << s << std::endl; |
| 268 | parser.caffeParser.reset(); |
| 269 | break; |
| 270 | } |
| 271 | network.markOutput(*blobNameToTensor->find(s.c_str())); |
| 272 | } |
| 273 | break; |
| 274 | } |
| 275 | case ModelFormat::kUFF: |
| 276 | { |
| 277 | using namespace nvuffparser; |
| 278 | parser.uffParser.reset(sampleCreateUffParser()); |
| 279 | UffBufferShutter bufferShutter; |
| 280 | for (auto const& s : model.uffInputs.inputs) |
| 281 | { |
| 282 | if (!parser.uffParser->registerInput( |
| 283 | s.first.c_str(), s.second, model.uffInputs.NHWC ? UffInputOrder::kNHWC : UffInputOrder::kNCHW)) |
| 284 | { |
| 285 | err << "Failed to register input " << s.first << std::endl; |
| 286 | parser.uffParser.reset(); |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | for (auto const& s : model.outputs) |
| 292 | { |
| 293 | if (!parser.uffParser->registerOutput(s.c_str())) |
| 294 | { |
| 295 | err << "Failed to register output " << s << std::endl; |
| 296 | parser.uffParser.reset(); |
no test coverage detected