コード例 #1
0
ファイル: CubeNet.cs プロジェクト: ssingh23-lab/warp
        public void Export(string newModelDir)
        {
            if (newModelDir.Last() != '/' && newModelDir.Last() != '\\')
            {
                newModelDir += "/";
            }

            TFOutput NodeSaver, NodeSaverPath;

            //if (!ForTraining)
            {
                NodeSaver     = Graph["save_1/control_dependency"][0];
                NodeSaverPath = Graph["save_1/Const"][0];
            }
            //else
            //{
            //    NodeSaver = Graph["save_2/control_dependency"][0];
            //    NodeSaverPath = Graph["save_2/Const"][0];
            //}

            Directory.CreateDirectory(newModelDir);

            if (new DirectoryInfo(newModelDir).FullName != new DirectoryInfo(ModelDir).FullName)
            {
                if (Directory.Exists(newModelDir + "variables"))
                {
                    Directory.Delete(newModelDir + "variables", true);
                }
                Directory.CreateDirectory(newModelDir + "variables");

                foreach (var fileName in Directory.EnumerateFiles(ModelDir))
                {
                    File.Copy(fileName, newModelDir + Helper.PathToNameWithExtension(fileName), true);
                }
            }

            TFTensor TensorPath = TFTensor.CreateString(Encoding.ASCII.GetBytes(newModelDir + "variables/variables"));
            var      Runner     = Session.GetRunner().AddInput(NodeSaverPath, TensorPath);

            Runner.Run(NodeSaver);

            if (Directory.EnumerateDirectories(newModelDir + "variables", "variables_temp*").Count() > 0)
            {
                string TempName = Directory.EnumerateDirectories(newModelDir + "variables", "variables_temp*").First();
                foreach (var oldPath in Directory.EnumerateFiles(TempName))
                {
                    string OldName = Helper.PathToNameWithExtension(oldPath);
                    string NewName = "variables" + OldName.Substring(OldName.IndexOf("."));
                    string NewPath = newModelDir + "variables/" + NewName;

                    File.Move(oldPath, NewPath);
                }
                Directory.Delete(TempName, true);
            }

            TensorPath.Dispose();
        }
コード例 #2
0
    public void LoadTFModel(Texture2D tex)
    {
        var      shape       = new TFShape(1, inputWidth, inputHeight, 3);
        var      input       = graph[inputName][0];
        TFTensor inputTensor = null;

        int  angle = 90;
        Flip flip  = Flip.NONE;

        if (input.OutputType == TFDataType.Float)
        {
            float[] imgData = Utils.DecodeTexture(tex, inputWidth, inputHeight,
                                                  inputMean, inputStd, angle, flip);
            inputTensor = TFTensor.FromBuffer(shape, imgData, 0, imgData.Length);
        }
        else if (input.OutputType == TFDataType.UInt8)
        {
            byte[] imgData = Utils.DecodeTexture(tex, inputWidth, inputHeight, angle, flip);
            inputTensor = TFTensor.FromBuffer(shape, imgData, 0, imgData.Length);
        }
        else
        {
            throw new Exception($"Input date type {input.OutputType} is not supported.");
        }

        var runner = session.GetRunner();

        runner.AddInput(input, inputTensor).Fetch(graph[outputName][0]);

        var output  = runner.Run()[0];
        var outputs = output.GetValue() as float[, ];

        inputTensor.Dispose();
        output.Dispose();

        var list = new List <KeyValuePair <string, float> >();

        for (int i = 0; i < labels.Length; i++)
        {
            var confidence = outputs[0, i];
            if (confidence < 0.05f)
            {
                continue;
            }

            list.Add(new KeyValuePair <string, float>(labels[i], confidence));
        }

        var results = list.OrderByDescending(i => i.Value).Take(3).ToList();

        foreach (KeyValuePair <string, float> value in results)
        {
            Debug.Log("my model: " + value.Key);
        }
    }
コード例 #3
0
    public IList Classify(Texture2D texture, int numResults = 5, float threshold = 0.1f,
                          int angle = 0, Flip flip = Flip.NONE)
    {
        var      shape       = new TFShape(1, inputWidth, inputHeight, 3);
        var      input       = graph[inputName][0];
        TFTensor inputTensor = null;

        if (input.OutputType == TFDataType.Float)
        {
            float[] imgData = Utils.DecodeTexture(texture, inputWidth, inputHeight,
                                                  inputMean, inputStd, angle, flip);
            inputTensor = TFTensor.FromBuffer(shape, imgData, 0, imgData.Length);
        }
        else if (input.OutputType == TFDataType.UInt8)
        {
            byte[] imgData = Utils.DecodeTexture(texture, inputWidth, inputHeight, angle, flip);
            inputTensor = TFTensor.FromBuffer(shape, imgData, 0, imgData.Length);
        }
        else
        {
            throw new Exception($"Input date type {input.OutputType} is not supported.");
        }

        var runner = session.GetRunner();

        runner.AddInput(input, inputTensor).Fetch(graph[outputName][0]);

        var output  = runner.Run()[0];
        var outputs = output.GetValue() as float[, ];

        inputTensor.Dispose();
        output.Dispose();

        var list = new List <KeyValuePair <string, float> >();

        for (int i = 0; i < labels.Length; i++)
        {
            var confidence = outputs[0, i];
            if (confidence < threshold)
            {
                continue;
            }

            list.Add(new KeyValuePair <string, float>(labels[i], confidence));
        }

        var results = list.OrderByDescending(i => i.Value).Take(numResults).ToList();

        //Utils.Log(results);

        return(results);
    }
    public override List <float> FetchIntermidiateOutput(Texture2D tex)
    {
        var      shape       = new TFShape(1, inputWidth, inputHeight, 3);
        var      input       = graph[inputName][0];
        TFTensor inputTensor = null;

        Flip flip = Flip.NONE;

        if (input.OutputType == TFDataType.Float || input.OutputType == TFDataType.UInt8)
        {
            float[] imgData = Utils.DecodeTexture(tex, inputWidth, inputHeight,
                                                  inputMean, inputStd, angle, flip);
            inputTensor = TFTensor.FromBuffer(shape, imgData, 0, imgData.Length);
        }
        //else if (input.OutputType == TFDataType.UInt8)
        //{
        //    byte[] imgData = Utils.DecodeTexture(tex, inputWidth, inputHeight, angle, flip);
        //    inputTensor = TFTensor.FromBuffer(shape, imgData, 0, imgData.Length);
        //}
        else
        {
            throw new Exception($"Input date type {input.OutputType} is not supported.");
        }

        var runner = session.GetRunner();

        runner.AddInput(input, inputTensor).Fetch(graph[outputName][0]);

        var output          = runner.Run()[0];
        var feautures       = output.GetValue() as float[, , , ];
        var jagged_features = ((float[][][][])output.GetValue(jagged: true));

        var flat3D = jagged_features.SelectMany(a => a).ToArray();
        var flat2D = flat3D.SelectMany(a => a).ToArray();
        var flat1D = flat2D.SelectMany(a => a).ToArray();

        List <float> featureVector = new List <float>(flat1D);

        inputTensor.Dispose();
        output.Dispose();

        return(featureVector);
    }
    public IList Detect(Texture2D texture, int numResultsPerClass = 1, float threshold = 0.2f,
                        int angle = 0, Flip flip = Flip.NONE)
    {
        var      shape       = new TFShape(1, _inputWidth, _inputHeight, 3);
        var      input       = graph[_inputName][0];
        TFTensor inputTensor = null;

        if (input.OutputType == TFDataType.Float)
        {
            float[] imgData = Utils.DecodeTexture(texture, _inputWidth, _inputHeight,
                                                  _inputMean, _inputStd, angle, flip);
            inputTensor = TFTensor.FromBuffer(shape, imgData, 0, imgData.Length);
        }
        else if (input.OutputType == TFDataType.UInt8)
        {
            byte[] imgData = Utils.DecodeTexture(texture, _inputWidth, _inputHeight, angle, flip);
            inputTensor = TFTensor.FromBuffer(shape, imgData, 0, imgData.Length);
        }
        else
        {
            throw new Exception($"Input date type {input.OutputType} is not supported.");
        }

        var runner = session.GetRunner();

        runner.AddInput(input, inputTensor);

        IList results;

        if (_detectionModel == DetectionModels.SSD)
        {
            results = ParseSSD(runner, threshold, numResultsPerClass);
        }
        else
        {
            results = ParseYOLO(runner, threshold, numResultsPerClass);
        }

        inputTensor.Dispose();

        return(results);
    }
コード例 #6
0
    void IRCamThread()
    {
        var runner = session.GetRunner();

        irRunning = true;
        while (irRunning)
        {
            byte[] imgsBytes = new byte[1 + 5 * INPUT_SIZE * INPUT_SIZE];
            UpdateDWIRHandImages(dwImuPluginObj, imgsBytes);
            if (imgsBytes[0] == 110) // hand imges not updated yet
            {
                Thread.Sleep(5);
                continue;
            }
            int imgNum = imgsBytes[0];

            int   pixInd    = 1;
            int   bestLabel = 3;
            float bestScore = 0.0f;
            for (int i = 0; i < imgNum; ++i)
            {
                // get image pixels
                float[] imgArray = new float[64 * 64];
                for (int ind = 0; ind < 64 * 64; ++ind)
                {
                    imgArray[ind] = imgsBytes[pixInd] / 255.0f;
                    pixInd++;
                }
                try
                {
                    // classify
                    runner = session.GetRunner();
                    TFTensor inputImg     = TFTensor.FromBuffer(new TFShape(new long[] { 1, 64, 64, 1 }), imgArray, 0, 64 * 64);
                    TFTensor inputDropout = new TFTensor(1.0f);
                    runner.AddInput(graph["data/X_placeholder"][0], inputImg);
                    runner.AddInput(graph["dropout"][0], inputDropout);
                    runner.Fetch(graph["softmax_linear/logits"][0]);
                    TFTensor[] output = runner.Run();
                    float[,] recurrent_tensor = output[0].GetValue() as float[, ];

                    // Dispose TFTensors
                    for (int ind = 0; ind < output.Length; ++ind)
                    {
                        output[ind].Dispose();
                    }
                    inputImg.Dispose();
                    inputDropout.Dispose();


                    // get best classification
                    int   predict = 0;
                    float prob    = recurrent_tensor[0, 0];
                    for (int k = 1; k < 4; ++k)
                    {
                        if (recurrent_tensor[0, k] > prob)
                        {
                            prob    = recurrent_tensor[0, k];
                            predict = k;
                        }
                    }
                    // classification != 3 means there is a valid gesture

                    /*
                     * 0: fist
                     * 1: finger
                     * 2: palm
                     * 3: other
                     */
                    if (predict != 3 && prob > bestScore)
                    {
                        bestScore = prob;
                        bestLabel = predict;
                    }
                }
                catch (Exception e)
                {
                    Debug.Log(e.ToString());
                }
            }

            // feed static gesture and get dynamic gesture
            byte[] outbuf = Encoding.ASCII.GetBytes(bestLabel.ToString());
            byte[] inbyte = new byte[2];
            UpdateHandGesture(dwImuPluginObj, outbuf, inbyte);
            gestureMsg = Encoding.ASCII.GetString(inbyte);
            UpdateGesture();
        }
        Debug.Log("DWPluginScripy_PC IRCamThread quit!");
    }
コード例 #7
0
        public static void Main(string [] args)
        {
            TFSessionOptions options = new TFSessionOptions();

            unsafe
            {
                //byte[] PUConfig = new byte[] { 0x32, 0x05, 0x20, 0x01, 0x2a, 0x01, 0x30, 0x38, 0x01 }; //gpu
                byte[] PUConfig = new byte[] { 0x0a, 0x07, 0x0a, 0x03, 0x67, 0x70, 0x75, 0x10, 0x00 }; //cpu
                fixed(void *ptr = &PUConfig[0])
                {
                    options.SetConfig(new IntPtr(ptr), PUConfig.Length);
                }
            }
            TFSession session;
            var       graph = new TFGraph();

            using (TFSession sess = new TFSession(graph, options))
                using (var metaGraphUnused = new TFBuffer())
                {
                    session = sess.FromSavedModel(options, null, "tzb", new[] { "serve" }, graph, metaGraphUnused);
                    IEnumerable <TensorFlow.DeviceAttributes> iem = session.ListDevices();
                    foreach (object obj in iem)
                    {
                        Console.WriteLine(((DeviceAttributes)obj).Name);
                    }
                    var labels = File.ReadAllLines("tzb/label.txt");
                    //打印节点名称

                    /*IEnumerable<TensorFlow.TFOperation> iem = graph.GetEnumerator();
                     * foreach (object obj in iem)
                     * {
                     *  Console.WriteLine(((TFOperation)obj).Name);
                     * }*/
                    //while(true)
                    float[] eimg = new float[224 * 224];
                    for (int i = 0; i < 224 * 224; i++)
                    {
                        eimg[i] = 0;
                    }
                    TFTensor ten = TFTensor.FromBuffer(tfs, eimg, 0, 224 * 224 * 1);
                    for (int j = 0; j < 3; j++)
                    {
                        var runner = session.GetRunner();
                        runner.AddInput(graph["images"][0], ten).Fetch(graph["classes"].Name);
                        var output = runner.Run();
                    }
                    ten.Dispose();
                    string[] files = Directory.GetFiles("tzb/images/defect", "*.*");
                    //while(true)
                    foreach (string file in files)
                    {
                        DateTime bft = DateTime.Now;
                        //var tensor = Image2Tensor(file);
                        //break;
                        var tensor = ImageUtil.CreateTensorFromImageFile(file);
                        //TFTensor tensor = TFTensor.FromBuffer(tfs, eimg, 0, 224 * 224);
                        var runner = session.GetRunner();
                        runner.AddInput(graph["images"][0], tensor).Fetch(graph["classes"].Name);
                        var      output = runner.Run();
                        DateTime aft    = DateTime.Now;
                        TimeSpan ts     = aft.Subtract(bft);
                        System.Threading.Thread.Sleep(50);
                        var result = output[0];
                        int class_ = ((int[])result.GetValue(jagged: true))[0];
                        Console.WriteLine(file + " best_match: " + class_ + " " + labels[class_] + " time: " + ts.TotalMilliseconds);
                    }
                }
        }