Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/xwi88/gp

go predict with tensorflow, pytorch and others
https://github.com/xwi88/gp

go prediction tensorflow tensorflow-examples

Last synced: 17 days ago
JSON representation

go predict with tensorflow, pytorch and others

Awesome Lists containing this project

README

        

# 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

- [tensorflow](https://github.com/tensorflow/tensorflow)
- [tensorflow-serving](https://github.com/tensorflow/serving)

### Go API

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

### C Lib

- [download](https://www.tensorflow.org/install/lang_c#download)
- [libtensorflow-cpu-darwin-x86_64-2.3.0](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-darwin-x86_64-2.3.0.tar.gz)
- [libtensorflow-cpu-linux-x86_64-2.3.0](https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-linux-x86_64-2.3.0.tar.gz)

```bash
#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**

```bash
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*

```bash
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

```cgo
#include
#include

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

```bash
# 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||

- more docker images ref: [v8fg/docker-compose-resources](https://github.com/v8fg/docker-compose-resources)
- official images: [TensorFlow Docker](https://www.tensorflow.org/install/docker)

## Quick run in local

```bash
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

- [version incompatible ref](https://github.com/tensorflow/tensorflow/issues/41808)
- https://github.com/photoprism/photoprism/pull/775
- [hack fixed, only ref](https://github.com/tensorflow/tensorflow/blob/master/tensorflow/go/README.md)
- [saved_model_half_plus_two](https://github.com/tensorflow/serving/blob/master/tensorflow_serving/servables/tensorflow/testdata/saved_model_half_plus_two.py)
- input, output, tags