Skip to content
/ gp Public

go predict with tensorflow, pytorch and others

License

Notifications You must be signed in to change notification settings

xwi88/gp

Repository files navigation

gp

go predict with tensorflow, pytorch and others

Env

work well in

  • go 1.16+
  • os: x86_64, Ubuntu 14.04+ | MacOS
  • Tensorflow C API: 2.3.0
  • github.com/tensorflow/tensorflow v2.1.3 or v2.1.4

Tensorflow

Ref

Go API

https://www.tensorflow.org/install/lang_go

C Lib

#export LIB_TENSORFLOW_FILE=libtensorflow-cpu-darwin-x86_64-2.3.0.tar.gz
export LIB_TENSORFLOW_FILE=libtensorflow-cpu-linux-x86_64-2.3.0.tar.gz

Standard Install

tar -C /usr/local -xzf ${LIB_TENSORFLOW_FILE}
# linux
ldconfig

# macos 11.2.2+ work well
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH}:/usr/local/lib"

Non Sys Dir Install

export LIB_TENSORFLOW_INSTALL_DIR=~/mydir
tar -C ${LIB_TENSORFLOW_INSTALL_DIR} -xzf ${LIB_TENSORFLOW_FILE}

# config linker
export LIBRARY_PATH=$LIBRARY_PATH:${LIB_TENSORFLOW_INSTALL_DIR}/lib # For both Linux and macOS X
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:${LIB_TENSORFLOW_INSTALL_DIR}/lib # For Linux only
export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:${LIB_TENSORFLOW_INSTALL_DIR}/lib # For macOS X only

C Lib Check

#include <stdio.h>
#include <tensorflow/c/c_api.h>

int main() {
  printf("Hello from TensorFlow C library version %s\n", TF_Version());
  return 0;
}
  • gcc hello_tf.c -ltensorflow -o hello_tf
  • ./hello_tf

Go Usage

go get github.com/xwi88/gp

Example

# register model
RegisterTFModelWithParamName(modelName, exportDir, tags, []string{"param_name_input"}, "param_name_output")

# predict.go
# get model
GetModel(modelName)
# predict with the special model
output, err := Predict(modelName, input ...interface{})

Params Look Up

  • pip install tensorflow
  • saved_model_cli show --all --dir output/keras

Docker Images

tf version now will be ok in: macos & ubuntu16.04

image repos target notes
v8fg/ubuntu:22.04-go1.17.4-tf-cpu build
v8fg/ubuntu:22.04-tf-cpu run
v8fg/ubuntu:16.04-go1.16-tf-cpu build
v8fg/ubuntu:16.04-tf-cpu run

Quick run in local

wget https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-darwin-x86_64-2.3.0.tar.gz
export LIB_TENSORFLOW_FILE=libtensorflow-cpu-darwin-x86_64-2.3.0.tar.gz
tar -C /usr/local -xzf ${LIB_TENSORFLOW_FILE}

# ll /usr/local/lib/libtensorflow*

-r-xr-xr-x  1 root  wheel   331M Jan  1  2000 /usr/local/lib/libtensorflow.2.3.0.dylib
lrwxr-xr-x  1 root  wheel    25B Jan  1  2000 /usr/local/lib/libtensorflow.2.dylib -> libtensorflow.2.3.0.dylib
lrwxr-xr-x  1 root  wheel    21B Jan  1  2000 /usr/local/lib/libtensorflow.dylib -> libtensorflow.2.dylib
-r-xr-xr-x  1 root  wheel    28M Jan  1  2000 /usr/local/lib/libtensorflow_framework.2.3.0.dylib
lrwxr-xr-x  1 root  wheel    35B Jan  1  2000 /usr/local/lib/libtensorflow_framework.2.dylib -> libtensorflow_framework.2.3.0.dylib
lrwxr-xr-x  1 root  wheel    31B Jan  1  2000 /usr/local/lib/libtensorflow_framework.dylib -> libtensorflow_framework.2.dylib

# ll /usr/local/include/tensorflow/*

-r-xr-xr-x  1 root  wheel    77K Jan  1  2000 c_api.h
-r-xr-xr-x  1 root  wheel    13K Jan  1  2000 c_api_experimental.h
drwxr-xr-x  5 root  wheel   160B Jun 11 00:24 eager
-r-xr-xr-x  1 root  wheel   2.4K Jan  1  2000 tensor_interface.h
-r-xr-xr-x  1 root  wheel   1.2K Jan  1  2000 tf_attrtype.h
-r-xr-xr-x  1 root  wheel   2.7K Jan  1  2000 tf_datatype.h
-r-xr-xr-x  1 root  wheel   1.2K Jan  1  2000 tf_file_statistics.h
-r-xr-xr-x  1 root  wheel   3.0K Jan  1  2000 tf_status.h
-r-xr-xr-x  1 root  wheel   7.4K Jan  1  2000 tf_tensor.h

Tips