2using System.Collections.Generic;
8using System.Threading.Tasks;
28 List<SimpleDatum> m_rgImg =
new List<SimpleDatum>();
41 public event EventHandler<ProgressArgs>
OnError;
57 m_evtCancel = evtCancel;
61 private string dataset_name
63 get {
return "VOC0712"; }
75 reportProgress(0, 0,
"Loading " + dataset_name +
" database...");
78 int nTotal = 5011 + 17125;
80 int nExtractTotal = 10935 + 40178;
84 Dictionary<string, int> rgNameToLabel = labelMap.
MapToLabel(m_log,
true);
85 string strSrc = dataset_name +
".training";
91 List<Tuple<int, string, Size>> rgFileSizes =
new List<Tuple<int, string, Size>>();
99 string strDir = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) +
"\\MyCaffe\\test_data\\data\\ssd\\VOC0712\\";
100 if (!Directory.Exists(strDir))
101 Directory.CreateDirectory(strDir);
103 saveFileSizes(rgFileSizes, strDir +
"train_name_size.txt");
107 rgFileSizes =
new List<Tuple<int, string, Size>>();
108 m_rgImg =
new List<SimpleDatum>();
112 nExtractTotal = 10347;
114 strSrc = dataset_name +
".testing";
123 saveFileSizes(rgFileSizes, strDir +
"test_name_size.txt");
133 catch (Exception excpt)
144 private void saveFileSizes(List<Tuple<int, string, Size>> rgFileSizes,
string strFile)
146 if (File.Exists(strFile))
147 File.Delete(strFile);
149 using (StreamWriter sw =
new StreamWriter(strFile))
151 foreach (Tuple<int, string, Size> item
in rgFileSizes)
153 string strLine = item.Item1.ToString() +
", ";
154 strLine += item.Item2.ToString() +
", ";
155 strLine += item.Item3.Height.ToString() +
", ";
156 strLine += item.Item3.Width.ToString();
157 sw.WriteLine(strLine);
162 private void addLabels(
int nSrcId, Dictionary<string, int> rgNameToLabel)
164 foreach (KeyValuePair<string, int> kv
in rgNameToLabel)
166 m_factory.
AddLabel(kv.Value, kv.Key, nSrcId);
170 private bool loadFile(
string strImagesFile,
string strSourceName,
int nExtractTotal, ref
int nExtractIdx,
int nTotal, ref
int nIdx,
Log log,
bool bExtractFiles, Dictionary<string, int> rgNameToLabel, List<Tuple<int, string, Size>> rgFileSizes)
172 Stopwatch sw =
new Stopwatch();
174 reportProgress(nIdx, nTotal,
" Source: " + strSourceName);
175 reportProgress(nIdx, nTotal,
" loading " + strImagesFile +
"...");
177 FileStream fs =
null;
181 int nSrcId = m_factory.
AddSource(strSourceName, 3, -1, -1,
false);
182 addLabels(nSrcId, rgNameToLabel);
185 int nPos = strImagesFile.ToLower().LastIndexOf(
".tar");
186 string strPath = strImagesFile.Substring(0, nPos);
188 if (!Directory.Exists(strPath))
189 Directory.CreateDirectory(strPath);
193 log.
Progress = (double)nIdx / nExtractTotal;
194 log.
WriteLine(
"Extracting files from '" + strImagesFile +
"'...");
196 if ((nExtractIdx =
TarFile.
ExtractTar(strImagesFile, strPath, m_evtCancel, log, nExtractTotal, nExtractIdx)) == 0)
205 int nResizeHeight = 0;
206 int nResizeWidth = 0;
210 List<Tuple<string, string>> rgFiles = createFileList(log, strPath);
213 for (
int i = 0; i < rgFiles.Count; i++)
215 SimpleDatum datum = loadDatum(log, rgFiles[i].Item1, rgFiles[i].Item2, nResizeHeight, nResizeWidth, type, rgNameToLabel);
225 if (sw.Elapsed.TotalMilliseconds > 1000)
227 log.
Progress = (double)nIdx / nTotal;
228 log.
WriteLine(
"Loading file " + i.ToString() +
" of " + rgFiles.Count.ToString() +
"...");
232 rgFileSizes.Add(
new Tuple<int, string, Size>(nIdx, rgFiles[i].Item1,
new Size(datum.
Width, datum.
Height)));
249 Bitmap bmp =
new Bitmap(strImgFile);
251 if (nResizeHeight == 0)
252 nResizeHeight = bmp.Height;
254 if (nResizeWidth == 0)
255 nResizeWidth = bmp.Width;
257 if (nResizeHeight != bmp.Height || nResizeWidth != bmp.Width)
265 loadAnnotationFile(log, strAnnotationFile, datum, type, rgNameToLabel);
274 log.
FAIL(
"Unknown annotation type '" + type.ToString() +
"'!");
281 string strExt = Path.GetExtension(strFile).ToLower();
286 return loadXmlAnnotationFile(log, strFile, datum, rgNameToLabel);
289 log.
FAIL(
"Unknown annotation file type '" + strExt +
"'!");
296 private bool loadXmlAnnotationFile(
Log log,
string strFile,
SimpleDatum datum, Dictionary<string, int> rgNameToLabel)
298 XDocument doc = XDocument.Load(strFile);
299 XElement size = doc.Descendants(
"size").First();
302 val = size.Descendants(
"width").First();
303 int nWidth =
int.Parse(val.Value);
305 val = size.Descendants(
"height").First();
306 int nHeight =
int.Parse(val.Value);
308 val = size.Descendants(
"depth").First();
309 int nChannels =
int.Parse(val.Value);
312 log.
FAIL(
"Inconsistent image size, expected (" + datum.
Channels.ToString() +
"," + datum.
Height.ToString() +
"," + datum.
Width.ToString() +
") but annotation has size (" + nChannels.ToString() +
"," + nHeight.ToString() +
"," + nWidth.ToString() +
").");
316 List<XElement> objects = doc.Descendants(
"object").ToList();
317 foreach (XElement obj
in objects)
319 val = obj.Descendants(
"name").First();
320 string strName = val.Value;
322 val = obj.Descendants(
"difficult").First();
323 bool bDifficult = (val.Value ==
"0") ?
false :
true;
325 XElement bndbox = obj.Descendants(
"bndbox").First();
327 val = bndbox.Descendants(
"xmin").First();
329 if (fxmin > nWidth || fxmin < 0)
330 log.
WriteLine(
"WARNING: '" + strFile +
"' bounding box exceeds image boundary.");
332 val = bndbox.Descendants(
"ymin").First();
334 if (fymin > nHeight || fymin < 0)
335 log.
WriteLine(
"WARNING: '" + strFile +
"' bounding box exceeds image boundary.");
337 val = bndbox.Descendants(
"xmax").First();
339 if (fxmax > nWidth || fxmax < 0)
340 log.
WriteLine(
"WARNING: '" + strFile +
"' bounding box exceeds image boundary.");
342 val = bndbox.Descendants(
"ymax").First();
344 if (fymax > nHeight || fymax < 0)
345 log.
WriteLine(
"WARNING: '" + strFile +
"' bounding box exceeds image boundary.");
347 if (!rgNameToLabel.ContainsKey(strName))
349 log.
FAIL(
"Could not find the label '" + strName +
"' in the label mapping!");
353 int nLabel = rgNameToLabel[strName];
385 private List<Tuple<string, string>> createFileList(
Log log,
string strFile)
387 List<Tuple<string, string>> rgFiles =
new List<Tuple<string, string>>();
389 string strPath = strFile;
390 int nPos = strFile.ToLower().LastIndexOf(
".tar");
392 strPath = strFile.Substring(0, nPos);
394 strPath +=
"\\VOCdevkit\\VOC";
396 if (strPath.Contains(
"2012"))
399 if (strPath.Contains(
"2007"))
402 loadFileList(log, strPath, rgFiles);
406 private void loadFileList(
Log log,
string strPath, List<Tuple<string, string>> rgFiles)
408 log.
WriteLine(
"Creating the list file for " + dataset_name +
" dataset...");
410 string strImgPath = strPath +
"\\JPEGImages";
411 string strLabelPath = strPath +
"\\Annotations";
413 string[] rgImgFiles = Directory.GetFiles(strImgPath);
414 string[] rgLabelFiles = Directory.GetFiles(strLabelPath);
416 if (rgImgFiles.Length != rgLabelFiles.Length)
418 log.
FAIL(
"The image path '" + strImgPath +
"' has " + rgImgFiles.Length.ToString() +
" files and label path '" + strLabelPath +
"' has " + rgLabelFiles.Length.ToString() +
" files - both paths should have the same number of files!");
422 for (
int i = 0; i < rgImgFiles.Length; i++)
424 rgFiles.Add(
new Tuple<string, string>(rgImgFiles[i], rgLabelFiles[i]));
428 private string test_data_path
432 string strPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData).TrimEnd(
'\\');
433 strPath +=
"\\MyCaffe\\test_data";
438 private string getDataFile(
string strSubDir,
string strFileName)
440 string strPath = test_data_path;
441 strPath +=
"\\data\\ssd\\" + strSubDir +
"\\" + strFileName;
447 string strFile = getDataFile(dataset_name,
"labelmap_voc.prototxt");
452 private void reportProgress(
int nIdx,
int nTotal,
string strMsg)
455 OnProgress(
this,
new ProgressArgs(
new ProgressInfo(nIdx, nTotal, strMsg)));
458 private void reportError(
int nIdx,
int nTotal, Exception err)
461 OnError(
this,
new ProgressArgs(
new ProgressInfo(nIdx, nTotal,
"ERROR", err)));
Defines a collection of AnnotationGroups.
void Add(AnnotationGroupCollection col)
Add another AnnotationGroupCollection to this one.
The AnnoationGroup class manages a group of annotations.
int group_label
Get/set the group label.
List< Annotation > annotations
Get/set the group annoations.
The Annotation class is used by annotations attached to SimpleDatum's and used in SSD.
The BaseParameter class is the base class for all other parameter classes.
static float ParseFloat(string strVal)
Parse float values using the US culture if the decimal separator = '.', then using the native culture...
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
void Reset()
Resets the event clearing any signaled state.
bool WaitOne(int nMs=int.MaxValue)
Waits for the signal state to occur.
The ImageData class is a helper class used to convert between Datum, other raw data,...
static Datum GetImageDataD(Bitmap bmp, int nChannels, bool bDataIsReal, int nLabel, bool bUseLockBitmap=true, int[] rgFocusMap=null)
The GetImageDataD function converts a Bitmap into a Datum using the double type for real data.
The Log class provides general output in text form.
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
void FAIL(string str)
Causes a failure which throws an exception with the desciptive text.
double Progress
Get/set the progress associated with the Log.
The NormalizedBBox manages a bounding box used in SSD.
The RawProtoFile class writes and reads prototxt to and from a file.
static RawProto LoadFromFile(string strFileName)
Loads a RawProto from a prototxt file.
The RawProto class is used to parse and output Google prototxt file data.
The SimpleDatum class holds a data input within host memory.
ANNOTATION_TYPE
Specifies the annotation type when using annotations.
void SetLabel(int nLabel)
Sets the label.
int Channels
Return the number of channels of the data.
AnnotationGroupCollection annotation_group
When using annoations, each annotation group contains an annotation for a particular class used with ...
ANNOTATION_TYPE annotation_type
When using annotations, the annotation type specifies the type of annotation. Currently,...
int Width
Return the width of the data.
int Height
Return the height of the data.
The TarFile functions are used to expand tar files.
static int ExtractTar(string strFileName, string strOutputDir, CancelEvent evtCancel=null, Log log=null, int nExpectedTotal=0, int nIdx=0)
Extract a Tar (*.tar) file to a specified output directory.
int ID
Get/set the database ID of the item.
The DatasetDescriptor class describes a dataset which contains both a training data source and testin...
The SourceDescriptor class contains all information describing a data source.
The VOCDataLoader is used to create the VOC0712 (VOC2007 and VOC2012) dataset and load it into the da...
EventHandler OnCompleted
The OnComplete event fires once the dataset creation has completed.
EventHandler< ProgressArgs > OnProgress
The OnProgress event fires during the creation process to show the progress.
EventHandler< ProgressArgs > OnError
The OnError event fires when an error occurs.
VOCDataLoader(VOCDataParameters param, Log log, CancelEvent evtCancel)
The constructor.
bool LoadDatabase(int nCreatorID=0)
Create the dataset and load it into the database.
Contains the dataset parameters used to create the VOC0712 dataset.
string DataBatchFileTest2007
Returns the testing file 'VOCtest_06-Nov-2007.tar'.
string DataBatchFileTrain2007
Returns the training file 'VOCtrain_06-Nov-2007.tar'.
string DataBatchFileTrain2012
Specifies the training file 'OCtrain_11-May-2012.tar'.
bool ExtractFiles
Returns whether or not to extract the tar files.
The Database class manages the actual connection to the physical database using Entity Framworks from...
FORCE_LOAD
Defines the force load type.
The DatasetFactory manages the connection to the Database object.
void PutRawImageCache(int nIdx, SimpleDatum sd, int nBackgroundWritingThreadCount=0, string strDescription=null, bool bActive=true, params ParameterData[] rgParams)
Add a SimpleDatum to the RawImage cache.
int GetSourceID(string strName)
Returns the ID of a data source given its name.
int AddLabel(int nLabel, string strName, int nSrcId=0, ConnectInfo ci=null)
Add a label to the database for a data source.
void DeleteSourceData(int nSrcId=0)
Delete the data source data (images, means, results and parameters) from the database.
int AddSource(SourceDescriptor src, ConnectInfo ci=null, bool? bSaveImagesToFileOverride=null)
Adds a new data source to the database.
int AddDataset(DatasetDescriptor ds, ConnectInfo ci=null, bool? bSaveImagesToFileOverride=null)
Adds or updates the training source, testing source, dataset creator and dataset to the database.
void Close()
Close the current data source used.
void ClearImageCache(bool bSave)
Clear the RawImage cache and optionally save the images.
SourceDescriptor LoadSource(string strSource)
Load the source descriptor from a data source name.
void UpdateDatasetCounts(int nDsId, ConnectInfo ci=null)
Updates the dataset counts, and training/testing source counts.
void Open(SourceDescriptor src, int nCacheMax=500, ConnectInfo ci=null)
Open a given data source.
Specifies the LabelMap used with SSD.
Dictionary< string, int > MapToLabel(Log log, bool bStrict)
Map the names to their labels.
static LabelMap FromProto(RawProto rp)
Parses the parameter from a RawProto.
The descriptors namespace contains all descriptor used to describe various items stored within the da...
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
The MyCaffe.data namespace contains dataset creators used to create common testing datasets such as M...
The MyCaffe.db.image namespace contains all image database related classes.
The MyCaffe.param.ssd namespace contains all SSD related parameter objects that correspond to the nat...
The MyCaffe.param namespace contains parameters used to create models.
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...