Ejemplo n.º 1
0
        private LearningManager GetLearningManager()
        {
            if (_LearningManager != null) return _LearningManager;

            _LearningManager = new LearningDBN();
            _LearningManager.Load(GetNeuroPath(_LearningManager.Filename));
            return _LearningManager;
        }
Ejemplo n.º 2
0
        private void BackgroundWorkerRain_DoWork(object sender, DoWorkEventArgs e)
        {
            Task task = e.Argument as Task;
            if (task == null) return;

            try
            {
                string path = Path.Combine(task.NeuralNetwork, LearningManager.Instance.Filename);
                if (_Learning == null)
                {
                    _Learning = LearningManager.Instance;
                    if (!_Learning.Load(path)) _Learning.Initialize();
                }

                if (task.Type == Task.TaskType.Learning)
                {
                    const int Cut = 18;
                    const int Step = 40;
                    List<RainFiles> list = new List<RainFiles>();
                    for (int i = 0; i < task.RainFiles.Count; i += Cut) list.Add(new RainFiles(task.RainFiles, i, Cut));
                    list = list.OrderBy(i => Guid.NewGuid()).ToList();

                    List<LearningImage> images = new List<LearningImage>();
                    for (int l = 0; l < list.Count; l++)
                    {
                        images.AddRange(GetRainImages(list[l].Files, 0, list[l].Files.Count));
                        if ((l + 1) % Step != 0) continue;

                        _Learning.Learn(images);
                        images.Clear();
                        GC.Collect();

                        Log.Instance.Info("Progress: " + l + " / " + list.Count);
                        BackgroundWorkerRain.ReportProgress(l * 100 / list.Count);
            #if DEBUG && false
                        int limit = LearningManager.Instance.HistoryLimit;
                        int pair = task.OutputFiles.Count / limit;
                        if (pair > 0)
                        {
                            for (int p = 0; p < pair; p++)
                            {
                                RainImage rain = _Learning.Forecast(GetRainImages(task.OutputFiles, p * limit, limit)) as RainImage;
                                rain.SavePngDetail("../i" + l + "-p" + p + ".png");
                            }
                        }
            #endif
                    }
                    _Learning.Save(path);
                }
                if(task.Type == Task.TaskType.Forecast)
                {
                    RainImage predicted = _Learning.Forecast(GetRainImages(task.RainFiles, 0, 3)) as RainImage;
                    string filename = Path.GetFileNameWithoutExtension(task.RainFiles.Last());
                    string normal = Path.Combine(task.NeuralNetwork, "../forecast/" + filename + ".png");
                    string detail = Path.Combine(task.NeuralNetwork, "../forecast/" + filename + ".detail.png");
                    predicted.SavePng(normal, 1, 0.025);
                    predicted.SavePngDetail(detail);
                    task.OutputFiles.Add(detail);
                    e.Result = task;
                }
            }
            catch(Exception ex)
            {
                Log.Instance.Error(ex.Message + "@" + ex.StackTrace);
                BackgroundWorkerRain.ReportProgress(100);
            }
        }