简介
在下是一家互联网公司的Java开发,身在nlp小组,自然要接触很多模型,我的任务就是tensoflow模型的工程部署,前不久我们开始使用tensorflow1.14,所以对应Java这边也需要使用tf1.14导入模型(PB文件),这里就来说说官方jar包的问题。
报错
Windows下无异常,Linux下服务启动时报:libtensorflow_framework.so.1 cannot open shared object file no such file or directory
原因
官方jar包在解压动态链接库时,没有解压libtensorflow_framework.so.1,而是解压了libtensorflow_framework.so,而tensorflow1.14需要前者。详见https://github.com/tensorflow/tensorflow/pull/32829
解决方法
在项目中新建包org.tensorflow,即在src/main/java下新建org.tensorflow
新建一个类NativeLibrary:
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
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.
==============================================================================*/
package org.tensorflow;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* Helper class for loading the TensorFlow Java native library.
*
* <p>The Java TensorFlow bindings require a native (JNI) library. This library
* (libtensorflow_jni.so on Linux, libtensorflow_jni.dylib on OS X, tensorflow_jni.dll on Windows)
* can be made available to the JVM using the java.library.path System property (e.g., using
* -Djava.library.path command-line argument). However, doing so requires an additional step of
* configuration.
*
* <p>Alternatively, the native libraries can be packaed in a .jar, making them easily usable from
* build systems like Maven. However, in such cases, the native library has to be extracted from the
* .jar archive.
*
* <p>NativeLibrary.load() takes care of this. First looking for the library in java.library.path
* and failing that, it tries to find the OS and architecture specific version of the library in the
* set of ClassLoader resources (under org/tensorflow/native/OS-ARCH). The resources paths used for
* lookup must be consistent with any packaging (such as on Maven Central) of the TensorFlow Java
* native libraries.
*/
final class NativeLibrary {
private static final boolean DEBUG =
System.getProperty("org.tensorflow.NativeLibrary.DEBUG") != null;
private static final String JNI_LIBNAME = "tensorflow_jni";
public static void load() {
if (isLoaded() || tryLoadLibrary()) {
// Either:
// (1) The native library has already been statically loaded, OR
// (2) The required native code has been statically linked (through a custom launcher), OR
// (3) The native code is part of another library (such as an application-level library)
// that has already been loaded. For example, tensorflow/examples/android and
// tensorflow/tools/android/inference_interface include the required native code in
// differently named libraries.
//
// Doesn't matter how, but it seems the native code is loaded, so nothing else to do.
return;
}
// Native code is not present, perhaps it has been packaged into the .jar file containing this.
// Extract the JNI library itself
final String jniLibName = System.mapLibraryName(JNI_LIBNAME);
final String jniResourceName = makeResourceName(jniLibName);
log("jniResourceName: " + jniResourceName);
final InputStream jniResource =
NativeLibrary.class.getClassLoader().getResourceAsStream(jniResourceName);
// Extract the JNI's dependency
final String frameworkLibName =
getVersionedLibraryName(System.mapLibraryName("tensorflow_framework"));
final String frameworkResourceName = makeResourceName(frameworkLibName);
log("frameworkResourceName: " + fram