|
36 | 36 | using System.IO.Compression; |
37 | 37 | using System.Net; |
38 | 38 | using System.Collections.Generic; |
| 39 | +using ExampleCommon; |
39 | 40 |
|
40 | 41 | namespace ExampleInceptionInference |
41 | 42 | { |
@@ -94,7 +95,7 @@ public static void Main (string [] args) |
94 | 95 | // For multiple images, session.Run() can be called in a loop (and |
95 | 96 | // concurrently). Alternatively, images can be batched since the model |
96 | 97 | // accepts batches of image data as input. |
97 | | - var tensor = CreateTensorFromImageFile (file); |
| 98 | + var tensor = ImageUtil.CreateTensorFromImageFile (file); |
98 | 99 |
|
99 | 100 | var runner = session.GetRunner (); |
100 | 101 | runner.AddInput (graph ["input"] [0], tensor).Fetch (graph ["output"] [0]); |
@@ -148,68 +149,7 @@ public static void Main (string [] args) |
148 | 149 | } |
149 | 150 | } |
150 | 151 | } |
151 | | - |
152 | | - // Convert the image in filename to a Tensor suitable as input to the Inception model. |
153 | | - static TFTensor CreateTensorFromImageFile (string file) |
154 | | - { |
155 | | - var contents = File.ReadAllBytes (file); |
156 | | - |
157 | | - // DecodeJpeg uses a scalar String-valued tensor as input. |
158 | | - var tensor = TFTensor.CreateString (contents); |
159 | | - |
160 | | - TFGraph graph; |
161 | | - TFOutput input, output; |
162 | | - |
163 | | - // Construct a graph to normalize the image |
164 | | - ConstructGraphToNormalizeImage (out graph, out input, out output); |
165 | | - |
166 | | - // Execute that graph to normalize this one image |
167 | | - using (var session = new TFSession (graph)) { |
168 | | - var normalized = session.Run ( |
169 | | - inputs: new [] { input }, |
170 | | - inputValues: new [] { tensor }, |
171 | | - outputs: new [] { output }); |
172 | | - |
173 | | - return normalized [0]; |
174 | | - } |
175 | | - } |
176 | | - |
177 | | - // The inception model takes as input the image described by a Tensor in a very |
178 | | - // specific normalized format (a particular image size, shape of the input tensor, |
179 | | - // normalized pixel values etc.). |
180 | | - // |
181 | | - // This function constructs a graph of TensorFlow operations which takes as |
182 | | - // input a JPEG-encoded string and returns a tensor suitable as input to the |
183 | | - // inception model. |
184 | | - static void ConstructGraphToNormalizeImage (out TFGraph graph, out TFOutput input, out TFOutput output) |
185 | | - { |
186 | | - // Some constants specific to the pre-trained model at: |
187 | | - // https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip |
188 | | - // |
189 | | - // - The model was trained after with images scaled to 224x224 pixels. |
190 | | - // - The colors, represented as R, G, B in 1-byte each were converted to |
191 | | - // float using (value - Mean)/Scale. |
192 | | - |
193 | | - const int W = 224; |
194 | | - const int H = 224; |
195 | | - const float Mean = 117; |
196 | | - const float Scale = 1; |
197 | | - |
198 | | - graph = new TFGraph (); |
199 | | - input = graph.Placeholder (TFDataType.String); |
200 | | - |
201 | | - output = graph.Div ( |
202 | | - x: graph.Sub ( |
203 | | - x: graph.ResizeBilinear ( |
204 | | - images: graph.ExpandDims ( |
205 | | - input: graph.Cast ( |
206 | | - graph.DecodeJpeg (contents: input, channels: 3), DstT: TFDataType.Float), |
207 | | - dim: graph.Const (0, "make_batch")), |
208 | | - size: graph.Const (new int [] { W, H }, "size")), |
209 | | - y: graph.Const (Mean, "mean")), |
210 | | - y: graph.Const (Scale, "scale")); |
211 | | - } |
212 | | - |
| 152 | + |
213 | 153 | // |
214 | 154 | // Downloads the inception graph and labels |
215 | 155 | // |
|
0 commit comments