コード例 #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);
        }