|
1 | | -using System; |
| 1 | +// An example for using the TensorFlow C# API for image recognition |
| 2 | +// using a pre-trained inception model (http://arxiv.org/abs/1512.00567). |
| 3 | +// |
| 4 | +// Sample usage: <program> -dir=/tmp/modeldir imagefile |
| 5 | +// |
| 6 | +// The pre-trained model takes input in the form of a 4-dimensional |
| 7 | +// tensor with shape [ BATCH_SIZE, IMAGE_HEIGHT, IMAGE_WIDTH, 3 ], |
| 8 | +// where: |
| 9 | +// - BATCH_SIZE allows for inference of multiple images in one pass through the graph |
| 10 | +// - IMAGE_HEIGHT is the height of the images on which the model was trained |
| 11 | +// - IMAGE_WIDTH is the width of the images on which the model was trained |
| 12 | +// - 3 is the (R, G, B) values of the pixel colors represented as a float. |
| 13 | +// |
| 14 | +// And produces as output a vector with shape [ NUM_LABELS ]. |
| 15 | +// output[i] is the probability that the input image was recognized as |
| 16 | +// having the i-th label. |
| 17 | +// |
| 18 | +// A separate file contains a list of string labels corresponding to the |
| 19 | +// integer indices of the output. |
| 20 | +// |
| 21 | +// This example: |
| 22 | +// - Loads the serialized representation of the pre-trained model into a Graph |
| 23 | +// - Creates a Session to execute operations on the Graph |
| 24 | +// - Converts an image file to a Tensor to provide as input to a Session run |
| 25 | +// - Executes the Session and prints out the label with the highest probability |
| 26 | +// |
| 27 | +// To convert an image file to a Tensor suitable for input to the Inception model, |
| 28 | +// this example: |
| 29 | +// - Constructs another TensorFlow graph to normalize the image into a |
| 30 | +// form suitable for the model (for example, resizing the image) |
| 31 | +// - Creates an executes a Session to obtain a Tensor in this normalized form. |
| 32 | +using System; |
2 | 33 | using TensorFlow; |
3 | 34 | using Mono.Options; |
4 | 35 | using System.IO; |
@@ -30,21 +61,118 @@ static void Help () |
30 | 61 | public static void Main (string [] args) |
31 | 62 | { |
32 | 63 | var files = options.Parse (args); |
33 | | - if (dir == null) |
34 | | - Error ("Must specify a directory with -m to store the training data"); |
35 | | - if (files == null) |
36 | | - Error ("No files were specified"); |
| 64 | + if (dir == null) { |
| 65 | + dir = "/tmp"; |
| 66 | + //Error ("Must specify a directory with -m to store the training data"); |
| 67 | + } |
| 68 | + string file; |
| 69 | + //if (files == null || files.Count == 0) |
| 70 | + // Error ("No files were specified"); |
| 71 | + //file = files [0]; |
| 72 | + file = "/tmp/demo.jpg"; |
37 | 73 |
|
38 | 74 | ModelFiles (dir); |
39 | 75 |
|
| 76 | + // Construct an in-memory graph from the serialized form. |
| 77 | + var graph = new TFGraph (); |
| 78 | + // Load the serialized GraphDef from a file. |
40 | 79 | var model = File.ReadAllBytes (modelFile); |
41 | 80 |
|
42 | | - var g = new TFGraph (); |
43 | | - g.Import (model, ""); |
44 | | - using (var s = new TFSession (g)) { |
| 81 | + graph.Import (model, ""); |
| 82 | + using (var session = new TFSession (graph)) { |
| 83 | + // Run inference on the image files |
| 84 | + // For multiple images, session.Run() can be called in a loop (and |
| 85 | + // concurrently). Alternatively, images can be batched since the model |
| 86 | + // accepts batches of image data as input. |
| 87 | + var tensor = CreateTensorFromImageFile (file); |
| 88 | + |
| 89 | + var output = session.Run (null, |
| 90 | + inputs: new [] { graph ["input"] [0] }, |
| 91 | + inputValues: new [] { tensor }, |
| 92 | + outputs: new [] { graph ["output"] [0] }); |
| 93 | + // output[0].Value() is a vector containing probabilities of |
| 94 | + // labels for each image in the "batch". The batch size was 1. |
| 95 | + // Find the most probably label index. |
| 96 | + |
| 97 | + var result = output [0]; |
| 98 | + var rshape = result.Shape; |
| 99 | + if (result.NumDims != 2 || rshape [0] != 1) { |
| 100 | + var shape = ""; |
| 101 | + foreach (var d in rshape) { |
| 102 | + shape += $"{d} "; |
| 103 | + } |
| 104 | + shape = shape.Trim (); |
| 105 | + Console.WriteLine ($"Error: expected to produce a [1 N] shaped tensor where N is the number of labels, instead it produced one with shape [{shape}]"); |
| 106 | + Environment.Exit (1); |
| 107 | + } |
| 108 | + int nlabels = (int) rshape [1]; |
| 109 | + |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + // Convert the image in filename to a Tensor suitable as input to the Inception model. |
| 114 | + static TFTensor CreateTensorFromImageFile (string file) |
| 115 | + { |
| 116 | + var contents = File.ReadAllBytes (file); |
| 117 | + |
| 118 | + // DecodeJpeg uses a scalar String-valued tensor as input. |
| 119 | + var tensor = (TFTensor) contents; |
| 120 | + |
| 121 | + TFGraph graph; |
| 122 | + TFOutput input, output; |
| 123 | + |
| 124 | + // Construct a graph to normalize the image |
| 125 | + ConstructGraphToNormalizeImage (out graph, out input, out output); |
| 126 | + |
| 127 | + // Execute that graph to normalize this one image |
| 128 | + using (var session = new TFSession (graph)) { |
| 129 | + var normalized = session.Run (null, |
| 130 | + inputs: new [] { input }, |
| 131 | + inputValues: new [] { tensor }, |
| 132 | + outputs: new [] { output }); |
| 133 | + |
| 134 | + return normalized [0]; |
45 | 135 | } |
46 | 136 | } |
47 | 137 |
|
| 138 | + // The inception model takes as input the image described by a Tensor in a very |
| 139 | + // specific normalized format (a particular image size, shape of the input tensor, |
| 140 | + // normalized pixel values etc.). |
| 141 | + // |
| 142 | + // This function constructs a graph of TensorFlow operations which takes as |
| 143 | + // input a JPEG-encoded string and returns a tensor suitable as input to the |
| 144 | + // inception model. |
| 145 | + static void ConstructGraphToNormalizeImage (out TFGraph graph, out TFOutput input, out TFOutput output) |
| 146 | + { |
| 147 | + // Some constants specific to the pre-trained model at: |
| 148 | + // https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip |
| 149 | + // |
| 150 | + // - The model was trained after with images scaled to 224x224 pixels. |
| 151 | + // - The colors, represented as R, G, B in 1-byte each were converted to |
| 152 | + // float using (value - Mean)/Scale. |
| 153 | + |
| 154 | + const int W = 224; |
| 155 | + const int H = 224; |
| 156 | + const float Mean = 117; |
| 157 | + const float Scale = 1; |
| 158 | + |
| 159 | + graph = new TFGraph (); |
| 160 | + input = graph.Placeholder (TFDataType.String); |
| 161 | + output = graph.Div ( |
| 162 | + x: graph.Sub ( |
| 163 | + x: graph.ResizeBilinear ( |
| 164 | + images: graph.ExpandDims ( |
| 165 | + input: graph.Cast ( |
| 166 | + graph.DecodeJpeg (contents: input, channels: 3), DstT: TFDataType.Float), |
| 167 | + dim: graph.Const (0, "make_batch")), |
| 168 | + size: graph.Const (new int [] { W, H })), |
| 169 | + y: graph.Const (Mean)), |
| 170 | + y: graph.Const (Scale)); |
| 171 | + } |
| 172 | + |
| 173 | + // |
| 174 | + // Downloads the inception graph and labels |
| 175 | + // |
48 | 176 | static void ModelFiles (string dir) |
49 | 177 | { |
50 | 178 | string url = "https://storage.googleapis.com/download.tensorflow.org/models/inception5h.zip"; |
|
0 commit comments