예제 #1
0
 public DataState(DataState s)
 {
     m_sd = s.m_sd;
 }
예제 #2
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));
        }