private PredictionFunction <ImageNetData, ImageNetPrediction> LoadModel(string dataLocation, string imagesFolder, string modelLocation) { ConsoleWriteHeader("Read model"); Console.WriteLine($"Model location: {modelLocation}"); Console.WriteLine($"Images folder: {imagesFolder}"); Console.WriteLine($"Training file: {dataLocation}"); Console.WriteLine($"Default parameters: image size=({ImageNetSettings.imageWidth},{ImageNetSettings.imageHeight}), image mean: {ImageNetSettings.mean}"); var loader = new TextLoader(mlContext, new TextLoader.Arguments { Column = new[] { new TextLoader.Column("ImagePath", DataKind.Text, 0), } }); var data = loader.Read(new MultiFileSource(dataLocation)); var pipeline = ImageEstimatorsCatalog.LoadImages(catalog: mlContext.Transforms, imageFolder: imagesFolder, columns: ("ImagePath", "ImageReal")) .Append(ImageEstimatorsCatalog.Resize(mlContext.Transforms, "ImageReal", "ImageReal", ImageNetSettings.imageHeight, ImageNetSettings.imageWidth)) .Append(ImageEstimatorsCatalog.ExtractPixels(mlContext.Transforms, new[] { new ImagePixelExtractorTransform.ColumnInfo("ImageReal", "input", interleave: ImageNetSettings.channelsLast, offset: ImageNetSettings.mean) })) .Append(new TensorFlowEstimator(mlContext, modelLocation, new[] { "input" }, new[] { "softmax2" })); var modeld = pipeline.Fit(data); var predictionFunction = modeld.MakePredictionFunction <ImageNetData, ImageNetPrediction>(mlContext); return(predictionFunction); }
private PredictionFunction <ImageInputData, ImageNetPrediction> CreatePredictionFunction() { try { var pipeline = ImageEstimatorsCatalog.LoadImages(catalog: _mlContext.Transforms, imageFolder: _imagesFolder, columns: ("ImagePath", "ImageReal")) .Append(ImageEstimatorsCatalog.Resize(_mlContext.Transforms, "ImageReal", "ImageReal", ImageNetSettings.imageHeight, ImageNetSettings.imageWidth)) .Append(ImageEstimatorsCatalog.ExtractPixels(_mlContext.Transforms, new[] { new ImagePixelExtractorTransform.ColumnInfo("ImageReal", InceptionSettings.InputTensorName, interleave: ImageNetSettings.channelsLast, offset: ImageNetSettings.mean) })) .Append(new TensorFlowEstimator(_mlContext, _modelLocation, new[] { InceptionSettings.InputTensorName }, new[] { InceptionSettings.OutputTensorName })); var model = pipeline.Fit(CreateDataView()); var predictionFunction = model.MakePredictionFunction <ImageInputData, ImageNetPrediction>(_mlContext); return(predictionFunction); } catch (Exception e) { throw e; } }
private PredictionFunction <ImageInputData, ImageNetPrediction> CreatePredictionFunction(string dataLocation, string imagesFolder, string modelLocation) { ConsoleWriteHeader("Read model"); Console.WriteLine($"Model location: {modelLocation}"); Console.WriteLine($"Images folder: {imagesFolder}"); Console.WriteLine($"Default parameters: image size=({ImageNetSettings.imageWidth},{ImageNetSettings.imageHeight}), image mean: {ImageNetSettings.mean}"); //Define pieplie for image tansformations var pipeline = ImageEstimatorsCatalog.LoadImages(catalog: mlContext.Transforms, imageFolder: imagesFolder, columns: ("ImagePath", "ImageReal")) .Append(ImageEstimatorsCatalog.Resize(mlContext.Transforms, "ImageReal", "ImageReal", ImageNetSettings.imageHeight, ImageNetSettings.imageWidth)) .Append(ImageEstimatorsCatalog.ExtractPixels(mlContext.Transforms, new[] { new ImagePixelExtractorTransform.ColumnInfo("ImageReal", "Placeholder", interleave: ImageNetSettings.channelsLast, offset: ImageNetSettings.mean) })) .Append(new TensorFlowEstimator(mlContext, modelLocation, new[] { "Placeholder" }, new[] { "loss" })); var model = pipeline.Fit(CreateDataView()); var predictionFunction = model.MakePredictionFunction <ImageInputData, ImageNetPrediction>(mlContext); return(predictionFunction); }