Esempio n. 1
0
        private void ImageBinaryClassificationAndDuplicateWorker(object sender, DoWorkEventArgs e)
        {
            //should probably check for inputdirecttext is validated
            _filesToProcess = Directory.GetFiles(InputDirText);
            //I need property and feild for data binding of the text box for model location
            //modelFilePath =
            _predictedResults          = new ObservableCollection <CustomTwoClassificationImagePredictionResults>();
            _targetOutputDirectoryPath = OutputDirText;


            //create output directories
            Console.WriteLine("-----------------------------------");
            Console.WriteLine(_targetOutputDirectoryPath);
            var inventoryDir = System.IO.Path.Combine(_targetOutputDirectoryPath, "inventory");

            Console.WriteLine(inventoryDir);
            Console.WriteLine(inventoryDir);
            var infrastructureDir = System.IO.Path.Combine(_targetOutputDirectoryPath, "infrastructure");

            Console.WriteLine(infrastructureDir);
            if (!System.IO.Directory.Exists(inventoryDir))
            {
                System.IO.Directory.CreateDirectory(inventoryDir);
            }
            if (!System.IO.Directory.Exists(infrastructureDir))
            {
                System.IO.Directory.CreateDirectory(infrastructureDir);
            }



            MLContext mlContext = new MLContext();
            // ModelOutput mop = ConsumeModel.Predict(mip, ConsumeModel.ClassicationModelEnum.classtwo);

            string modelPath = _modelInputFileText;

            //// Load model & create prediction engine
            ITransformer mlModel = mlContext.Model.Load(modelPath, out var modelInputSchema);

            foreach (var item in _filesToProcess)
            {
                Console.WriteLine(_imageClassificationCount.ToString());
                _imageClassificationCount++;
                Console.WriteLine(_imageClassificationCount); //parse to model input
                ModelInput mip = new ModelInput();
                mip.Label       = "none";                     //useful for evaluation section
                mip.ImageSource = item;



                //##########################################################

                var predEngine = mlContext.Model.CreatePredictionEngine <ModelInput, ModelOutput>(mlModel);

                Console.WriteLine($"number of columns is ======= {modelInputSchema.Count.ToString()}");
                foreach (var inputItem in modelInputSchema)
                {
                    Console.WriteLine($"name of column is ========={inputItem.Name}");
                }

                // Console.WriteLine($"count is ======= {modelInputSchema.}");

                // Use model to make prediction on input data
                ModelOutput result = predEngine.Predict(mip);



                string toprintDebugConsole = $"prediction class: {result.Prediction}|| score: {result.Score.FirstOrDefault()}";

                //##########################################################
                Console.WriteLine(toprintDebugConsole);
                Console.WriteLine(result.Prediction.ToString());
                Console.WriteLine(result.Score.FirstOrDefault());
                Console.WriteLine("##########################");
                Console.WriteLine(item);
                var filename = System.IO.Path.GetFileName(item);
                Console.WriteLine(filename);
                string disclass = System.IO.Path.Combine(_targetOutputDirectoryPath, result.Prediction);
                Console.WriteLine(disclass);
                var destfile = System.IO.Path.Combine(disclass, filename);
                Console.WriteLine(destfile);
                System.IO.File.Copy(item, destfile, true);



                //update ui with
                var newPredictionToUpdateOutputStatus = new CustomTwoClassificationImagePredictionResults();
                newPredictionToUpdateOutputStatus.PredictionId      = _imageClassificationCount.ToString();
                newPredictionToUpdateOutputStatus.ImageOriginalPath = item.ToString();
                Console.WriteLine(item.ToString());
                newPredictionToUpdateOutputStatus.ModelOutputscore      = result.Score.FirstOrDefault().ToString();
                newPredictionToUpdateOutputStatus.ModelOutputPrediction = result.Prediction;


                Console.WriteLine("number of items in observable collection");


                //from the backgroud thread in backgroudworker, I need to call the ui thread to update the listview
                uiContext.Send(x => PredictionResults.Add(newPredictionToUpdateOutputStatus), null);
                //PredictionResults.Add(newPredictionToUpdateOutputStatus);


                Console.WriteLine(PredictionResults.Count.ToString());
            }
        }