运维开发网

CentOS 6 编译 TensorFlow for Java 以及 Maven Pom

运维开发网 https://www.qedev.com 2020-04-28 14:40 出处:网络 作者:运维开发网整理
我们的系统环境 CentOS 6.5, JDK 1.8 更新yum源 $ yum update 安装 Python 2.7 $ yum install python27 python27-numpy python27-python-devel python27-python-wheel
  • 我们的系统环境

    CentOS 6.5, JDK 1.8

  • 更新yum源

    $ yum update

  • 安装 Python 2.7

    $ yum install python27 python27-numpy python27-python-devel python27-python-wheel

  • 升级 gcc 至 4.8.2

    $ cd /opt/

    $ wget http://people.centos.org/tru/devtools-2/devtools-2.repo -O /etc/yum.repos.d/devtools-2.repo

    $ yum install --nogpg -y zip unzip patch libcurl-devel git devtoolset-2-gcc devtoolset-2-binutils devtoolset-2-gcc-c++

    $ scl enable devtoolset-2 python27 bash

    $ gcc -v

  • JDK版本

    1.8(如1.8.0_73)

  • 安装bazel(编译不同版本的tensorflow,需要不同版本的bazel。这里编译tensorflow_v1.6.0,需要bazel_0.9.0)

    $ cd /opt

    $ wget https://github.com/bazelbuild/bazel/releases/download/0.9.0/bazel-0.9.0-dist.zip

    $ unzip bazel-0.9.0-dist.zip -d bazel-0.9.0-dist

    $ cd bazel-0.9.0-dist

    $ ./compile.sh

    $ mkdir -p ~/bin

    $ cp output/bazel ~/bin/

  • 编译tensorflow

    $ cd /opt

    $ git clone https://github.com/tensorflow/tensorflow && cd tensorflow

    $ git checkout v1.6.0

    $ ./configure

    配置中提示的问题,全部选择n(no)

    在执行bazel build之前,

    把文件/opt/tensorflow/tensorflow/tensorflow.bzl中的代码片段

def tf_cc_shared_object(
    name,
    srcs=[],
    deps=[],
    linkopts=[],
    framework_so=tf_binary_additional_srcs(),
    **kwargs):

修改为

def tf_cc_shared_object(
    name,
    srcs=[],
    deps=[],
    linkopts=[‘-lrt‘],
    framework_so=tf_binary_additional_srcs(),
    **kwargs):

把文件/opt/tensorflow/tensorflow/java/BUILD中的代码片段

tf_cc_binary(
    name = "generate_pom",
    srcs = ["generate_pom.cc"],
    deps = ["//tensorflow/c:c_api"],
)

修改为

tf_cc_binary(
    linkopts = ["-lrt"],
    name = "generate_pom",
    srcs = ["generate_pom.cc"],
    deps = ["//tensorflow/c:c_api"],
)

$ bazel build --linkopt=‘-lrt‘ -c opt //tensorflow/java:tensorflow //tensorflow/java:libtensorflow_jni //tensorflow/java:pom

$ cd /opt/tensorflow/tensorflow/java/maven/

  • 部署 libtensorflow

    修改 /opt/tensorflow/bazel-bin/tensorflow/java/pom.xml 中的groupId, version用于deploy到自己公司的远程仓库中,并指定仓库的repositoryId和url。

    $ mvn deploy:deploy-file -Dfile=../../../bazel-bin/tensorflow/java/libtensorflow.jar -DpomFile=../../../bazel-bin/tensorflow/java/pom.xml -DrepositoryId= -Durl=

  • 部署 libtensorflow_jni

    修改/opt/tensorflow/tensorflow/java/maven/pom.xml和/opt/tensorflow/tensorflow/java/maven/libtensorflow_jni/pom.xml中的groupId, version,并添加自己公司的distributionManagement配置,同时只需保留libtensorflow_jni模块。

    $ cd /opt/tensorflow/tensorflow/java/maven/

    $ mkdir -p libtensorflow_jni/src/main/resources/org/tensorflow/native/Linux-x86_64

    $ cp ../../../bazel-bin/tensorflow/libtensorflow_framework.so libtensorflow_jni/src/main/resources/org/tensorflow/native/Linux-x86_64/

    $ cp ../../../bazel-bin/tensorflow/java/libtensorflow_jni.so libtensorflow_jni/src/main/resources/org/tensorflow/native/Linux-x86_64/

    $ mvn versions:set -DnewVersion="${TENSORFLOW_VERSION}-cpu-optimized"

    $ mvn package -Dgpg.skip=true

    $ mvn deploy -Dgpg.skip=true

  • 参考资料
    • TensorFlow for Java
    • installing-tensorflow-from-sources
    • Install Tensorflow on Centos6.4
    • Building TensorFlow for CentOS 6
    • Building Tensorflow Java Maven dependency for upload in internal repo
    • install tensorflow on centos 6.5
    • 从源代码安装 TensorFlow
0

精彩评论

暂无评论...
验证码 换一张
取 消