コード例 #1
0
        private static int Main(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("No test image here.");
                Console.WriteLine("Usage: ./image-classification-predict apple.jpg");
                return(0);
            }

            var testFile = args[0];

            // Models path for your model, you have to modify it
            var jsonFile   = "model/Inception/Inception-BN-symbol.json";
            var paramFile  = "model/Inception/Inception-BN-0126.params";
            var synsetFile = "model/Inception/synset.txt";
            var ndFile     = "model/Inception/mean_224.nd";

            if (!File.Exists(jsonFile))
            {
                Console.WriteLine($"{jsonFile} is not found.");
                return(0);
            }

            if (!File.Exists(paramFile))
            {
                Console.WriteLine($"{paramFile} is not found.");
                return(0);
            }

            if (!File.Exists(synsetFile))
            {
                Console.WriteLine($"{synsetFile} is not found.");
                return(0);
            }

            var jsonData  = new BufferFile(jsonFile);
            var paramData = new BufferFile(paramFile);

            // Parameters
            var          devType       = 1;  // 1: cpu, 2: gpu
            const int    devId         = 0;  // arbitrary.
            var          numInputNodes = 1u; // 1 for feedforward
            const string inputKey      = "data";

            string[] inputKeys = { inputKey };

            // Image size and channels
            const int width    = 224;
            const int height   = 224;
            const int channels = 3;

            var inputShapeIndptr = new[] { 0u, 4u };
            var inputShapeData   = new[] { (uint)1,
                                           (uint)channels,
                                           (uint)height,
                                           (uint)width };

            if (jsonData.GetLength() == 0 || paramData.GetLength() == 0)
            {
                return(-1);
            }

            MXNet.MXPredCreate(File.ReadAllText(jsonFile),
                               paramData.GetBuffer(),
                               paramData.GetLength(),
                               devType,
                               devId,
                               numInputNodes,
                               inputKeys,
                               inputShapeIndptr,
                               inputShapeData,
                               out var predHnd);

            var imageSize = (uint)(width * height * channels);

            // Read Mean Data
            float[]      ndData = null;
            NDListHandle ndHnd  = null;
            var          ndBuf  = new BufferFile(ndFile);

            if (ndBuf.GetLength() > 0)
            {
                const uint nd_index = 0u;
                var        ndLen    = 0u;
                uint[]     ndShape  = null;
                string     ndKey    = null;
                var        ndNdim   = 0u;

                MXNet.MXNDListCreate(ndBuf.GetBuffer(), ndBuf.GetLength(), out ndHnd, out ndLen);

                MXNet.MXNDListGet(ndHnd, nd_index, out ndKey, out ndData, out ndShape, out ndNdim);
            }

            // Read Image Data
            var imageData = new float[imageSize];

            GetImageFile(testFile, imageData, channels, new Size(width, height), ndData);

            // Set Input Image
            MXNet.MXPredSetInput(predHnd, "data", imageData, imageSize);

            // Do Predict Forward
            MXNet.MXPredForward(predHnd);

            var outputIndex = 0u;

            // Get Output Result
            MXNet.MXPredGetOutputShape(predHnd, outputIndex, out var shape, out var shape_len);

            var size = 1u;

            for (var i = 0u; i < shape_len; ++i)
            {
                size *= shape[i];
            }

            var data = new float[size];

            MXNet.MXPredGetOutput(predHnd, outputIndex, data, size);

            // Release NDList
            ndHnd?.Dispose();

            // Release Predictor
            predHnd?.Dispose();


            // Synset path for your model, you have to modify it
            var synset = LoadSynset(synsetFile);

            // Print Output Data
            PrintOutputResult(data, synset);

            return(0);
        }
コード例 #2
0
        public float[] Detect(string img)
        {
            if (!File.Exists(img))
            {
                throw new FileNotFoundException($"{img} is not found.");
            }

            using (var image = Cv2.ImRead(img))
            {
                if (image.Empty())
                {
                    throw new ArgumentException($"Unable to load image file: {img}");
                }

                if (image.Channels() != 3)
                {
                    throw new ArgumentException($"RGB image required");
                }

                using (var resized = image.Resize(new Size(this._Width, this._Height)))
                {
                    var size   = resized.Channels() * resized.Rows * resized.Cols;
                    var inData = new float[size];

                    // de-interleave and minus means
                    unsafe
                    {
                        var ptr = (byte *)resized.Ptr();
                        fixed(float *dataPtr = &inData[0])
                        {
                            var tmp = dataPtr;

                            for (var i = 0; i < size; i += 3)
                            {
                                *tmp = ptr[i] - this._MeanR;
                                tmp++;
                            }

                            for (var i = 1; i < size; i += 3)
                            {
                                *tmp = ptr[i] - this._MeanG;
                                tmp++;
                            }

                            for (var i = 2; i < size; i += 3)
                            {
                                *tmp = ptr[i] - this._MeanB;
                                tmp++;
                            }
                        }
                    }

                    // usr model to forwad
                    MXNet.MXPredSetInput(this._Predictor, "data", inData, (uint)size);

                    var sw = new Stopwatch();
                    sw.Start();
                    MXNet.MXPredForward(this._Predictor);
                    MXNet.MXPredGetOutputShape(this._Predictor, 0, out var shape, out var shapeLen);

                    var ttSize = 1u;
                    for (var i = 0u; i < shapeLen; ++i)
                    {
                        ttSize *= shape[i];
                    }

                    var outputs = new float[ttSize];
                    MXNet.MXPredGetOutput(this._Predictor, 0, outputs, ttSize);
                    sw.Stop();
                    Console.WriteLine($"Forward elapsed time: {sw.ElapsedMilliseconds} ms");

                    return(outputs);
                }
            }
        }