Beispiel #1
0
        /// <summary>
        /// Returns the image based on its label and image selection method.
        /// </summary>
        /// <param name="state">Specifies the query state.</param>
        /// <param name="labelSelectionMethod">Specifies the label selection method.</param>
        /// <param name="imageSelectionMethod">Specifies the image selection method.</param>
        /// <param name="log">Specifies the Log for status output.</param>
        /// <param name="nLabel">Optionally, specifies the label (default = null).</param>
        /// <param name="nDirectIdx">Optionally, specifies the image index to use when loading a specific index (default = -1).</param>
        /// <param name="bLoadDataCriteria">Optionally, specifies to load the data criteria data (default = false).</param>
        /// <param name="bLoadDebugData">Optionally, specifies to load the debug data (default = false).</param>
        /// <returns>The SimpleDatum containing the image is returned.</returns>
        public SimpleDatum GetImage(QueryState state, IMGDB_LABEL_SELECTION_METHOD labelSelectionMethod, IMGDB_IMAGE_SELECTION_METHOD imageSelectionMethod, Log log, int?nLabel = null, int nDirectIdx = -1, bool bLoadDataCriteria = false, bool bLoadDebugData = false)
        {
            if ((imageSelectionMethod & IMGDB_IMAGE_SELECTION_METHOD.BOOST) == IMGDB_IMAGE_SELECTION_METHOD.BOOST &&
                (labelSelectionMethod & IMGDB_LABEL_SELECTION_METHOD.RANDOM) == IMGDB_LABEL_SELECTION_METHOD.RANDOM)
            {
                labelSelectionMethod |= IMGDB_LABEL_SELECTION_METHOD.BOOST;
            }

            if (!nLabel.HasValue && (labelSelectionMethod & IMGDB_LABEL_SELECTION_METHOD.RANDOM) == IMGDB_LABEL_SELECTION_METHOD.RANDOM)
            {
                nLabel = state.GetNextLabel(labelSelectionMethod);
            }

            int?nIdx = state.GetNextImage(imageSelectionMethod, nLabel, nDirectIdx);

            if (!nIdx.HasValue || nIdx.Value < 0)
            {
                nIdx = state.GetNextImage(imageSelectionMethod, nLabel, nDirectIdx);
                if (!nIdx.HasValue || nIdx.Value < 0)
                {
                    string strBoosted = ((imageSelectionMethod & IMGDB_IMAGE_SELECTION_METHOD.BOOST) == IMGDB_IMAGE_SELECTION_METHOD.BOOST) ? "Boosted" : "";
                    string strLabel   = (nLabel.HasValue) ? " for label '" + nLabel.Value.ToString() + "'." : ".";
                    throw new Exception("Failed to find the image index! The data source '" + m_src.Name + "' has no " + strBoosted + " images" + strLabel + ". You may need to re-index the dataset.");
                }
            }

            SimpleDatum sd = m_masterList.GetImage(nIdx.Value, bLoadDataCriteria, bLoadDebugData, m_loadMethod);

            state.UpdateStats(sd);

            return(sd);
        }
Beispiel #2
0
        private LabelSet getLabelSet(IMGDB_LABEL_SELECTION_METHOD labelSelectionMethod)
        {
            double dfBoostTotal = m_dfLabelBoostTotal;
            Dictionary <int, double> rgBoosts = m_rgLabelBoosts;
            int nIdx;

            if ((labelSelectionMethod & IMGDB_LABEL_SELECTION_METHOD.BOOST) != IMGDB_LABEL_SELECTION_METHOD.BOOST)
            {
                nIdx = m_random.Next(m_rgLabelSetWithData.Count);
                return(m_rgLabelSetWithData[nIdx]);
            }


            //---------------------------------------------
            //  Handle Label Sets with label boost.
            //---------------------------------------------
            else
            {
                double dfVal   = m_random.NextDouble() * dfBoostTotal;
                double dfTotal = 0;

                nIdx = m_rgLabelSet.Count - 1;

                for (int i = 0; i < m_rgLabelSet.Count; i++)
                {
                    int nLabel = m_rgLabelSet[i].Label.ActiveLabel;

                    if (rgBoosts != null && rgBoosts.ContainsKey(nLabel))
                    {
                        dfTotal += (double)rgBoosts[nLabel];
                    }
                    else
                    {
                        dfTotal += 1;
                    }

                    if (dfTotal >= dfVal)
                    {
                        nIdx = i;
                        break;
                    }
                }

                return(m_rgLabelSet[nIdx]);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Returns the next label in the Index set selected based on the selection criteria.
        /// </summary>
        /// <param name="lblSel">Specifies the label selection method used.</param>
        /// <returns>The next label index is returned.</returns>
        public int?GetNextLabel(IMGDB_LABEL_SELECTION_METHOD lblSel)
        {
            Index.SELECTION_TYPE selType = Index.SELECTION_TYPE.RANDOM;
            bool bBoost = false;

            if ((lblSel & IMGDB_LABEL_SELECTION_METHOD.RANDOM) != IMGDB_LABEL_SELECTION_METHOD.RANDOM)
            {
                selType = Index.SELECTION_TYPE.SEQUENTIAL;
            }

            if ((lblSel & IMGDB_LABEL_SELECTION_METHOD.BOOST) == IMGDB_LABEL_SELECTION_METHOD.BOOST)
            {
                bBoost = true;
            }

            return(GetNextLabel(selType, bBoost));
        }
Beispiel #4
0
        /// <summary>
        /// Returns the image based on its label and image selection method.
        /// </summary>
        /// <param name="nIdx">Specifies the image index to use when loading sequentially.</param>
        /// <param name="labelSelectionMethod">Specifies the label selection method.</param>
        /// <param name="imageSelectionMethod">Specifies the image selection method.</param>
        /// <param name="log">Specifies the Log for status output.</param>
        /// <param name="bLoadDataCriteria">Specifies to load the data criteria data (default = false).</param>
        /// <param name="bLoadDebugData">Specifies to load the debug data (default = false).</param>
        /// <returns>The SimpleDatum containing the image is returned.</returns>
        public SimpleDatum GetImage(int nIdx, IMGDB_LABEL_SELECTION_METHOD labelSelectionMethod, IMGDB_IMAGE_SELECTION_METHOD imageSelectionMethod, Log log, bool bLoadDataCriteria = false, bool bLoadDebugData = false)
        {
            lock (m_syncObj)
            {
                SimpleDatum[] rgImages = m_rgImages;

                if (m_nLoadLimit > 0 && m_rgImagesLimitLoaded.Count == m_nLoadLimit)
                {
                    rgImages = m_rgImagesLimitLoaded.ToArray();
                }

                if (rgImages.Length == 0)
                {
                    throw new Exception("There are no images in the dataset '" + m_src.Name + "' to select from!");
                }

                SimpleDatum sd = null;

                if ((labelSelectionMethod & IMGDB_LABEL_SELECTION_METHOD.RANDOM) == IMGDB_LABEL_SELECTION_METHOD.RANDOM)
                {
                    if (m_rgLabelSet.Count == 0)
                    {
                        throw new Exception("There are no label specified in the Labels table for the dataset '" + m_src.Name + "'!");
                    }

                    LabelSet labelSet = getLabelSet(labelSelectionMethod);
                    sd = labelSet.GetImage(nIdx, imageSelectionMethod);
                }

                int nImageIdx = 0;

                if (sd == null)
                {
                    sd = LabelSet.GetImage(rgImages, rgImages.Length, nIdx, m_random, imageSelectionMethod, ref m_nLastIdx, ref m_nFixedIndex, out nImageIdx);
                }


                //-----------------------------------------
                //  Handle dynamic loading of the image.
                //-----------------------------------------

                bool bRawDataLoaded = false;

                if (sd == null)
                {
                    int nRetries = 1;

                    if ((imageSelectionMethod & IMGDB_IMAGE_SELECTION_METHOD.RANDOM) == IMGDB_IMAGE_SELECTION_METHOD.RANDOM)
                    {
                        nRetries = 5;
                    }

                    for (int i = 0; i < nRetries; i++)
                    {
                        sd = m_factory.LoadImageAt(nImageIdx, bLoadDataCriteria, bLoadDebugData);
                        if (sd != null)
                        {
                            bRawDataLoaded = true;
                            Add(nImageIdx, sd);
                            break;
                        }

                        if (i < nRetries - 1)
                        {
                            nImageIdx = m_random.Next(rgImages.Length);
                        }
                    }

                    if (sd == null)
                    {
                        log.WriteLine("WARNING! The dataset needs to be re-indexed. Could not find the image at index " + nImageIdx.ToString() + " - attempting several random queries to get an image.");
                    }
                }

                if (!bRawDataLoaded)
                {
                    m_factory.LoadRawData(sd, bLoadDebugData, bLoadDataCriteria);
                }

                return(sd);
            }
        }