6using System.Collections.Generic;
12using System.Threading.Tasks;
41 public event EventHandler<ProgressArgs>
OnError;
56 m_extractor.
OnProgress += m_extractor_OnProgress;
57 m_extractor.
OnError += m_extractor_OnError;
61 m_evtCancel = evtCancel;
65 private void m_extractor_OnError(
object sender,
ProgressArgs e)
71 private void m_extractor_OnProgress(
object sender, ProgressArgs e)
77 private string dataset_name
79 get {
return "MNIST"; }
94 List<Tuple<byte[], int>> rgTrainImg;
95 List<Tuple<byte[], int>> rgTestImg;
99 reportProgress(nIdx, nTotal,
"Loading " + dataset_name +
" database...");
102 string strExportFolder =
null;
106 strExportFolder = m_param.
ExportPath.TrimEnd(
'\\') +
"\\";
107 if (!Directory.Exists(strExportFolder))
108 Directory.CreateDirectory(strExportFolder);
111 string strTrainSrc =
"training";
116 strTrainSrc = dataset_name +
"." + strTrainSrc;
122 if (!loadFile(factory, rgTrainImg, m_extractor.
Channels, m_extractor.
Height, m_extractor.
Width, strTrainSrc, strExportFolder))
125 string strTestSrc =
"testing";
128 strTestSrc = dataset_name +
"." + strTestSrc;
134 if (!loadFile(factory, rgTestImg, m_extractor.
Channels, m_extractor.
Height, m_extractor.
Width, strTestSrc, strExportFolder))
148 catch (Exception excpt)
159 private bool loadFile(
DatasetFactory factory, List<Tuple<
byte[],
int>> rgData,
int nC,
int nH,
int nW,
string strSourceName,
string strExportPath)
161 if (strExportPath !=
null)
163 strExportPath += strSourceName;
165 if (!Directory.Exists(strExportPath))
166 Directory.CreateDirectory(strExportPath);
169 Stopwatch sw =
new Stopwatch();
171 reportProgress(0, 0,
" Source: " + strSourceName);
177 int nSrcId = factory.
AddSource(strSourceName, nC, nW, nH,
false, 0,
true);
187 Datum datum =
new Datum(
false, nC, nW, nH, -1, DateTime.MinValue,
new List<byte>(), 0,
false, -1);
188 string strAction = (m_param.
ExportToFile) ?
"exporing" :
"loading";
190 reportProgress(0, rgData.Count,
" " + strAction +
" a total of " + rgData.Count.ToString() +
" items.");
191 reportProgress(0, rgData.Count,
" (with rows: " + nH.ToString() +
", cols: " + nW.ToString() +
")");
195 List<SimpleDatum> rgImg =
new List<SimpleDatum>();
197 FileStream fsFileDesc =
null;
198 StreamWriter swFileDesc =
null;
201 string strFile = strExportPath +
"\\file_list.txt";
202 fsFileDesc = File.OpenWrite(strFile);
203 swFileDesc =
new StreamWriter(fsFileDesc);
206 for (
int i = 0; i < rgData.Count; i++)
208 rgPixels = rgData[i].Item1;
209 nLabel = rgData[i].Item2;
211 if (sw.Elapsed.TotalMilliseconds > 1000)
213 reportProgress(i, rgData.Count,
" " + strAction +
" data...");
217 datum.
SetData(rgPixels, nLabel);
221 else if (strExportPath !=
null)
222 saveToFile(strExportPath, i, datum, swFileDesc);
230 if (swFileDesc !=
null)
234 swFileDesc.Dispose();
237 fsFileDesc.Dispose();
247 reportProgress(rgData.Count, rgData.Count,
" " + strAction +
" completed.");
256 private void saveToFile(
string strPath,
int nIdx,
Datum d, StreamWriter sw)
258 string strFile = strPath.TrimEnd(
'\\') +
"\\" + getImageFileName(nIdx, d);
265 sw.WriteLine(strFile +
" " + d.
Label.ToString());
268 private string getImageFileName(
int nIdx,
SimpleDatum sd)
270 return "img_" + nIdx.ToString() +
"-" + sd.
Label.ToString() +
".png";
273 private void Log_OnWriteLine(
object sender,
LogArg e)
278 private string expandFile(
string strFile)
280 FileInfo fi =
new FileInfo(strFile);
281 string strNewFile = fi.DirectoryName;
282 int nPos = fi.Name.LastIndexOf(
'.');
285 strNewFile +=
"\\" + fi.Name.Substring(0, nPos) +
".bin";
287 strNewFile +=
"\\" + fi.Name +
".bin";
289 if (!File.Exists(strNewFile))
291 using (FileStream fs = fi.OpenRead())
293 using (FileStream fsBin = File.Create(strNewFile))
295 using (GZipStream decompStrm =
new GZipStream(fs, CompressionMode.Decompress))
297 decompStrm.CopyTo(fsBin);
306 private void reportProgress(
int nIdx,
int nTotal,
string strMsg)
309 OnProgress(
this,
new ProgressArgs(
new ProgressInfo(nIdx, nTotal, strMsg)));
312 private void reportError(
int nIdx,
int nTotal, Exception err)
315 OnError(
this,
new ProgressArgs(
new ProgressInfo(nIdx, nTotal,
"ERROR", err)));
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 Datum class is a simple wrapper to the SimpleDatum class to ensure compatibility with the origina...
The ImageData class is a helper class used to convert between Datum, other raw data,...
static Bitmap GetImage(SimpleDatum d, ColorMapper clrMap=null, List< int > rgClrOrder=null)
Converts a SimplDatum (or Datum) into an image, optionally using a ColorMapper.
The LogArg is passed as an argument to the Log::OnWriteLine event.
string Message
Returns the message logged.
The Log class provides general output in text form.
double Progress
Returns the progress value.
The SimpleDatum class holds a data input within host memory.
static SimpleDatum CalculateMean(Log log, SimpleDatum[] rgImg, WaitHandle[] rgAbort)
Calculate the mean of an array of SimpleDatum and return the mean as a new SimpleDatum.
void SetData(SimpleDatum d)
Set the data of the current SimpleDatum by copying the data of another.
int Label
Return the known label of the data.
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 MnistDataLoader is used to create the MNIST dataset and load it into the database managed by the ...
EventHandler OnCompleted
The OnComplete event fires once the dataset creation has completed.
EventHandler< ProgressArgs > OnError
The OnError event fires when an error occurs.
MnistDataLoader(MnistDataParameters param, Log log, CancelEvent evtCancel)
The constructor.
EventHandler< ProgressArgs > OnProgress
The OnProgress event fires during the creation process to show the progress.
bool LoadDatabase(int nCreatorID=0)
Create the dataset and load it into the database.
The MnistDataLoader is used to extrac the MNIST dataset to disk and load the data into the training p...
int Channels
Return the image channel count (should = 1 for black and white images).
void ExtractImages(out List< Tuple< byte[], int > > rgTrainingData, out List< Tuple< byte[], int > > rgTestingData)
Extract the images from the .bin files and save to disk
int Width
Return the image with.
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.
int Height
Return the image height.
Contains the dataset parameters used to create the MNIST dataset.
string TrainImagesFile
Specifies the training image file 'train-images-idx3-ubyte.gz'.
string ExportPath
Specifies where to export the files when 'ExportToFile' = true.
bool ExportToFile
Specifies to export the images to a folder.
Defines the arguments sent to the OnProgress and OnError events.
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.
bool SaveImageMean(SimpleDatum sd, bool bUpdate, int nSrcId=0)
Save the SimpleDatum as a RawImageMean in the database.
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.
void UpdateSourceCounts()
Saves the label cache, updates the label counts from the database and then updates the source counts ...
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 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.
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 namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...