ArmNN
 21.02
RecordByRecordCaffeParser Class Reference

#include <RecordByRecordCaffeParser.hpp>

Inheritance diagram for RecordByRecordCaffeParser:
ICaffeParser::CaffeParserImpl

Public Member Functions

virtual armnn::INetworkPtr CreateNetworkFromBinaryFile (const char *graphFile, const std::map< std::string, armnn::TensorShape > &inputShapes, const std::vector< std::string > &requestedOutputs) override
 Create the network from a protobuf binary file on disk. More...
 
 RecordByRecordCaffeParser ()
 
- Public Member Functions inherited from ICaffeParser::CaffeParserImpl
armnn::INetworkPtr CreateNetworkFromTextFile (const char *graphFile, const std::map< std::string, armnn::TensorShape > &inputShapes, const std::vector< std::string > &requestedOutputs)
 Create the network from a protobuf text file on disk. More...
 
armnn::INetworkPtr CreateNetworkFromString (const char *protoText, const std::map< std::string, armnn::TensorShape > &inputShapes, const std::vector< std::string > &requestedOutputs)
 Creates the network directly from protobuf text in a string. Useful for debugging/testing. More...
 
BindingPointInfo GetNetworkInputBindingInfo (const std::string &name) const
 Retrieves binding info (layer id and tensor info) for the network input identified by the given layer name. More...
 
BindingPointInfo GetNetworkOutputBindingInfo (const std::string &name) const
 Retrieves binding info (layer id and tensor info) for the network output identified by the given layer name. More...
 
 CaffeParserImpl ()
 
virtual ~CaffeParserImpl ()=default
 

Additional Inherited Members

- Static Public Member Functions inherited from ICaffeParser::CaffeParserImpl
static const std::string GetVersion ()
 Retrieve version in X.Y.Z form. More...
 
- Protected Types inherited from ICaffeParser::CaffeParserImpl
using OperationParsingFunction = void(CaffeParserImpl::*)(const caffe::LayerParameter &layerParam)
 
- Protected Member Functions inherited from ICaffeParser::CaffeParserImpl
armnn::TensorInfo BlobShapeToTensorInfo (const caffe::BlobShape &blobShape) const
 Converts Caffe's protobuf tensor shape format to ArmNN's. More...
 
void TrackInputBinding (armnn::IConnectableLayer *layer, armnn::LayerBindingId id, const armnn::TensorInfo &tensorInfo)
 
void TrackOutputBinding (armnn::IConnectableLayer *layer, armnn::LayerBindingId id, const armnn::TensorInfo &tensorInfo)
 
void SetArmnnOutputSlotForCaffeTop (const std::string &caffeTopName, armnn::IOutputSlot &armnnOutputSlot)
 
armnn::IOutputSlotGetArmnnOutputSlotForCaffeTop (const std::string &caffeTopName) const
 Retrieves the Armnn IOutputSlot representing the given Caffe top. More...
 
void Cleanup ()
 
armnn::INetworkPtr CreateNetworkFromNetParameter (caffe::NetParameter &netParam, const std::map< std::string, armnn::TensorShape > &inputShapes, const std::vector< std::string > &requestedOutputs)
 Parses a NetParameter loaded into memory from one of the other CreateNetwork*. More...
 
void LoadNetParam (caffe::NetParameter &netParameter)
 does the actual conversion from caffe::NetParameter to armnn::INetwork More...
 
std::vector< const caffe::LayerParameter * > GetInputs (const caffe::LayerParameter &layerParam)
 Find the Caffe layers listed as inputs (bottoms) for a given layer. More...
 
void ResolveInPlaceLayers (caffe::NetParameter &netParameter)
 Modifies the Caffe network to replace "in-place" layers (whose top() and bottom() are both the same) with regular layers. More...
 
void ParseInputLayer (const caffe::LayerParameter &layerParam)
 Adds an armnn layer to m_Network given a Caffe LayerParameter of the correct type and is responsible for recording any newly created IOutputSlots using SetArmnnOutputSlotForCaffeTop(). More...
 
void ParseConvLayer (const caffe::LayerParameter &layerParam)
 
void ParseDeconvLayer (const caffe::LayerParameter &layerParam)
 
void ParsePoolingLayer (const caffe::LayerParameter &layerParam)
 
void ParseReluLayer (const caffe::LayerParameter &layerParam)
 
void ParseLRNLayer (const caffe::LayerParameter &layerParam)
 
void ParseInnerProductLayer (const caffe::LayerParameter &layerParam)
 
void ParseSoftmaxLayer (const caffe::LayerParameter &layerParam)
 
void ParseEltwiseLayer (const caffe::LayerParameter &layerParam)
 
void ParseConcatLayer (const caffe::LayerParameter &layerParam)
 
void ParseBatchNormLayer (const caffe::LayerParameter &layerParam)
 
void ParseScaleLayer (const caffe::LayerParameter &layerParam)
 
void ParseSplitLayer (const caffe::LayerParameter &layerParam)
 
void ParseDropoutLayer (const caffe::LayerParameter &layerParam)
 
void ParseArgmaxLayer (const caffe::LayerParameter &layerParam)
 
void AddConvLayerWithSplits (const caffe::LayerParameter &layerParam, const armnn::Convolution2dDescriptor &desc, unsigned int kernelW, unsigned int kernelH)
 ParseConv may use these helpers depending on the group parameter. More...
 
void AddConvLayerWithDepthwiseConv (const caffe::LayerParameter &layerParam, const armnn::Convolution2dDescriptor &desc, unsigned int kernelW, unsigned int kernelH)
 
void AddDeconvLayerWithSplits (const caffe::LayerParameter &layerParam, const armnn::TransposeConvolution2dDescriptor &desc, unsigned int kernelW, unsigned int kernelH)
 
- Static Protected Member Functions inherited from ICaffeParser::CaffeParserImpl
static void TrackBindingPoint (armnn::IConnectableLayer *layer, armnn::LayerBindingId id, const armnn::TensorInfo &tensorInfo, const char *bindingPointDesc, std::unordered_map< std::string, BindingPointInfo > &nameToBindingInfo)
 
static std::pair< armnn::LayerBindingId, armnn::TensorInfoGetBindingInfo (const std::string &layerName, const char *bindingPointDesc, const std::unordered_map< std::string, BindingPointInfo > &bindingInfos)
 
- Protected Attributes inherited from ICaffeParser::CaffeParserImpl
std::unordered_map< std::string, BindingPointInfom_NetworkInputsBindingInfo
 maps input layer names to their corresponding ids and tensor infos More...
 
std::unordered_map< std::string, BindingPointInfom_NetworkOutputsBindingInfo
 maps output layer names to their corresponding ids and tensor infos More...
 
armnn::INetworkPtr m_Network
 
std::map< std::string, armnn::TensorShapem_InputShapes
 
std::unordered_map< std::string, armnn::IOutputSlot * > m_ArmnnOutputSlotForCaffeTop
 As we add armnn layers we store the armnn IOutputSlot which corresponds to the Caffe tops. More...
 
std::vector< std::string > m_RequestedOutputs
 
std::map< std::string, const caffe::LayerParameter * > m_CaffeLayersByTopName
 
- Static Protected Attributes inherited from ICaffeParser::CaffeParserImpl
static const std::map< std::string, OperationParsingFunctionms_CaffeLayerNameToParsingFunctions
 Maps Caffe layer names to parsing member functions. More...
 

Detailed Description

Definition at line 25 of file RecordByRecordCaffeParser.hpp.

Constructor & Destructor Documentation

◆ RecordByRecordCaffeParser()

Member Function Documentation

◆ CreateNetworkFromBinaryFile()

armnn::INetworkPtr CreateNetworkFromBinaryFile ( const char *  graphFile,
const std::map< std::string, armnn::TensorShape > &  inputShapes,
const std::vector< std::string > &  requestedOutputs 
)
overridevirtual

Create the network from a protobuf binary file on disk.

Implements ICaffeParser::CaffeParserImpl.

Definition at line 462 of file RecordByRecordCaffeParser.cpp.

References ICaffeParser::CaffeParserImpl::Cleanup(), IOutputSlot::Connect(), INetwork::Create(), ICaffeParser::CaffeParserImpl::GetArmnnOutputSlotForCaffeTop(), armnn::info, ICaffeParser::CaffeParserImpl::m_InputShapes, ICaffeParser::CaffeParserImpl::m_Network, ICaffeParser::CaffeParserImpl::m_NetworkInputsBindingInfo, ICaffeParser::CaffeParserImpl::m_NetworkOutputsBindingInfo, ICaffeParser::CaffeParserImpl::ms_CaffeLayerNameToParsingFunctions, armnn::numeric_cast(), ICaffeParser::CaffeParserImpl::ResolveInPlaceLayers(), and ICaffeParser::CaffeParserImpl::TrackOutputBinding().

466 {
467 
468  m_InputShapes = inputShapes;
469  if (requestedOutputs.size() == 0)
470  {
471  throw armnn::ParseException("requestedOutputs must have at least one entry");
472  }
473  m_RequestedOutputs = requestedOutputs;
474 
475  std::ifstream ifs(graphFile, std::ifstream::in|std::ifstream::binary);
476  if (ifs.fail())
477  {
478  throw armnn::FileNotFoundException("Failed to open graph file '" + std::string(graphFile) + "'");
479  }
480 
481  std::vector<LayerParameterInfo> layerInfo;
482  NetParameterInfo netParameterInfo;
483  while(true)
484  {
485  ProtobufFieldInfo fieldInfo = readFieldInfo(ifs);
486  if (fieldInfo.eof())
487  {
488  break;
489  }
490  switch(fieldInfo.field_type())
491  {
492  case 0:
493  {
494  ReadBase128(ifs);
495  break;
496  }
497  case 2:
498  {
499  // The values of interest from the caffe.proto schema are:
500  // optional string name = 1; // consider giving the network a name
501  // DEPRECATED. See InputParameter. The input blobs to the network.
502  // repeated string input = 3;
503  // DEPRECATED. See InputParameter. The shape of the input blobs.
504  // repeated BlobShape input_shape = 8;
505 
506  // 4D input dimensions -- deprecated. Use "input_shape" instead.
507  // If specified, for each input blob there should be four
508  // values specifying the num, channels, height and width of the input blob.
509  // Thus, there should be a total of (4 * #input) numbers.
510  // repeated int32 input_dim = 4;
511 
512  // The layers that make up the net. Each of their configurations, including
513  // connectivity and behavior, is specified as a LayerParameter.
514  // repeated LayerParameter layer = 100; // ID 100 so layers are printed last.
515 
516  // The first four will (if present) be read into the NetParameterInfo
517  // the LayerParameters will be read into the LayerParameterInfo vector.
518 
519  int size = ReadBase128(ifs);
520  std::streamoff posStartOfData = ifs.tellg();
521  ifs.seekg(size, std::ios_base::cur);
522  if(!ifs.good())
523  {
524  throw armnn::ParseException("failed to seek ahead in binary caffe file");
525  }
526  std::streamoff endOfLayer = ifs.tellg();
527  if (fieldInfo.field_id() == 1)
528  {
529  VarLenDataInfo dataInfo = CreateVarLenDataInfo(posStartOfData, endOfLayer);
530  auto graphName = AllocateBuffer(ifs, dataInfo);
531  netParameterInfo.set_name(graphName, dataInfo.SizeOfData());
532  }
533  if (fieldInfo.field_id() == 3)
534  {
535  VarLenDataInfo dataInfo = CreateVarLenDataInfo(posStartOfData, endOfLayer);
536  auto inputName = AllocateBuffer(ifs, dataInfo);
537  netParameterInfo.add_input(inputName, dataInfo.SizeOfData());
538  }
539  if (fieldInfo.field_id() == 8)
540  {
541  VarLenDataInfo dataInfo = CreateVarLenDataInfo(posStartOfData, endOfLayer);
542  auto inputShape = AllocateBuffer(ifs, dataInfo);
543  caffe::BlobShape blobShape;
544  bool bRet = blobShape.ParseFromArray(inputShape.get(), static_cast<int>(dataInfo.SizeOfData()));
545  if (!bRet)
546  {
547  throw armnn::ParseException("Failed to parse input shape");
548  }
549  netParameterInfo.add_blob_shape(blobShape);
550  }
551  if (fieldInfo.field_id() == 4)
552  {
553  int input_dim = ReadBase128(ifs);
554  netParameterInfo.add_input_dimension(input_dim);
555  }
556  if (fieldInfo.field_id() == 100)
557  {
558  LayerParameterInfo info(CreateVarLenDataInfo(posStartOfData, endOfLayer));
559  ReadTopologicalInfoForLayerParameter(info, ifs);
560  layerInfo.push_back(info);
561  }
562  break;
563  }
564  default:
565  {
566  break;
567  }
568  }
569  }
570  std::vector<const LayerParameterInfo*> sortedNodes;
571  ProcessLayers(netParameterInfo, layerInfo, m_RequestedOutputs, sortedNodes);
572  armnn::INetworkPtr networkPtr = LoadLayers(ifs, sortedNodes, netParameterInfo);
573  return networkPtr;
574 
575 }
std::map< std::string, armnn::TensorShape > m_InputShapes
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:173

The documentation for this class was generated from the following files: