Beispiel #1
0
        private void SetParameters(string inputPath, ScanningMethod scanningMethod, DescriptorMethod descriptorMethod, MachineLearningMethod machineLearningMethod)
        {
            _symbolParameters       = new Utility.SymbolParameters();
            _hessianThreshould      = _symbolParameters.HessianThresh;
            _uniquenessThresh       = _symbolParameters.UniquenessThress;
            _histogramMatchingScore = _symbolParameters.HistogramMatchingScore;
            _tm = _symbolParameters.TM;

            _log = File.AppendText(inputPath + "/log.txt");


            _machineLearningMethod = machineLearningMethod;
            _scanningMethod        = scanningMethod;
            _descriptorType        = descriptorMethod;
            //check the FilePath
            _inputPath        = inputPath;
            _inPath           = inputPath + "/in";
            _outPath          = inputPath + "/out";
            _intermediatePath = inputPath + "/Intermediate";

            ValidateFilePath(_inPath, "in");
            ValidateFilePath(_outPath, "out");
            ValidateFilePath(_intermediatePath, "Intermediate");
            ValidateFilePath(_inputPath, "");

            _descriptor      = new Descriptor();
            _machineLearning = new MachineLearning();
            _scanImage       = new ScanImage();
        }
 // Start is called before the first frame update
 void Start()
 {
     rb              = GetComponent <Rigidbody2D>();
     anim            = GetComponent <Animator>();
     stateController = stateControllerObject.GetComponent <StateController>();
     player          = GameObject.Find("RL AI").GetComponent <MachineLearning>();
 }
Beispiel #3
0
        /// <summary>
        /// Callback for when the Train Model button is clicked. Disables all button and launches a new thread which asks extracts features (if not present) and then trains the model using
        /// <see cref="MachineLearning.TrainModel{TFaceData}"/> (if not present), prompting the user if an existing version is found. The face data type passed in is dependent on <see cref="selectedFaceDataType"/>.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void TrainModelButton_Click(object sender, RoutedEventArgs e)
        {
            TrainModelButton.IsEnabled      = false;
            PredictEmotionButton.IsEnabled  = false;
            LoadImageToPredict.IsEnabled    = false;
            EvaluateModelButton.IsEnabled   = false;
            RunBenchmarksMenuItem.IsEnabled = false;
            var trainButtonOriginalLabel = (string)TrainModelButton.Content;

            TrainModelButton.Content = $"{trainButtonOriginalLabel} (Training...)";

            await Task.Run(() =>
            {
                var existingFile = MachineLearning.GetFile(MachineLearning.GetFacialFeaturesFileName(selectedFaceDataType), false);

                bool recalculatedFacialFeatures = false;

                if (existingFile != null && IsExistingFileValid(existingFile))
                {
                    if (MessageBox.Show("Existing facial landmarks file exists, extract again (will take extra time)?",
                                        "Extracted Facial Feature File Found", MessageBoxButton.YesNo, MessageBoxImage.Question) ==
                        MessageBoxResult.Yes)
                    {
                        MachineLearning.ExtractFeatures(selectedFaceDataType);
                        recalculatedFacialFeatures = true;
                    }
                }
                else
                {
                    MachineLearning.ExtractFeatures(selectedFaceDataType);
                    recalculatedFacialFeatures = true;
                }

                if (recalculatedFacialFeatures)
                {
                    TrainModelAccordingToCurrentFaceMode();
                }
                else if (MachineLearning.GetFile(MachineLearning.GetModelZipFileName(selectedFaceDataType), false) !=
                         null)
                {
                    if (MessageBox.Show("Trained model found, use the one stored on disk?", "Trained Model Found",
                                        MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.No)
                    {
                        TrainModelAccordingToCurrentFaceMode();
                    }
                }
                else
                {
                    TrainModelAccordingToCurrentFaceMode();
                }
            });

            TrainModelButton.Content        = $"{trainButtonOriginalLabel} (Trained)";
            TrainModelButton.IsEnabled      = true;
            PredictEmotionButton.IsEnabled  = true;
            LoadImageToPredict.IsEnabled    = true;
            EvaluateModelButton.IsEnabled   = true;
            RunBenchmarksMenuItem.IsEnabled = true;
        }
        public override async Task HandleAsync(IUpdateContext context, UpdateDelegate next, string[] args,
                                               CancellationToken cancellationToken)
        {
            _telegramService = new TelegramService(context);
            var message = _telegramService.Message;
            var msgText = message.Text.Split(' ');
            var param1  = "\t";

            // var param1 = msgText.ValueOfIndex(1).Trim() ?? ",";

            if (!_telegramService.IsSudoer())
            {
                Log.Information("This user is not sudoer");
                return;
            }

            if (message.ReplyToMessage != null)
            {
                await _telegramService.SendTextAsync("Sedang mengimport dataset")
                .ConfigureAwait(false);

                var repMessage = message.ReplyToMessage;
                if (repMessage.Document != null)
                {
                    var document  = repMessage.Document;
                    var chatId    = message.Chat.Id.ToString().TrimStart('-');
                    var msgId     = repMessage.MessageId;
                    var fileName  = document.FileName;
                    var filePath  = $"learn-dataset-{chatId}-{msgId}-{fileName}";
                    var savedFile = await _telegramService.DownloadFileAsync(filePath)
                                    .ConfigureAwait(false);

                    await _telegramService.ImportCsv(savedFile, param1).ConfigureAwait(false);

                    await _telegramService.EditAsync("Sedang mempelajari dataset")
                    .ConfigureAwait(false);

                    await MachineLearning.SetupEngineAsync()
                    .ConfigureAwait(false);

                    await _telegramService.EditAsync("Import selesai")
                    .ConfigureAwait(false);
                }
                else
                {
                    var typeHint = "File yang mau di import harus berupa dokumen bertipe csv, tsv atau sejenis";
                    await _telegramService.SendTextAsync(typeHint)
                    .ConfigureAwait(false);
                }
            }
            else
            {
                await _telegramService.SendTextAsync("Balas file yang mau di import")
                .ConfigureAwait(false);
            }
        }
Beispiel #5
0
        private static void runExample(string title, string mlConfigPath)
        {
            var mlConfigFile2 = mlConfigPath;

            Console.WriteLine(Environment.NewLine);
            Console.WriteLine($"****{title}****");
            Console.WriteLine(Environment.NewLine);
            var token2 = new CancellationToken();

            MachineLearning.Run(mlConfigFile2, DeviceDescriptor.UseDefaultDevice(), token2, trainingProgress, null);
        }
Beispiel #6
0
        public EvaluationMetrics(IDataView testSetDataView, Type TFaceData)
        {
            InitializeComponent();

            // Test model and display results.
            MachineLearning.TestModel(testSetDataView, TFaceData, out var metrics);
            ConfusionMatrixTextBlock.Text  = "";
            ConfusionMatrixTextBlock.Text  = metrics.ConfusionMatrix.GetFormattedConfusionTable();
            MicroAccuracyTextBlock.Text    = metrics.MicroAccuracy.ToString("#.###");
            MacroAccuracyTextBlock.Text    = metrics.MacroAccuracy.ToString("#.###");
            LogLossTextBlock.Text          = metrics.LogLoss.ToString("#.###");
            LogLossReductionTextBlock.Text = metrics.LogLossReduction.ToString("#.###");
        }
        /// <summary>
        /// Extract face features by calling <see cref="MachineLearning.ExtractFeatures"/> for the <paramref name="faceDataType"/>, if the <paramref name="checkIfPresent"/> is true.
        /// </summary>
        /// <param name="faceDataType"></param>
        /// <param name="checkIfPresent"></param>
        /// <seealso cref="MachineLearning.ExtractFeatures"/>
        public static void ExtractFacialFeatures(Type faceDataType, bool checkIfPresent = true)
        {
            if (checkIfPresent)
            {
                var foundFile = MachineLearning.GetFile(MachineLearning.GetFacialFeaturesFileName(faceDataType), false);

                if (foundFile != null)
                {
                    return;
                }
            }
            MachineLearning.ExtractFeatures(faceDataType);
        }
Beispiel #8
0
        /// <summary>
        /// Calls <see cref="MachineLearning.TrainModel{TFaceData}"/> with the TFaceData set according to <see cref="selectedFaceDataType"/> as a workaround
        /// as type variables cannot be cast to generic types. Shows a message box informing the user once its done training.
        /// </summary>
        private void TrainModelAccordingToCurrentFaceMode()
        {
            // Because you can't pass FaceDataType into a generic without serious workarounds due to the use of an out paramater.
            if (selectedFaceDataType == typeof(FaceData1))
            {
                MachineLearning.TrainModel <FaceData1>(out testSetDataView, 100000);
            }
            else if (selectedFaceDataType == typeof(FaceData2))
            {
                MachineLearning.TrainModel <FaceData2>(out testSetDataView, 1000000);
            }
            else if (selectedFaceDataType == typeof(FaceData3))
            {
                MachineLearning.TrainModel <FaceData3>(out testSetDataView, 100000);
            }

            MessageBox.Show("Training Complete", "Training is complete.", MessageBoxButton.OK,
                            MessageBoxImage.Information);
        }
Beispiel #9
0
        public async Task <IActionResult> Index(IFormFile file)
        {
            if (file != null)
            {
                string path = Path.Combine(Path.GetTempPath(), "test-files");
                long   size = file.Length;
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                var filePath = Path.Combine(path, file.FileName);
                if (file.Length > 0)
                {
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        await file.CopyToAsync(stream);
                    }
                }

                //string sha256 = new StaticAnalysisProject.Helpers.Hash.SHA256().GetHash(filePath);
                IFileReport report = new FileReport(filePath);
                var         ml     = new MachineLearning().Predict(report);

                object[] json = new object[] {
                    report.ToJson(),
                         ml.ToJson()
                };

                if (System.IO.File.Exists(filePath))
                {
                    System.IO.File.Delete(filePath);
                }

                return(Content(JsonConvert.SerializeObject(json)));
            }

            Response.StatusCode = (int)HttpStatusCode.BadRequest;
            return(Ok("{\"error\"}"));
        }
        public override async Task HandleAsync(IUpdateContext context, UpdateDelegate next, string[] args,
                                               CancellationToken cancellationToken)
        {
            _telegramService = new TelegramService(context);
            var message = _telegramService.Message;

            if (message.ReplyToMessage != null)
            {
                var repMsg     = message.ReplyToMessage;
                var repMsgText = repMsg.Text;

                await _telegramService.SendTextAsync("Sedang memprediksi pesan")
                .ConfigureAwait(false);

                var isSpam = MachineLearning.PredictMessage(repMsgText);
                await _telegramService.EditAsync($"IsSpam: {isSpam}").ConfigureAwait(false);

                return;
            }
            else
            {
                await _telegramService.SendTextAsync("Predicting message").ConfigureAwait(false);
            }
        }
Beispiel #11
0
    public override async Task HandleAsync(IUpdateContext context, UpdateDelegate next, string[] args)
    {
        await _telegramService.AddUpdateContext(context);

        var message = _telegramService.Message;

        if (message.ReplyToMessage != null)
        {
            var repMsg     = message.ReplyToMessage;
            var repMsgText = repMsg.Text;

            await _telegramService.SendTextMessageAsync("Sedang memprediksi pesan")
            ;

            var isSpam = MachineLearning.PredictMessage(repMsgText);
            await _telegramService.EditMessageTextAsync($"IsSpam: {isSpam}");

            return;
        }
        else
        {
            await _telegramService.SendTextMessageAsync("Predicting message");
        }
    }
Beispiel #12
0
    void Start()
    {
        mL             = GetComponent <MachineLearning>();
        weights        = GetComponent <Weights>();
        fitness        = GetComponent <FitnessFunction>();
        onLevelCreated = gameObject.GetComponent <OnLevelCreated>();
        noiseMap       = GetComponent <NoiseMap>();
        chiseling      = GetComponent <Chiseling>();
        bnm            = GetComponent <BuildNavMesh>();
        gg             = GetComponent <GGManager>();



        modules.AddRange(moduleSO.list);
        weights.AssignWeights(modules);
        //startBuilding();

        for (int i = 0; i < loadScreen.list[0].transform.childCount; i++)
        {
            loadScreen.list[0].transform.GetChild(i).gameObject.SetActive(true);
        }

        //InitializeSlots();
    }
Beispiel #13
0
        /// <summary>
        /// Callback for when the <see cref="PredictEmotionButton"/> is clicked. This launches a new task which calls <see cref="MachineLearning.PredictEmotion"/> with the
        /// <see cref="currentImageFileInfo"/> and <see cref="selectedFaceDataType"/>, which results are then used to update <see cref="PredictedEmotionMainLabel"/> and
        /// <see cref="PredictedEmotionsListBox"/>.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private async void PredictEmotionButton_Click(object sender, RoutedEventArgs e)
        {
            var originalLabel = PredictEmotionButton.Content;

            PredictedEmotionMainLabel.Content = "Predicting...";
            PredictedEmotionsListBox.Items.Clear();
            PredictEmotionButton.Content   = (string)PredictEmotionButton.Content + " (Predicting...)";
            PredictEmotionButton.IsEnabled = false;
            FaceOutput predictedEmotion = null;
            Dictionary <string, float> predictedEmotionsWithAllLabels = null;

            MachineLearning.DrawPointsOfLandmarks(currentImageFileInfo);

            await Task.Run(() =>
            {
                MachineLearning.PredictEmotion(currentImageFileInfo, selectedFaceDataType, out var prediction, out var predictionWithLabels);
                predictedEmotion = prediction;
                predictedEmotionsWithAllLabels = predictionWithLabels;
            });

            PredictedEmotionMainLabel.Content = $"{FirstLetterToUpper(predictedEmotion.PredictedEmotion)}" +
                                                $" ({FloatToPercent(predictedEmotionsWithAllLabels[predictedEmotion.PredictedEmotion])})";

            PredictedEmotionsListBox.Items.Clear();
            predictedEmotionsWithAllLabels = predictedEmotionsWithAllLabels.OrderByDescending(c => c.Value).ToDictionary(i => i.Key, i => i.Value);
            foreach (var predictedEmotionLabelAndScore in predictedEmotionsWithAllLabels)
            {
                PredictedEmotionsListBox.Items.Add(
                    $"{FirstLetterToUpper(predictedEmotionLabelAndScore.Key)}: {FloatToPercent(predictedEmotionLabelAndScore.Value)}");
            }

            PredictedEmotionsListBox.Items.RemoveAt(0);

            PredictEmotionButton.Content   = originalLabel;
            PredictEmotionButton.IsEnabled = true;
        }
        /// <summary>
        /// Run the benchmark on the designated <paramref name="faceDataType"/> saving the results in a csv.
        /// </summary>
        /// <param name="faceDataType"></param>
        public static void RunMaximumIterationBenchmarks(Type faceDataType)
        {
            ExtractFacialFeatures(faceDataType, true);

            const int maxRepeatsExclusive = 5;
            const int maxIterationMultipliersInclusive = 6;
            const int factorMultiplier = 10; // The 10 part of 10^n
            Dictionary <int, BenchmarkStatistics> bsIterations = new Dictionary <int, BenchmarkStatistics>(maxIterationMultipliersInclusive * maxRepeatsExclusive);

            for (int repeats = 0; repeats < maxRepeatsExclusive; ++repeats)
            {
                for (int iterationMultiplier = 1; iterationMultiplier <= maxIterationMultipliersInclusive; ++iterationMultiplier)
                {
                    IDataView testSet    = null;
                    int       iterations = (int)Math.Pow(factorMultiplier, iterationMultiplier);

                    Stopwatch sw = new Stopwatch();
                    sw.Start();

                    if (faceDataType == typeof(FaceData1))
                    {
                        MachineLearning.TrainModel <FaceData1>(out testSet, iterations, false);
                    }
                    else if (faceDataType == typeof(FaceData2))
                    {
                        MachineLearning.TrainModel <FaceData2>(out testSet, iterations, false);
                    }
                    else if (faceDataType == typeof(FaceData3))
                    {
                        MachineLearning.TrainModel <FaceData3>(out testSet, iterations, false);
                    }
                    else
                    {
                        throw new ArgumentException("No corresponding logic for the specified faceDataType",
                                                    nameof(faceDataType));
                    }

                    sw.Stop();

                    MachineLearning.TestModel(testSet, faceDataType, out var testMetrics);

                    if (!bsIterations.ContainsKey(iterationMultiplier))
                    {
                        bsIterations.Add(iterationMultiplier, new BenchmarkStatistics(testMetrics.ConfusionMatrix.NumberOfClasses));
                    }

                    bsIterations[iterationMultiplier].AddData(testMetrics, (double)sw.ElapsedMilliseconds);
                }
            }

            using (var writer = new StreamWriter($"Iterations Benchmark {faceDataType.ToString()}.csv", false))
            {
                // GENERAL STATS

                writer.Write("Log Loss\n");
                writer.Write($"Iterations, Median, Max, Min\n");
                foreach (var keyValuePair in bsIterations)
                {
                    writer.Write($"{Math.Pow(factorMultiplier, keyValuePair.Key)}, {keyValuePair.Value.LogLoss.Median}, {keyValuePair.Value.LogLoss.Max}, {keyValuePair.Value.LogLoss.Min},\n");
                }
                writer.Write("\n");

                writer.Write("Log Loss Reduction\n");
                writer.Write($"Iterations, Median, Max, Min\n");
                foreach (var keyValuePair in bsIterations)
                {
                    writer.Write($"{Math.Pow(factorMultiplier, keyValuePair.Key)}, {keyValuePair.Value.LogLossReduction.Median}, {keyValuePair.Value.LogLossReduction.Max}, {keyValuePair.Value.LogLossReduction.Min},\n");
                }
                writer.Write("\n");

                writer.Write("Micro Accuracy\n");
                writer.Write($"Iterations, Median, Max, Min\n");
                foreach (var keyValuePair in bsIterations)
                {
                    writer.Write($"{Math.Pow(factorMultiplier, keyValuePair.Key)}, {keyValuePair.Value.MicroAccuracy.Median}, {keyValuePair.Value.MicroAccuracy.Max}, {keyValuePair.Value.MicroAccuracy.Min},\n");
                }
                writer.Write("\n");

                writer.Write("Macro Accuracy\n");
                writer.Write($"Iterations, Median, Max, Min\n");
                foreach (var keyValuePair in bsIterations)
                {
                    writer.Write($"{Math.Pow(factorMultiplier, keyValuePair.Key)}, {keyValuePair.Value.MacroAccuracy.Median}, {keyValuePair.Value.MacroAccuracy.Max}, {keyValuePair.Value.MacroAccuracy.Min},\n");
                }
                writer.Write("\n");

                writer.Write("Time Taken\n");
                writer.Write($"Iterations, Median, Max, Min\n");
                foreach (var keyValuePair in bsIterations)
                {
                    writer.Write($"{Math.Pow(factorMultiplier, keyValuePair.Key)}, {keyValuePair.Value.TimeTakenMiliSeconds.Median}, {keyValuePair.Value.TimeTakenMiliSeconds.Max}, {keyValuePair.Value.TimeTakenMiliSeconds.Min},\n");
                }
                writer.Write("\n");

                // CONFUSION MATRICES
                foreach (var keyValuePair in bsIterations)
                {
                    writer.Write("\n");
                    writer.Write(keyValuePair.Value.StatConfusionMatrix.GetString($"Iterations {Math.Pow(factorMultiplier, keyValuePair.Key)}"));
                    writer.Write("\n");
                }

                writer.Flush();
            }
        }
Beispiel #15
0
    public override async Task HandleAsync(IUpdateContext context, UpdateDelegate next, string[] args)
    {
        await _telegramService.AddUpdateContext(context);

        var message = _telegramService.Message;

        if (!_telegramService.IsFromSudo)
        {
            Log.Information("This user is not sudoer");
            return;
        }

        if (message.ReplyToMessage != null)
        {
            var repMessage = message.ReplyToMessage;
            var repText    = repMessage.Text ?? repMessage.Caption;
            var param      = message.Text.SplitText(" ").ToArray();
            var mark       = param.ValueOfIndex(1);
            var opts       = new List <string>
            {
                "spam", "ham"
            };

            if (!opts.Contains(mark))
            {
                await _telegramService.SendTextMessageAsync("Spesifikasikan spam atau ham (bukan spam)");

                return;
            }

            await _telegramService.SendTextMessageAsync("Sedang memperlajari pesan");

            var learnData = new LearnData
            {
                Message = repText.Replace("\n", " "),
                Label   = mark,
                ChatId  = _telegramService.ChatId,
                FromId  = _telegramService.FromId
            };

            if (_learningService.IsExist(learnData))
            {
                Log.Information("This message has learned");
                await _telegramService.EditMessageTextAsync("Pesan ini mungkin sudah di tambahkan.");

                return;
            }

            await _learningService.Save(learnData);

            await _telegramService.EditMessageTextAsync("Memperbarui local dataset");

            await _telegramService.EditMessageTextAsync("Sedang mempelajari dataset");

            await MachineLearning.SetupEngineAsync();

            await _telegramService.EditMessageTextAsync("Pesan berhasil di tambahkan ke Dataset");
        }
        else
        {
            await _telegramService.SendTextMessageAsync("Sedang mempelajari dataset");

            await MachineLearning.SetupEngineAsync();

            await _telegramService.EditMessageTextAsync("Training selesai");
        }
    }
Beispiel #16
0
        public override async Task HandleAsync(IUpdateContext context, UpdateDelegate next, string[] args,
                                               CancellationToken cancellationToken)
        {
            _telegramService = new TelegramService(context);
            _rssService      = new RssService(context.Update.Message);

            var chatId = _telegramService.Message.Chat.Id;
            var fromId = _telegramService.Message.From.Id;
            var msg    = _telegramService.Message;

            if (fromId.IsSudoer())
            {
                Log.Information("Test started..");
                await _telegramService.SendTextAsync("Sedang mengetes sesuatu")
                .ConfigureAwait(false);

                // var data = await new Query("rss_history")
                //     .Where("chat_id", chatId)
                //     .ExecForMysql()
                //     .GetAsync();
                //
                // var rssHistories = data
                //     .ToJson()
                //     .MapObject<List<RssHistory>>();
                //
                // ConsoleHelper.WriteLine(data.GetType());
                // // ConsoleHelper.WriteLine(data.ToJson(true));
                //
                // ConsoleHelper.WriteLine(rssHistories.GetType());
                // // ConsoleHelper.WriteLine(rssHistories.ToJson(true));
                //
                // ConsoleHelper.WriteLine("Test completed..");

                // await "This test".LogToChannel();

                // await RssHelper.SyncRssHistoryToCloud();
                // await BotHelper.ClearLog();

                // await SyncHelper.SyncGBanToLocalAsync();
                // var greet = TimeHelper.GetTimeGreet();

                var inlineKeyboard = new InlineKeyboardMarkup(new[]
                {
                    // new[]
                    // {
                    // InlineKeyboardButton.WithCallbackData("Warn Username Limit", "info warn-username-limit"),
                    // InlineKeyboardButton.WithCallbackData("-", "callback-set warn_username_limit 3"),
                    // InlineKeyboardButton.WithCallbackData("4", "info setelah"),
                    // InlineKeyboardButton.WithCallbackData("+", "callback-set warn_username_limit 5")
                    // },
                    new[]
                    {
                        // InlineKeyboardButton.WithCallbackData("Warn Username Limit", "info warn-username-limit"),
                        InlineKeyboardButton.WithCallbackData("-", "callback-set warn_username_limit 3"),
                        InlineKeyboardButton.WithCallbackData("4", "info setelah"),
                        InlineKeyboardButton.WithCallbackData("+", "callback-set warn_username_limit 5")
                    }
                });

                // await _telegramService.EditAsync("Warn Username Limit", inlineKeyboard);

                // LearningHelper.Setup2();
                // LearningHelper.Predict();


                if (msg.ReplyToMessage != null)
                {
                    var repMsg     = msg.ReplyToMessage;
                    var repMsgText = repMsg.Text;

                    Log.Information("Predicting message");
                    var isSpam = MachineLearning.PredictMessage(repMsgText);
                    await _telegramService.EditAsync($"IsSpam: {isSpam}")
                    .ConfigureAwait(false);

                    return;
                }

                await _telegramService.EditAsync("Complete")
                .ConfigureAwait(false);
            }

            // else
            // {
            //     await _requestProvider.SendTextAsync("Unauthorized.");
            // }
        }
Beispiel #17
0
 private void Awake()
 {
     machineLearning = machineLearningObject.GetComponent <MachineLearning>();
 }
Beispiel #18
0
 public Painter(MachineLearning machineLearningWindow)
 {
     //            this.MlWindow = machineLearningWindow;
     //            this.Canvas = this.MlWindow.Canvas;
     //            this.ResetCanvas();
 }
        private void UpdateDataGridView(MachineLearning.Learning.Regression.LearningRound lastRound)
        {
            string[] row = new string[cmd.exp.mlSettings.numberOfRounds * 2];
            row[0] = lastRound.round.ToString();
            row[1] = lastRound.learningError.ToString();
            double relativeError = 0.0;
            cmd.exp.learning.computeError(lastRound.FeatureSet, GlobalState.allMeasurements.Configurations, out relativeError);
            row[2] = relativeError.ToString();

            lastRound.learningError.ToString();

            foreach (Feature f in lastRound.FeatureSet)
            {
                string name = f.ToString();
                if (!termToIndex.ContainsKey(name))
                {
                    perfInfGridView.Invoke((MethodInvoker)(() => perfInfGridView.Columns[termToIndex.Count + perfInfGrid_definedColumns].Name = name));

                    termToIndex.Add(name, termToIndex.Count + perfInfGrid_definedColumns);

                }
                row[termToIndex[name]] = Math.Round(f.Constant,2).ToString();
            }

            perfInfGridView.Invoke((MethodInvoker)(() => this.perfInfGridView.Rows.Add(row)));
        }
Beispiel #20
0
        public void SymbolRecognitionBySVM(string positivePath, string negativePath, string testPath)
        {
            double[][]          inputs, testInputs;
            int[]               outputs;
            Image <Bgr, Byte>[] Positives, Negatives;
            Image <Bgr, Byte>   test;
            int total   = 0;
            int counter = 0;

            _scanImage  = new ScanImage();
            _descriptor = new Descriptor();
            _scanImage.SetImages(positivePath, negativePath, testPath, out Positives, out Negatives, out test);
            if (Negatives != null && Negatives.Length != 0)
            {
                total += Negatives.Length;
            }
            if (Positives != null && Positives.Length != 0)
            {
                total += Positives.Length;
            }
            inputs  = new double[total][];
            outputs = new Int32[total];

            if (Negatives != null || Negatives.Length != 0)
            {
                for (int i = 0; i < Negatives.Length; i++)
                {
                    inputs[counter]    = _descriptor.GetImageVector(Negatives[i], 100);
                    outputs[counter++] = 0;
                }
            }
            if (Positives != null || Positives.Length != 0)
            {
                for (int i = 0; i < Positives.Length; i++)
                {
                    inputs[counter]    = _descriptor.GetImageVector(Positives[i], 100);
                    outputs[counter++] = 1;
                }
            }

            _scanImage.ScanByPixel(@"C:\Users\simakmo\Documents\Sima\Symbol Reconition\data\", test, Positives[0], 50, 50);



            string[] filePaths = Directory.GetFiles(@"C:\Users\simakmo\Documents\Sima\Symbol Reconition\data\Intermediate", "*.jpg");
            testInputs = new double[filePaths.Length][];
            counter    = 0;
            foreach (string path in filePaths)
            {
                testInputs[counter++] = _descriptor.GetImageVector(new Image <Bgr, Byte>(path), 150);
            }
            _machineLearning = new MachineLearning();

            int dimention = Positives[0].Height * Positives[0].Width;

            int[]        testOutputs = _machineLearning.ApplySVMByGussianKernel(2, 2.5, 0.001, 0.2, inputs, outputs, dimention, testInputs);
            List <Point> results     = new List <Point>();

            for (int j = 0; j < testOutputs.Length; j++)
            {
                int x, y, num;
                if (testOutputs[j] == 1)
                {
                    GetImageParameters(out x, out y, out num, filePaths[j]);
                    results.Add(new Point(x, y));
                }
            }
            Visualization.DrawResults(results, Positives[0].Size, test, testPath);
        }
Beispiel #21
0
        public async Task <IActionResult> GetRelatedArticles(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            const int NUM_OF_RELATED_ARTICLES = 3;
            var       relatedArticles         = new List <Article>();

            try
            {
                int mlRelatedArticle = MachineLearning.GetRelatedArticle(_articlesTransitionDataPath, id.Value);

                if (mlRelatedArticle != id.Value)
                {
                    var article = await _context.Articles.FirstAsync(c => c.ArticleId == mlRelatedArticle);

                    if (article != null)
                    {
                        relatedArticles.Add(article);

                        // Try to find a related article to the related article
                        int mlRelatedToRelatedArticle = MachineLearning.GetRelatedArticle(_articlesTransitionDataPath, mlRelatedArticle);

                        if (mlRelatedToRelatedArticle != mlRelatedArticle && mlRelatedToRelatedArticle != id.Value)
                        {
                            article = await _context.Articles.FirstAsync(c => c.ArticleId == mlRelatedToRelatedArticle);

                            if (article != null)
                            {
                                relatedArticles.Add(article);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                // It's just mean that there is no data yet for this article
            }

            int    articlesCount = _context.Articles.Count();
            Random random        = new Random();

            // Fill the rest of the related articles with random articles, in order to
            // have the option for diversity (and by that make better predictions in the future)
            while (relatedArticles.Count() < NUM_OF_RELATED_ARTICLES)
            {
                int randomArticleId = random.Next(1, articlesCount);

                if (randomArticleId != id.Value && !relatedArticles.Exists(c => c.ArticleId == randomArticleId))
                {
                    var randomArticle = await _context.Articles.FirstAsync(c => c.ArticleId == randomArticleId);

                    if (randomArticle != null)
                    {
                        relatedArticles.Add(randomArticle);
                    }
                }
            }

            return(Json(relatedArticles));
        }
Beispiel #22
0
        static void Main(string[] args)
        {
            #region TEST
            //var a = new AFile(@"C:\Users\Zelvar\source\repos\KeyLoggerVSB\KeyLoggerVSB\bin\Release\App.exe");
            //Console.Write(new Hashes(File.ReadAllBytes(@"C:\Users\Zelvar\source\repos\KeyLoggerVSB\KeyLoggerVSB\bin\Release\App.exe")).ToString());
            //Console.WriteLine();
            //Console.Write(new PE(@"D:\SW\Arduino\Arduino.exe").ToString());
            //Console.WriteLine();
            //Console.Write(new PE(@"E:\SW\RadioDJv2\bass.dll").ToString());

            /*HashSet<A> abc = new HashSet<A>();
             * var a = new A();
             * var b = new A();
             * a.Name = "123";
             * b.Name = "123";
             *
             * abc.Add(a);
             * abc.Add(b);
             *
             * Console.WriteLine(abc.Count);
             *
             * foreach (var obj in abc) {
             *
             *  Console.WriteLine("{0} {1}", obj.GetHashCode(), obj.Name);
             *
             * }*/

            //////////////////////////////////////////////

            //new Strings(@"D:\SW\Arduino\Arduino.exe");
            //var a = new Strings(@"C:\Users\Zelvar\source\repos\KeyLoggerVSB\KeyLoggerVSB\bin\Release\App.exe");
            //var a = new Strings(@"D:\SW\Arduino\Arduino.exe");

            //Console.WriteLine(a.ContainsIP("10.0.0.1"));
            //Console.WriteLine(a.ContainsIP("FFFF::"));
            //Console.WriteLine(a.ContainsIP("10.A.0.1"));
            //Console.WriteLine(a.ContainsIP("10.0.0.1"));
            //Console.WriteLine(a.ContainsIP("FFFF::"));

            //Console.WriteLine(a.ContainsURL("http://10.A.0.1"));
            //Console.WriteLine(a.ContainsURL("http://blabla.com"));

            //Console.WriteLine(a.ContainsEmail("tralala [email protected]"));

            //Console.WriteLine(a.ContainsFileName("soubor.dll"));
            //Console.WriteLine(a.ContainsFileName("Tralala soubor.rar"));
            //Console.WriteLine(a.ContainsFileName("Tralala"));

            /*
             * foreach (var ip in a.ExtractIP())
             * {
             *  Console.WriteLine("IP: {0}", ip.ToString());
             * }
             *
             * foreach (var mail in a.ExtractMail())
             * {
             *  Console.WriteLine("Mail: {0}", mail.ToString());
             * }
             *
             * foreach (var url in a.ExtractURL())
             * {
             *  Console.WriteLine("Url: {0}", url.ToString());
             * }
             *
             * foreach (var file in a.ExtractFiles())
             * {
             *  Console.WriteLine("File: {0}", file.ToString());
             * }
             *
             * foreach (var file in a.ExtractKnownMethods())
             * {
             *  Console.WriteLine("Known methods: {0}", file.ToString());
             * }*/

            //Console.WriteLine(new VirusTotal(@"C:\Users\Zelvar\source\repos\KeyLoggerVSB\KeyLoggerVSB\bin\Release\App.exe").ToString());
            //Console.WriteLine(new Strings(@"C:\Users\Zelvar\source\repos\KeyLoggerVSB\KeyLoggerVSB\bin\Release\App.exe"));

            //new DetectWithYara(@"E:\Steam\steamapps\common\Grand Theft Auto V\GTA5.exe");

            //IFileReport a = new FileReport(@"E:\Steam\steamapps\common\Grand Theft Auto V\GTA5.exe");
            //IFileReport a = new FileReport(@"E:\Steam\steamapps\common\Grand Theft Auto V\GTA5.exe");
            //IFileReport a = new FileReport(@"E:\Steam\steamapps\common\Grand Theft Auto V\GTA5.exe");
            //IFileReport a = new FileReport(@"C:\Users\Zelvar\source\repos\KeyLoggerVSB\KeyLoggerVSB\bin\Release\HookLibrary.dll", "malware");
            //string json = JsonSerializer.Serialize(a);

            //Console.WriteLine(json);

            //byte[] a = File.ReadAllBytes(@"E:\Steam\steamapps\common\Grand Theft Auto V\GTA5.exe");

            //Console.WriteLine(a.Entropy());

            //Console.WriteLine("Val: {0} Efc: {1}", ent.Value, ent.Efficiency);

            //Console.WriteLine(ent.ToString());

            //byte[] b = new byte[] { (byte)'A',(byte)'h',(byte)'o',(byte)'j',(byte)' ',(byte)'j',(byte)'a',(byte)'k',(byte)' ',(byte)'s',(byte)'e',(byte)' ',(byte)'m',(byte)'a',(byte)'s'};

            //Console.WriteLine(b.Entropy());


            /*var hs = new HashSet<string>();
             * IList<IFileReport> _fileReports = new List<IFileReport>();
             *
             * IList<FileReportRecovered> list = ExtensionHelpers.ListFromJson(File.ReadAllText("malware.json"));
             * //IList<FileReportRecovered> list = ExtensionHelpers.ListFromJson(File.ReadAllText("software.json"));
             * _fileReports = _fileReports.Concat(list).ToList();
             * //_fileReports = _fileReports.Concat(list2).ToList();
             *
             * foreach (var a in _fileReports) {
             *
             *  if(a.Behavior.Contains(""))
             *
             *  foreach (var b in a.Behavior)
             *  {
             *      hs.Add(b.ToString());
             *  }
             * }
             *
             * foreach(var a in hs)
             * {
             *  Console.WriteLine(a);
             * }*/

            //Build model
            #endregion

            var ent = new MachineLearning();

            var data = Directory.GetFiles(@"W:\DP-Vystup\malware\malware", "*", SearchOption.AllDirectories).ToArray();

            foreach (var file in data)
            {
                var predicted = ent.Predict(file);
                Console.WriteLine("{1} is malware: {0} {2}", predicted.IsMalware, file, predicted.Probability);
            }

            //Console.WriteLine("Is malware: {0}", ent.Predict(@"W:\DP-Vystup\malware\Adobe, Acrobat PDF Writer 3._exe").IsMalware);

            /*var abc = ent.Predict(@"C:\Users\Zelvar\source\repos\KeyLoggerVSB\KeyLoggerVSB\bin\Release\HookLibrary.dll");
             * Console.WriteLine("Is malware: {0} {1}", abc.IsMalware, abc.Probability);
             * abc = ent.Predict(@"C:\Users\Zelvar\source\repos\KeyLoggerVSB\KeyLoggerVSB\bin\Release\App.exe");
             * Console.WriteLine("Is malware: {0} {1}", abc.IsMalware, abc.Probability);
             * //abc = ent.Predict(@"C:\Users\Zelvar\source\repos\FontCreator\FontCreator\bin\Debug\FontCreator.exe");
             * //Console.WriteLine("Is malware: {0} {1}", abc.IsMalware, abc.Probability);
             *
             * //Console.WriteLine("Is malware: {0}", ent.Predict(@"E:\Steam\steamapps\common\Grand Theft Auto V\GTA5.exe").IsMalware);
             * abc = ent.Predict(@"C:\Users\Zelvar\source\repos\DLLInjection\x64\Release\Dll-compresed.dll");
             * Console.WriteLine("Is malware: {0} {1}", abc.IsMalware, abc.Probability);
             * abc = ent.Predict(@"C:\Users\Zelvar\source\repos\DLLInjection\x64\Release\Dll.dll");
             * Console.WriteLine("Is malware: {0} {1}", abc.IsMalware, abc.Probability);
             * abc = ent.Predict(@"C:\Users\Zelvar\source\repos\KeyLoggerVSB\KeyLoggerVSB\bin\Release\HookLibrary.dll");
             * Console.WriteLine("Is malware: {0} {1}", abc.IsMalware, abc.Probability);
             *
             * abc = ent.Predict(@"W:\DP-Vystup\SW\DP\MalwareSample.BotnetListener\bin\Release\netcoreapp3.1\MalwareSample.BotnetListener.exe");
             * Console.WriteLine("Is malware: {0} {1}", abc.IsMalware, abc.Probability);
             * abc = ent.Predict(@"W:\DP-Vystup\SW\DP\MalwareSample.BotnetListener\bin\Release\netcoreapp3.1\MalwareSample.BotnetListener.dll");
             * Console.WriteLine("Is malware: {0} {1}", abc.IsMalware, abc.Probability);
             *
             * abc = ent.Predict(@"W:\DP-Vystup\SW\DP\MalwareSample.Keylogger\bin\Release\netcoreapp3.1\MalwareSample.Keylogger.exe");
             * Console.WriteLine("Is malware: {0} {1}", abc.IsMalware, abc.Probability);
             * abc = ent.Predict(@"W:\DP-Vystup\SW\DP\MalwareSample.Keylogger\bin\Release\netcoreapp3.1\MalwareSample.Keylogger.dll");
             * Console.WriteLine("Is malware: {0} {1}", abc.IsMalware, abc.Probability);*/
        }
        private void SetParameters(string inputPath, ScanningMethod scanningMethod, DescriptorMethod descriptorMethod, MachineLearningMethod machineLearningMethod)
        {
            _symbolParameters = new Utility.SymbolParameters();
            _hessianThreshould = _symbolParameters.HessianThresh;
            _uniquenessThresh = _symbolParameters.UniquenessThress;
            _histogramMatchingScore = _symbolParameters.HistogramMatchingScore;
            _tm = _symbolParameters.TM;

            _log = File.AppendText(inputPath + "/log.txt");

            _machineLearningMethod = machineLearningMethod;
            _scanningMethod = scanningMethod;
            _descriptorType = descriptorMethod;
            //check the FilePath
            _inputPath = inputPath;
            _inPath = inputPath + "/in";
            _outPath = inputPath + "/out";
            _intermediatePath = inputPath + "/Intermediate";

            ValidateFilePath(_inPath, "in");
            ValidateFilePath(_outPath, "out");
            ValidateFilePath(_intermediatePath, "Intermediate");
            ValidateFilePath(_inputPath, "");

            _descriptor = new Descriptor();
            _machineLearning = new MachineLearning();
            _scanImage = new ScanImage();
        }
Beispiel #24
0
        // This function is called via javascript in the "Details" view of an article
        public async Task <IActionResult> GetRelatedArticles(int?id)
        {
            if (id == null)
            {
                return(RedirectToAction("ArticleError"));
            }

            const int      NUM_OF_RELATED_ARTICLES = 3;
            List <Article> relatedArticles         = new List <Article>();

            try
            {
                // Get the related article to the viewed article
                int mlRelatedArticle = MachineLearning.GetRelatedArticle(_articlesTransitionDataPath, id.Value);

                // Make sure we don't recommend the same article as the viewed article
                if (mlRelatedArticle != id.Value)
                {
                    var article = await _context.Articles.FirstAsync(c => c.ArticleId == mlRelatedArticle);

                    if (article != null)
                    {
                        relatedArticles.Add(article);

                        // Get the related article to the related article
                        int mlRelatedToRelatedArticle = MachineLearning.GetRelatedArticle(_articlesTransitionDataPath, mlRelatedArticle);

                        // Check that the recommended article isn't itself or the previous recommended article
                        if (mlRelatedToRelatedArticle != mlRelatedArticle && mlRelatedToRelatedArticle != id.Value)
                        {
                            article = await _context.Articles.FirstAsync(c => c.ArticleId == mlRelatedToRelatedArticle);

                            if (article != null)
                            {
                                relatedArticles.Add(article);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
                // We don't do anything since it is OK for the algorithm to fail is there isn't any data for the viewed article
            }

            int    articlesCount = _context.Articles.Count();
            Random random        = new Random();

            // Fill the rest of the articles with random articles (in case the ML returned the same article and for better training of the model)
            while (relatedArticles.Count() < NUM_OF_RELATED_ARTICLES)
            {
                int randomArticleId = random.Next(1, articlesCount);

                if (randomArticleId != id.Value && !relatedArticles.Exists(c => c.ArticleId == randomArticleId))
                {
                    var randomArticle = await _context.Articles.FirstAsync(c => c.ArticleId == randomArticleId);

                    if (randomArticle != null)
                    {
                        relatedArticles.Add(randomArticle);
                    }
                }
            }

            return(Json(relatedArticles));
        }
Beispiel #25
0
        public async Task <IActionResult> GetRelatedItems(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            const int NUM_OF_RELATED_ITEMS = 3;
            var       relatedItems         = new List <Item>();

            try
            {
                int mlRelatedItem = MachineLearning.GetRelatedItem(_itemsTransitionDataPath, id.Value);

                if (mlRelatedItem != id.Value)
                {
                    var item = await _context.Items.FirstAsync(c => c.ItemId == mlRelatedItem);

                    if (item != null)
                    {
                        relatedItems.Add(item);

                        // Try to find a related item to the related item
                        int mlRelatedToRelatedItem = MachineLearning.GetRelatedItem(_itemsTransitionDataPath, mlRelatedItem);

                        if (mlRelatedToRelatedItem != mlRelatedItem && mlRelatedToRelatedItem != id.Value)
                        {
                            item = await _context.Items.FirstAsync(c => c.ItemId == mlRelatedToRelatedItem);

                            if (item != null)
                            {
                                relatedItems.Add(item);
                            }
                        }
                    }
                }
            }
            catch (Exception)
            {
            }

            int    itemsCount = _context.Items.Count();
            Random random     = new Random();

            // Fill the rest of the related items with random items, in order to
            // have the option for diversity (and by that make better predictions in the future)
            while (relatedItems.Count() < NUM_OF_RELATED_ITEMS)
            {
                int randomItemId = random.Next(1, itemsCount);

                if (randomItemId != id.Value && !relatedItems.Exists(c => c.ItemId == randomItemId))
                {
                    var randomItem = await _context.Items.FirstAsync(c => c.ItemId == randomItemId);

                    if (randomItem != null)
                    {
                        relatedItems.Add(randomItem);
                    }
                }
            }

            return(Json(relatedItems));
        }
Beispiel #26
0
 void Start()
 {
     behaviour = GameObject.Find("RL AI").GetComponent <MachineLearning>();
     player    = GameObject.Find("Player").GetComponent <PlayerBehaviour>();
 }
        public void SymbolRecognitionBySVM(string positivePath, string negativePath, string testPath)
        {
            double[][] inputs, testInputs;
            int[] outputs;
            Image<Bgr, Byte>[] Positives, Negatives;
            Image<Bgr, Byte> test;
            int total = 0;
            int counter = 0;
            _scanImage = new ScanImage();
            _descriptor = new Descriptor();
            _scanImage.SetImages(positivePath, negativePath, testPath, out Positives, out Negatives, out test);
            if (Negatives != null && Negatives.Length != 0)
                total += Negatives.Length;
            if (Positives != null && Positives.Length != 0)
                total += Positives.Length;
            inputs = new double[total][];
            outputs = new Int32[total];

            if (Negatives != null || Negatives.Length != 0)
                for (int i = 0; i < Negatives.Length; i++)
                {
                    inputs[counter] = _descriptor.GetImageVector(Negatives[i], 100);
                    outputs[counter++] = 0;
                }
            if (Positives != null || Positives.Length != 0)
                for (int i = 0; i < Positives.Length; i++)
                {
                    inputs[counter] = _descriptor.GetImageVector(Positives[i], 100);
                    outputs[counter++] = 1;
                }

            _scanImage.ScanByPixel(@"C:\Users\simakmo\Documents\Sima\Symbol Reconition\data\", test, Positives[0], 50, 50);

            string[] filePaths = Directory.GetFiles(@"C:\Users\simakmo\Documents\Sima\Symbol Reconition\data\Intermediate", "*.jpg");
            testInputs = new double[filePaths.Length][];
             counter = 0;
            foreach (string path in filePaths)
                testInputs[counter++] = _descriptor.GetImageVector(new Image<Bgr, Byte>(path), 150);
            _machineLearning = new MachineLearning();

            int dimention = Positives[0].Height * Positives[0].Width;

            int[] testOutputs = _machineLearning.ApplySVMByGussianKernel(2, 2.5, 0.001, 0.2, inputs, outputs, dimention, testInputs);
            List<Point> results = new List<Point>();
            for (int j = 0; j < testOutputs.Length; j++)
            {
                int x, y, num;
                if (testOutputs[j] == 1)
                {
                    GetImageParameters(out x, out y, out num, filePaths[j]);
                    results.Add(new Point(x, y));
                }
            }
            Visualization.DrawResults(results, Positives[0].Size, test, testPath);
        }
        public override async Task HandleAsync(IUpdateContext context, UpdateDelegate next, string[] args,
                                               CancellationToken cancellationToken)
        {
            _telegramService = new TelegramService(context);

            var message = _telegramService.Message;

            _learningService = new LearningService(message);

            if (!_telegramService.IsSudoer())
            {
                Log.Information("This user is not sudoer");
                return;
            }

            if (message.ReplyToMessage != null)
            {
                var repMessage = message.ReplyToMessage;
                var repText    = repMessage.Text ?? repMessage.Caption;
                var param      = message.Text.SplitText(" ").ToArray();
                var mark       = param.ValueOfIndex(1);
                var opts       = new List <string>()
                {
                    "spam", "ham"
                };

                if (!opts.Contains(mark))
                {
                    await _telegramService.SendTextAsync("Spesifikasikan spam atau ham (bukan spam)")
                    .ConfigureAwait(false);

                    return;
                }

                await _telegramService.SendTextAsync("Sedang memperlajari pesan")
                .ConfigureAwait(false);

                var learnData = new LearnData()
                {
                    Message = repText.Replace("\n", " "),
                    Label   = mark
                };

                if (LearningService.IsExist(learnData))
                {
                    Log.Information("This message has learned");
                    await _telegramService.EditAsync("Pesan ini mungkin sudah di tambahkan.")
                    .ConfigureAwait(false);

                    return;
                }

                await _learningService.Save(learnData).ConfigureAwait(false);

                await _telegramService.EditAsync("Memperbarui local dataset")
                .ConfigureAwait(false);

                // MachineLearning.WriteToCsv();

                await _telegramService.EditAsync("Sedang mempelajari dataset")
                .ConfigureAwait(false);

                await MachineLearning.SetupEngineAsync().ConfigureAwait(false);

                // BackgroundJob.Enqueue(() => LearningHelper.SetupEngine());

                await _telegramService.EditAsync("Pesan berhasil di tambahkan ke Dataset")
                .ConfigureAwait(false);

                return;
            }
            else
            {
                await _telegramService.SendTextAsync("Sedang mempelajari dataset")
                .ConfigureAwait(false);

                await MachineLearning.SetupEngineAsync()
                .ConfigureAwait(false);

                await _telegramService.EditAsync("Training selesai")
                .ConfigureAwait(false);

                return;
            }

            await _telegramService.SendTextAsync("Balas pesan yang ingin di pelajari")
            .ConfigureAwait(false);
        }