ArmNN
 20.02
RecordByRecordCaffeParser Class Reference

#include <RecordByRecordCaffeParser.hpp>

Inheritance diagram for RecordByRecordCaffeParser:
CaffeParserBase ICaffeParser

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 CaffeParserBase
virtual armnn::INetworkPtr CreateNetworkFromTextFile (const char *graphFile, const std::map< std::string, armnn::TensorShape > &inputShapes, const std::vector< std::string > &requestedOutputs) override
 Create the network from a protobuf text file on disk. More...
 
virtual armnn::INetworkPtr CreateNetworkFromString (const char *protoText, const std::map< std::string, armnn::TensorShape > &inputShapes, const std::vector< std::string > &requestedOutputs) override
 Creates the network directly from protobuf text in a string. Useful for debugging/testing. More...
 
virtual BindingPointInfo GetNetworkInputBindingInfo (const std::string &name) const override
 Retrieves binding info (layer id and tensor info) for the network input identified by the given layer name. More...
 
virtual BindingPointInfo GetNetworkOutputBindingInfo (const std::string &name) const override
 Retrieves binding info (layer id and tensor info) for the network output identified by the given layer name. More...
 
 CaffeParserBase ()
 

Additional Inherited Members

- Static Public Member Functions inherited from ICaffeParser
static ICaffeParserCreateRaw ()
 
static ICaffeParserPtr Create ()
 
static void Destroy (ICaffeParser *parser)
 
- Protected Types inherited from CaffeParserBase
using OperationParsingFunction = void(CaffeParserBase::*)(const caffe::LayerParameter &layerParam)
 
- Protected Member Functions inherited from CaffeParserBase
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 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 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)
 
- Protected Member Functions inherited from ICaffeParser
virtual ~ICaffeParser ()
 
- Static Protected Member Functions inherited from CaffeParserBase
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 CaffeParserBase
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 CaffeParserBase
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.

Definition at line 464 of file RecordByRecordCaffeParser.cpp.

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

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

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