示例#1
0
        private int ClassifyCategory(Texture2D input_tex)
        {
            float startclass = Time.realtimeSinceStartup;

            // extract feautures from network.
            var featureVector = featureExtractor.FetchOutput <List <float>, Texture2D>(input_tex);

            // normalize feature vector
            var           output_array = GenericUtils.ConvertToDouble(featureVector.ToArray()); // convert to double format.
            var           norm_fv      = svm_model.NormalizeElements(output_array, svm_model.muData, svm_model.sigmaData);
            List <double> norm_fv_list = new List <double>(norm_fv);

            // calculate propabilities
            var probs = svmClassifier.FetchOutput <List <float>, List <double> >(norm_fv_list);

            // get max propability class.
            float maxValue       = probs.Max();
            int   category_index = probs.IndexOf(maxValue);

            float endclass = Time.realtimeSinceStartup;

            classificationTime = CalculateTimeDifference(startclass, endclass);

            return(category_index);
        }
        /// <summary>
        /// This methods aims to classify a 2D texture based on the svm model that has been initialized
        /// in the start method of this class.
        /// </summary>
        /// <param name="input_Tex"> Texture2D obj</param>
        /// <returns>Integer type</returns>
        private int ClassifyCategory(Texture2D input_tex)
        {
            Texture2D tempTex = new Texture2D(input_tex.width, input_tex.height, TextureFormat.RGB24, false);

#if UNITY_ANDROID
            //Graphics.CopyTexture(input_tex, tempTex);
            if (SystemInfo.copyTextureSupport == UnityEngine.Rendering.CopyTextureSupport.None)
            {
                Color[] sourcePixels = input_tex.GetPixels();
                tempTex.SetPixels(sourcePixels);
                tempTex.Apply();
            }
            else
            {
                Graphics.CopyTexture(input_tex, tempTex);
            }
#endif

#if UNITY_IOS
            if (SystemInfo.copyTextureSupport == UnityEngine.Rendering.CopyTextureSupport.None)
            {
                Color[] sourcePixels = input_tex.GetPixels();
                tempTex.SetPixels(sourcePixels);
                tempTex.Apply();
            }
            else
            {
                Graphics.CopyTexture(input_tex, tempTex);
            }
#endif

            float startclass = Time.realtimeSinceStartup;

            // extract feautures from network.
            var featureVector = featureExtractor.FetchOutput <List <float>, Texture2D>(tempTex);

            // normalize feature vector
            var           output_array = ConvertToDouble(featureVector.ToArray()); // convert to double format.
            var           norm_fv      = svm_model.NormalizeElements(output_array, svm_model.muData, svm_model.sigmaData);
            List <double> norm_fv_list = new List <double>(norm_fv);

            // calculate propabilities
            var probs = svmClassifier.FetchOutput <List <float>, List <double> >(norm_fv_list);

            // get max propability class.
            float maxValue      = probs.Max();
            int   categoryIndex = probs.IndexOf(maxValue);

            float endclass = Time.realtimeSinceStartup;
            classificationTime = CalculateTimeDifference(startclass, endclass);
            DestroyImmediate(tempTex);
            return(categoryIndex);
        }
    // Start is called before the first frame update
    void Start()
    {
        TFSharpClassification classifier = new TFSharpClassification("input_1", "Logits/Softmax", 224, 224, 127.5f, 127.5f, model, labelsFile, angle, 0.05f);

        var tex = Resources.Load <Texture2D>("Textures/Masoutis/" + image_file);
        //var tex = Resources.Load<Texture2D>("Textures/" + image_file);

        //Texture2D tex = new Texture2D(img.texture.width, img.texture.height);
        //tex = img.texture as Texture2D;

        RawImage img2 = GameObject.Find("img2").GetComponent <RawImage>();

        img2.texture = tex;;

        //var oute = classifier.FetchOutput(tex);
        //foreach (KeyValuePair<string, float> value in oute)
        //{
        //    Debug.Log("class :" + value.Key + ": " + value.Value);
        //}



        //print("===============================");

        //IModelPrediction classifier_3 = new TFClassification("input_1", "Logits/Softmax", 224, 224, 127.5f, 127.5f, model, labelsFile, angle, 0.01f);

        //var output = classifier_3.FetchOutput<IList, Texture2D>(tex);
        //foreach (KeyValuePair<string, float> value in output)
        //{
        //    Debug.Log("class :" + value.Key + ": " + value.Value);
        //}

        IModelPrediction classifier_4 = new TFFeatureExtraction("input_1", "block_15_project/convolution", 224, 224, 127.5f, 127.5f, model, labelsFile, angle, 0.01f);
        var output = classifier_4.FetchOutput <List <float>, Texture2D>(tex);

        double[] output_array = GenericUtils.ConvertToDouble(output.ToArray());

        SVMClassification obj = new SVMClassification();

        obj.SetModelParameters("SVM_Weights", "mu", "sigma");
        var              norm_fv      = obj.NormalizeElements(output_array, obj.muData, obj.sigmaData);
        List <double>    norm_fv_list = new List <double>(norm_fv);
        IModelPrediction svm          = obj;
        var              probs        = svm.FetchOutput <List <float>, List <double> >(norm_fv_list);

        foreach (var item in probs)
        {
            Debug.Log(item);
        }
    }