/// <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)); }
public TransformerChain <KeyToValueMappingTransformer> TrainResnetV250() { var options = new ImageClassificationEstimator.Options() { FeaturesColumnName = "Image", LabelColumnName = "Label", Arch = ImageClassificationEstimator.Architecture.ResnetV250, Epoch = 50, BatchSize = 10, LearningRate = 0.01f, EarlyStoppingCriteria = new ImageClassificationEstimator.EarlyStopping(minDelta: 0.001f, patience: 20, metric: ImageClassificationEstimator.EarlyStoppingMetric.Loss), ValidationSet = testDataset, ModelSavePath = assetsPath, DisableEarlyStopping = true }; var pipeline = mlContext.Model.ImageClassification(options) .Append(mlContext.Transforms.Conversion.MapKeyToValue( outputColumnName: "PredictedLabel", inputColumnName: "PredictedLabel")); return(pipeline.Fit(trainDataset)); }
public static void Example() { string assetsRelativePath = @"../../../assets"; string assetsPath = GetAbsolutePath(assetsRelativePath); var outputMlNetModelFilePath = Path.Combine(assetsPath, "outputs", "imageClassifier.zip"); string imagesDownloadFolderPath = Path.Combine(assetsPath, "inputs", "images"); //Download the image set and unzip string finalImagesFolderName = DownloadImageSet( imagesDownloadFolderPath); string fullImagesetFolderPath = Path.Combine( imagesDownloadFolderPath, finalImagesFolderName); try { MLContext mlContext = new MLContext(seed: 1); //Load all the original images info IEnumerable <ImageData> images = LoadImagesFromDirectory( folder: fullImagesetFolderPath, useFolderNameAsLabel: true); IDataView shuffledFullImagesDataset = mlContext.Data.ShuffleRows( mlContext.Data.LoadFromEnumerable(images)); shuffledFullImagesDataset = mlContext.Transforms.Conversion .MapValueToKey("Label") .Append(mlContext.Transforms.LoadImages("Image", fullImagesetFolderPath, false, "ImagePath")) .Fit(shuffledFullImagesDataset) .Transform(shuffledFullImagesDataset); // Split the data 90:10 into train and test sets, train and // evaluate. TrainTestData trainTestData = mlContext.Data.TrainTestSplit( shuffledFullImagesDataset, testFraction: 0.1, seed: 1); IDataView trainDataset = trainTestData.TrainSet; IDataView testDataset = trainTestData.TestSet; var options = new ImageClassificationEstimator.Options() { FeaturesColumnName = "Image", LabelColumnName = "Label", // Just by changing/selecting InceptionV3/MobilenetV2/ResnetV250 here instead of // ResnetV2101 you can try a different architecture/ // pre-trained model. Arch = ImageClassificationEstimator.Architecture.ResnetV2101, Epoch = 50, BatchSize = 10, LearningRate = 0.01f, MetricsCallback = (metrics) => Console.WriteLine(metrics), ValidationSet = testDataset, DisableEarlyStopping = true }; var pipeline = mlContext.Model.ImageClassification(options) .Append(mlContext.Transforms.Conversion.MapKeyToValue( outputColumnName: "PredictedLabel", inputColumnName: "PredictedLabel")); Console.WriteLine("*** Training the image classification model " + "with DNN Transfer Learning on top of the selected " + "pre-trained model/architecture ***"); // Measuring training time var watch = System.Diagnostics.Stopwatch.StartNew(); var trainedModel = pipeline.Fit(trainDataset); watch.Stop(); long elapsedMs = watch.ElapsedMilliseconds; Console.WriteLine("Training with transfer learning took: " + (elapsedMs / 1000).ToString() + " seconds"); mlContext.Model.Save(trainedModel, shuffledFullImagesDataset.Schema, "model.zip"); ITransformer loadedModel; DataViewSchema schema; using (var file = File.OpenRead("model.zip")) loadedModel = mlContext.Model.Load(file, out schema); EvaluateModel(mlContext, testDataset, loadedModel); watch = System.Diagnostics.Stopwatch.StartNew(); // Predict image class using an in-memory image. TrySinglePrediction(fullImagesetFolderPath, mlContext, loadedModel); watch.Stop(); elapsedMs = watch.ElapsedMilliseconds; Console.WriteLine("Prediction engine took: " + (elapsedMs / 1000).ToString() + " seconds"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.WriteLine("Press any key to finish"); Console.ReadKey(); }
public static void Example() { string assetsRelativePath = @"../../../assets"; string assetsPath = GetAbsolutePath(assetsRelativePath); var outputMlNetModelFilePath = Path.Combine(assetsPath, "outputs", "imageClassifier.zip"); string imagesDownloadFolderPath = Path.Combine(assetsPath, "inputs", "images"); // Download Cifar Dataset. string finalImagesFolderName = DownloadImageSet( imagesDownloadFolderPath); string finalImagesFolderNameTrain = "cifar\\train"; string fullImagesetFolderPathTrain = Path.Combine( imagesDownloadFolderPath, finalImagesFolderNameTrain); string finalImagesFolderNameTest = "cifar\\test"; string fullImagesetFolderPathTest = Path.Combine( imagesDownloadFolderPath, finalImagesFolderNameTest); try { MLContext mlContext = new MLContext(seed: 1); //Load all the original train images info IEnumerable <ImageData> train_images = LoadImagesFromDirectory( folder: fullImagesetFolderPathTrain, useFolderNameAsLabel: true); IDataView trainDataset = mlContext.Data.LoadFromEnumerable(train_images); trainDataset = mlContext.Transforms.Conversion .MapValueToKey("Label") .Append(mlContext.Transforms.LoadImages("Image", fullImagesetFolderPathTrain, false, "ImagePath")) .Fit(trainDataset) .Transform(trainDataset); //Load all the original test images info IEnumerable <ImageData> test_images = LoadImagesFromDirectory( folder: fullImagesetFolderPathTest, useFolderNameAsLabel: true); IDataView testDataset = mlContext.Data.LoadFromEnumerable(test_images); testDataset = mlContext.Transforms.Conversion .MapValueToKey("Label") .Append(mlContext.Transforms.LoadImages("Image", fullImagesetFolderPathTest, false, "ImagePath")) .Fit(testDataset) .Transform(testDataset); var options = new ImageClassificationEstimator.Options() { FeaturesColumnName = "Image", LabelColumnName = "Label", // Just by changing/selecting InceptionV3/MobilenetV2 here instead of // ResnetV2101 you can try a different architecture/ // pre-trained model. Arch = ImageClassificationEstimator.Architecture.ResnetV2101, Epoch = 182, BatchSize = 128, LearningRate = 0.01f, MetricsCallback = (metrics) => Console.WriteLine(metrics), ValidationSet = testDataset, DisableEarlyStopping = true, ReuseValidationSetBottleneckCachedValues = false, ReuseTrainSetBottleneckCachedValues = false, // Use linear scaling rule and Learning rate decay as an option // This is known to do well for Cifar dataset and Resnet models // You can also try other types of Learning rate scheduling methods // available in LearningRateScheduler.cs LearningRateScheduler = new LsrDecay() }; var pipeline = mlContext.Model.ImageClassification(options) .Append(mlContext.Transforms.Conversion.MapKeyToValue( outputColumnName: "PredictedLabel", inputColumnName: "PredictedLabel")); Console.WriteLine("*** Training the image classification model " + "with DNN Transfer Learning on top of the selected " + "pre-trained model/architecture ***"); // Measuring training time var watch = System.Diagnostics.Stopwatch.StartNew(); var trainedModel = pipeline.Fit(trainDataset); watch.Stop(); long elapsedMs = watch.ElapsedMilliseconds; Console.WriteLine("Training with transfer learning took: " + (elapsedMs / 1000).ToString() + " seconds"); mlContext.Model.Save(trainedModel, testDataset.Schema, "model.zip"); ITransformer loadedModel; DataViewSchema schema; using (var file = File.OpenRead("model.zip")) loadedModel = mlContext.Model.Load(file, out schema); EvaluateModel(mlContext, testDataset, loadedModel); watch = System.Diagnostics.Stopwatch.StartNew(); // Predict image class using an in-memory image. TrySinglePrediction(fullImagesetFolderPathTest, mlContext, loadedModel); watch.Stop(); elapsedMs = watch.ElapsedMilliseconds; Console.WriteLine("Prediction engine took: " + (elapsedMs / 1000).ToString() + " seconds"); } catch (Exception ex) { Console.WriteLine(ex.ToString()); } Console.WriteLine("Press any key to finish"); Console.ReadKey(); }