Skip to content

Instantly share code, notes, and snippets.

@cheyang
Created February 26, 2017 07:13
Show Gist options
  • Save cheyang/f9db97e39c9de687357e75cad5a5a01d to your computer and use it in GitHub Desktop.
Save cheyang/f9db97e39c9de687357e75cad5a5a01d to your computer and use it in GitHub Desktop.

Revisions

  1. cheyang created this gist Feb 26, 2017.
    35 changes: 35 additions & 0 deletions generate_tf_proto.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    #!/bin/bash
    # This script is used to compile Tensorflow Serving protobuf definition into Python
    # The generated definitions in Python are stored in the tensorflow_serving_apis folder
    # Usage:
    # ./compile_ts_serving_proto.sh 333325e413e9680d67ae90196fa123f5271fcf615
    #
    : ${1?”Error. Please provide the Tensorflow Serving git commit hash/branch name. Usage: ./compile_ts_serving_proto.sh my_awesome_branch “}
    script_dir=”$( cd$( dirname “${BASH_SOURCE[0]})&& pwd )
    ts_git_revision=$1 #branch/release or commit hash
    local_ts_api_dir=”${script_dir}/tensorflow_serving_apis/” #directory that stores the compiled python proto definition
    rm -rf build
    mkdir build && cd build
    # Check out the Tensor Serving for the revision required
    git clone — recurse-submodules https://github.com/tensorflow/serving.git
    cd${script_dir}/build/serving” && git checkout ${ts_git_revision}
    # Organize the directory to match the proto imports expected
    mv tensorflow tensorflow_base && mv tensorflow_base/tensorflow tensorflow
    echo “Checked out Tensorflow Serving revision: ${ts_git_revision}
    # Copy the Protobuf definitions from TS Serving APIs
    cp tensorflow_serving/apis/*.py “${local_ts_api_dir}/tensorflow_serving/apis/”
    # Download Protobuf Compiler
    # Note this assume that you are running the script from Mac. Change the download path for the protobuf if you’re on different operating system
    PROTOBUF_VERSION=$(grep ‘set(PROTOBUF_URL’ “${script_dir}/build/serving/tensorflow/contrib/cmake/external/protobuf.cmake” | grep -oE ‘v[0–9.]+’)
    echo “Downloading Protobuf version: ${PROTOBUF_VERSION}
    curl -sLOS “https://github.com/google/protobuf/releases/download/${PROTOBUF_VERSION}/protoc-${PROTOBUF_VERSION#"v"}-osx-x86_64.zip"
    unzip “protoc-${PROTOBUF_VERSION#”v”}-osx-x86_64.zip”
    echo “Compiling the Protobuf definition”
    bin/protoc — proto_path=”${script_dir}/build/serving” — python_out=${local_ts_api_dir}${script_dir}/build/serving/tensorflow_serving/apis/model.proto”
    bin/protoc — proto_path=”${script_dir}/build/serving” — python_out=${local_ts_api_dir}${script_dir}/build/serving/tensorflow_serving/apis/predict.proto”
    bin/protoc — proto_path=”${script_dir}/build/serving” — python_out=${local_ts_api_dir}${script_dir}/build/serving/tensorflow_serving/apis/prediction_service.proto”
    touch ${script_dir}/build/serving/setup.py
    touch ${script_dir}/build/serving/tensorflow_serving/__init__.py
    touch ${script_dir}/build/serving/tensorflow_serving/apis/__init__.py
    cp ${local_ts_api_dir}/*.py ${script_dir}/build/serving/tensorflow_serving/apis/
    echo “All done. Check this directory for the compiled python definitions: ${local_ts_api_dir}