Ejemplo n.º 1
0
        public Detector(string modelPrefix, int epoch, int width, int height, float meanR, float meanG, float meanB, int deviceType, int deviceId)
        {
            if (epoch < 0 || epoch > 9999)
            {
                throw new ArgumentOutOfRangeException($"Invalid epoch number: {epoch}");
            }

            var modelFile = $"{modelPrefix}-{epoch:D4}.params";

            if (!File.Exists(modelFile))
            {
                throw new FileNotFoundException($"{modelFile} is not found");
            }

            var jsonFile = $"{modelPrefix}-symbol.json";

            if (!File.Exists(jsonFile))
            {
                throw new FileNotFoundException($"{jsonFile} is not found");
            }

            if (width < 1 || height < 1)
            {
                throw new ArgumentOutOfRangeException($"Invalid width or height: {width}, {height}");
            }

            this._Width       = width;
            this._Height      = height;
            this.InputName    = "data";
            this.InputKeys    = new string[1];
            this.InputKeys[0] = this.InputName;
            var inputShapeIndptr = new[] { 0u, 4u };
            var inputShapeData   = new[] { 1u, 3u, (uint)this._Width, (uint)this._Height };

            this._MeanR = meanR;
            this._MeanG = meanG;
            this._MeanB = meanB;

            // load model
            var paramFileBuffer = File.ReadAllBytes(modelFile);
            var json            = File.ReadAllText(jsonFile);

            if (paramFileBuffer.Length > 0)
            {
                MXNet.MXPredCreate(json, paramFileBuffer, paramFileBuffer.Length, deviceType, deviceId, 1, this.InputKeys, inputShapeIndptr, inputShapeData, out this._Predictor);
            }
            else
            {
                throw new ArgumentException($"Unable to read model file: {modelFile}");
            }
        }