This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const ( | |
graphFile = "/model/imagenet_comp_graph_label_strings.txt" | |
labelsFile = "/model/imagenet_comp_graph_label_strings.txt" | |
) | |
graph, labels, err := loadModel() | |
if err != nil { | |
log.Fatalf("unable to load model: %v", err) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func normalizeImage(body io.ReadCloser) (*tensorflow.Tensor, error) { | |
var buf bytes.Buffer | |
io.Copy(&buf, body) | |
tensor, err := tensorflow.NewTensor(buf.String()) | |
if err != nil { | |
return nil, err | |
} | |
graph, input, output, err := getNormalizedGraph() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a session for inference over modelGraph. | |
session, err := tf.NewSession(modelGraph, nil) | |
if err != nil { | |
log.Fatalf("could not init session: %v", err) | |
} | |
defer session.Close() | |
output, err := session.Run( | |
map[tf.Output]*tf.Tensor{ | |
modelGraph.Operation("input").Output(0): tensor, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
res := getTopFiveLabels(labels, output[0].Value().([][]float32)[0]) | |
for _, l := range res { | |
fmt.Printf("label: %s, probability: %.2f%%\n", l.Label, l.Probability*100) | |
} | |
func getTopFiveLabels(labels []string, probabilities []float32) []Label { | |
var resultLabels []Label | |
for i, p := range probabilities { | |
if i >= len(labels) { | |
break |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM ctava/tensorflow-go | |
RUN mkdir -p /model && \ | |
curl -o /model/inception5h.zip -s "http://download.tensorflow.org/models/inception5h.zip" && \ | |
unzip /model/inception5h.zip -d /model | |
WORKDIR /go/src/imgrecognition | |
COPY . . | |
RUN go build | |
ENTRYPOINT [ "/go/src/imgrecognition/imgrecognition" ] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FindAnagrams(string word) []string |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var tests = []struct { | |
name string | |
word string | |
want []string | |
}{ | |
{"empty input string", "", []string{}}, | |
{"two anagrams", "Protectionism", []string{"Cite no imports", "Nice to imports"}}, | |
{"input with space", "Real fun", []string{"funeral"}}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func TestFindAnagrams(t *testing.T) { | |
for _, tt := range tests { | |
t.Run(tt.name, func(t *testing.T) { | |
got := FindAnagrams(tt.word) | |
if got != tt.want { | |
t.Errorf("FindAnagrams(%s) got %v, want %v", tt.word, got, tt.want) | |
} | |
}) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=== RUN TestFindAnagrams | |
=== RUN TestFindAnagrams/empty_input_string | |
=== RUN TestFindAnagrams/two_anagrams | |
=== RUN TestFindAnagrams/input_with_space |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
t.Run(tt.name, func(subtest *testing.T) { | |
subtest.Parallel() | |
got := FindAnagrams(tt.word) | |
// assertion | |
}) |