This repository has been archived by the owner on Nov 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mnist_infer.go
96 lines (81 loc) · 2.66 KB
/
mnist_infer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"fmt"
"io/ioutil"
"log"
"path/filepath"
tf "github.com/tensorflow/tensorflow/tensorflow/go"
)
func runInferenceModel(imgPaths []string, imgsForTensor [][3][224][224]float32) {
// Load a frozen graph to use for queries
// modelpath := filepath.Join("mnistmodel/", "SavedModel.pb")
// trial_modelPath := filepath.Join("/Users/kahlil/projects/holmusk/Food Scoring Eval/go/mnist_infer/models/trial_model/", "model.pb")
food_notFoodModelPath := filepath.Join("/Users/kahlil/Documents/AIA AI Food Scoring /S3 AIA models/Phase1/TF/models/FoodOrNonfood/", "epoch_149.pb")
model, err := ioutil.ReadFile(food_notFoodModelPath)
if err != nil {
log.Fatal(err)
}
// Construct an in-memory graph from the serialized form.
graph := tf.NewGraph()
if err := graph.Import(model, ""); err != nil {
log.Fatal(err)
}
// graphInputOp := graph.Operation("IO/input")
// fmt.Printf("List of operations in the graph: ", graphInputOp)
// Create a session for inference over graph.
session, err := tf.NewSession(graph, nil)
if err != nil {
log.Fatal(err)
}
defer session.Close()
// //constTensor (for probability)
// _, ctErr := constNumTensor(1.0)
// if ctErr != nil {
// fmt.Printf("Error creating const num tensor: %s\n", terr.Error())
// return
// }
newTensor, newErr := tf.NewTensor(imgsForTensor)
if newErr != nil {
fmt.Printf("Error creating Tensor of Image, err: %s\n", newErr.Error())
return
}
result, runErr := session.Run(
map[tf.Output]*tf.Tensor{
graph.Operation("IO/input").Output(0): newTensor,
},
[]tf.Output{
graph.Operation("IO/output").Output(0),
},
nil,
)
if runErr != nil {
fmt.Printf("Error running the session with input, err: %s\n", runErr.Error())
return
}
// var tensorValInGo [2]float32
tensorValInGo := result[0].Value()
logitsVal, _ := tensorValInGo.([][]float32)
for idx := 0; idx < len(logitsVal); idx++ {
logitSlice := logitsVal[idx]
firstLogit, secondLogit := logitSlice[0], logitSlice[1]
var resultOfModel int
if firstLogit > secondLogit {
resultOfModel = 0
} else if firstLogit < secondLogit {
resultOfModel = 1
} else {
resultOfModel = -1
}
fmt.Printf("Img: %v ", imgPaths[idx])
fmt.Printf("Logits: %v %v Result: %v \n", firstLogit, secondLogit, resultOfModel)
}
}
func dummyInputTensor(size int) (*tf.Tensor, error) {
// imageData := [][]float32{make([]float32, size)}
newImagedata := [1][3][224][224]float32{}
return tf.NewTensor(newImagedata)
}
// func constNumTensor(numVal float32) (*tf.Tensor, error) {
// return tf.NewTensor(numVal)
// }
// pred_value = sess.run(['prefix/metric/yhat:0'], feed_dict={'prefix/IO/input:0': X_test, 'prefix/IO/keep_probability:0':1.0})