private void OnDownloadComplete(object sender, AsyncCompletedEventArgs e) { try { var stringResult = new StringBuilder(); using (var imageTensor = ImageIO.ReadTensorFromImageFile <float>(_fileName, 256, 256)) { var results = _inceptionGraph.Recognize(imageTensor); foreach (var recognitionResult in results.OrderByDescending(x => x.Probability).Take(5)) { if (decimal.TryParse(recognitionResult.Probability.ToString(), out decimal resultResult)) { stringResult.Append($"Object is {recognitionResult.Label} with {resultResult}% probability.") .AppendLine(); } } } InformationText = stringResult.ToString(); IsModalVisible = false; } catch (Exception ex) { _exceptionLogDataAccess.LogException(ex.ToString()); } IsModalVisible = false; }
public void Detect(String fileName) { Tensor imageTensor = ImageIO.ReadTensorFromImageFile(fileName, 224, 224, 128.0f, 1.0f / 128.0f); Stopwatch watch = Stopwatch.StartNew(); MultiboxGraph.Result result = graph.Detect(imageTensor); watch.Stop(); Bitmap bmp = new Bitmap(fileName); MultiboxGraph.DrawResults(bmp, result, 0.1f); if (InvokeRequired) { this.Invoke((MethodInvoker)(() => { resultPictureBox.Image = bmp; messageLabel.Text = String.Format("Detection completed in {0} milliseconds", watch.ElapsedMilliseconds); })); } else { resultPictureBox.Image = bmp; messageLabel.Text = String.Format("Detection completed in {0} milliseconds", watch.ElapsedMilliseconds); } }
public void TestMultiSession() { Tensor imageTensor = ImageIO.ReadTensorFromImageFile <float>("grace_hopper.jpg", 224, 224, 128.0f, 1.0f); int sessionCount = 10; Inception[] graphs = new Inception[sessionCount]; bool[] processCompleted = new bool[sessionCount]; for (int i = 0; i < sessionCount; i++) { graphs[i] = new Inception(); processCompleted[i] = false; graphs[i].OnDownloadCompleted += (sender, e) => { Inception.RecognitionResult[] results = graphs[i].Recognize(imageTensor); processCompleted[i] = true; }; graphs[i].Init(); Trace.WriteLine(System.String.Format("Reading graph {0}", i)); Thread.Sleep(1000); } while (!processCompleted.All((v) => v)) { Thread.Sleep(1000); } }
public void TestEncodeJpeg() { Tensor image = ImageIO.ReadTensorFromImageFile("surfers.jpg"); byte[] jpegRaw = ImageIO.EncodeJpeg(image); File.WriteAllBytes("surefers_out.jpg", jpegRaw); }
public static string Predict(string ModelFile = " ", string LabelFile = " ", string inputFile = " ") { ModelDeploy modelDecoderGraph = new ModelDeploy( ); modelDecoderGraph.Init(new string[] { ModelFile, LabelFile }, "Mul", "final_result"); Tensor imageTensor = ImageIO.ReadTensorFromImageFile(inputFile, 299, 299, 128.0f, 1.0f / 128.0f); //modelDecoderGraph.ImportGraph(); Stopwatch sw = Stopwatch.StartNew(); float[] probability = modelDecoderGraph.Recognize(imageTensor); sw.Stop(); String resStr = String.Empty; if (probability != null) { String[] labels = modelDecoderGraph.Labels; float maxVal = 0; int maxIdx = 0; for (int i = 0; i < probability.Length; i++) { if (probability[i] > maxVal) { maxVal = probability[i]; maxIdx = i; } } resStr = String.Format("Object is {0} with {1}% probability. \n Recognition done in {2} milliseconds.", labels[maxIdx], maxVal * 100, sw.ElapsedMilliseconds); } return(resStr); }
public void TestEncodeJpeg() { Tensor image = ImageIO.ReadTensorFromImageFile <float>("surfers.jpg", 299, 299, 0, 1.0f / 255.0f, true, false); byte[] jpegRaw = ImageIO.TensorToJpeg(image, 255.0f, 0.0f); File.WriteAllBytes("surefers_out.jpg", jpegRaw); }
public void TestMultiboxPeopleDetect() { Tensor imageResults = ImageIO.ReadTensorFromImageFile <float>("surfers.jpg", 224, 224, 128.0f, 1.0f / 128.0f); MultiboxGraph multiboxGraph = new MultiboxGraph(); bool processCompleted = false; multiboxGraph.OnDownloadCompleted += (sender, e) => { MultiboxGraph.Result[] result = multiboxGraph.Detect(imageResults); //Bitmap bmp = new Bitmap("surfers.jpg"); //MultiboxGraph.DrawResults(bmp, result, 0.1f); //MultiboxGraph. //bmp.Save("surfers_result.jpg"); processCompleted = true; }; multiboxGraph.Init(); while (!processCompleted) { Thread.Sleep(1000); } }
public async Task TestStylize() { StylizeGraph stylizeGraph = new StylizeGraph(); await stylizeGraph.Init(); Tensor image = ImageIO.ReadTensorFromImageFile <float>("surfers.jpg"); Tensor stylizedImage = stylizeGraph.Stylize(image, 0); }
public void Recognize(String fileName) { Tensor imageTensor = ImageIO.ReadTensorFromImageFile(fileName, 224, 224, 128.0f, 1.0f / 128.0f); float[] probability; if (_coldSession) { //First run of the recognition graph, here we will compile the graph and initialize the session //This is expected to take much longer time than consecutive runs. probability = inceptionGraph.Recognize(imageTensor); _coldSession = false; } //Here we are trying to time the execution of the graph after it is loaded //If we are not interest in the performance, we can skip the 3 lines that follows Stopwatch sw = Stopwatch.StartNew(); probability = inceptionGraph.Recognize(imageTensor); sw.Stop(); String resStr = String.Empty; if (probability != null) { String[] labels = inceptionGraph.Labels; float maxVal = 0; int maxIdx = 0; for (int i = 0; i < probability.Length; i++) { if (probability[i] > maxVal) { maxVal = probability[i]; maxIdx = i; } } resStr = String.Format("Object is {0} with {1}% probability. Recognition done in {2} in {3} milliseconds.", labels[maxIdx], maxVal * 100, TfInvoke.IsGoogleCudaEnabled ? "GPU" : "CPU", sw.ElapsedMilliseconds); } if (InvokeRequired) { this.Invoke((MethodInvoker)(() => { fileNameTextBox.Text = fileName; pictureBox.ImageLocation = fileName; messageLabel.Text = resStr; })); } else { fileNameTextBox.Text = fileName; pictureBox.ImageLocation = fileName; messageLabel.Text = resStr; } }
public async Task TestMultiboxPeopleDetect() { Tensor imageResults = ImageIO.ReadTensorFromImageFile <float>("surfers.jpg", 224, 224, 128.0f, 1.0f / 128.0f); MultiboxGraph multiboxGraph = new MultiboxGraph(); //bool processCompleted = false; await multiboxGraph.Init(); MultiboxGraph.Result[] result = multiboxGraph.Detect(imageResults); }
public void Detect(String fileName) { //MultiboxGraph.Download(); MultiboxGraph graph = new MultiboxGraph(); Tensor imageTensor = ImageIO.ReadTensorFromImageFile(fileName, 224, 224, 128.0f, 1.0f / 128.0f); MultiboxGraph.Result result = graph.Detect(imageTensor); Bitmap bmp = new Bitmap(fileName); MultiboxGraph.DrawResults(bmp, result, 0.1f); resultPictureBox.Image = bmp; }
public void TestMultiboxPeopleDetect() { Tensor imageResults = ImageIO.ReadTensorFromImageFile("surfers.jpg", 224, 224, 128.0f, 1.0f / 128.0f); MultiboxGraph multiboxGraph = new MultiboxGraph(); MultiboxGraph.Result result = multiboxGraph.Detect(imageResults); Bitmap bmp = new Bitmap("surfers.jpg"); MultiboxGraph.DrawResults(bmp, result, 0.1f); bmp.Save("surfers_result.jpg"); }
public async Task TestMaskRCNN() { using (Tensor imageTensor = ImageIO.ReadTensorFromImageFile <byte>( "surfers.jpg", -1, -1, 0, 1.0f)) using (MaskRcnnInceptionV2Coco model = new MaskRcnnInceptionV2Coco()) { await model.Init(); MaskRcnnInceptionV2Coco.RecognitionResult[][] results = model.Recognize(imageTensor); } }
public void Recognize(String fileName) { Tensor imageTensor = ImageIO.ReadTensorFromImageFile(fileName, 224, 224, 128.0f, 1.0f / 128.0f); Stopwatch sw = Stopwatch.StartNew(); float[] probability = inceptionGraph.Recognize(imageTensor); sw.Stop(); String resStr = String.Empty; if (probability != null) { String[] labels = inceptionGraph.Labels; float maxVal = 0; int maxIdx = 0; for (int i = 0; i < probability.Length; i++) { if (probability[i] > maxVal) { maxVal = probability[i]; maxIdx = i; } } resStr = String.Format("Object is {0} with {1}% probability. Recognition done in {2} milliseconds.", labels[maxIdx], maxVal * 100, sw.ElapsedMilliseconds); } if (InvokeRequired) { this.Invoke((MethodInvoker)(() => { fileNameTextBox.Text = fileName; pictureBox.ImageLocation = fileName; messageLabel.Text = resStr; })); } else { fileNameTextBox.Text = fileName; pictureBox.ImageLocation = fileName; messageLabel.Text = resStr; } }
private void OnDownloadComplete(object sender, AsyncCompletedEventArgs e) { try { using (var imageTensor = ImageIO.ReadTensorFromImageFile <float>(_fileName, 224, 224, 128.0f, 1.0f / 128.0f)) { var detectionResult = _graph.Detect(imageTensor); var detectionAnnotations = MultiboxGraph.FilterResults(detectionResult, 0.1f); var detectionImage = NativeImageIO.ImageFileToJpeg(_fileName, detectionAnnotations); var typeConverter = TypeDescriptor.GetConverter(typeof(Bitmap)); var detectionBmpImage = (Bitmap)typeConverter.ConvertFrom(detectionImage.Raw); ImageSource = BitmapConverter.ConvertBitmap(detectionBmpImage); } IsModalVisible = false; } catch (Exception ex) { _exceptionLogDataAccess.LogException(ex.ToString()); IsModalVisible = false; } }
public void Detect(String fileName) { Tensor imageTensor = ImageIO.ReadTensorFromImageFile(fileName, 224, 224, 128.0f, 1.0f / 128.0f); MultiboxGraph.Result[] result; if (_coldSession) { //First run of the detection, here we will compile the graph and initialize the session //This is expected to take much longer time than consecutive runs. result = graph.Detect(imageTensor); _coldSession = false; } //Here we are trying to time the execution of the graph after it is loaded //If we are not interest in the performance, we can skip the 3 lines that follows Stopwatch watch = Stopwatch.StartNew(); result = graph.Detect(imageTensor); watch.Stop(); //Bitmap bmp = new Bitmap(fileName); float[][] rectangles = MultiboxGraph.FilterResults(result, 0.1f); byte[] jpeg = MultiboxGraph.DrawRectanglesToJpeg(fileName, rectangles); //MultiboxGraph.DrawResults(bmp, result, 0.1f); Bitmap bmp = new Bitmap() if (InvokeRequired) { this.Invoke((MethodInvoker)(() => { RenderResult(bmp, watch.ElapsedMilliseconds); })); } else { RenderResult(bmp, watch.ElapsedMilliseconds); } }
public void Recognize(String fileName) { fileNameTextBox.Text = fileName; pictureBox.ImageLocation = fileName; //Use the following code for the full inception model Inception inceptionGraph = new Inception(); Tensor imageTensor = ImageIO.ReadTensorFromImageFile(fileName, 224, 224, 128.0f, 1.0f / 128.0f); //Uncomment the following code to use a retrained model to recognize followers, downloaded from the internet //Inception inceptionGraph = new Inception(null, new string[] {"optimized_graph.pb", "output_labels.txt"}, "https://github.com/emgucv/models/raw/master/inception_flower_retrain/", "Mul", "final_result"); //Tensor imageTensor = ImageIO.ReadTensorFromImageFile(fileName, 299, 299, 128.0f, 1.0f / 128.0f); //Uncomment the following code to use a retrained model to recognize followers, if you deployed the models with the application //For ".pb" and ".txt" bundled with the application, set the url to null //Inception inceptionGraph = new Inception(null, new string[] {"optimized_graph.pb", "output_labels.txt"}, null, "Mul", "final_result"); //Tensor imageTensor = ImageIO.ReadTensorFromImageFile(fileName, 299, 299, 128.0f, 1.0f / 128.0f); float[] probability = inceptionGraph.Recognize(imageTensor); String resStr = String.Empty; if (probability != null) { String[] labels = inceptionGraph.Labels; float maxVal = 0; int maxIdx = 0; for (int i = 0; i < probability.Length; i++) { if (probability[i] > maxVal) { maxVal = probability[i]; maxIdx = i; } } resStr = String.Format("Object is {0} with {1}% probability.", labels[maxIdx], maxVal * 100); } messageLabel.Text = resStr; }
public void TestNativeImageIO() { int inputHeight = 299; int inputWidth = 299; String file = "surfers.jpg"; Tensor t0 = new Tensor(DataType.Uint8, new int[] { 1, (int)inputHeight, (int)inputWidth, 3 }); NativeImageIO.ReadImageFileToTensor <float>(file, t0.DataPointer, inputHeight, inputWidth, 0.0f, 1.0f / 255.0f); Tensor t1 = ImageIO.ReadTensorFromImageFile <float>(file); float maxDiff = 0.0f; var ta0 = t0.GetData(false) as float[]; var ta1 = t1.GetData(false) as float[]; for (int i = 0; i < ta0.Length; i++) { float diff = Math.Abs(ta0[i] - ta1[i]); if (diff < maxDiff) { maxDiff = diff; } } }
public void TestInception() { Tensor imageTensor = ImageIO.ReadTensorFromImageFile("grace_hopper.jpg", 224, 224, 128.0f, 1.0f); Inception inceptionGraph = new Inception(); float[] probability = inceptionGraph.Recognize(imageTensor); if (probability != null) { String[] labels = inceptionGraph.Labels; float maxVal = 0; int maxIdx = 0; for (int i = 0; i < probability.Length; i++) { if (probability[i] > maxVal) { maxVal = probability[i]; maxIdx = i; } } Trace.WriteLine(String.Format("Object is {0} with {1}% probability", labels[maxIdx], maxVal * 100)); } }
public async Task TestResnet() { using (Tensor imageTensor = ImageIO.ReadTensorFromImageFile <float>("surfers.jpg", 224, 224, 0, 1.0f / 255.0f)) using (Resnet resnet = new Resnet()) { await resnet.Init(); MetaGraphDef metaGraphDef = MetaGraphDef.Parser.ParseFrom(resnet.MetaGraphDefBuffer.Data); var signatureDef = metaGraphDef.SignatureDef["serving_default"]; var inputNode = signatureDef.Inputs; var outputNode = signatureDef.Outputs; HashSet <string> opNames = new HashSet <string>(); HashSet <string> couldBeInputs = new HashSet <string>(); HashSet <string> couldBeOutputs = new HashSet <string>(); foreach (Operation op in resnet.Graph) { String name = op.Name; opNames.Add(name); if (op.NumInputs == 0 && op.OpType.Equals("Placeholder")) { couldBeInputs.Add(op.Name); AttrMetadata dtypeMeta = op.GetAttrMetadata("dtype"); AttrMetadata shapeMeta = op.GetAttrMetadata("shape"); DataType type = op.GetAttrType("dtype"); Int64[] shape = op.GetAttrShape("shape"); Buffer valueBuffer = op.GetAttrValueProto("shape"); Buffer shapeBuffer = op.GetAttrTensorShapeProto("shape"); Tensorflow.TensorShapeProto shapeProto = Tensorflow.TensorShapeProto.Parser.ParseFrom(shapeBuffer.Data); } if (op.OpType.Equals("Const")) { AttrMetadata dtypeMeta = op.GetAttrMetadata("dtype"); AttrMetadata valueMeta = op.GetAttrMetadata("value"); using (Tensor valueTensor = op.GetAttrTensor("value")) { var dim = valueTensor.Dim; } } if (op.OpType.Equals("Conv2D")) { AttrMetadata stridesMeta = op.GetAttrMetadata("strides"); AttrMetadata paddingMeta = op.GetAttrMetadata("padding"); AttrMetadata boolMeta = op.GetAttrMetadata("use_cudnn_on_gpu"); Int64[] strides = op.GetAttrIntList("strides"); bool useCudnn = op.GetAttrBool("use_cudnn_on_gpu"); String padding = op.GetAttrString("padding"); } foreach (Output output in op.Outputs) { int[] shape = resnet.Graph.GetTensorShape(output); if (output.NumConsumers == 0) { couldBeOutputs.Add(name); } } Buffer buffer = resnet.Graph.GetOpDef(op.OpType); Tensorflow.OpDef opDef = Tensorflow.OpDef.Parser.ParseFrom(buffer.Data); } using (Buffer versionDef = resnet.Graph.Versions()) { int l = versionDef.Length; } Resnet.RecognitionResult[][] results = resnet.Recognize(imageTensor); } }
public void TestStylize() { StylizeGraph stylizeGraph = new StylizeGraph(); Tensor image = ImageIO.ReadTensorFromImageFile <float>("surfers.jpg"); Tensor stylizedImage = stylizeGraph.Stylize(image, 0); }
public void TestInception() { using (Tensor imageTensor = ImageIO.ReadTensorFromImageFile <float>("grace_hopper.jpg", 224, 224, 128.0f, 1.0f)) using (Inception inceptionGraph = new Inception()) { bool processCompleted = false; inceptionGraph.OnDownloadCompleted += (sender, e) => { HashSet <string> opNames = new HashSet <string>(); HashSet <string> couldBeInputs = new HashSet <string>(); HashSet <string> couldBeOutputs = new HashSet <string>(); foreach (Operation op in inceptionGraph.Graph) { String name = op.Name; opNames.Add(name); if (op.NumInputs == 0 && op.OpType.Equals("Placeholder")) { couldBeInputs.Add(op.Name); AttrMetadata dtypeMeta = op.GetAttrMetadata("dtype"); AttrMetadata shapeMeta = op.GetAttrMetadata("shape"); DataType type = op.GetAttrType("dtype"); Int64[] shape = op.GetAttrShape("shape"); Buffer valueBuffer = op.GetAttrValueProto("shape"); Buffer shapeBuffer = op.GetAttrTensorShapeProto("shape"); Tensorflow.TensorShapeProto shapeProto = Tensorflow.TensorShapeProto.Parser.ParseFrom(shapeBuffer.Data); } if (op.OpType.Equals("Const")) { AttrMetadata dtypeMeta = op.GetAttrMetadata("dtype"); AttrMetadata valueMeta = op.GetAttrMetadata("value"); using (Tensor valueTensor = op.GetAttrTensor("value")) { var dim = valueTensor.Dim; } } if (op.OpType.Equals("Conv2D")) { AttrMetadata stridesMeta = op.GetAttrMetadata("strides"); AttrMetadata paddingMeta = op.GetAttrMetadata("padding"); AttrMetadata boolMeta = op.GetAttrMetadata("use_cudnn_on_gpu"); Int64[] strides = op.GetAttrIntList("strides"); bool useCudnn = op.GetAttrBool("use_cudnn_on_gpu"); String padding = op.GetAttrString("padding"); } foreach (Output output in op.Outputs) { int[] shape = inceptionGraph.Graph.GetTensorShape(output); if (output.NumConsumers == 0) { couldBeOutputs.Add(name); } } Buffer buffer = inceptionGraph.Graph.GetOpDef(op.OpType); Tensorflow.OpDef opDef = Tensorflow.OpDef.Parser.ParseFrom(buffer.Data); } using (Buffer versionDef = inceptionGraph.Graph.Versions()) { int l = versionDef.Length; } Inception.RecognitionResult[] results = inceptionGraph.Recognize(imageTensor); Trace.WriteLine(String.Format("Object is {0} with {1}% probability", results[0].Label, results[0].Probability * 100)); processCompleted = true; }; inceptionGraph.Init(); while (!processCompleted) { Thread.Sleep(1000); } } }
public void TestInception() { using (Tensor imageTensor = ImageIO.ReadTensorFromImageFile("grace_hopper.jpg", 224, 224, 128.0f, 1.0f)) using (Inception inceptionGraph = new Inception()) { bool processCompleted = false; inceptionGraph.OnDownloadCompleted += (sender, e) => { HashSet <string> opNames = new HashSet <string>(); HashSet <string> couldBeInputs = new HashSet <string>(); HashSet <string> couldBeOutputs = new HashSet <string>(); foreach (Operation op in inceptionGraph.Graph) { String name = op.Name; opNames.Add(name); if (op.NumInputs == 0 && op.OpType.Equals("Placeholder")) { couldBeInputs.Add(op.Name); } foreach (Output output in op.Outputs) { int[] shape = inceptionGraph.Graph.GetTensorShape(output); if (output.NumConsumers == 0) { couldBeOutputs.Add(name); } } } using (Buffer versionDef = inceptionGraph.Graph.Versions()) { int l = versionDef.Length; } float[] probability = inceptionGraph.Recognize(imageTensor); if (probability != null) { String[] labels = inceptionGraph.Labels; float maxVal = 0; int maxIdx = 0; for (int i = 0; i < probability.Length; i++) { if (probability[i] > maxVal) { maxVal = probability[i]; maxIdx = i; } } Trace.WriteLine(String.Format("Object is {0} with {1}% probability", labels[maxIdx], maxVal * 100)); } processCompleted = true; }; inceptionGraph.Init(); while (!processCompleted) { Thread.Sleep(1000); } } }