Ejemplo n.º 1
0
        public static ScoreCollection Load(BinaryReader br)
        {
            ScoreCollection col    = new ScoreCollection();
            int             nCount = br.ReadInt32();

            for (int i = 0; i < nCount; i++)
            {
                col.Add(Score.Load(br));
            }

            return(col);
        }
Ejemplo n.º 2
0
        private void save(int nDim, int nWid, ScoreCollection col)
        {
            string strFile = save_file;

            using (FileStream fs = File.OpenWrite(strFile))
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    bw.Write(nDim);
                    bw.Write(nWid);

                    col.Save(bw);
                }
        }
Ejemplo n.º 3
0
        public ScoreCollection CopyFrom(int nStartIdx, int nCount)
        {
            ScoreCollection col = new ScoreCollection();

            for (int i = 0; i < nCount; i++)
            {
                if (nStartIdx + i < m_rgItems.Count)
                {
                    col.Add(m_rgItems[nStartIdx + i]);
                }
            }

            return(col);
        }
Ejemplo n.º 4
0
        private ScoreCollection load(out int nDim, out int nWid)
        {
            string strFile = save_file;

            nDim = 0;
            nWid = 0;

            if (!File.Exists(strFile))
            {
                return(new ScoreCollection());
            }

            m_log.WriteLine("Loading pre-run data from '" + strFile + "'.");

            using (FileStream fs = File.OpenRead(strFile))
                using (BinaryReader br = new BinaryReader(fs))
                {
                    nDim = br.ReadInt32();
                    nWid = br.ReadInt32();

                    return(ScoreCollection.Load(br));
                }
        }
Ejemplo n.º 5
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));
        }