#include <cassert>
#include <cerrno>
#include <cstring>
+#include <stdex/Memory.h>
#include <stdexcept>
+#include <utility>
#include <vector>
namespace nnc
using mir::Shape;
Caffe2Importer::Caffe2Importer(std::string predict_net, std::string init_net,
- std::vector<std::vector<int>> input_shapes)
- : _predictNet(std::move(predict_net)), _initNet(std::move(init_net)), _graph(new mir::Graph()),
- _opCreator(new Caffe2OpCreator(_graph))
+ const std::vector<std::vector<int>> &input_shapes)
+ : _predictNet(std::move(predict_net)), _initNet(std::move(init_net))
{
for (auto &shape : input_shapes)
_inputShapes.emplace_back(shape);
+
+ _graph = stdex::make_unique<mir::Graph>();
+ _opCreator = stdex::make_unique<Caffe2OpCreator>(_graph.get());
}
Caffe2Importer::~Caffe2Importer() = default;
-void Caffe2Importer::cleanup() { delete _graph; }
-
static void loadModelFile(const std::string &filename, caffe2::NetDef *net)
{
+ GOOGLE_PROTOBUF_VERIFY_VERSION;
+
int file_handle = open(filename.c_str(), O_RDONLY);
if (file_handle == -1)
void Caffe2Importer::import()
{
- GOOGLE_PROTOBUF_VERIFY_VERSION;
-
- _net.reset(new NetDef());
+ _net = stdex::make_unique<NetDef>();
loadModelFile(_predictNet, _net.get());
- std::unique_ptr<NetDef> net2;
- net2.reset(new NetDef());
+ auto net2 = stdex::make_unique<NetDef>();
loadModelFile(_initNet, net2.get());
_net->MergeFrom(*net2);
preloadAllTensors();
}
-mir::Graph *Caffe2Importer::createIR()
+std::unique_ptr<mir::Graph> Caffe2Importer::createIR()
{
for (auto &op : _net->op())
createMIRNodesFromOp(op);
setGraphOutputs();
- return _graph;
+ return std::move(_graph);
}
std::unique_ptr<mir::Graph> Caffe2Importer::importModel()
{
import();
- return std::unique_ptr<mir::Graph>(createIR());
+ return createIR();
}
void Caffe2Importer::collectUnsupportedOps()
{
public:
explicit Caffe2Importer(std::string predict_net, std::string init_net,
- std::vector<std::vector<int>> input_shapes);
-
- /**
- * @brief Import model from file, must be called before 'createIR' method
- * @throw PassException in case, if model couldn't be parsed or NNC doesn't support it
- */
- void import();
-
- /**
- * @brief Create MIR graph from caffe model, must be called after 'import' method
- * @return MIR graph, corresponding to processed caffe model
- */
- mir::Graph *createIR();
+ const std::vector<std::vector<int>> &input_shapes);
/// @brief Load the model and convert it into a MIR Graph.
std::unique_ptr<mir::Graph> importModel();
- void cleanup();
-
~Caffe2Importer();
private:
std::string _predictNet;
std::string _initNet;
- mir::Graph *_graph;
+ std::unique_ptr<mir::Graph> _graph;
std::unique_ptr<::caffe2::NetDef> _net;
std::unique_ptr<Caffe2OpCreator> _opCreator;
std::vector<mir::Shape> _inputShapes;
std::map<std::string, mir::TensorVariant> _MIRTensors;
+ void import();
+ std::unique_ptr<mir::Graph> createIR();
+
/**
* @brief Pass through caffe2 graph and collect ops unsupported by NNC
* @throw PassException with message, containing detected problems