Beispiel #1
0
        public float[] Run(int nN)
        {
            try
            {
                Stopwatch sw            = new Stopwatch();
                float[]   rgPredictions = new float[nN];

                sw.Start();

                m_bIsDataReal = true;

                if (m_rgVocabulary != null)
                {
                    m_bIsDataReal = m_rgVocabulary.IsDataReal;
                }

                m_mycaffe.Log.Enable = false;

                if (m_bIsDataReal && !m_bUsePreloadData)
                {
                    string strSolverErr = "";
                    int    nLookahead   = 1;
                    if (m_nSolverSequenceLength >= 0 && m_nSolverSequenceLength < m_nSequenceLength)
                    {
                        nLookahead = m_nSequenceLength - m_nSolverSequenceLength;
                    }

                    rgPredictions = new float[nN * 2 * nLookahead];

                    for (int i = 0; i < nN; i++)
                    {
                        GetDataArgs e = getDataArgs(Phase.RUN, 0, 0, true);
                        m_icallback.OnGetData(e);

                        int nExpectedCount = m_blobData.count();
                        m_mycaffe.Log.CHECK_EQ(nExpectedCount, e.State.Data.ItemCount, strSolverErr + "The size of the data received ('" + e.State.Data.ItemCount.ToString() + "') does mot match the expected data count of '" + nExpectedCount.ToString() + "'!");
                        m_blobData.mutable_cpu_data = e.State.Data.GetData <T>();

                        if (m_blobLabel != null)
                        {
                            nExpectedCount = m_blobLabel.count();
                            m_mycaffe.Log.CHECK_EQ(nExpectedCount, e.State.Label.ItemCount, strSolverErr + "The size of the label received ('" + e.State.Label.ItemCount.ToString() + "') does not match the expected label count of '" + nExpectedCount.ToString() + "'!");
                            m_blobLabel.mutable_cpu_data = e.State.Label.GetData <T>();
                        }

                        double             dfLoss;
                        BlobCollection <T> colResults = m_net.Forward(out dfLoss);
                        Blob <T>           blobOutput = colResults[0];

                        if (m_blobOutput != null)
                        {
                            blobOutput = m_blobOutput;
                        }

                        float[] rgResults = Utility.ConvertVecF <T>(blobOutput.update_cpu_data());

                        for (int j = nLookahead; j > 0; j--)
                        {
                            float fPrediction = getLastPrediction(rgResults, m_rgVocabulary, j);
                            int   nIdx        = e.State.Label.ItemCount - j;
                            float fActual     = (float)e.State.Label.GetDataAtF(nIdx);

                            int nIdx0 = ((nLookahead - j) * nN * 2);
                            int nIdx1 = nIdx0 + nN;

                            if (m_dfScale != 1.0 && m_dfScale > 0)
                            {
                                fActual /= (float)m_dfScale;
                            }

                            if (m_rgVocabulary == null || m_bDisableVocabulary)
                            {
                                if (m_dfScale != 1.0 && m_dfScale > 0)
                                {
                                    fPrediction /= (float)m_dfScale;
                                }

                                rgPredictions[nIdx0 + i] = fPrediction;
                                rgPredictions[nIdx1 + i] = fActual;
                            }
                            else
                            {
                                rgPredictions[nIdx0 + i] = (float)m_rgVocabulary.GetValueAt((int)fPrediction, true);
                                rgPredictions[nIdx1 + i] = (float)m_rgVocabulary.GetValueAt((int)fActual, true);
                            }
                        }

                        if (sw.Elapsed.TotalMilliseconds > 1000)
                        {
                            double dfPct = (double)i / (double)nN;
                            m_mycaffe.Log.Enable   = true;
                            m_mycaffe.Log.Progress = dfPct;
                            m_mycaffe.Log.WriteLine("Running at " + dfPct.ToString("P") + " complete...");
                            m_mycaffe.Log.Enable = false;
                            sw.Restart();
                        }

                        if (m_mycaffe.CancelEvent.WaitOne(0))
                        {
                            break;
                        }
                    }
                }
                else
                {
                    int      nIdx    = 0;
                    List <T> rgInput = rgInput = getInitialInput(m_bIsDataReal);

                    for (int i = 0; i < nN; i++)
                    {
                        T[] rgInputVector = new T[m_blobData.count()];
                        for (int j = 0; j < m_nSequenceLength; j++)
                        {
                            // The batch is filled with 0 except for the first sequence which is the one we want to use for prediction.
                            nIdx = j * m_nBatchSize;
                            rgInputVector[nIdx] = rgInput[j];
                        }

                        m_blobData.mutable_cpu_data = rgInputVector;

                        double             dfLoss;
                        BlobCollection <T> colResults = m_net.Forward(out dfLoss);
                        Blob <T>           blobOutput = colResults[0];

                        if (m_blobOutput != null)
                        {
                            blobOutput = m_blobOutput;
                        }

                        float[] rgResults   = Utility.ConvertVecF <T>(blobOutput.update_cpu_data());
                        float   fPrediction = getLastPrediction(rgResults, m_rgVocabulary, 1);

                        //Add the new prediction and discard the oldest one
                        rgInput.Add((T)Convert.ChangeType(fPrediction, typeof(T)));
                        rgInput.RemoveAt(0);

                        if (m_rgVocabulary == null || m_bDisableVocabulary)
                        {
                            rgPredictions[i] = fPrediction;
                        }
                        else
                        {
                            rgPredictions[i] = (float)m_rgVocabulary.GetValueAt((int)fPrediction);
                        }

                        if (sw.Elapsed.TotalMilliseconds > 1000)
                        {
                            double dfPct = (double)i / (double)nN;
                            m_mycaffe.Log.Enable   = true;
                            m_mycaffe.Log.Progress = dfPct;
                            m_mycaffe.Log.WriteLine("Running at " + dfPct.ToString("P") + " complete...");
                            m_mycaffe.Log.Enable = false;
                            sw.Restart();
                        }

                        if (m_mycaffe.CancelEvent.WaitOne(0))
                        {
                            break;
                        }
                    }
                }

                return(rgPredictions);
            }
            catch (Exception excpt)
            {
                throw excpt;
            }
            finally
            {
                m_mycaffe.Log.Enable = true;
            }
        }
Beispiel #2
0
        public float[] Run(int nN)
        {
            m_bIsDataReal = false;

            if (m_rgVocabulary != null)
            {
                m_bIsDataReal = m_rgVocabulary.IsDataReal;
            }

            int       nIdx = 0;
            Stopwatch sw   = new Stopwatch();

            float[]  rgPredictions = new float[nN];
            List <T> rgInput       = getInitialInput(m_bIsDataReal);

            sw.Start();

            for (int i = 0; i < nN; i++)
            {
                T[] rgInputVector = new T[m_blobData.count()];
                for (int j = 0; j < m_nSequenceLength; j++)
                {
                    // The batch is filled with 0 except for the first sequence which is the one we want to use for prediction.
                    nIdx = j * m_nBatchSize;
                    rgInputVector[nIdx] = rgInput[j];
                }

                m_blobData.mutable_cpu_data = rgInputVector;

                double             dfLoss;
                BlobCollection <T> colResults = m_net.Forward(out dfLoss);
                float fPrediction             = getLastPrediction(colResults[0], m_rgVocabulary);

                //Add the new prediction and discard the oldest one
                rgInput.Add((T)Convert.ChangeType(fPrediction, typeof(T)));
                rgInput.RemoveAt(0);

                if (m_rgVocabulary == null)
                {
                    rgPredictions[i] = fPrediction;
                }
                else
                {
                    rgPredictions[i] = (float)m_rgVocabulary.GetValueAt((int)fPrediction);
                }

                if (sw.Elapsed.TotalMilliseconds > 1000)
                {
                    double dfPct = (double)i / (double)nN;
                    m_mycaffe.Log.Progress = dfPct;
                    m_mycaffe.Log.WriteLine("Running at " + dfPct.ToString("P") + " complete...");
                    sw.Restart();
                }

                if (m_mycaffe.CancelEvent.WaitOne(0))
                {
                    break;
                }
            }

            return(rgPredictions);
        }