Convert the image in filename to a Tensor suitable as input to the Inception model.
(filename string)
| 147 | |
| 148 | // Convert the image in filename to a Tensor suitable as input to the Inception model. |
| 149 | func 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, |
no test coverage detected