{"id":17045173,"url":"https://github.com/sjkaliski/infer","last_synced_at":"2025-03-22T17:31:22.659Z","repository":{"id":144202328,"uuid":"123984537","full_name":"sjkaliski/infer","owner":"sjkaliski","description":"🔮 Use TensorFlow models in Go to evaluate Images (and more soon!)","archived":false,"fork":false,"pushed_at":"2018-08-29T03:03:17.000Z","size":36,"stargazers_count":63,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-18T13:51:16.684Z","etag":null,"topics":["go","golang","inference","machine-learning","prediction","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sjkaliski.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2018-03-05T22:03:00.000Z","updated_at":"2024-02-24T09:39:22.000Z","dependencies_parsed_at":null,"dependency_job_id":"7b91951b-73bb-4654-8109-01644e658092","html_url":"https://github.com/sjkaliski/infer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjkaliski%2Finfer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjkaliski%2Finfer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjkaliski%2Finfer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sjkaliski%2Finfer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sjkaliski","download_url":"https://codeload.github.com/sjkaliski/infer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244995139,"owners_count":20544292,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["go","golang","inference","machine-learning","prediction","tensorflow"],"created_at":"2024-10-14T09:36:37.672Z","updated_at":"2025-03-22T17:31:22.634Z","avatar_url":"https://github.com/sjkaliski.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# infer\n\nInfer is a Go package for running predicitions in TensorFlow models.\n\n[![Build Status](https://travis-ci.org/sjkaliski/infer.png)](https://travis-ci.org/sjkaliski/infer)\n[![GoDoc](https://godoc.org/github.com/sjkaliski/infer?status.svg)](https://godoc.org/github.com/sjkaliski/infer)\n[![Go Report Card](https://goreportcard.com/badge/github.com/sjkaliski/infer)](https://goreportcard.com/report/github.com/sjkaliski/infer)\n\n## Overview\n\nThis package provides abstractions for running inferences in TensorFlow models for common types. At the moment it only has methods for images, however in the future it can certainly support more.\n\n## Getting Started\n\nThe easiest way to get going is looking at some examples, two have been provided:\n\n1. [Image Recognition API using Inception](examples/inception).\n2. [MNIST](examples/mnist)\n\n## Setup\n\nThis package requires \n\n- [Go](https://golang.org/dl/)\n- [TensorFlow for Go](https://godoc.org/github.com/tensorflow/tensorflow/tensorflow/go)\n\nInstallation instructions can be found [here](https://www.tensorflow.org/install/install_go). Additionally, a [Dockerfile](examples/Dockerfile) has been included which can be used to run the [examples](examples).\n\n## Usage\n\n### Overview\n\nTo use infer, a TensorFlow [Graph](https://www.tensorflow.org/programmers_guide/graphs) is required, as well as a defined Input and Output.\n\nClasses may also be included, a slice of possible values. It's assumed the results of any model execution refer to these classes, in order. (e.g. 0 -\u003e mountain, 1 -\u003e cat, 2 -\u003e apple).\n\n```go\nm, _ = infer.New(\u0026infer.Model{\n  Graph: graph,\n  Classes: classes,\n  Input: \u0026infer.Input{\n    Key:        \"input\",\n    Dimensions: []int32{100, 100},\n  },\n  Output: \u0026infer.Output{\n    Key: \"output\",\n  },\n})\n```\n\nOnce a new model is defined, inferences can be executed.\n\n```go\npredictions, _ := m.FromImage(file, \u0026infer.ImageOptions{})\n```\n\nPredictions are returned sorted by score (most accurate first). A `Prediction` looks like\n\n```go\nPrediction{\n  Class: \"mountain\",\n  Score: 0.97,\n}\n```\n\n### Graph\n\nAn `infer.Model` requires a `tf.Graph`. The Graph defines the computations required to determine an output based on a provided input. The Graph can be included in two ways:\n\n1. Create the Graph using Go in your application.\n2. Load an existing model.\n\nFor the latter, an existing model (containing the graph and weights) can be loaded using Go:\n\n```go\nmodel, _ := ioutil.ReadFile(\"/path/to/model.pb\")\ngraph := tf.NewGraph()\ngraph.Import(model, \"\")\n```\n\nFor more information on TensorFlow model files, [see here](https://www.tensorflow.org/extend/tool_developers/).\n\n### Input \u0026 Output\n\n`infer.Input` and `infer.Output` describe [TensorFlow layers](https://www.tensorflow.org/tutorials/layers). In practice this is the layer the input data should be fed to and the layer from which to fetch results.\n\nEach require a `Key`. This is the unique identifier (name) in the TensorFlow graph for that layer. To see a list of layers and type, the following can be run:\n\n```go\nops := graph.Operations()\nfor _, o := range ops {\n  log.Println(o.Name(), o.Type())\n}\n```\n\nIf you're not using a pre-trained model, the layers can be named, which can ease in identifying the appropriate layers.\n\n### Analyzing Results\n\nIn the [MNIST example](examples/mnist), we can execute a prediction and inspect results as so: \n\n```go\npredictions, err := m.FromImage(img, opts)\nif err != nil {\n  panic(err)\n}\n\n// predictions[0].Class -\u003e 8\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsjkaliski%2Finfer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsjkaliski%2Finfer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsjkaliski%2Finfer/lists"}