MCPcopy Create free account
hub / github.com/DeepRec-AI/DeepRec / makeTensorFromImage

Function makeTensorFromImage

tensorflow/go/example_inception_inference_test.go:149–178  ·  view source on GitHub ↗

Convert the image in filename to a Tensor suitable as input to the Inception model.

(filename string)

Source from the content-addressed store, hash-verified

147
148// Convert the image in filename to a Tensor suitable as input to the Inception model.
149func makeTensorFromImage(filename string) (*tf.Tensor, error) {
150 bytes, err := ioutil.ReadFile(filename)
151 if err != nil {
152 return nil, err
153 }
154 // DecodeJpeg uses a scalar String-valued tensor as input.
155 tensor, err := tf.NewTensor(string(bytes))
156 if err != nil {
157 return nil, err
158 }
159 // Construct a graph to normalize the image
160 graph, input, output, err := constructGraphToNormalizeImage()
161 if err != nil {
162 return nil, err
163 }
164 // Execute that graph to normalize this one image
165 session, err := tf.NewSession(graph, nil)
166 if err != nil {
167 return nil, err
168 }
169 defer session.Close()
170 normalized, err := session.Run(
171 map[tf.Output]*tf.Tensor{input: tensor},
172 []tf.Output{output},
173 nil)
174 if err != nil {
175 return nil, err
176 }
177 return normalized[0], nil
178}
179
180// The inception model takes as input the image described by a Tensor in a very
181// specific normalized format (a particular image size, shape of the input tensor,

Callers 1

ExampleFunction · 0.85

Calls 5

CloseMethod · 0.95
RunMethod · 0.95
ReadFileMethod · 0.80
NewSessionMethod · 0.45

Tested by

no test coverage detected