internal BatchPredictionEngine(IHostEnvironment env, Stream modelStream, bool ignoreMissingColumns,
                                       SchemaDefinition inputSchemaDefinition = null, SchemaDefinition outputSchemaDefinition = null)
        {
            Contracts.AssertValue(env);
            Contracts.AssertValue(modelStream);
            Contracts.AssertValueOrNull(inputSchemaDefinition);
            Contracts.AssertValueOrNull(outputSchemaDefinition);

            // Initialize pipe.
            _srcDataView = DataViewConstructionUtils.CreateFromEnumerable(env, new TSrc[] { }, inputSchemaDefinition);

            // Load transforms.
            var pipe = env.LoadTransforms(modelStream, _srcDataView);

            // Load predictor (if present) and apply default scorer.
            // REVIEW: distinguish the case of predictor / no predictor?
            var predictor = env.LoadPredictorOrNull(modelStream);

            if (predictor != null)
            {
                var roles = ModelFileUtils.LoadRoleMappingsOrNull(env, modelStream);
                pipe = roles != null
                    ? env.CreateDefaultScorer(RoleMappedData.CreateOpt(pipe, roles), predictor)
                    : env.CreateDefaultScorer(env.CreateExamples(pipe, "Features"), predictor);
            }

            _pipeEngine = new PipeEngine <TDst>(env, pipe, ignoreMissingColumns, outputSchemaDefinition);
        }
Ejemplo n.º 2
0
        public static IDataView LoadPipeWithPredictor(IHostEnvironment env, Stream modelStream, IDataView view)
        {
            // Load transforms.
            var pipe = env.LoadTransforms(modelStream, view);

            // Load predictor (if present) and apply default scorer.
            // REVIEW: distinguish the case of predictor / no predictor?
            var predictor = env.LoadPredictorOrNull(modelStream);

            if (predictor != null)
            {
                var roles = ModelFileUtils.LoadRoleMappingsOrNull(env, modelStream);
                pipe = roles != null
                    ? env.CreateDefaultScorer(new RoleMappedData(pipe, roles, opt : true), predictor)
                       : env.CreateDefaultScorer(new RoleMappedData(pipe, label : null, "Features"), predictor);
            }
            return(pipe);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="env">environment</param>
        /// <param name="modelStream">stream</param>
        /// <param name="output">name of the output column</param>
        /// <param name="outputIsFloat">output is a gloat (true) or a vector of floats (false)</param>
        /// <param name="conc">number of concurrency threads</param>
        /// <param name="features">features name</param>
        public ValueMapperPredictionEngineFloat(IHostEnvironment env, Stream modelStream,
                                                string output   = "Probability", bool outputIsFloat = true, int conc = 1,
                                                string features = "Features")
        {
            _env = env;
            if (_env == null)
            {
                throw Contracts.Except("env must not be null");
            }
            var inputs = new FloatVectorInput[0];
            var view   = ComponentCreation.CreateStreamingDataView <FloatVectorInput>(_env, inputs);

            long modelPosition = modelStream.Position;

            _predictor = ComponentCreation.LoadPredictorOrNull(_env, modelStream);
            if (_predictor == null)
            {
                throw _env.Except("Unable to load a model.");
            }
            modelStream.Seek(modelPosition, SeekOrigin.Begin);
            _transforms = ComponentCreation.LoadTransforms(_env, modelStream, view);
            if (_transforms == null)
            {
                throw _env.Except("Unable to load a model.");
            }

            var data = _env.CreateExamples(_transforms, features);

            if (data == null)
            {
                throw _env.Except("Cannot create rows.");
            }
            var scorer = _env.CreateDefaultScorer(data, _predictor);

            if (scorer == null)
            {
                throw _env.Except("Cannot create a scorer.");
            }

            _valueMapper = new ValueMapperFromTransformFloat <VBuffer <float> >(_env,
                                                                                scorer, features, output, conc: conc);
            if (_valueMapper == null)
            {
                throw _env.Except("Cannot create a mapper.");
            }
            if (outputIsFloat)
            {
                _mapper       = _valueMapper.GetMapper <VBuffer <float>, float>();
                _mapperVector = null;
            }
            else
            {
                _mapper       = null;
                _mapperVector = _valueMapper.GetMapper <VBuffer <float>, VBuffer <float> >();
            }
        }
            public ValueMapperExample(string modelName, string features)
            {
                _env       = EnvHelper.NewTestEnvironment();
                _predictor = ModelFileUtils.LoadPredictorOrNull(_env, File.OpenRead(modelName));
                var inputs = new Input[0];

                var view = DataViewConstructionUtils.CreateFromEnumerable(_env, inputs);

                _transforms = ModelFileUtils.LoadTransforms(_env, view, File.OpenRead(modelName));
                var data   = _env.CreateExamples(_transforms, features);
                var scorer = _env.CreateDefaultScorer(data, _predictor);

                _valueMapper = new ValueMapperFromTransformFloat <VBuffer <float> >(_env, scorer, "Features", "Probability");
                _mapper      = _valueMapper.GetMapper <VBuffer <float>, float>();
            }
Ejemplo n.º 5
0
            public ValueMapperExample(string modelName, string features)
            {
                _env       = EnvHelper.NewTestEnvironment();
                _predictor = _env.LoadPredictorOrNull(File.OpenRead(modelName));
                var inputs = new Input[0];

                var view = _env.CreateStreamingDataView <Input>(inputs);

                _transforms = ComponentCreation.LoadTransforms(_env, File.OpenRead(modelName), view);
                var data   = _env.CreateExamples(_transforms, features);
                var scorer = _env.CreateDefaultScorer(data, _predictor);

                _valueMapper = new ValueMapperFromTransformFloat <VBuffer <float> >(_env, scorer, "Features", "Probability");
                _mapper      = _valueMapper.GetMapper <VBuffer <float>, float>();
            }
Ejemplo n.º 6
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="env">environment</param>
        /// <param name="modelStream">stream</param>
        /// <param name="conc">number of concurrency threads</param>
        /// <param name="features">features column</param>
        public ValueMapperPredictionEngine(IHostEnvironment env, Stream modelStream,
                                           int conc = 1, string features = "Features")
        {
            _env = env;
            if (_env == null)
            {
                throw Contracts.Except("env must not be null");
            }
            var inputs = new TRowValue[0];
            var view   = ComponentCreation.CreateStreamingDataView <TRowValue>(_env, inputs);

            long modelPosition = modelStream.Position;

            _predictor = ComponentCreation.LoadPredictorOrNull(_env, modelStream);
            if (_predictor == null)
            {
                throw _env.Except("Unable to load a model.");
            }
            modelStream.Seek(modelPosition, SeekOrigin.Begin);
            _transforms = ComponentCreation.LoadTransforms(_env, modelStream, view);
            if (_transforms == null)
            {
                throw _env.Except("Unable to load a model.");
            }

            var data = _env.CreateExamples(_transforms, features);

            if (data == null)
            {
                throw _env.Except("Cannot create rows.");
            }
            var scorer = _env.CreateDefaultScorer(data, _predictor);

            if (scorer == null)
            {
                throw _env.Except("Cannot create a scorer.");
            }
            _CreateMapper(scorer, conc);
        }