public void Recognize(Mat m) { Tensor imageTensor = Emgu.TF.TensorConvert.ReadTensorFromMatBgr(m, 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.ReadTensorFromMatBgr(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.ReadTensorFromMatBgr(fileName, 299, 299, 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 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. Recognized in {2} milliseconds.", labels[maxIdx], maxVal * 100, sw.ElapsedMilliseconds); } if (InvokeRequired) { this.Invoke((MethodInvoker)(() => { messageLabel.Text = resStr; pictureBox.Image = m.Bitmap; })); } else { messageLabel.Text = resStr; pictureBox.Image = m.Bitmap; } }
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; } }
partial void inceptionClicked(NSObject sender) { messageLabel.StringValue = "Inception Clicked"; mainImageView.Image = null; Inception inceptionGraph = new Inception(); String fileName = "space_shuttle.jpg"; Tensor imageTensor = Emgu.TF.Models.ImageIO.ReadTensorFromImageFile(fileName, 224, 224, 128.0f, 1.0f / 128.0f); //MultiboxGraph.Result detectResult = graph.Detect(imageTensor); 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.StringValue = resStr; mainImageView.Image = new NSImage(fileName); }
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; }
async void inceptionClicked(NSObject sender) { SetMessage("Please wait while we download Inception model from internet..."); SetImage(null); if (_inceptionGraph == null) { _inceptionGraph = new Inception(); _inceptionGraph.OnDownloadProgressChanged += OnDownloadProgressChanged; //_inceptionGraph.OnDownloadCompleted += inceptionGraph_OnDownloadCompleted; } await _inceptionGraph.Init(); String fileName = "space_shuttle.jpg"; //Tensor imageTensor = Emgu.TF.Models.ImageIO.ReadTensorFromImageFile<float>(fileName, 224, 224, 128.0f, 1.0f / 128.0f); Tensor imageTensor = Emgu.TF.Models.ImageIO.ReadTensorFromImageFile <float>(fileName, 224, 224, 128.0f, 1.0f); var recognitionResults = _inceptionGraph.Recognize(imageTensor); String resStr = String.Empty; if (recognitionResults != null && recognitionResults.Length > 0) { resStr = String.Format("Object is {0} with {1}% probability.", recognitionResults[0].Label, recognitionResults[0].Probability * 100); } SetMessage(resStr); SetImage(new NSImage(fileName)); }
private async void OnButtonClicked(Object sender, EventArgs args) { SetMessage("Please wait while the Inception Model is being downloaded..."); await _inception.Init(); if (!_inception.Imported) { SetMessage("Failed to initialize Inception Model."); return; } SetImage(); String[] imageFiles = await LoadImages(new string[] { "tulips.jpg" }); //handle user cancel if (imageFiles == null || (imageFiles.Length > 0 && imageFiles[0] == null)) { SetMessage(""); return; } Stopwatch watch = Stopwatch.StartNew(); var result = _inception.Recognize(imageFiles[0]); watch.Stop(); String resStr = String.Format("Object is {0} with {1}% probability. Recognition completed in {2} milliseconds.", result[0].Label, result[0].Probability * 100, watch.ElapsedMilliseconds); SetImage(imageFiles[0]); SetMessage(resStr); }
private void RecognizeAndUpdateText(Texture2D texture) { if (_inceptionGraph == null) { return; } Tensor imageTensor = ImageIO.ReadTensorFromTexture2D(texture, 224, 224, 128.0f, 1.0f, true); float[] probability = _inceptionGraph.Recognize(imageTensor); //String resStr = String.Empty; if (probability != null) { float maxVal = 0; int maxIdx = 0; for (int i = 0; i < probability.Length; i++) { if (probability[i] > maxVal) { maxVal = probability[i]; maxIdx = i; } } DisplayText.text = String.Format("Object is {0} with {1}% probability.", _inceptionLabels[maxIdx], maxVal * 100); } }
private void onDownloadCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e) { Stopwatch watch = Stopwatch.StartNew(); var result = _inception.Recognize(_imageFiles[0]); watch.Stop(); String resStr = String.Format("Object is {0} with {1}% probability. Recognition completed in {2} milliseconds.", result[0].Label, result[0].Probability * 100, watch.ElapsedMilliseconds); SetImage(_imageFiles[0]); SetMessage(resStr); }
private void RecognizeAndUpdateText(Texture2D texture) { if (!_inceptionGraph.Imported) { return; } Tensor imageTensor = ImageIO.ReadTensorFromTexture2D(texture, 224, 224, 128.0f, 1.0f, true); Inception.RecognitionResult[] results = _inceptionGraph.Recognize(imageTensor); _displayMessage = String.Format("Object is {0} with {1}% probability.", results[0].Label, results[0].Probability * 100); }
private async void OnButtonClicked(Object sender, EventArgs args) { SetMessage("Please wait while the Inception Model is being downloaded..."); await _inception.Init(); SetImage(); String[] imageFiles = await LoadImages(new string[] { "tulips.jpg" }); Stopwatch watch = Stopwatch.StartNew(); var result = _inception.Recognize(imageFiles[0]); watch.Stop(); String resStr = String.Format("Object is {0} with {1}% probability. Recognition completed in {2} milliseconds.", result[0].Label, result[0].Probability * 100, watch.ElapsedMilliseconds); SetImage(imageFiles[0]); SetMessage(resStr); }
public async Task TestInceptionBatch() { //using (Tensor imageTensor = ImageIO.ReadTensorFromImageFile<float>("grace_hopper.jpg", 224, 224, 128.0f, 1.0f)) using (Tensor imageTensor = ImageIO.ReadTensorFromImageFiles <float>( new String[] { "grace_hopper.jpg", "grace_hopper.jpg" }, 224, 224, 128.0f, 1.0f)) using (Inception inceptionGraph = new Inception()) { await inceptionGraph.Init(); Inception.RecognitionResult[][] results = inceptionGraph.Recognize(imageTensor); Trace.WriteLine(String.Format("Object is {0} with {1}% probability", results[0][0].Label, results[0][0].Probability * 100)); } }
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 RecognizeAndUpdateText(Color32[] pixels, int width, int height) { if (_inceptionGraph == null) { return; } if (!_inceptionGraph.Imported) { return; } Inception.RecognitionResult[][] results; using (Tensor imageTensor = ImageIO.ReadTensorFromColor32(pixels, width, height, 224, 224, 128.0f, 1.0f, true)) { results = _inceptionGraph.Recognize(imageTensor); } _displayMessage = String.Format( "Object is {0} with {1}% probability.", results[0][0].Label, results[0][0].Probability * 100); }
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 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 InceptionPage(Model model) : base() { Title = model == Model.Flower ? "Flower Recognition" : "Object recognition (Inception)"; _model = model; if (_inceptionGraph == null) { SessionOptions so = new SessionOptions(); if (TfInvoke.IsGoogleCudaEnabled) { Tensorflow.ConfigProto config = new Tensorflow.ConfigProto(); config.GpuOptions = new Tensorflow.GPUOptions(); config.GpuOptions.AllowGrowth = true; so.SetConfig(config.ToProtobuf()); } _inceptionGraph = new Inception(null, so); _inceptionGraph.OnDownloadProgressChanged += onDownloadProgressChanged; _inceptionGraph.OnDownloadCompleted += onDownloadCompleted; _inceptionGraph.OnDownloadCompleted += (sender, e) => { OnButtonClicked(sender, e); }; } OnImagesLoaded += (sender, image) => { #if !DEBUG try #endif { SetMessage("Please wait..."); SetImage(); Tensor imageTensor; if (_model == Model.Flower) { imageTensor = Emgu.TF.Models.ImageIO.ReadTensorFromImageFile <float>(image[0], 299, 299, 0.0f, 1.0f / 255.0f, false, false); } else { imageTensor = Emgu.TF.Models.ImageIO.ReadTensorFromImageFile <float>(image[0], 224, 224, 128.0f, 1.0f); } Inception.RecognitionResult result; 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. result = _inceptionGraph.Recognize(imageTensor)[0]; _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 following 3 lines Stopwatch sw = Stopwatch.StartNew(); result = _inceptionGraph.Recognize(imageTensor)[0]; sw.Stop(); String msg = String.Format("Object is {0} with {1}% probability. Recognized in {2} milliseconds.", result.Label, result.Probability * 100, sw.ElapsedMilliseconds); SetMessage(msg); var jpeg = Emgu.Models.NativeImageIO.ImageFileToJpeg(image[0]); SetImage(jpeg.Raw, jpeg.Width, jpeg.Height); } #if !DEBUG catch (Exception excpt) { String msg = excpt.Message.Replace(System.Environment.NewLine, " "); SetMessage(msg); } #endif }; }
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); } } }
public InceptionPage() : base() { var button = this.GetButton(); button.Text = "Perform Image Recognition"; button.Clicked += OnButtonClicked; OnImagesLoaded += async(sender, image) => { SetMessage("Please wait..."); SetImage(); Task <Tuple <string, string, long> > t = new Task <Tuple <string, string, long> >( () => { try { SetMessage("Please wait while we download the Inception Model from internet."); Inception inceptionGraph = new Inception(); SetMessage("Please wait..."); Tensor imageTensor = Emgu.TF.Models.ImageIO.ReadTensorFromImageFile(image[0], 224, 224, 128.0f, 1.0f / 128.0f); //MultiboxGraph.Result detectResult = graph.Detect(imageTensor); 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); } return(new Tuple <string, string, long>(image[0], resStr, 0)); //SetImage(t.Result.Item1); //GetLabel().Text = String.Format("Detected {0} in {1} milliseconds.", t.Result.Item2, t.Result.Item3); } catch (Exception e) { String msg = e.Message.Replace(System.Environment.NewLine, " "); SetMessage(msg); return(new Tuple <string, string, long>(null, msg, 0)); } }); t.Start(); #if !(__UNIFIED__) var result = await t; SetImage(t.Result.Item1); GetLabel().Text = t.Result.Item2; #endif }; }