/// <summary> /// Returns the next image in the Index set based on the selection criteria. /// </summary> /// <param name="imgSel">Specifies the image selection method used.</param> /// <param name="nLabel">Optionally, specifies a label (default = null).</param> /// <param name="nDirectIdx">Optionally, specifies to query the image at this index (only applies when type = DIRECT).</param> /// <returns></returns> public int?GetNextImage(IMGDB_IMAGE_SELECTION_METHOD imgSel, int?nLabel, int nDirectIdx) { if (!nLabel.HasValue && imgSel == IMGDB_IMAGE_SELECTION_METHOD.NONE && nDirectIdx >= 0) { return(GetNextImage(Index.SELECTION_TYPE.DIRECT, null, false, nDirectIdx)); } Index.SELECTION_TYPE selType = Index.SELECTION_TYPE.RANDOM; bool bBoosted = false; if ((imgSel & IMGDB_IMAGE_SELECTION_METHOD.RANDOM) != IMGDB_IMAGE_SELECTION_METHOD.RANDOM) { selType = Index.SELECTION_TYPE.SEQUENTIAL; // When using PAIR, advance to the next item. if ((imgSel & IMGDB_IMAGE_SELECTION_METHOD.PAIR) == IMGDB_IMAGE_SELECTION_METHOD.PAIR) { GetNextImage(selType, nLabel, bBoosted, -1); } } if ((imgSel & IMGDB_IMAGE_SELECTION_METHOD.BOOST) == IMGDB_IMAGE_SELECTION_METHOD.BOOST) { bBoosted = true; } return(GetNextImage(selType, nLabel, bBoosted, -1)); }
/// <summary> /// The DataLayer constructor. /// </summary> /// <param name="cuda">Specifies the CudaDnn connection to Cuda.</param> /// <param name="log">Specifies the Log for output.</param> /// <param name="p">Specifies the LayerParameter data_param</param> /// <param name="db">Specifies the external database to use.</param> /// <param name="evtCancel">Specifies the CancelEvent used to cancel any pre-fetching operations.</param> public DataLayer(CudaDnn <T> cuda, Log log, LayerParameter p, IXImageDatabaseBase db, CancelEvent evtCancel) : base(cuda, log, p, db, evtCancel) { m_type = LayerParameter.LayerType.DATA; if (p.data_param.synchronize_target) { m_rgBatchLabels = new LabelCollection(); } Tuple <IMGDB_LABEL_SELECTION_METHOD, IMGDB_IMAGE_SELECTION_METHOD> kvSel = db.GetSelectionMethod(); IMGDB_IMAGE_SELECTION_METHOD imgSel = kvSel.Item2; if (m_param.data_param.enable_pair_selection.HasValue) { if (m_param.data_param.enable_pair_selection.Value) { imgSel |= IMGDB_IMAGE_SELECTION_METHOD.PAIR; } else { imgSel &= (~IMGDB_IMAGE_SELECTION_METHOD.PAIR); } } if (m_param.data_param.enable_random_selection.HasValue) { if (m_param.data_param.enable_random_selection.Value) { imgSel |= IMGDB_IMAGE_SELECTION_METHOD.RANDOM; } else { imgSel &= (~IMGDB_IMAGE_SELECTION_METHOD.RANDOM); } } db.SetSelectionMethod(null, imgSel); m_db = new data.DB <T>(db); m_db.Open(p.data_param.source); if (p.data_param.display_timing) { m_swTimerBatch = new Stopwatch(); m_swTimerTransaction = new Stopwatch(); } if (m_param.transform_param.mask_param != null && m_param.transform_param.mask_param.Active) { m_blobMask = new Blob <T>(cuda, log, false); m_blobMask1 = new Blob <T>(cuda, log, false); } if (m_param.data_param.enable_debug_output) { m_blobDebug1 = new Blob <T>(cuda, log, false); } }
/// <summary> /// The AnnotatedDataLayer constructor. /// </summary> /// <param name="cuda">Specifies the CudaDnn connection to Cuda.</param> /// <param name="log">Specifies the Log for output.</param> /// <param name="p">provides LayerParameter annotated_data_param.</param> /// <param name="db">Specifies the external database to use.</param> /// <param name="evtCancel">Specifies the CancelEvent used to cancel any pre-fetching operations.</param> public AnnotatedDataLayer(CudaDnn <T> cuda, Log log, LayerParameter p, IXImageDatabaseBase db, CancelEvent evtCancel) : base(cuda, log, p, db, evtCancel) { m_type = LayerParameter.LayerType.ANNOTATED_DATA; m_random = new CryptoRandom(CryptoRandom.METHOD.DEFAULT, p.transform_param.random_seed.GetValueOrDefault(0)); m_tMinusOne = (T)Convert.ChangeType(-1, typeof(T)); if (db == null) { m_log.FAIL("Currently, the AnnotatedDataLayer requires the MyCaffe Image Database!"); } Tuple <IMGDB_LABEL_SELECTION_METHOD, IMGDB_IMAGE_SELECTION_METHOD> kvSel = db.GetSelectionMethod(); IMGDB_IMAGE_SELECTION_METHOD imgSel = kvSel.Item2; if (m_param.data_param.enable_pair_selection.HasValue) { if (m_param.data_param.enable_pair_selection.Value) { imgSel |= IMGDB_IMAGE_SELECTION_METHOD.PAIR; } else { imgSel &= (~IMGDB_IMAGE_SELECTION_METHOD.PAIR); } } if (m_param.data_param.enable_random_selection.HasValue) { if (m_param.data_param.enable_random_selection.Value) { imgSel |= IMGDB_IMAGE_SELECTION_METHOD.RANDOM; } else { imgSel &= (~IMGDB_IMAGE_SELECTION_METHOD.RANDOM); } } if (!db.GetLoadImageDataCriteria()) { m_log.WriteError(new Exception("The 'Load Image Data Criteria' must be set to TRUE in order to load the Annotation data.")); } db.SetSelectionMethod(null, imgSel); m_db = new data.DB <T>(db); m_db.Open(p.data_param.source); if (p.data_param.display_timing) { m_swTimerBatch = new Stopwatch(); m_swTimerTransaction = new Stopwatch(); } m_sampler = new SsdSampler <T>(cuda, log); }
/// <summary> /// The AnnotatedDataLayer constructor. /// </summary> /// <param name="cuda">Specifies the CudaDnn connection to Cuda.</param> /// <param name="log">Specifies the Log for output.</param> /// <param name="p">provides LayerParameter annotated_data_param.</param> /// <param name="db">Specifies the external database to use.</param> /// <param name="evtCancel">Specifies the CancelEvent used to cancel any pre-fetching operations.</param> public AnnotatedDataLayer(CudaDnn <T> cuda, Log log, LayerParameter p, IXImageDatabase db, CancelEvent evtCancel) : base(cuda, log, p, db, evtCancel) { m_type = LayerParameter.LayerType.ANNOTATED_DATA; Tuple <IMGDB_LABEL_SELECTION_METHOD, IMGDB_IMAGE_SELECTION_METHOD> kvSel = db.GetSelectionMethod(); IMGDB_IMAGE_SELECTION_METHOD imgSel = kvSel.Item2; if (m_param.data_param.enable_pair_selection.HasValue) { if (m_param.data_param.enable_pair_selection.Value) { imgSel |= IMGDB_IMAGE_SELECTION_METHOD.PAIR; } else { imgSel &= (~IMGDB_IMAGE_SELECTION_METHOD.PAIR); } } if (m_param.data_param.enable_random_selection.HasValue) { if (m_param.data_param.enable_random_selection.Value) { imgSel |= IMGDB_IMAGE_SELECTION_METHOD.RANDOM; } else { imgSel &= (~IMGDB_IMAGE_SELECTION_METHOD.RANDOM); } } if (!db.GetLoadImageDataCriteria()) { m_log.WriteError(new Exception("The 'Load Image Data Criteria' must be set to TRUE in order to load the Annotation data.")); } db.SetSelectionMethod(null, imgSel); m_db = new data.DB(db); m_db.Open(p.data_param.source); m_cursor = m_db.NewCursor(); if (p.data_param.display_timing) { m_swTimerBatch = new Stopwatch(); m_swTimerTransaction = new Stopwatch(); } m_sampler = new SsdSampler <T>(cuda, log); }
/// <summary> /// Returns an image from the LabelSet using the image selection method. /// </summary> /// <param name="nIdx">Specifies the index to use when performing sequential selection.</param> /// <param name="selectionMethod">Specifies the image selection method.</param> /// <returns>The image is returned.</returns> public SimpleDatum GetImage(int nIdx, IMGDB_IMAGE_SELECTION_METHOD selectionMethod) { if ((selectionMethod & IMGDB_IMAGE_SELECTION_METHOD.RANDOM) != IMGDB_IMAGE_SELECTION_METHOD.RANDOM) { throw new ArgumentException("Label balancing image selection only supports the RANDOM and RANDOM+BOOST selection methods"); } if (m_nCurrentIdx == 0) { return(null); } int nLastIdx = 0; int nFixedIdx = -1; int nImageIdx = 0; return(GetImage(m_rgImages, m_rgIdx, m_nCurrentIdx, nIdx, m_random, selectionMethod, ref nLastIdx, ref nFixedIdx, out nImageIdx)); }
/// <summary> /// The AnnotatedDataLayer constructor. /// </summary> /// <param name="cuda">Specifies the CudaDnn connection to Cuda.</param> /// <param name="log">Specifies the Log for output.</param> /// <param name="p">provides LayerParameter annotated_data_param.</param> /// <param name="db">Specifies the external database to use.</param> /// <param name="evtCancel">Specifies the CancelEvent used to cancel any pre-fetching operations.</param> public AnnotatedDataLayer(CudaDnn <T> cuda, Log log, LayerParameter p, IXImageDatabase db, CancelEvent evtCancel) : base(cuda, log, p, db, evtCancel) { m_type = LayerParameter.LayerType.ANNOTATED_DATA; Tuple <IMGDB_LABEL_SELECTION_METHOD, IMGDB_IMAGE_SELECTION_METHOD> kvSel = db.GetSelectionMethod(); IMGDB_IMAGE_SELECTION_METHOD imgSel = kvSel.Item2; if (m_param.data_param.enable_pair_selection.HasValue) { if (m_param.data_param.enable_pair_selection.Value) { imgSel |= IMGDB_IMAGE_SELECTION_METHOD.PAIR; } else { imgSel &= (~IMGDB_IMAGE_SELECTION_METHOD.PAIR); } } if (m_param.data_param.enable_random_selection.HasValue) { if (m_param.data_param.enable_random_selection.Value) { imgSel |= IMGDB_IMAGE_SELECTION_METHOD.RANDOM; } else { imgSel &= (~IMGDB_IMAGE_SELECTION_METHOD.RANDOM); } } db.SetSelectionMethod(null, imgSel); m_db = new data.DB(db); m_db.Open(p.data_param.source); m_cursor = m_db.NewCursor(); if (p.data_param.display_timing) { m_swTimerBatch = new Stopwatch(); m_swTimerTransaction = new Stopwatch(); } }
/// <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); }
/// <summary> /// Returns an image from a list of images. /// </summary> /// <param name="rgImages">Specifies the image list to select from.</param> /// <param name="rgIdx">Specifies the list of indexes to choose from.</param> /// <param name="nCount">Specifies the maximum count to use.</param> /// <param name="nIdx">Specifies the index to use when selecting sequentially or in pair selection.</param> /// <param name="random">Specifies the random number generator to use.</param> /// <param name="selectionMethod">Specifies the image selection method.</param> /// <param name="nLastIndex">Specifies the last index used.</param> /// <param name="nFixedIndex">Specifies the fixed index to use.</param> /// <param name="nImageIdx">Returns the image index used.</param> /// <returns></returns> public static SimpleDatum GetImage(SimpleDatum[] rgImages, List <int> rgIdx, int nCount, int nIdx, CryptoRandom random, IMGDB_IMAGE_SELECTION_METHOD selectionMethod, ref int nLastIndex, ref int nFixedIndex, out int nImageIdx) { if ((selectionMethod & IMGDB_IMAGE_SELECTION_METHOD.BOOST) == IMGDB_IMAGE_SELECTION_METHOD.BOOST) { IEnumerable <SimpleDatum> iQuery = rgImages.Where(p => p != null && p.Boost > 0); List <SimpleDatum> rgItems = new List <SimpleDatum>(iQuery); if (rgItems.Count > 0) { if (rgIdx.Count > rgItems.Count) { rgIdx.Clear(); } if (rgIdx.Count == 0) { for (int i = 0; i < rgItems.Count; i++) { rgIdx.Add(i); } } if ((selectionMethod & IMGDB_IMAGE_SELECTION_METHOD.RANDOM) == IMGDB_IMAGE_SELECTION_METHOD.RANDOM) { nIdx = rgIdx[random.Next(rgIdx.Count)]; rgIdx.Remove(nIdx); } SimpleDatum sd = rgItems[nIdx]; nImageIdx = nIdx; return(sd); } } int nMin = ((selectionMethod & IMGDB_IMAGE_SELECTION_METHOD.PAIR) == IMGDB_IMAGE_SELECTION_METHOD.PAIR) ? 2 : 1; if (rgIdx.Count < nMin) { rgIdx.Clear(); for (int i = 0; i < rgImages.Length; i++) { rgIdx.Add(i); } } if ((selectionMethod & IMGDB_IMAGE_SELECTION_METHOD.PAIR) == IMGDB_IMAGE_SELECTION_METHOD.PAIR) { nIdx = nLastIndex + 1; if (nIdx == rgIdx.Count) { nIdx = 0; } rgIdx.Remove(nIdx); } else if ((selectionMethod & IMGDB_IMAGE_SELECTION_METHOD.RANDOM) == IMGDB_IMAGE_SELECTION_METHOD.RANDOM) { nIdx = rgIdx[random.Next(rgIdx.Count)]; rgIdx.Remove(nIdx); } else if (selectionMethod == IMGDB_IMAGE_SELECTION_METHOD.FIXEDINDEX) { nFixedIndex = nIdx; } else if ((selectionMethod & IMGDB_IMAGE_SELECTION_METHOD.CLEARFIXEDINDEX) == IMGDB_IMAGE_SELECTION_METHOD.CLEARFIXEDINDEX) { nFixedIndex = -1; } if (nFixedIndex >= 0) { nIdx = nFixedIndex; } if (nIdx >= rgImages.Length) { nIdx = nIdx % rgImages.Length; } nLastIndex = nIdx; nImageIdx = nIdx; return(rgImages[nIdx]); }
/// <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); } }