dotnet / machinelearning

ML.NET is an open source and cross-platform machine learning framework for .NET.
https://dot.net/ml
MIT License
9.05k stars 1.89k forks source link

Method not found: 'Tensorflow.Tensor Tensorflow.tensorflow.truncated_normal(Int32[], Single, Single, Tensorflow.TF_DataType, System.Nullable`1<Int32>, System.String)'. #5367

Closed shivanand870 closed 4 years ago

shivanand870 commented 4 years ago

System information

Issue

i am not able to Train a Model using C# with MulticlassClassification.Trainers.ImageClassification iam getting below error message

Source code / logs

var options = new ImageClassificationTrainer.Options() { FeatureColumnName = "Image", LabelColumnName = "Label",

            Arch = ImageClassificationTrainer.Architecture.ResnetV2101,
            Epoch = 50,
            BatchSize = 10,
            LearningRate = 0.01f,
            MetricsCallback = (metrics) => Console.WriteLine(metrics),
            ValidationSet = testDataset,
             EarlyStoppingCriteria = new ImageClassificationTrainer.EarlyStopping(minDelta: 0.001f, patience: 20, metric: ImageClassificationTrainer.EarlyStoppingMetric.Loss)
        };

        var pipeline = mlContext.MulticlassClassification.Trainers.ImageClassification(options)
            .Append(mlContext.Transforms.Conversion.MapKeyToValue(outputColumnName: "PredictedLabel", inputColumnName: "PredictedLabel"));

        var trainedModel = pipeline.Fit(trainDataset);

Please paste or attach the code or logs or traces that would be helpful to diagnose the issue you are reporting.

*****Error StackTrace**** System.MissingMethodException HResult=0x80131513 Message=Method not found: 'Tensorflow.Tensor Tensorflow.tensorflow.truncated_normal(Int32[], Single, Single, Tensorflow.TF_DataType, System.Nullable1<Int32>, System.String)'. Source=Microsoft.ML.Vision StackTrace: at Microsoft.ML.Vision.ImageClassificationTrainer.<>c__DisplayClass64_1.<AddFinalRetrainOps>b__4(NameScope <p0>) at Tensorflow.Binding.tf_with[T](T py, Action1 action) at Microsoft.ML.Vision.ImageClassificationTrainer.<>c__DisplayClass64_0.b__1(NameScope scope) at Tensorflow.Binding.tf_with[T](T py, Action1 action) at Microsoft.ML.Vision.ImageClassificationTrainer.AddFinalRetrainOps(Int32 classCount, String labelColumn, String scoreColumnName, Tensor bottleneckTensor, Boolean isTraining, Boolean useLearningRateScheduler, Single learningRate) at Microsoft.ML.Vision.ImageClassificationTrainer.AddTransferLearningLayer(String labelColumn, String scoreColumnName, Single learningRate, Boolean useLearningRateScheduling, Int32 classCount) at Microsoft.ML.Vision.ImageClassificationTrainer.InitializeTrainingGraph(IDataView input) at Microsoft.ML.Vision.ImageClassificationTrainer.TrainModelCore(TrainContext trainContext) at Microsoft.ML.Trainers.TrainerEstimatorBase2.TrainTransformer(IDataView trainSet, IDataView validationSet, IPredictor initPredictor) at Microsoft.ML.Data.EstimatorChain`1.Fit(IDataView input) at VWS.ML.API.VWSTrainObjectDetectionAdvancedOptions.CreateImagepipeline() in G:\Automation\3.Projects\VS 2017\Utility\VWSMLStudio\VWS.ML.API\VWSImageClassification.cs:line 377 at VWS.ML.API.VWSImageClassification.TrainData(IDataView loadImageData1, String GetInputPath, String GetModelPath, String FeatureColumnName, String ScoreColumnName, String PredictedLabelColumnName, String LabelColumnName) in G:\Automation\3.Projects\VS 2017\Utility\VWSMLStudio\VWS.ML.API\VWSImageClassification.cs:line 702 at ConsoleML.Program.Main(String[] args) in G:\Automation\3.Projects\VS 2017\Utility\VWSMLStudio\ConsoleML\Program.cs:line 20

michaelgsharp commented 4 years ago

Can you upload some of your sample data so we can take a look at it?

shivanand870 commented 4 years ago

Thanks for your reply

here is the images which i am using for my testing also attached data file

NOTE : If i am using the .Net Framework 4.8 with 64 bit its not working, if i use .Net Core it works fine

Sample Data Images.zip TestDataFile.zip

**Code *** public void GetData(string Inputfoldername) {

        string assetsPath = Inputfoldername;       
        string imagesDownloadFolderPath = Path.Combine(assetsPath, "Images");        
        string finalImagesFolderName = imagesDownloadFolderPath;
        string fullImagesetFolderPath = Path.Combine(imagesDownloadFolderPath, finalImagesFolderName);
        MLContext mlContext = new MLContext(seed: 1);
        //Load all the original images info.
        IEnumerable<ImageData> images = LoadImagesFromDirectory(folder: fullImagesetFolderPath, useFolderNameAsLabel: true);
        IDataView shuffledFullImagesDataset;
        // Shuffle images.
        shuffledFullImagesDataset = mlContext.Data.ShuffleRows(mlContext.Data.LoadFromEnumerable(images));

        shuffledFullImagesDataset = mlContext.Transforms.Conversion.MapValueToKey("Label", keyOrdinality: Microsoft.ML.Transforms.ValueToKeyMappingEstimator.KeyOrdinality.ByValue)
         .Append(mlContext.Transforms.LoadRawImageBytes("Image", fullImagesetFolderPath, "ImagePath")).Fit(shuffledFullImagesDataset).Transform(shuffledFullImagesDataset);

        mlContext.Data.SaveAsText(shuffledFullImagesDataset, File.Create(Path.Combine(assetsPath, "TestDataFile.csv")), ',', true, true);    

        TrainTestData trainTestData = mlContext.Data.TrainTestSplit(shuffledFullImagesDataset, testFraction: 0.1, seed: 1);
        IDataView trainDataset = trainTestData.TrainSet;
        IDataView testDataset = trainTestData.TestSet;
        var options = new ImageClassificationTrainer.Options()
        {
            FeatureColumnName = "Image",
            LabelColumnName = "Label",
            Arch = ImageClassificationTrainer.Architecture.ResnetV2101,
            Epoch = 50,
            BatchSize = 10,
            LearningRate = 0.01f,
            MetricsCallback = (metrics) => Console.WriteLine(metrics),
            ValidationSet = testDataset,
            EarlyStoppingCriteria = null
        };

        var pipeline = mlContext.MulticlassClassification.Trainers.ImageClassification(options)
            .Append(mlContext.Transforms.Conversion.MapKeyToValue(outputColumnName: "PredictedLabel", inputColumnName: "PredictedLabel"));

        var trainedModel = pipeline.Fit(trainDataset);
        mlContext.Model.Save(trainedModel, shuffledFullImagesDataset.Schema, Path.Combine(assetsPath, "model.zip"));

        ITransformer loadedModel;
        DataViewSchema schema;
        using (var file = File.OpenRead(Path.Combine(assetsPath, "model.zip")))
            loadedModel = mlContext.Model.Load(file, out schema);

        EvaluateModel(mlContext, testDataset, loadedModel);

        //TrySinglePrediction(fullImagesetFolderPath, mlContext, loadedModel);

    }
shivanand870 commented 4 years ago

Can you upload some of your sample data so we can take a look at it?

I have uploaded data and data file with code pls help out fix this issue

michaelgsharp commented 4 years ago

Yes, I am looking into it. I'll get back to you about it as soon as I can.

michaelgsharp commented 4 years ago

So I have made a new console app with .net framework version 4.8, and the sample code you have sent me runs just fine in it. I don't have the EvaluateModel method, but other than that the training ran to completion. I am using: Microsoft.ML.* version 1.5.1 SciSharp.TensorFlow.Redist 1.14.0 are you using those same versions? Currently the only version of TensorFlow that is supported is 1.14.0.

Make sure you are using those same version and get back to me about it.

shivanand870 commented 4 years ago

Thanks !

Sure i will make changes as per your suggestion and let you know after that

shivanand870 commented 4 years ago

I have tried with above version as per your suggestion when run project it stops there only i am attaching project file , Images which one i am using for testing and screenshot of execution

TestMLNETImages.zip ImageIssue

i am not able attach packages folder packages

Please let me if i am missing anything or what is wrong with project file

Thanks your support advance

michaelgsharp commented 4 years ago

Currently it shows you have both version of SciSharp.TensorFlow in there. Can you try removing the 2.3.0 version?

Is the error happening on the call to fit?

shivanand870 commented 4 years ago

Yes your right its happening on Fit call

I have removed the 2.3.0 v of SciSharp.TensorFlow and tested, but still same issue packages

Thanks your support advance

shivanand870 commented 4 years ago

Michael,

Your help is appreciated for fixing the above issue pls can help on this fixing

Thanks your support advance

Lynx1820 commented 4 years ago

Hi @shivanand870 ,

This issue seems related: 4678, could you try to use TensorFlow.NET version 0.11.8.1?

frank-dong-ms-zz commented 4 years ago

Close this issue since we didn't hear back for a long time, @shivanand870 feel free to reopen if this suggestion not work for your or you have any further questions.