Exemple #1
0
        /// <summary>
        ///  This event is called by the Solver to get training data for this next training Step.
        ///  Within this event, the data is loaded for the next training step.
        /// </summary>
        /// <param name="sender">Specifies the sender of the event (e.g. the solver)</param>
        /// <param name="args">n/a</param>
        private void onTrainingStart(object sender, EventArgs args)
        {
            Blob <float> blobData  = m_mycaffe.GetInternalNet(Phase.TRAIN).FindBlob("data");
            Blob <float> blobLabel = m_mycaffe.GetInternalNet(Phase.TRAIN).FindBlob("label");
            Blob <float> blobClip1 = m_mycaffe.GetInternalNet(Phase.TRAIN).FindBlob("clip1");

            // Load a batch of data where:
            // 'data' contains a batch of 10D detected images in sequence by label.
            // 'label' contains a batch of 1D future signals in sequence.
            List <float> rgYb  = new List <float>();
            List <float> rgFYb = new List <float>();

            for (int i = 0; i < m_model.Batch; i++)
            {
                for (int t = 0; t < m_model.TimeSteps; t++)
                {
                    // Get images one number at a time, in order by label, but randomly selected.
                    SimpleDatum sd = m_imgDb.QueryImage(m_ds.TrainingSource.ID, 0, null, IMGDB_IMAGE_SELECTION_METHOD.RANDOM, m_nLabelSeq);
                    m_mycaffeInput.Run(sd);

                    Net <float>  inputNet = m_mycaffeInput.GetInternalNet(Phase.RUN);
                    Blob <float> input_ip = inputNet.FindBlob(m_strInputOutputBlobName);
                    float[]      rgY1     = input_ip.mutable_cpu_data;

                    rgYb.AddRange(rgY1);
                    Dictionary <string, float[]> data = Signal.GenerateSample(1, m_nLabelSeq / 10.0f, 1, m_model.InputLabel, m_model.TimeSteps);
                    float[] rgFY1 = data["FY"];

                    // Add future steps corresponding to m_nLabelSeq time step;
                    rgFYb.AddRange(rgFY1);

                    m_nLabelSeq++;
                    if (m_nLabelSeq > 9)
                    {
                        m_nLabelSeq = 0;
                    }
                }
            }

            float[] rgY = SimpleDatum.Transpose(rgYb.ToArray(), blobData.channels, blobData.num, blobData.count(2)); // Transpose for Sequence Major ordering.
            blobData.mutable_cpu_data = rgY;

            float[] rgFY = SimpleDatum.Transpose(rgFYb.ToArray(), blobLabel.channels, blobLabel.num, blobLabel.count(2)); // Transpose for Sequence Major ordering.
            blobLabel.mutable_cpu_data = rgFY;

            blobClip1.SetData(1);
            blobClip1.SetData(0, 0, m_model.Batch);
        }
Exemple #2
0
        /// <summary>
        /// Retrieve the Datum at the current cursor location within the data source.
        /// </summary>
        /// <param name="nLabel">Optionally, specifies a label for which the cursor should query from.</param>
        /// <param name="bLoadDataCriteria">Specifies whether or not to load the data criteria.</param>
        /// <param name="imgSel">Optionally, specifies the image selection method (default = null).</param>
        /// <returns>The Datum retrieved is returned.</returns>
        public SimpleDatum GetValue(int?nLabel = null, bool bLoadDataCriteria = false, IMGDB_IMAGE_SELECTION_METHOD?imgSel = null)
        {
            SimpleDatum sd = m_db.QueryImage(m_nSrcID, m_nIdx, null, imgSel, nLabel, bLoadDataCriteria, false);

            if (m_log != null)
            {
                m_log.WriteLine(m_strSrc + ": Idx = " + sd.Index.ToString() + " Label = " + sd.Label.ToString());
            }

            m_transformer.TransformLabel(sd);

            return(sd);
        }
Exemple #3
0
        /// <summary>
        /// Initialize the gym with the specified properties.
        /// </summary>
        /// <param name="log">Specifies the output log to use.</param>
        /// <param name="properties">Specifies the properties containing Gym specific initialization parameters.</param>
        /// <remarks>
        /// The ModelGym uses the following initialization properties.
        ///
        /// 'GpuID' - the GPU to run on.
        /// 'ModelDescription' - the model description of the model to use.
        /// 'Dataset' - the name of the dataset to use.
        /// 'Weights' - the model trained weights.
        /// 'CudaPath' - the path of the CudaDnnDLL to use.
        /// 'BatchSize' - the batch size used when running images through the model (default = 16).
        /// 'RecreateData' - when 'True' the data is re-run through the model, otherwise if already run the data is loaded from file (faster).
        /// </remarks>
        public void Initialize(Log log, PropertySet properties)
        {
            m_nGpuID        = properties.GetPropertyAsInt("GpuID");
            m_strModelDesc  = properties.GetProperty("ModelDescription");
            m_strDataset    = properties.GetProperty("Dataset");
            m_rgWeights     = properties.GetPropertyBlob("Weights");
            m_nBatchSize    = properties.GetPropertyAsInt("BatchSize", 16);
            m_bRecreateData = properties.GetPropertyAsBool("RecreateData", false);
            m_strProject    = properties.GetProperty("ProjectName");
            if (string.IsNullOrEmpty(m_strProject))
            {
                m_strProject = "default";
            }

            string strCudaPath = properties.GetProperty("CudaPath");

            SettingsCaffe s = new SettingsCaffe();

            s.GpuIds            = m_nGpuID.ToString();
            s.ImageDbLoadMethod = IMAGEDB_LOAD_METHOD.LOAD_ON_DEMAND_BACKGROUND;

            m_imgdb = new MyCaffeImageDatabase2(log);
            m_imgdb.InitializeWithDsName1(s, m_strDataset);
            m_ds = m_imgdb.GetDatasetByName(m_strDataset);

            SimpleDatum sd    = m_imgdb.QueryImage(m_ds.TrainingSource.ID, 0, IMGDB_LABEL_SELECTION_METHOD.NONE, IMGDB_IMAGE_SELECTION_METHOD.NONE);
            BlobShape   shape = new BlobShape(1, sd.Channels, sd.Height, sd.Width);

            if (m_evtCancel == null)
            {
                m_evtCancel = new CancelEvent();
            }

            m_mycaffe = new MyCaffeControl <float>(s, log, m_evtCancel, null, null, null, null, strCudaPath);
            m_mycaffe.LoadToRun(m_strModelDesc, m_rgWeights, shape);

            m_log = log;
        }
Exemple #4
0
        /// <summary>
        /// Step the gym one step in the data.
        /// </summary>
        /// <param name="nAction">Specifies the action to run on the gym.</param>
        /// <param name="bGetLabel">Not used.</param>
        /// <param name="extraProp">Optionally, specifies extra properties.</param>
        /// <returns>A tuple containing state data, the reward, and the done state is returned.</returns>
        public Tuple <State, double, bool> Step(int nAction, bool bGetLabel = false, PropertySet extraProp = null)
        {
            DataState       data   = new DataState();
            ScoreCollection scores = null;

            if (ActivePhase == Phase.RUN)
            {
                if (extraProp == null)
                {
                    throw new Exception("The extra properties are needed when querying data during the RUN phase.");
                }

                int    nDataCount   = extraProp.GetPropertyAsInt("DataCountRequested");
                string strStartTime = extraProp.GetProperty("SeedTime");

                int      nStartIdx = m_scores.Count - nDataCount;
                DateTime dt;
                if (DateTime.TryParse(strStartTime, out dt))
                {
                    nStartIdx = m_scores.FindIndexAt(dt, nDataCount);
                }

                scores = m_scores.CopyFrom(nStartIdx, nDataCount);
            }
            else
            {
                int nCount = 0;

                m_scores = load(out m_nDim, out m_nWidth);

                if (m_bRecreateData || m_scores.Count != m_ds.TrainingSource.ImageCount)
                {
                    Stopwatch sw = new Stopwatch();
                    sw.Start();

                    m_scores = new ScoreCollection();

                    while (m_nCurrentIdx < m_ds.TrainingSource.ImageCount)
                    {
                        // Query images sequentially by index in batches
                        List <SimpleDatum> rgSd = new List <SimpleDatum>();

                        for (int i = 0; i < m_nBatchSize; i++)
                        {
                            SimpleDatum sd = m_imgdb.QueryImage(m_ds.TrainingSource.ID, m_nCurrentIdx + i, IMGDB_LABEL_SELECTION_METHOD.NONE, IMGDB_IMAGE_SELECTION_METHOD.NONE);
                            rgSd.Add(sd);
                            nCount++;

                            if (nCount == m_ds.TrainingSource.ImageCount)
                            {
                                break;
                            }
                        }

                        List <ResultCollection> rgRes = m_mycaffe.Run(rgSd, ref m_blobWork);

                        if (m_nWidth == 0)
                        {
                            m_nWidth = rgRes[0].ResultsOriginal.Count;
                            m_nDim   = rgRes[0].ResultsOriginal.Count * 2;
                        }

                        // Fill SimpleDatum with the ordered label,score pairs starting with the detected label.
                        for (int i = 0; i < rgRes.Count; i++)
                        {
                            m_scores.Add(new Score(rgSd[i].TimeStamp, rgSd[i].Index, rgRes[i]));
                            m_nCurrentIdx++;
                        }

                        if (sw.Elapsed.TotalMilliseconds > 1000)
                        {
                            m_log.Progress = (double)m_nCurrentIdx / (double)m_ds.TrainingSource.ImageCount;
                            m_log.WriteLine("Running model on image " + m_nCurrentIdx.ToString() + " of " + m_ds.TrainingSource.ImageCount.ToString() + " of '" + m_strDataset + "' dataset.");

                            if (m_evtCancel.WaitOne(0))
                            {
                                return(null);
                            }
                        }
                    }

                    save(m_nDim, m_nWidth, m_scores);
                }
                else
                {
                    m_nCurrentIdx = m_scores.Count;
                }

                scores = m_scores;
            }

            float[]     rgfRes = scores.Data;
            SimpleDatum sdRes  = new SimpleDatum(scores.Count, m_nWidth, 2, rgfRes, 0, rgfRes.Length);

            data.SetData(sdRes);
            m_nCurrentIdx = 0;

            return(new Tuple <State, double, bool>(data, 0, false));
        }