set_target_properties(${nn_import_common} PROPERTIES POSITION_INDEPENDENT_CODE ON)
add_subdirectory(tflite)
+add_subdirectory(caffe)
--- /dev/null
+# Try to get compiled caffe proto and return if not successful
+# Note: this creates a target called "caffeproto" that contains compiled caffe.proto sources,
+# and after linking with it caffe.pb.h will be available as "caffe/proto/caffe.pb.h"
+# Note2: DOWNLOAD_CAFFE cmake option should be on
+nncc_find_package(CaffeProto QUIET)
+if(NOT CaffeProto_FOUND)
+ return()
+endif()
+
+###################
+# Caffe importer #
+###################
+
+file(GLOB caffe_importer_sources ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp)
+file(GLOB caffe_importer_headers ${CMAKE_CURRENT_SOURCE_DIR}/include/*.h)
+
+add_nncc_library(caffe_importer SHARED ${caffe_importer_sources}
+ ${caffe_importer_headers})
+
+target_include_directories(caffe_importer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
+
+target_link_libraries(caffe_importer PUBLIC caffeproto)
+target_link_libraries(caffe_importer PUBLIC ${nn_import_common})
+target_link_libraries(caffe_importer PRIVATE nncc_core)
--- /dev/null
+#ifndef NNCC_PROTO_READER_H
+#define NNCC_PROTO_READER_H
+
+#include "google/protobuf/io/coded_stream.h"
+#include "google/protobuf/io/zero_copy_stream_impl.h"
+#include "google/protobuf/text_format.h"
+
+namespace nncc
+{
+namespace contrib
+{
+namespace frontend
+{
+namespace caffe
+{
+namespace util
+{
+
+using google::protobuf::io::FileInputStream;
+using google::protobuf::io::ZeroCopyInputStream;
+using google::protobuf::io::CodedInputStream;
+
+bool readProtoFromTextFile(const char* filename, ::caffe::NetParameter* proto);
+bool readProtoFromBinaryFile(const char* filename, ::caffe::NetParameter* proto);
+
+} // namespace util
+} // namespace caffe
+} // namespace frontend
+} // namespace contrib
+} // namespace nncc
+
+#endif // NNCC_PROTO_READER_H
--- /dev/null
+#include <iostream>
+#include <fcntl.h>
+#include <unistd.h>
+#include <memory>
+
+#include "caffe/proto/caffe.pb.h"
+
+#include "proto_reader.h"
+
+namespace nncc
+{
+namespace contrib
+{
+namespace frontend
+{
+namespace caffe
+{
+namespace util
+{
+
+const int protoBytesLimit = INT_MAX;
+const int protoBytesWarningLimit = 1024 * 1024 * 512;
+
+bool readProtoFromTextFile(const char* filename, ::caffe::NetParameter* proto)
+{
+ int fd = open(filename, O_RDONLY);
+ if (fd == -1)
+ {
+ std::cout << "File not found: " << filename << std::endl;
+ return false;
+ }
+
+ FileInputStream input{fd};
+
+ bool success = google::protobuf::TextFormat::Parse(&input, proto);
+
+ close(fd);
+
+ return success;
+}
+
+bool readProtoFromBinaryFile(const char* filename, ::caffe::NetParameter* proto)
+{
+ int fd = open(filename, O_RDONLY);
+ if (fd == -1)
+ {
+ std::cout << "File not found: " << filename << std::endl;
+ return false;
+ }
+
+ FileInputStream raw_input{fd};
+ CodedInputStream coded_input{&raw_input};
+ coded_input.SetTotalBytesLimit(protoBytesLimit, protoBytesWarningLimit);
+
+ bool success = proto->ParseFromCodedStream(&coded_input);
+
+ close(fd);
+
+ return success;
+}
+
+} // namespace util
+} // namespace caffe
+} // namespace frontend
+} // namespace contrib
+} // namespace nncc