serving

package
v0.0.0-...-018471a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2020 License: MIT Imports: 12 Imported by: 2

README

GoDoc

Tensorflow Serving client for go

Go client for Tensorflow Serving

Example

Example uses pre-trained model found under testdata/models wide_deep

import "github.com/Applifier/go-tensorflow/serving"

// Init client
cli, _ := serving.NewModelPredictionClientFromAddr(
    getServingAddr(),
    "wide_deep",
    "serving_default",
    1527087570,
)

// Create Example and Features
example, _ := serving.NewExampleFromMap(map[string]interface{}{
    "age":            35.0,
    "capital_gain":   0.0,
    "capital_loss":   0.0,
    "education":      "Masters",
    "education_num":  14.0,
    "gender":         "Female",
    "hours_per_week": 29.0,
    "native_country": "United-States",
    "occupation":     "Prof-specialty",
    "relationship":   "Husband",
    "workclass":      "Private",
})

// Convert example to protobuf
exampleSerialized, _ := example.Marshal()

// Convert serialized example to tensor
tensor, _ := NewTensor([][]byte{exampleSerialized})

res, _ := cli.Predict(context.Background(), serving.TensorMap{
    "inputs": tensor,
}, nil)

fmt.Printf("scores %+v\n", res.Outputs["scores"].FloatVal)

// Output: scores [0.54612064 0.45387936]

Documentation

Overview

Package serving is a client for Tensorflow Serving written in Go

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewPredictor

func NewPredictor(modelClient ModelPredictionClient) predict.Predictor

NewPredictor returns a new predictor (implements Predictor interface) using a given TF Serving client

func ShapeFromTensor

func ShapeFromTensor(t *Tensor) []int64

ShapeFromTensor returns shape from a tensor

func ValueFromTensor

func ValueFromTensor(t *Tensor) interface{}

ValueFromTensor returns value from a given tensor

Types

type ModelPredictionClient

type ModelPredictionClient interface {
	// Classify.
	Classify(ctx context.Context, input *serving.Input, opts ...grpc.CallOption) (*serving.ClassificationResponse, error)
	// Regress.
	Regress(ctx context.Context, input *serving.Input, opts ...grpc.CallOption) (*serving.RegressionResponse, error)
	// Predict -- provides access to loaded TensorFlow model.
	Predict(ctx context.Context, inputs TensorMap, outputFilter []string, opts ...grpc.CallOption) (*serving.PredictResponse, error)
	// GetModelMetadata - provides access to metadata for loaded models.
	GetModelMetadata(ctx context.Context, opts ...grpc.CallOption) (*serving.GetModelMetadataResponse, error)

	io.Closer
}

ModelPredictionClient represents a prediction client for a single model

Example
// Init client
cli, _ := NewModelPredictionClientFromAddr(
	getServingAddr(),
	"wide_deep",
	"serving_default",
)

// Create Example and Features
example, _ := utils.NewExampleFromMap(map[string]interface{}{
	"age":            35.0,
	"capital_gain":   0.0,
	"capital_loss":   0.0,
	"education":      "Masters",
	"education_num":  14.0,
	"gender":         "Female",
	"hours_per_week": 29.0,
	"native_country": "United-States",
	"occupation":     "Prof-specialty",
	"relationship":   "Husband",
	"workclass":      "Private",
})

// Convert example to protobuf
exampleSerialized, _ := example.Marshal()

// Convert serialized example to tensor
tensor, _ := NewTensor([][]byte{exampleSerialized})

res, _ := cli.Predict(context.Background(), TensorMap{
	"inputs": tensor,
}, nil)

fmt.Printf("scores %+v\n", res.Outputs["scores"].FloatVal)
Output:

scores [0.54612064 0.45387936]

func NewModelPredictionClientFromAddr

func NewModelPredictionClientFromAddr(serverAddr string, name string, signatureName string) (ModelPredictionClient, error)

NewModelPredictionClientFromAddr returns a new client for a given TensorFlow Serving address

func NewModelPredictionClientFromConn

func NewModelPredictionClientFromConn(conn *grpc.ClientConn, name string, signatureName string) ModelPredictionClient

NewModelPredictionClientFromConn returns a new client from a GRPC client connection

func NewModelPredictionClientFromPredictionServiceClient

func NewModelPredictionClientFromPredictionServiceClient(cli serving.PredictionServiceClient, name string, signatureName string) ModelPredictionClient

NewModelPredictionClientFromPredictionServiceClient returns a new client from a serving PredictionServiceClient

type Shape

Shape tensor shape

func NewShape

func NewShape(dims []int64) *Shape

NewShape creates a new Shape

type ShapeDim

ShapeDim shape dimension

type Tensor

type Tensor = framework.TensorProto

Tensor a tensorflow tensor

func NewTensor

func NewTensor(value interface{}) (*Tensor, error)

NewTensor returns a Tensor for a given go value

func NewTensorWithShape

func NewTensorWithShape(value interface{}, shape []int64) (*Tensor, error)

NewTensorWithShape returns a tensor with a specific shape

type TensorMap

type TensorMap = map[string]*framework.TensorProto

TensorMap map of tensors

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL