Skip to content

Commit

Permalink
Add Onnx2NeoML executable (#507)
Browse files Browse the repository at this point in the history
* Add Onnx2NeoML executable

Signed-off-by: Boris Zimka <boris.zimka@abbyy.com>

* Change Onnx2NeoML binary dir to default

Signed-off-by: Boris Zimka <boris.zimka@abbyy.com>

* Change Onnx2NeoML project name

Signed-off-by: Boris Zimka <boris.zimka@abbyy.com>

* Explicitly define Onnx2NeoML Help as static

Signed-off-by: Boris Zimka <boris.zimka@abbyy.com>

* Replace constexpr with const

Signed-off-by: Boris Zimka <boris.zimka@abbyy.com>

* Turn off Onnx2NeoML for mobile

Signed-off-by: Boris Zimka <boris.zimka@abbyy.com>

* Fix code review

Signed-off-by: Boris Zimka <boris.zimka@abbyy.com>

Co-authored-by: Boris Zimka <boris.zimka@abbyy.com>
Co-authored-by: Valeriy Fedyunin <valery.fedyunin@abbyy.com>
  • Loading branch information
3 people committed Dec 22, 2021
1 parent 77dfc6c commit 2d0e109
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
3 changes: 3 additions & 0 deletions NeoML/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ if(NeoML_INSTALL)
set(INSTALLED_TARGETS NeoML NeoMathEngine)
if(NeoOnnx_BUILD)
list(APPEND INSTALLED_TARGETS NeoOnnx)
if (NOT ANDROID AND NOT IOS)
list(APPEND INSTALLED_TARGETS Onnx2NeoML)
endif()
endif()

if(USE_FINE_OBJECTS)
Expand Down
5 changes: 5 additions & 0 deletions NeoOnnx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ add_subdirectory(src)
if(NeoML_INSTALL)
install(DIRECTORY include/NeoOnnx DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
endif()

# Build Onnx2NeoML
if(NOT ANDROID AND NOT IOS)
add_subdirectory(Onnx2NeoML)
endif()
13 changes: 13 additions & 0 deletions NeoOnnx/Onnx2NeoML/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.11 FATAL_ERROR)

project( Onnx2NeoML LANGUAGES CXX)

include(Version)
include(Settings)
include(GNUInstallDirs)

set_global_variables()

add_executable(Onnx2NeoML Onnx2NeoML.cpp)

target_link_libraries(Onnx2NeoML PRIVATE NeoML NeoOnnx NeoMathEngine)
65 changes: 65 additions & 0 deletions NeoOnnx/Onnx2NeoML/Onnx2NeoML.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/* Copyright © 2017-2020 ABBYY Production LLC
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
--------------------------------------------------------------------------------------------------------------*/
#include <string>
#include <NeoOnnx/NeoOnnx.h>
#include <NeoMathEngine/NeoMathEngine.h>
#include <NeoML/Random.h>
#include <iostream>

using namespace NeoML;

int ConvertOnnx2NeoML( const char* inputOnnxFilename, const char* outputDnnArchiveFileName )
{
CArray<const char*> inputs;
CArray<const char*> outputs;
IMathEngine& mathEng = GetDefaultCpuMathEngine();
CRandom random( 0x123 );
CDnn net( random, mathEng );
try {
NeoOnnx::LoadFromOnnx( inputOnnxFilename, net, inputs, outputs );
{
CArchiveFile file( outputDnnArchiveFileName, CArchive::store );
CArchive archive( &file, CArchive::SD_Storing );
archive.Serialize( net );
}
return 0;
} catch( std::exception& exc ) {
std::cout << "Exception" << std::endl;
std::cout << exc.what() << std::endl;
return 1;
}
};
static const char* Help =
"Usage:\n$ Onnx2NeoML <path to some existing model.onnx> <path to new saved model.dnnarchive>\n"
"Use this tool to convert your ONNX model "
"into the archived NeoML::CDnn. To be converted your model "
"must use only supported NeoOnnx operators.";


int main( int argc, char* argv[] )
{
if( argc == 1 ) {
std::cout << Help << std::endl;
return 0;
};
if( argc != 3 ) {
std::cout << "Error, expected 2 args, got " << argc - 1 << std::endl;
std::cout << Help << std::endl;
return 1;
}
const char* inpath = argv[1];
const char* outpath = argv[2];
return ConvertOnnx2NeoML( inpath, outpath );
}

0 comments on commit 2d0e109

Please sign in to comment.