Skip to content

Instantly share code, notes, and snippets.

View plutov's full-sized avatar
🎥
https://www.youtube.com/packagemain

Alex Pliutau plutov

🎥
https://www.youtube.com/packagemain
View GitHub Profile
@plutov
plutov / tensorflow3.go
Created January 8, 2018 10:33
tensorflow3.go
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)
}
@plutov
plutov / tensorflow4.go
Created January 8, 2018 10:34
tensorflow4.go
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()
@plutov
plutov / tensorflow5.go
Created January 8, 2018 10:35
tensorflow5.go
// 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,
@plutov
plutov / tensorflow6.go
Created January 8, 2018 10:36
tensorflow6.go
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
@plutov
plutov / tensorflow.Dockerfile
Created January 8, 2018 10:44
tensorflow.Dockerfile
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" ]
FindAnagrams(string word) []string
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"}},
}
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)
}
})
}
}
=== RUN TestFindAnagrams
=== RUN TestFindAnagrams/empty_input_string
=== RUN TestFindAnagrams/two_anagrams
=== RUN TestFindAnagrams/input_with_space
t.Run(tt.name, func(subtest *testing.T) {
subtest.Parallel()
got := FindAnagrams(tt.word)
// assertion
})