ONE - On-device Neural Engine
Loading...
Searching...
No Matches
caffe_importer.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "caffe_importer.h"
18#include "caffe/proto/caffe.pb.h"
19#include "caffe_op_creator.h"
20#include "caffe_op_types.h"
21
22#include "mir/ops/OutputOp.h"
23
24#include <google/protobuf/io/zero_copy_stream_impl.h>
25#include <google/protobuf/io/coded_stream.h>
26#include <google/protobuf/text_format.h>
27
28#include <fcntl.h>
29
30#include <cassert>
31#include <cerrno>
32#include <cstring>
33#include <memory>
34#include <stdexcept>
35#include <utility>
36#include <vector>
37#include <set>
38
39namespace mir_caffe
40{
41
42namespace
43{
44
45class CaffeImporter
46{
47public:
49 std::unique_ptr<mir::Graph> importModelFromBinaryFile(const std::string &filename);
50 std::unique_ptr<mir::Graph> importModelFromTextFile(const std::string &filename);
51
52private:
53 std::unique_ptr<mir::Graph> importModel();
54
55 std::unique_ptr<caffe::NetParameter> _net;
56 std::unique_ptr<CaffeOpCreator> _opCreator;
57
58 // Maps Caffe blob names to corresponding MIR operation outputs.
59 std::map<std::string, mir::Operation::Output *> _blobNameToOpOutput;
60
61 static const std::map<std::string, CaffeOpType> _operatorTypes;
62
66 void setGraphOutputs(mir::Graph *graph);
67
72 void collectUnsupportedLayers();
73
77 void createMIRNodesFromLayer(const caffe::LayerParameter &layer);
78
79 mir::Operation::Output *getOutputForBlob(const std::string &blob_name) const;
80 void setOutputForBlob(const std::string &blob_name, mir::Operation::Output *output);
81
85 void collectUnsupportedOp(const caffe::LayerParameter &layer, std::set<std::string> &problems);
86
90 std::vector<mir::Operation::Output *> getMIRInputsForLayer(const caffe::LayerParameter &layer);
91
92 void processDeprecatedInput();
93};
94
95void loadModelFromBinaryFile(const std::string &filename, caffe::NetParameter *net)
96{
97 GOOGLE_PROTOBUF_VERIFY_VERSION;
98
99 int file_handle = open(filename.c_str(), O_RDONLY);
100
101 if (file_handle == -1)
102 throw std::runtime_error("Couldn't open file \"" + filename + "\": " + std::strerror(errno) +
103 ".");
104
105 google::protobuf::io::FileInputStream file_stream(file_handle);
106 file_stream.SetCloseOnDelete(true);
107
108 google::protobuf::io::CodedInputStream coded_stream(&file_stream);
109 coded_stream.SetTotalBytesLimit(INT_MAX, INT_MAX);
110
111 if (!net->ParseFromCodedStream(&coded_stream))
112 throw std::runtime_error("Couldn't parse file \"" + filename + "\".");
113
114 // If the file has not been consumed entirely, assume that the file is in the wrong format.
115 if (!coded_stream.ConsumedEntireMessage())
116 throw std::runtime_error("File \"" + filename + "\" has not been consumed entirely.");
117}
118
119void loadModelFromTextFile(const std::string &filename, caffe::NetParameter *net)
120{
121 GOOGLE_PROTOBUF_VERIFY_VERSION;
122
123 int file_handle = open(filename.c_str(), O_RDONLY);
124
125 if (file_handle == -1)
126 throw std::runtime_error("Couldn't open file \"" + filename + "\": " + std::strerror(errno) +
127 ".");
128
129 google::protobuf::io::FileInputStream file_stream(file_handle);
130 file_stream.SetCloseOnDelete(true);
131
132 if (!google::protobuf::TextFormat::Parse(&file_stream, net))
133 throw std::runtime_error("Couldn't parse file \"" + filename + "\".");
134}
135
136std::unique_ptr<mir::Graph> CaffeImporter::importModel()
137{
138 auto graph = std::make_unique<mir::Graph>();
139 _opCreator = std::make_unique<CaffeOpCreator>(graph.get());
140
141 collectUnsupportedLayers();
142
143 for (int i = 0; i < _net->layer_size(); ++i)
144 createMIRNodesFromLayer(_net->layer(i));
145
146 setGraphOutputs(graph.get());
147
148 return graph;
149}
150
151std::unique_ptr<mir::Graph> CaffeImporter::importModelFromBinaryFile(const std::string &filename)
152{
153 _net = std::make_unique<caffe::NetParameter>();
154 loadModelFromBinaryFile(filename, _net.get());
155
156 return importModel();
157}
158
159std::unique_ptr<mir::Graph> CaffeImporter::importModelFromTextFile(const std::string &filename)
160{
161 _net = std::make_unique<caffe::NetParameter>();
162 loadModelFromTextFile(filename, _net.get());
163
164 return importModel();
165}
166
167void CaffeImporter::collectUnsupportedLayers()
168{
169 processDeprecatedInput();
170
171 std::set<std::string> problems;
172
173 for (const caffe::LayerParameter &layer : _net->layer())
174 collectUnsupportedOp(layer, problems);
175
176 if (!problems.empty())
177 {
178 std::string msg("NNC can't load model. Detected problems:");
179 for (const auto &problemStr : problems)
180 msg.append("\n * " + problemStr);
181 throw std::runtime_error(msg);
182 }
183}
184
185void CaffeImporter::createMIRNodesFromLayer(const caffe::LayerParameter &layer)
186{
187 std::vector<mir::Operation::Output *> inputs = getMIRInputsForLayer(layer);
188 std::vector<mir::Operation::Output *> outputs;
189
190 switch (_operatorTypes.at(layer.type()))
191 {
193 outputs = _opCreator->convertInput(layer);
194 break;
196 outputs = _opCreator->convertConvolution(layer, inputs);
197 break;
199 outputs = _opCreator->convertInnerProduct(layer, inputs);
200 break;
202 outputs = _opCreator->convertPooling(layer, inputs);
203 break;
205 outputs = _opCreator->convertConcat(layer, inputs);
206 break;
208 outputs = _opCreator->convertReshape(layer, inputs);
209 break;
211 outputs = _opCreator->convertReLU(layer, inputs);
212 break;
214 outputs = _opCreator->convertSoftmax(layer, inputs);
215 break;
217 outputs = _opCreator->convertScale(layer, inputs);
218 break;
220 outputs = _opCreator->convertBatchNorm(layer, inputs);
221 break;
223 outputs = _opCreator->convertDropout(layer, inputs);
224 break;
226 outputs = _opCreator->convertTanH(layer, inputs);
227 break;
228 case CaffeOpType::ELU:
229 outputs = _opCreator->convertELU(layer, inputs);
230 break;
232 outputs = _opCreator->convertEltwise(layer, inputs);
233 break;
235 outputs = _opCreator->convertEmbed(layer, inputs);
236 break;
238 outputs = _opCreator->convertDeconvolution(layer, inputs);
239 break;
241 outputs = _opCreator->convertSplit(layer, inputs);
242 break;
244 outputs = _opCreator->convertSigmoid(layer, inputs);
245 break;
247 outputs = _opCreator->convertLSTM(layer, inputs);
248 break;
249 default:
250 assert(false && "All unsupported types should have been found before this pass.");
251 }
252
253 assert(static_cast<int>(outputs.size()) == layer.top_size() && "Number of outputs differs.");
254 for (int i = 0; i < layer.top_size(); ++i)
255 setOutputForBlob(layer.top(i), outputs[i]);
256}
257
258void CaffeImporter::collectUnsupportedOp(const caffe::LayerParameter &layer,
259 std::set<std::string> &problems)
260{
261 auto it = _operatorTypes.find(layer.type());
262 if (it == _operatorTypes.end())
263 {
264 problems.insert(layer.type() + ": unknown layer");
265 return;
266 }
267
268 CaffeOpType op_type = it->second;
269
270 switch (op_type)
271 {
279 case CaffeOpType::ELU:
285 // No checks
286 break;
289 _opCreator->checkConvolution(layer, problems);
290 break;
292 _opCreator->checkPooling(layer, problems);
293 break;
295 _opCreator->checkReshape(layer, problems);
296 break;
298 _opCreator->checkBatchNorm(layer, problems);
299 break;
301 _opCreator->checkLSTM(layer, problems);
302 break;
303 default:
304 problems.insert(layer.type() + ": unsupported layer");
305 break;
306 }
307}
308
309void CaffeImporter::processDeprecatedInput()
310{
311 if (_net->input_dim_size() != 0 || _net->input_shape_size() != 0)
312 throw std::runtime_error("Deprecated Caffe input types are not supported");
313}
314
315std::vector<mir::Operation::Output *>
316CaffeImporter::getMIRInputsForLayer(const caffe::LayerParameter &layer)
317{
318 std::vector<mir::Operation::Output *> inputs;
319
320 for (const auto &input_name : layer.bottom())
321 inputs.push_back(getOutputForBlob(input_name));
322
323 return inputs;
324}
325
326mir::Operation::Output *CaffeImporter::getOutputForBlob(const std::string &blob_name) const
327{
328 return _blobNameToOpOutput.at(blob_name);
329}
330
331void CaffeImporter::setOutputForBlob(const std::string &blob_name, mir::Operation::Output *output)
332{
333 const auto it = _blobNameToOpOutput.find(blob_name);
334 if (it != _blobNameToOpOutput.cend())
335 {
336 // caffe input blob name could be same as output blob name, and next line will overwrite
337 // '_blobNameToOpOutput' element, but in all networks that I saw it was not a problem
338 it->second->setName("");
339 }
340
341 // Do not overwrite the name in case of fall-through layers (ex. Dropout, Split).
342 // TODO Find a way to handle it properly.
343 if (output->getName().empty())
344 output->setName(blob_name);
345
346 _blobNameToOpOutput[blob_name] = output;
347}
348
349void CaffeImporter::setGraphOutputs(mir::Graph *graph)
350{
351 // TODO For now, we assume that:
352 // - there is exactly one output;
353 // - the output is from the last layer.
354 const auto &last_layer = *_net->layer().rbegin();
355 auto output = getOutputForBlob(last_layer.top(0));
357}
358
359const std::map<std::string, CaffeOpType> CaffeImporter::_operatorTypes = {
360 {"AbsVal", CaffeOpType::absVal},
361 {"Accuracy", CaffeOpType::accuracy},
362 {"ArgMax", CaffeOpType::argMax},
363 {"BatchNorm", CaffeOpType::batchNorm},
364 {"BatchReindex", CaffeOpType::batchReindex},
365 {"Bias", CaffeOpType::bias},
366 {"BNLL", CaffeOpType::BNLL},
367 {"Clip", CaffeOpType::clip},
368 {"Concat", CaffeOpType::concat},
369 {"ContrastiveLoss", CaffeOpType::contrastiveLoss},
370 {"Convolution", CaffeOpType::convolution},
371 {"Crop", CaffeOpType::crop},
372 {"Data", CaffeOpType::data},
373 {"Deconvolution", CaffeOpType::deconvolution},
374 {"Dropout", CaffeOpType::dropout},
375 {"DummyData", CaffeOpType::dummyData},
376 {"Eltwise", CaffeOpType::eltwise},
377 {"ELU", CaffeOpType::ELU},
378 {"Embed", CaffeOpType::embed},
379 {"EuclidianLoss", CaffeOpType::euclidianLoss},
380 {"Exp", CaffeOpType::exp},
381 {"Filter", CaffeOpType::filter},
382 {"Flatten", CaffeOpType::flatten},
383 {"HDF5Data", CaffeOpType::HDF5Data},
384 {"HDF5Output", CaffeOpType::HDF5Output},
385 {"HingeLoss", CaffeOpType::hingeLoss},
386 {"Im2Col", CaffeOpType::im2Col},
387 {"ImageData", CaffeOpType::imageData},
388 {"InfogainLoss", CaffeOpType::infogainLoss},
389 {"InnerProduct", CaffeOpType::innerProduct},
390 {"Input", CaffeOpType::input},
391 {"Log", CaffeOpType::log},
392 {"LRN", CaffeOpType::LRN},
393 {"LSTM", CaffeOpType::LSTM},
394 {"MemoryData", CaffeOpType::memoryData},
395 {"MultinomialLogisticLoss", CaffeOpType::multinomialLogisticLoss},
396 {"MVN", CaffeOpType::MVN},
397 {"Parameter", CaffeOpType::parameter},
398 {"Pooling", CaffeOpType::pooling},
399 {"Power", CaffeOpType::power},
400 {"PReLU", CaffeOpType::PReLU},
401 {"Python", CaffeOpType::python},
402 {"Recurrent", CaffeOpType::recurrent},
403 {"Reduction", CaffeOpType::reduction},
404 {"ReLU", CaffeOpType::ReLU},
405 {"Reshape", CaffeOpType::reshape},
406 {"RNN", CaffeOpType::RNN},
407 {"Scale", CaffeOpType::scale},
408 {"SigmoidCrossEntropyLoss", CaffeOpType::sigmoidCrossEntropyLoss},
409 {"Sigmoid", CaffeOpType::sigmoid},
410 {"Silence", CaffeOpType::silence},
411 {"Softmax", CaffeOpType::softmax},
412 {"SoftmaxWithLoss", CaffeOpType::softmaxWithLoss},
413 {"SPP", CaffeOpType::SPP},
414 {"Split", CaffeOpType::split},
415 {"Slice", CaffeOpType::slice},
416 {"TanH", CaffeOpType::tanh},
417 {"Threshold", CaffeOpType::threshold},
418 {"Tile", CaffeOpType::tile},
419 {"WindowData", CaffeOpType::windowData}};
420} // namespace
421
422std::unique_ptr<mir::Graph> importModelFromBinaryFile(const std::string &filename)
423{
424 CaffeImporter importer;
425 return importer.importModelFromBinaryFile(filename);
426}
427
428std::unique_ptr<mir::Graph> importModelFromTextFile(const std::string &filename)
429{
430 CaffeImporter importer;
431 return importer.importModelFromTextFile(filename);
432}
433
434std::unique_ptr<mir::Graph> loadModel(const std::string &filename)
435{
436 return importModelFromBinaryFile(filename);
437}
438
439} // namespace mir_caffe
Represents an output of a node.
Definition Operation.h:60
std::unique_ptr< mir::Graph > importModelFromTextFile(const std::string &filename)
std::unique_ptr< mir::Graph > loadModel(const std::string &filename)
std::unique_ptr< mir::Graph > importModelFromBinaryFile(const std::string &filename)