Пример #1
0
        internal static ModelProto ConvertToOnnxProtobuf(this ModelOperationsCatalog catalog, ITransformer transform, IDataView inputData, int opSetVersion)
        {
            var env = catalog.GetEnvironment();
            var ctx = new OnnxContextImpl(env, "model", "ML.NET", "0", 0, "machinelearning.dotnet", OnnxVersion.Stable, opSetVersion);

            return(ConvertToOnnxProtobufCore(env, ctx, transform, inputData));
        }
Пример #2
0
        /// <summary>
        /// Performs image classification using transfer learning.
        /// usage of this API requires additional NuGet dependencies on TensorFlow redist, see linked document for more information.
        /// <format type="text/markdown">
        /// <![CDATA[
        /// [!include[io](~/../docs/samples/docs/api-reference/tensorflow-usage.md)]
        /// ]]>
        /// </format>
        /// </summary>
        /// <param name="catalog"></param>
        /// <param name="options">An <see cref="Options"/> object specifying advanced options for <see cref="ImageClassificationEstimator"/>.</param>
        public static ImageClassificationEstimator ImageClassification(
            this ModelOperationsCatalog catalog, Options options)
        {
            options.EarlyStoppingCriteria = options.DisableEarlyStopping ? null : options.EarlyStoppingCriteria ?? new EarlyStopping();

            var env = CatalogUtils.GetEnvironment(catalog);

            return(new ImageClassificationEstimator(env, options, DnnUtils.LoadDnnModel(env, options.Arch, true)));
        }
        internal static ModelProto ConvertToOnnxProtobuf(this ModelOperationsCatalog catalog, ITransformer transform, IDataView inputData)
        {
            var env        = catalog.Environment;
            var ctx        = new OnnxContextImpl(env, "model", "ML.NET", "0", 0, "machinelearning.dotnet", OnnxVersion.Stable);
            var outputData = transform.Transform(inputData);
            LinkedList <ITransformCanSaveOnnx> transforms = null;

            using (var ch = env.Start("ONNX conversion"))
            {
                SaveOnnxCommand.GetPipe(ctx, ch, outputData, out IDataView root, out IDataView sink, out transforms);
                return(SaveOnnxCommand.ConvertTransformListToOnnxModel(ctx, ch, root, sink, transforms, null, null));
            }
        }
Пример #4
0
        /// <summary>
        /// Create the ML context.
        /// </summary>
        /// <param name="seed">Random seed. Set to <c>null</c> for a non-deterministic environment.</param>
        /// <param name="conc">Concurrency level. Set to 1 to run single-threaded. Set to 0 to pick automatically.</param>
        public MLContext(int?seed = null, int conc = 0)
        {
            _env = new LocalEnvironment(seed, conc, MakeCompositionContainer);
            _env.AddListener(ProcessMessage);

            BinaryClassification     = new BinaryClassificationContext(_env);
            MulticlassClassification = new MulticlassClassificationContext(_env);
            Regression = new RegressionContext(_env);
            Clustering = new ClusteringContext(_env);
            Ranking    = new RankingContext(_env);
            Transforms = new TransformsCatalog(_env);
            Model      = new ModelOperationsCatalog(_env);
            Data       = new DataOperations(_env);
        }
Пример #5
0
        /// <summary>
        /// Create the ML context.
        /// </summary>
        /// <param name="seed">Random seed. Set to <c>null</c> for a non-deterministic environment.</param>
        public MLContext(int?seed = null)
        {
            _env = new LocalEnvironment(seed);
            _env.AddListener(ProcessMessage);

            BinaryClassification     = new BinaryClassificationCatalog(_env);
            MulticlassClassification = new MulticlassClassificationCatalog(_env);
            Regression       = new RegressionCatalog(_env);
            Clustering       = new ClusteringCatalog(_env);
            Ranking          = new RankingCatalog(_env);
            AnomalyDetection = new AnomalyDetectionCatalog(_env);
            Transforms       = new TransformsCatalog(_env);
            Model            = new ModelOperationsCatalog(_env);
            Data             = new DataOperationsCatalog(_env);
        }
Пример #6
0
        /// <summary>
        /// Performs image classification using transfer learning.
        /// usage of this API requires additional NuGet dependencies on TensorFlow redist, see linked document for more information.
        /// <format type="text/markdown">
        /// <![CDATA[
        /// [!include[io](~/../docs/samples/docs/api-reference/tensorflow-usage.md)]
        /// ]]>
        /// </format>
        /// </summary>
        /// <param name="catalog"></param>
        /// <param name="featuresColumnName">The name of the input features column.</param>
        /// <param name="labelColumnName">The name of the labels column.</param>
        /// <param name="scoreColumnName">The name of the output score column.</param>
        /// <param name="predictedLabelColumnName">The name of the output predicted label columns.</param>
        /// <param name="validationSet">Validation set.</param>

        public static ImageClassificationEstimator ImageClassification(
            this ModelOperationsCatalog catalog,
            string featuresColumnName,
            string labelColumnName,
            string scoreColumnName          = "Score",
            string predictedLabelColumnName = "PredictedLabel",
            IDataView validationSet         = null
            )
        {
            var options = new ImageClassificationEstimator.Options()
            {
                FeaturesColumnName       = featuresColumnName,
                LabelColumnName          = labelColumnName,
                ScoreColumnName          = scoreColumnName,
                PredictedLabelColumnName = predictedLabelColumnName,
                ValidationSet            = validationSet
            };

            return(ImageClassification(catalog, options));
        }
Пример #7
0
        /// <summary>
        /// Retrain the dnn model on new data.
        /// </summary>
        /// <param name="catalog"></param>
        /// <param name="inputColumnNames"> The names of the model inputs.</param>
        /// <param name="outputColumnNames">The names of the requested model outputs.</param>
        /// <param name="labelColumnName">Name of the label column.</param>
        /// <param name="dnnLabel">Name of the node in DNN graph that is used as label during training in Dnn.
        /// The value of <paramref name="labelColumnName"/> from <see cref="IDataView"/> is fed to this node.</param>
        /// <param name="optimizationOperation">The name of the optimization operation in the Dnn graph.</param>
        /// <param name="modelPath">Path to model file to retrain.</param>
        /// <param name="epoch">Number of training iterations.</param>
        /// <param name="batchSize">Number of samples to use for mini-batch training.</param>
        /// <param name="lossOperation">The name of the operation in the Dnn graph to compute training loss (Optional).</param>
        /// <param name="metricOperation">The name of the operation in the Dnn graph to compute performance metric during training (Optional).</param>
        /// <param name="learningRateOperation">The name of the operation in the Dnn graph which sets optimizer learning rate (Optional).</param>
        /// <param name="learningRate">Learning rate to use during optimization (Optional).</param>
        /// <param name="addBatchDimensionInput">Add a batch dimension to the input e.g. input = [224, 224, 3] => [-1, 224, 224, 3].
        /// This parameter is used to deal with models that have unknown shape but the internal operators in the model require data to have batch dimension as well.</param>
        /// <param name="dnnFramework"></param>
        /// <remarks>
        /// The support for retraining is under preview.
        /// </remarks>
        public static DnnEstimator RetrainDnnModel(
            this ModelOperationsCatalog catalog,
            string[] outputColumnNames,
            string[] inputColumnNames,
            string labelColumnName,
            string dnnLabel,
            string optimizationOperation,
            string modelPath,
            int epoch                    = 10,
            int batchSize                = 20,
            string lossOperation         = null,
            string metricOperation       = null,
            string learningRateOperation = null,
            float learningRate           = 0.01f,
            bool addBatchDimensionInput  = false,
            DnnFramework dnnFramework    = DnnFramework.Tensorflow)
        {
            var options = new Options()
            {
                ModelLocation         = modelPath,
                InputColumns          = inputColumnNames,
                OutputColumns         = outputColumnNames,
                LabelColumn           = labelColumnName,
                TensorFlowLabel       = dnnLabel,
                OptimizationOperation = optimizationOperation,
                LossOperation         = lossOperation,
                MetricOperation       = metricOperation,
                Epoch = epoch,
                LearningRateOperation   = learningRateOperation,
                LearningRate            = learningRate,
                BatchSize               = batchSize,
                AddBatchDimensionInputs = addBatchDimensionInput,
                ReTrain = true
            };

            var env = CatalogUtils.GetEnvironment(catalog);

            return(new DnnEstimator(env, options, DnnUtils.LoadDnnModel(env, modelPath, true)));
        }
 /// <summary>
 /// Convert the specified <see cref="ITransformer"/> to ONNX format and writes to a stream.
 /// </summary>
 /// <param name="catalog">The class that <see cref="ConvertToOnnx(ModelOperationsCatalog, ITransformer, IDataView, Stream)"/> attached to.</param>
 /// <param name="transform">The <see cref="ITransformer"/> that will be converted into ONNX format.</param>
 /// <param name="inputData">The input of the specified transform.</param>
 /// <param name="stream">The stream to write the protobuf model to.</param>
 /// <returns>An ONNX model equivalent to the converted ML.NET model.</returns>
 public static void ConvertToOnnx(this ModelOperationsCatalog catalog, ITransformer transform, IDataView inputData, Stream stream) =>
 ConvertToOnnxProtobuf(catalog, transform, inputData).WriteTo(stream);
 internal ExplainabilityTransforms(ModelOperationsCatalog owner)
 {
     _env = owner._env;
 }
 internal ExplainabilityTransforms(ModelOperationsCatalog owner) : base(owner)
 {
 }
 protected SubCatalogBase(ModelOperationsCatalog owner)
 {
     Environment = owner.Environment;
 }
Пример #12
0
 /// <summary>
 /// Load TensorFlow model into memory. This is the convenience method that allows the model to be loaded once and subsequently use it for querying schema and creation of
 /// <see cref="TensorFlowEstimator"/> using <see cref="TensorFlowModel.ScoreTensorFlowModel(string, string, bool)"/>.
 /// </summary>
 /// <param name="catalog">The transform's catalog.</param>
 /// <param name="modelLocation">Location of the TensorFlow model.</param>
 /// <example>
 /// <format type="text/markdown">
 /// <![CDATA[
 /// [!code-csharp[LoadTensorFlowModel](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/TextClassification.cs)]
 /// ]]>
 /// </format>
 /// </example>
 public static TensorFlowModel LoadTensorFlowModel(this ModelOperationsCatalog catalog, string modelLocation)
 => TensorFlowUtils.LoadTensorFlowModel(CatalogUtils.GetEnvironment(catalog), modelLocation);
Пример #13
0
 /// <summary>
 /// Convert the specified <see cref="ITransformer"/> to ONNX format and writes to a stream.
 /// </summary>
 /// <param name="catalog">The class that <see cref="ConvertToOnnx(ModelOperationsCatalog, ITransformer, IDataView, Stream)"/> attached to.</param>
 /// <param name="transform">The <see cref="ITransformer"/> that will be converted into ONNX format.</param>
 /// <param name="inputData">The input of the specified transform.</param>
 /// <param name="stream">The stream to write the protobuf model to.</param>
 /// <param name="outputColumns">List of output columns we want to keep.</param>
 /// <returns>An ONNX model equivalent to the converted ML.NET model.</returns>
 public static void ConvertToOnnx(this ModelOperationsCatalog catalog, ITransformer transform, IDataView inputData, Stream stream, params string[] outputColumns) =>
 ConvertToOnnxProtobuf(catalog, transform, inputData, outputColumns).WriteTo(stream);
Пример #14
0
        /// <summary>
        /// Performs image classification using transfer learning.
        /// usage of this API requires additional NuGet dependencies on TensorFlow redist, see linked document for more information.
        /// <format type="text/markdown">
        /// <![CDATA[
        /// [!include[io](~/../docs/samples/docs/api-reference/tensorflow-usage.md)]
        /// ]]>
        /// </format>
        /// </summary>
        /// <param name="catalog"></param>
        /// <param name="featuresColumnName">The name of the input features column.</param>
        /// <param name="labelColumnName">The name of the labels column.</param>
        /// <param name="scoreColumnName">The name of the output score column.</param>
        /// <param name="predictedLabelColumnName">The name of the output predicted label columns.</param>
        /// <param name="arch">The architecture of the image recognition DNN model.</param>
        /// <param name="epoch">Number of training iterations. Each iteration/epoch refers to one pass over the dataset.</param>
        /// <param name="batchSize">The batch size for training.</param>
        /// <param name="learningRate">The learning rate for training.</param>
        /// <param name="disableEarlyStopping">Whether to disable use of early stopping technique. Training will go on for the full epoch count.</param>
        /// <param name="earlyStopping">Early stopping technique parameters to be used to terminate training when training metric stops improving.</param>
        /// <param name="metricsCallback">Callback for reporting model statistics during training phase.</param>
        /// <param name="statisticFrequency">Indicates the frequency of epochs at which to report model statistics during training phase.</param>
        /// <param name="framework">Indicates the choice of DNN training framework. Currently only tensorflow is supported.</param>
        /// <param name="modelSavePath">Optional name of the path where a copy new graph should be saved. The graph will be saved as part of model.</param>
        /// <param name="finalModelPrefix">The name of the prefix for the final mode and checkpoint files.</param>
        /// <param name="validationSet">Validation set.</param>
        /// <param name="testOnTrainSet">Indicates to evaluate the model on train set after every epoch.</param>
        /// <param name="reuseTrainSetBottleneckCachedValues">Indicates to not re-compute cached trainset bottleneck values if already available in the bin folder.</param>
        /// <param name="reuseValidationSetBottleneckCachedValues">Indicates to not re-compute validataionset cached bottleneck validationset values if already available in the bin folder.</param>
        /// <param name="trainSetBottleneckCachedValuesFilePath">Indicates the file path to store trainset bottleneck values for caching.</param>
        /// <param name="validationSetBottleneckCachedValuesFilePath">Indicates the file path to store validationset bottleneck values for caching.</param>
        /// <remarks>
        /// The support for image classification is under preview.
        /// </remarks>
        public static ImageClassificationEstimator ImageClassification(
            this ModelOperationsCatalog catalog,
            string featuresColumnName,
            string labelColumnName,
            string scoreColumnName          = "Score",
            string predictedLabelColumnName = "PredictedLabel",
            Architecture arch           = Architecture.InceptionV3,
            int epoch                   = 100,
            int batchSize               = 10,
            float learningRate          = 0.01f,
            bool disableEarlyStopping   = false,
            EarlyStopping earlyStopping = null,
            ImageClassificationMetricsCallback metricsCallback = null,
            int statisticFrequency  = 1,
            DnnFramework framework  = DnnFramework.Tensorflow,
            string modelSavePath    = null,
            string finalModelPrefix = "custom_retrained_model_based_on_",
            IDataView validationSet = null,
            bool testOnTrainSet     = true,
            bool reuseTrainSetBottleneckCachedValues           = false,
            bool reuseValidationSetBottleneckCachedValues      = false,
            string trainSetBottleneckCachedValuesFilePath      = "trainSetBottleneckFile.csv",
            string validationSetBottleneckCachedValuesFilePath = "validationSetBottleneckFile.csv"
            )
        {
            var options = new ImageClassificationEstimator.Options()
            {
                ModelLocation            = ModelLocation[arch],
                InputColumns             = new[] { featuresColumnName },
                OutputColumns            = new[] { scoreColumnName, predictedLabelColumnName },
                LabelColumn              = labelColumnName,
                TensorFlowLabel          = labelColumnName,
                Epoch                    = epoch,
                LearningRate             = learningRate,
                BatchSize                = batchSize,
                EarlyStoppingCriteria    = disableEarlyStopping ? null : earlyStopping == null ? new EarlyStopping() : earlyStopping,
                ScoreColumnName          = scoreColumnName,
                PredictedLabelColumnName = predictedLabelColumnName,
                FinalModelPrefix         = finalModelPrefix,
                Arch                = arch,
                MetricsCallback     = metricsCallback,
                StatisticsFrequency = statisticFrequency,
                Framework           = framework,
                ModelSavePath       = modelSavePath,
                ValidationSet       = validationSet,
                TestOnTrainSet      = testOnTrainSet,
                TrainSetBottleneckCachedValuesFilePath      = trainSetBottleneckCachedValuesFilePath,
                ValidationSetBottleneckCachedValuesFilePath = validationSetBottleneckCachedValuesFilePath,
                ReuseTrainSetBottleneckCachedValues         = reuseTrainSetBottleneckCachedValues,
                ReuseValidationSetBottleneckCachedValues    = reuseValidationSetBottleneckCachedValues
            };

            if (!File.Exists(options.ModelLocation))
            {
                if (options.Arch == Architecture.InceptionV3)
                {
                    var baseGitPath = @"https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/InceptionV3.meta";
                    using (WebClient client = new WebClient())
                    {
                        client.DownloadFile(new Uri($"{baseGitPath}"), @"InceptionV3.meta");
                    }

                    baseGitPath = @"https://github.com/SciSharp/TensorFlow.NET/raw/master/data/tfhub_modules.zip";
                    using (WebClient client = new WebClient())
                    {
                        client.DownloadFile(new Uri($"{baseGitPath}"), @"tfhub_modules.zip");
                        ZipFile.ExtractToDirectory(Path.Combine(Directory.GetCurrentDirectory(), @"tfhub_modules.zip"), @"tfhub_modules");
                    }
                }
                else if (options.Arch == Architecture.ResnetV2101)
                {
                    var baseGitPath = @"https://aka.ms/mlnet-resources/image/ResNet101Tensorflow/resnet_v2_101_299.meta";
                    using (WebClient client = new WebClient())
                    {
                        client.DownloadFile(new Uri($"{baseGitPath}"), @"resnet_v2_101_299.meta");
                    }
                }
                else if (options.Arch == Architecture.MobilenetV2)
                {
                    var baseGitPath = @"https://tlcresources.blob.core.windows.net/image/MobileNetV2TensorFlow/mobilenet_v2.meta";
                    using (WebClient client = new WebClient())
                    {
                        client.DownloadFile(new Uri($"{baseGitPath}"), @"mobilenet_v2.meta");
                    }
                }
            }

            var env = CatalogUtils.GetEnvironment(catalog);

            return(new ImageClassificationEstimator(env, options, DnnUtils.LoadDnnModel(env, options.ModelLocation, true)));
        }
Пример #15
0
        /// <summary>
        /// Performs image classification using transfer learning.
        /// </summary>
        /// <param name="catalog"></param>
        /// <param name="featuresColumnName">The name of the input features column.</param>
        /// <param name="labelColumnName">The name of the labels column.</param>
        /// <param name="outputGraphPath">Optional name of the path where a copy new graph should be saved. The graph will be saved as part of model.</param>
        /// <param name="scoreColumnName">The name of the output score column.</param>
        /// <param name="predictedLabelColumnName">The name of the output predicted label columns.</param>
        /// <param name="checkpointName">The name of the prefix for checkpoint files.</param>
        /// <param name="arch">The architecture of the image recognition DNN model.</param>
        /// <param name="dnnFramework">The backend DNN framework to use, currently only Tensorflow is supported.</param>
        /// <param name="epoch">Number of training epochs.</param>
        /// <param name="batchSize">The batch size for training.</param>
        /// <param name="learningRate">The learning rate for training.</param>
        /// <remarks>
        /// The support for image classification is under preview.
        /// </remarks>
        public static DnnEstimator ImageClassification(
            this ModelOperationsCatalog catalog,
            string featuresColumnName,
            string labelColumnName,
            string outputGraphPath          = null,
            string scoreColumnName          = "Score",
            string predictedLabelColumnName = "PredictedLabel",
            string checkpointName           = "_retrain_checkpoint",
            Architecture arch         = Architecture.ResnetV2101,
            DnnFramework dnnFramework = DnnFramework.Tensorflow,
            int epoch          = 10,
            int batchSize      = 20,
            float learningRate = 0.01f)
        {
            var options = new Options()
            {
                ModelLocation            = arch == Architecture.ResnetV2101 ? @"resnet_v2_101_299.meta" : @"InceptionV3.meta",
                InputColumns             = new[] { featuresColumnName },
                OutputColumns            = new[] { scoreColumnName, predictedLabelColumnName },
                LabelColumn              = labelColumnName,
                TensorFlowLabel          = labelColumnName,
                Epoch                    = epoch,
                LearningRate             = learningRate,
                BatchSize                = batchSize,
                AddBatchDimensionInputs  = arch == Architecture.InceptionV3 ? false : true,
                TransferLearning         = true,
                ScoreColumnName          = scoreColumnName,
                PredictedLabelColumnName = predictedLabelColumnName,
                CheckpointName           = checkpointName,
                Arch = arch,
                MeasureTrainAccuracy = false
            };

            if (!File.Exists(options.ModelLocation))
            {
                if (options.Arch == Architecture.InceptionV3)
                {
                    var baseGitPath = @"https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/graph/InceptionV3.meta";
                    using (WebClient client = new WebClient())
                    {
                        client.DownloadFile(new Uri($"{baseGitPath}"), @"InceptionV3.meta");
                    }

                    baseGitPath = @"https://github.com/SciSharp/TensorFlow.NET/raw/master/data/tfhub_modules.zip";
                    using (WebClient client = new WebClient())
                    {
                        client.DownloadFile(new Uri($"{baseGitPath}"), @"tfhub_modules.zip");
                        ZipFile.ExtractToDirectory(Path.Combine(Directory.GetCurrentDirectory(), @"tfhub_modules.zip"), @"tfhub_modules");
                    }
                }
                else if (options.Arch == Architecture.ResnetV2101)
                {
                    var baseGitPath = @"https://aka.ms/mlnet-resources/image/ResNet101Tensorflow/resnet_v2_101_299.meta";
                    using (WebClient client = new WebClient())
                    {
                        client.DownloadFile(new Uri($"{baseGitPath}"), @"resnet_v2_101_299.meta");
                    }
                }
            }

            var env = CatalogUtils.GetEnvironment(catalog);

            return(new DnnEstimator(env, options, DnnUtils.LoadDnnModel(env, options.ModelLocation, true)));
        }
Пример #16
0
 /// <summary>
 /// Load TensorFlow model into memory. This is the convenience method that allows the model to be loaded once and subsequently use it for querying schema and creation of
 /// <see cref="TensorFlowEstimator"/> using <see cref="TensorFlowModel.ScoreTensorFlowModel(string, string, bool)"/>.
 /// usage of this API requires additional NuGet dependencies on TensorFlow redist, see linked document for more information.
 /// <see cref="TensorFlowModel"/> also holds references to unmanaged resources that need to be freed either with an explicit
 /// call to Dispose() or implicitly by declaring the variable with the "using" syntax/>
 ///
 /// <format type="text/markdown">
 /// <![CDATA[
 /// [!include[io](~/../docs/samples/docs/api-reference/tensorflow-usage.md)]
 /// ]]>
 /// </format>
 /// </summary>
 /// <param name="catalog">The transform's catalog.</param>
 /// <param name="modelLocation">Location of the TensorFlow model.</param>
 /// <param name="treatOutputAsBatched">If the first dimension of the output is unknown, should it be treated as batched or not.</param>
 /// <example>
 /// <format type="text/markdown">
 /// <![CDATA[
 /// [!code-csharp[LoadTensorFlowModel](~/../docs/samples/docs/samples/Microsoft.ML.Samples/Dynamic/TensorFlow/TextClassification.cs)]
 /// ]]>
 /// </format>
 /// </example>
 public static TensorFlowModel LoadTensorFlowModel(this ModelOperationsCatalog catalog, string modelLocation, bool treatOutputAsBatched)
 => TensorFlowUtils.LoadTensorFlowModel(CatalogUtils.GetEnvironment(catalog), modelLocation, treatOutputAsBatched);