private void ThisAddIn_Startup(object sender, System.EventArgs e) { richTextBox1 = new RichTextBox(); richTextBox1.Dock = DockStyle.Fill; richTextBox1.SelectionFont = new Font("Verdana", 12, FontStyle.Bold); richTextBox1.SelectionColor = Color.Red; Clipboard.SetImage(Image.FromFile(_imagePath)); richTextBox1.Paste(); // Load and create the model outToLog($"Loading modelfile '{_modelPath}' on the '{_deviceName}' device"); int ticks = Environment.TickCount; _model = LearningModel.LoadFromFilePath(_modelPath); ticks = Environment.TickCount - ticks; outToLog($"model file loaded in { ticks } ticks"); // Create the evaluation session with the model and device _session = new LearningModelSession(_model); outToLog("Getting color management mode..."); ColorManagementMode colorManagementMode = GetColorManagementMode(); outToLog("Loading the image..."); ImageFeatureValue imageTensor = LoadImageFile(colorManagementMode); // create a binding object from the session outToLog("Binding..."); LearningModelBinding binding = new LearningModelBinding(_session); binding.Bind(_model.InputFeatures.ElementAt(0).Name, imageTensor); outToLog("Running the model..."); ticks = Environment.TickCount; var results = _session.Evaluate(binding, "RunId"); ticks = Environment.TickCount - ticks; outToLog($"model run took { ticks } ticks"); // retrieve results from evaluation var resultTensor = results.Outputs[_model.OutputFeatures.ElementAt(0).Name] as TensorFloat; var resultVector = resultTensor.GetAsVectorView(); PrintResults(resultVector); Form form1 = new Form(); form1.Size = new Size(800, 800); form1.Controls.Add(richTextBox1); //form1.Show(); form1.ShowDialog(); }
// usage: SqueezeNet [modelfile] [imagefile] [cpu|directx] static int Main(string[] args) { if (!ParseArgs(args)) { Console.WriteLine("Usage: [executable_name] [modelfile] [imagefile] [cpu|directx]"); return(-1); } // Load and create the model Console.WriteLine($"Loading modelfile '{_modelPath}' on the '{_deviceName}' device"); int ticks = Environment.TickCount; _model = LearningModel.LoadFromFilePath(_modelPath); ticks = Environment.TickCount - ticks; Console.WriteLine($"model file loaded in { ticks } ticks"); // Create the evaluation session with the model and device _session = new LearningModelSession(_model, new LearningModelDevice(_deviceKind)); Console.WriteLine("Getting color management mode..."); ColorManagementMode colorManagementMode = GetColorManagementMode(); Console.WriteLine("Loading the image..."); ImageFeatureValue imageTensor = LoadImageFile(colorManagementMode); // create a binding object from the session Console.WriteLine("Binding..."); LearningModelBinding binding = new LearningModelBinding(_session); binding.Bind(_model.InputFeatures.ElementAt(0).Name, imageTensor); Console.WriteLine("Running the model..."); ticks = Environment.TickCount; var results = _session.Evaluate(binding, "RunId"); ticks = Environment.TickCount - ticks; Console.WriteLine($"model run took { ticks } ticks"); // retrieve results from evaluation var resultTensor = results.Outputs[_model.OutputFeatures.ElementAt(0).Name] as TensorFloat; var resultVector = resultTensor.GetAsVectorView(); PrintResults(resultVector); return(0); }
private static ImageFeatureValue LoadImageFile(ColorManagementMode colorManagementMode) { BitmapDecoder decoder = null; try { StorageFile imageFile = AsyncHelper(StorageFile.GetFileFromPathAsync(System.IO.Path.GetFullPath(_imagePath))); IRandomAccessStream stream = AsyncHelper(imageFile.OpenReadAsync()); decoder = AsyncHelper(BitmapDecoder.CreateAsync(stream)); } catch (Exception e) { Console.WriteLine("Failed to load image file! Make sure that fully qualified paths are used."); Console.WriteLine(" Exception caught.\n {0}", e); System.Environment.Exit(e.HResult); } SoftwareBitmap softwareBitmap = null; try { softwareBitmap = AsyncHelper( decoder.GetSoftwareBitmapAsync( decoder.BitmapPixelFormat, decoder.BitmapAlphaMode, new BitmapTransform(), ExifOrientationMode.RespectExifOrientation, colorManagementMode ) ); } catch (Exception e) { Console.WriteLine("Failed to create SoftwareBitmap! Please make sure that input image is within the model's colorspace."); Console.WriteLine(" Exception caught.\n {0}", e); System.Environment.Exit(e.HResult); } softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied); VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap); return(ImageFeatureValue.CreateFromVideoFrame(inputImage)); }