Example #1
0
        public Inception3(int classes = 1000, string prefix = "", ParameterDict @params = null) : base(prefix,
                                                                                                       @params)
        {
            Features = new HybridSequential("");
            Features.Add(Inception.MakeBasicConv(32, (3, 3), (2, 2)));
            Features.Add(Inception.MakeBasicConv(32, (3, 3)));
            Features.Add(Inception.MakeBasicConv(64, (3, 3), padding: (1, 1)));
            Features.Add(new MaxPool2D((3, 3), (2, 2)));
            Features.Add(Inception.MakeBasicConv(80, (1, 1)));
            Features.Add(Inception.MakeBasicConv(192, (3, 3)));
            Features.Add(new MaxPool2D((3, 3), (2, 2)));
            Features.Add(Inception.MakeA(32, "A1_"));
            Features.Add(Inception.MakeA(64, "A2_"));
            Features.Add(Inception.MakeA(64, "A3_"));
            Features.Add(Inception.MakeB("B_"));
            Features.Add(Inception.MakeC(128, "C1_"));
            Features.Add(Inception.MakeC(160, "C2_"));
            Features.Add(Inception.MakeC(160, "C3_"));
            Features.Add(Inception.MakeC(192, "C4_"));
            Features.Add(Inception.MakeD("D_"));
            Features.Add(Inception.MakeE("E1_"));
            Features.Add(Inception.MakeE("E2_"));
            Features.Add(new AvgPool2D((8, 8)));
            Features.Add(new Dropout(0.5f));
            RegisterChild(Features, "features");

            Output = new Dense(classes);
            RegisterChild(Output, "output");
        }
Example #2
0
    // Use this for initialization
    void Start()
    {
        bool loaded = TfInvoke.CheckLibraryLoaded();

        _inceptionGraph = new Inception();
        _liveCameraView = false;

        /*
         * WebCamDevice[] devices = WebCamTexture.devices;
         * cameraCount = devices.Length;
         *
         * if (cameraCount == 0)
         * {
         *  _liveCameraView = false;
         * }
         * else
         * {
         *  _liveCameraView = true;
         *  webcamTexture = new WebCamTexture(devices[0].name);
         *
         *  baseRotation = transform.rotation;
         *  webcamTexture.Play();
         *  //data = new Color32[webcamTexture.width * webcamTexture.height];
         * }*/

        StartCoroutine(_inceptionGraph.Init());
    }
Example #3
0
        public InceptionPage()
            : base()
        {
            _inceptionGraph = new Inception();
            _inceptionGraph.OnDownloadProgressChanged += onDownloadProgressChanged;
            _inceptionGraph.OnDownloadCompleted       += onDownloadCompleted;

            OnImagesLoaded += (sender, image) =>
            {
                try
                {
                    SetMessage("Please wait...");
                    SetImage();


                    Tensor    imageTensor = Emgu.TF.Models.ImageIO.ReadTensorFromImageFile(image[0], 224, 224, 128.0f, 1.0f / 128.0f);
                    Stopwatch watch       = Stopwatch.StartNew();
                    Inception.RecognitionResult result = _inceptionGraph.MostLikely(imageTensor);

                    SetMessage(String.Format("Object is {0} with {1}% probability. Recognized in {2} milliseconds.", result.Label, result.Probability * 100, watch.ElapsedMilliseconds));

                    SetImage(image[0]);
                }
                catch (Exception excpt)
                {
                    String msg = excpt.Message.Replace(System.Environment.NewLine, " ");
                    SetMessage(msg);
                }
            };
        }
        public IActionResult ReTrainInception()
        {
            Inception.Model = Inception.LoadAndScoreModel(tagsTsv, inceptionTrainImagesFolder, inceptionPb, imageClassifierZip);
            Console.WriteLine("inception re-trained");

            return(Ok("inception re-trained"));
        }
Example #5
0
        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);
            }
        }
Example #6
0
        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);
        }
Example #7
0
        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));
        }
        /// <summary>
        ///  Ovládání hry v jednotlivých tabech
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TabChanged(object sender, SelectionChangedEventArgs e)
        {
            if (inc.IsSelected) // při spuštění hry
            {
                incB = new Inception(p);
            }

            if (story.IsSelected) //příběh
            {
                storyB = new Story();
            }

            /*
             * if (equip.IsSelected) //výměna oblečených itemů
             * {
             *  equipB = new Equip();
             * }
             */
            if (stats.IsSelected) //editace statů
            {
                statsB = new Stats();
            }

            if (fight.IsSelected) //soubojová meachanika a spuštění
            {
                fightB = new Fight(enemy, p);
            }
        }
Example #9
0
    // Use this for initialization
    void Start()
    {
        bool loaded = TfInvoke.Init();

        _inceptionGraph = new Inception();

        //Change the following flag to set default detection based on image / live camera view
        _liveCameraView = false;

        if (_liveCameraView)
        {
            _devices     = WebCamTexture.devices;
            _cameraCount = _devices.Length;

            if (_cameraCount == 0)
            {
                _liveCameraView = false;
            }
            else
            {
                _liveCameraView = true;
                _webcamTexture  = new WebCamTexture(_devices[0].name);

                baseRotation = transform.rotation;
                _webcamTexture.Play();
                //data = new Color32[webcamTexture.width * webcamTexture.height];
            }
        }

        StartCoroutine(_inceptionGraph.Init());
    }
        public void Recognise(string fileName)
        {
            FileSource    = BitmapFrame.Create(new Uri(fileName));
            ImageFileName = fileName;

            try
            {
                using (_inceptionGraph = new Inception())
                {
                    _fileName = fileName;
                    _inceptionGraph.OnDownloadCompleted += OnDownloadComplete;
                    if (!string.IsNullOrEmpty(DownloadURL) &&
                        !string.IsNullOrEmpty(InceptionGraphFileName) &&
                        !string.IsNullOrEmpty(OutputLabelsFileName))
                    {
                        _inceptionGraph.Init(new string[] { InceptionGraphFileName, OutputLabelsFileName }, DownloadURL);
                    }
                    else
                    {
                        _inceptionGraph.Init();
                    }
                }
            }
            catch (Exception ex)
            {
                _exceptionLogDataAccess.LogException(ex.ToString());
                IsModalVisible = false;
            }
        }
Example #11
0
        public InceptionPage()
            : base()
        {
            var button = this.TopButton;

            button.Text     = "Perform Image Classification";
            button.Clicked += OnButtonClicked;

            _inception = new Inception();
            _inception.OnDownloadProgressChanged += onDownloadProgressChanged;
        }
 /// <summary>
 /// Two DnsResourceDataTransactionKey are equal iff their algorithm, inception, expiration, mode, error, key and other fields are equal.
 /// </summary>
 public bool Equals(DnsResourceDataTransactionKey other)
 {
     return(other != null &&
            Algorithm.Equals(other.Algorithm) &&
            Inception.Equals(other.Inception) &&
            Expiration.Equals(other.Expiration) &&
            Mode.Equals(other.Mode) &&
            Error.Equals(other.Error) &&
            Key.Equals(other.Key) &&
            Other.Equals(other.Other));
 }
Example #13
0
        partial 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;
            }
            _inceptionGraph.Init();
        }
Example #14
0
        public override bool IsValid(params ILegalPerson[] persons)
        {
            var lessor = this.Lessor(persons);
            var lessee = this.Lessee(persons);

            if (lessor == null || lessee == null)
            {
                return(false);
            }

            var orTitle = lessor.GetLegalPersonTypeName();
            var eeTitle = lessee.GetLegalPersonTypeName();

            if (Inception == DateTime.MinValue)
            {
                AddReasonEntry($"{orTitle} {lessor.Name} and {eeTitle} {lessee.Name}, {nameof(Inception)} is unassigned");
                return(false);
            }

            if (Terminus != null && Terminus.Value <= Inception)
            {
                AddReasonEntry($"{orTitle} {lessor.Name} and {eeTitle} {lessee.Name}, {nameof(Terminus)} " +
                               $"date of {Terminus.Value.ToShortDateString()} is less-than-equal-to " +
                               $"the {nameof(Inception)} date of {Inception.ToShortDateString()}");
                return(false);
            }

            if (Assent != null && !Assent.IsValid(persons))
            {
                AddReasonEntryRange(Assent.GetReasonEntries());
                return(false);
            }

            if (SubjectProperty == null || Offer == null)
            {
                AddReasonEntry($"{orTitle} {lessor.Name} and {eeTitle} {lessee.Name}, " +
                               $"{nameof(SubjectProperty)} '{SubjectProperty?.Name}' " +
                               $"or {nameof(Offer)} '{Offer?.Name}' is unassigned");
                return(false);
            }

            if (Acceptance?.Invoke(Offer) == null)
            {
                AddReasonEntry($"{orTitle} {lessor.Name} and {eeTitle} {lessee.Name}, {nameof(Acceptance)} " +
                               $"using {nameof(Offer)} '{Offer.Name}' returned nothing");
                return(false);
            }

            return(true);
        }
Example #15
0
        public MainForm()
        {
            InitializeComponent();

            TfInvoke.CheckLibraryLoaded();
            messageLabel.Text = String.Empty;
            cameraButton.Text = "Start Camera";

            DisableUI();

            //Use the following code for the full inception model
            inceptionGraph = new Inception();
            inceptionGraph.OnDownloadProgressChanged += OnDownloadProgressChangedEventHandler;
            inceptionGraph.OnDownloadCompleted       += onDownloadCompleted;

            inceptionGraph.Init();
        }
Example #16
0
    // Use this for initialization
    void Start()
    {
        //Warning: The following code is used to get around a https certification issue for downloading tesseract language files from Github
        //Do not use this code in a production environment. Please make sure you understand the security implication from the following code before using it
        ServicePointManager.ServerCertificateValidationCallback += delegate(object sender, X509Certificate cert, X509Chain chain, SslPolicyErrors sslPolicyErrors) {
            HttpWebRequest webRequest = sender as HttpWebRequest;
            if (webRequest != null)
            {
                String requestStr = webRequest.Address.AbsoluteUri;
                if (requestStr.StartsWith(@"https://github.com/") || requestStr.StartsWith(@"https://raw.githubusercontent.com/"))
                {
                    return(true);
                }
            }
            return(false);
        };

        TfInvoke.CheckLibraryLoaded();

        WebCamDevice[] devices = WebCamTexture.devices;
        cameraCount      = devices.Length;
        _inceptionGraph  = new Inception();
        _inceptionLabels = _inceptionGraph.Labels;
        if (cameraCount == 0)
        {
            _liveCameraView = false;
            Texture2D texture = Resources.Load <Texture2D>("space_shuttle");

            RecognizeAndUpdateText(texture);


            this.GetComponent <GUITexture>().texture    = texture;
            this.GetComponent <GUITexture>().pixelInset = new Rect(-texture.width / 2, -texture.height / 2, texture.width, texture.height);
            //this.GetComponent<GUIText>().pixelOffset = new Vector2();
        }
        else
        {
            _liveCameraView = true;
            webcamTexture   = new WebCamTexture(devices[0].name);

            baseRotation = transform.rotation;
            webcamTexture.Play();
            //data = new Color32[webcamTexture.width * webcamTexture.height];
        }
    }
Example #17
0
        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));
                }
        }
Example #18
0
 private void PrivateBoot()
 {
     try
     {
         Application app = (Application)Inception.CreateInstanceAndUnwrap("Framework", "Dover.Framework.Application");
         SAPServiceFactory.PrepareForInception(Inception); // need to be after Application creation because of assembly resolving from embedded resources.
         Inception.SetData("assemblyName", "Framework");   // Used to get current AssemblyName for logging and reflection
         InceptionAddinManager = app.Resolve <AddinManager>();
         Sponsor <Application>  appSponsor          = new Sponsor <Application>(app);
         Sponsor <AddinManager> addInManagerSponsor = new Sponsor <AddinManager>(InceptionAddinManager);
         app.RunInception();
         AppDomain.Unload(Inception); // release AppDomain on shutdown.
     }
     catch (Exception e)
     {
         System.Windows.Forms.MessageBox.Show(string.Format("{0}\n{1}", e.Message, e.StackTrace));
         throw e;
     }
 }
Example #19
0
        public MainForm()
        {
            InitializeComponent();

            TfInvoke.CheckLibraryLoaded();
            messageLabel.Text = String.Empty;

            DisableUI();


            inceptionGraph = new Inception();
            inceptionGraph.OnDownloadProgressChanged += OnDownloadProgressChangedEventHandler;
            inceptionGraph.OnDownloadCompleted       += onDownloadCompleted;

            //Use the following code for the full inception model
            inceptionGraph.Init();

            //Uncomment the following code to use a retrained model to recognize followers, downloaded from the internet
            //inceptionGraph.Init(new string[] {"optimized_graph.pb", "output_labels.txt"}, "https://github.com/emgucv/models/raw/master/inception_flower_retrain/", "Mul", "final_result");
        }
Example #20
0
        public InceptionPage()
            : base()
        {
            var button = this.GetButton();

            button.Text     = "Perform Image Classification";
            button.Clicked += OnButtonClicked;

            _inception = new Inception();

            OnImagesLoaded += (sender, imageFiles) =>
            {
                SetMessage("Please wait...");
                SetImage();
                _imageFiles = imageFiles;

#if !DEBUG
                try
#endif
                {
                    if (_inception.Imported)
                    {
                        onDownloadCompleted(this, new System.ComponentModel.AsyncCompletedEventArgs(null, false, null));
                    }
                    else
                    {
                        SetMessage("Please wait while the Inception Model is being downloaded...");
                        _inception.OnDownloadProgressChanged += onDownloadProgressChanged;
                        _inception.OnDownloadCompleted       += onDownloadCompleted;
                        _inception.Init();
                    }
                }
#if !DEBUG
                catch (Exception e)
                {
                    String msg = e.Message.Replace(System.Environment.NewLine, " ");
                    SetMessage(msg);
                }
#endif
            };
        }
Example #21
0
        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;
        }
Example #22
0
        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));
            }
        }
Example #23
0
        public async Task <IActionResult> ImagePredictAsync()
        {
            using var reader = new StreamReader(Request.Body, Encoding.UTF8);
            string body = await reader.ReadToEndAsync();

            byte[] imageBytes = Convert.FromBase64String(body);
            string result     = "";

            try
            {
                var testImage = Path.Combine(inceptionTrainImagesFolder, "capture.jpg");

                Image image = Image.FromStream(new MemoryStream(imageBytes));
                image.Save(testImage, ImageFormat.Jpeg);

                var imageData = new ImageNetData()
                {
                    ImagePath = testImage,
                    Label     = Path.GetFileNameWithoutExtension(testImage)
                };

                if (Inception.Model is null)
                {
                    Inception.Model = Inception.LoadModel(tagsTsv, inceptionTrainImagesFolder, inceptionPb, imageClassifierZip);
                }

                var prediction = Inception.Model.Predict(imageData);
                result = prediction.PredictedLabelValue;
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            return(Ok(result));
        }
Example #24
0
    // Update is called once per frame
    void Update()
    {
        if (_inceptionGraph == null)
        {
            if (_loadingModel)
            {
                return;
            }
            _loadingModel    = true;
            DisplayText.text = String.Format("Loading Inception Model, please wait...");
            System.Threading.ThreadPool.QueueUserWorkItem(
                (o) =>
            {
                try
                {
                    _inceptionGraph  = new Inception();
                    _inceptionLabels = _inceptionGraph.Labels;
                }
                catch (Exception e)
                {
                    //DisplayText.text = e.Message;
                    Console.WriteLine(e);
                    return;
                    //throw;
                }

                _loadingModel = false;
            });
            return;
        }

        if (_liveCameraView)
        {
            if (webcamTexture != null && webcamTexture.didUpdateThisFrame)
            {
                #region convert the webcam texture to RGBA bytes

                if (data == null || (data.Length != webcamTexture.width * webcamTexture.height))
                {
                    data = new Color32[webcamTexture.width * webcamTexture.height];
                }
                webcamTexture.GetPixels32(data);

                if (bytes == null || bytes.Length != data.Length * 4)
                {
                    bytes = new byte[data.Length * 4];
                }
                GCHandle handle = GCHandle.Alloc(data, GCHandleType.Pinned);
                Marshal.Copy(handle.AddrOfPinnedObject(), bytes, 0, bytes.Length);
                handle.Free();

                #endregion

                #region convert the RGBA bytes to texture2D

                if (resultTexture == null || resultTexture.width != webcamTexture.width ||
                    resultTexture.height != webcamTexture.height)
                {
                    resultTexture = new Texture2D(webcamTexture.width, webcamTexture.height, TextureFormat.RGBA32,
                                                  false);
                }

                resultTexture.LoadRawTextureData(bytes);
                resultTexture.Apply();

                #endregion

                if (!_textureResized)
                {
                    this.GetComponent <GUITexture>().pixelInset = new Rect(-webcamTexture.width / 2,
                                                                           -webcamTexture.height / 2, webcamTexture.width, webcamTexture.height);
                    _textureResized = true;
                }

                transform.rotation = baseRotation * Quaternion.AngleAxis(webcamTexture.videoRotationAngle, Vector3.up);

                RecognizeAndUpdateText(resultTexture);

                this.GetComponent <GUITexture>().texture = resultTexture;
                //count++;
            }
        }
        else
        {
            if (!_staticViewRendered)
            {
                if (_inceptionGraph == null)
                {
                    return;
                }
                Texture2D texture = Resources.Load <Texture2D>("space_shuttle");

                RecognizeAndUpdateText(texture);

                this.GetComponent <GUITexture>().texture    = texture;
                this.GetComponent <GUITexture>().pixelInset = new Rect(-texture.width / 2, -texture.height / 2, texture.width, texture.height);
                _staticViewRendered = true;
            }
        }
    }
Example #25
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);
                    }
                }
        }
Example #26
0
        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);
                    }
                }
        }
Example #27
0
        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
            };
        }
Example #28
0
        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
            };
        }
Example #29
0
 public void TestLoadLargeGraph()
 {
     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");
 }