📄 mnistdoc.cpp
字号:
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMNistDoc commands
void CMNistDoc::OnButtonOpenMnistFiles()
{
// opens the MNIST image data file and the label file
struct FILEBEGINNING
{
union
{
char raw0[ 4 ];
int nMagic;
};
union
{
char raw1[ 4 ];
UINT nItems;
};
union
{
char raw2[ 4 ];
int nRows;
};
union
{
char raw3[ 4 ];
int nCols;
};
};
if ( m_bFilesOpen != FALSE )
{
::MessageBox( NULL, _T("Files already open"), _T("Files already open"), MB_ICONEXCLAMATION );
return;
}
CWaitCursor wc;
// we need to open four files: (1) training images, (2) training labels, (3) testing images, (4) testing labels
// (1) Training images
CFileDialog fd( TRUE ); // constructs an "open" dialog; we are happy with the remaining defaults
fd.m_ofn.lpstrFilter = _T("All Files (*.*)\0*.*\0\0");
fd.m_ofn.lpstrTitle = _T("TRAINING Images");
fd.m_ofn.lpstrInitialDir = theApp.m_sModulePath;
if ( fd.DoModal() != IDOK ) return;
// store chosen directory so that subsequent open-file dialogs can use it
CString newPath = fd.GetPathName();
::PathMakePretty( newPath.GetBuffer(255) );
::PathRemoveFileSpec( newPath.GetBuffer(255) );
newPath.ReleaseBuffer();
// open the file
CFileException fe;
if ( m_fileTrainingImages.Open( (LPCTSTR)fd.GetPathName(), CFile::modeRead | CFile::shareDenyNone, &fe ) != 0 )
{
// opened successfully
// check for expected magic number and for expected dimensionality
FILEBEGINNING fileBegin;
m_fileTrainingImages.Read( fileBegin.raw0, sizeof( fileBegin ) );
// convert endian-ness (using winsock functions for convenience)
fileBegin.nMagic = ::ntohl( fileBegin.nMagic );
fileBegin.nItems = ::ntohl( fileBegin.nItems );
fileBegin.nRows = ::ntohl( fileBegin.nRows );
fileBegin.nCols = ::ntohl( fileBegin.nCols );
// check against expected values
if ( fileBegin.nMagic != ::GetPreferences().m_nMagicTrainingImages ||
fileBegin.nItems != ::GetPreferences().m_nItemsTrainingImages ||
fileBegin.nRows != ::GetPreferences().m_nRowsImages ||
fileBegin.nCols != ::GetPreferences().m_nColsImages )
{
// file is not configured as expected
CString msg;
msg.Format( _T("File header for training images contains unexpected values as follows\n")
_T("Magic number: got %i, expected %i \n")
_T("Number of items: got %i, expected %i \n")
_T("Number of rows: got %i, expected %i \n")
_T("Number of columns: got %i, expected %i \n")
_T("\nCheck integrity of file for training images and/or adjust the expected values in the INI file"),
fileBegin.nMagic, ::GetPreferences().m_nMagicTrainingImages,
fileBegin.nItems, ::GetPreferences().m_nItemsTrainingImages,
fileBegin.nRows, ::GetPreferences().m_nRowsImages,
fileBegin.nCols, ::GetPreferences().m_nColsImages );
::MessageBox( NULL, msg, _T("Unexpected Format"), MB_OK|MB_ICONEXCLAMATION );
// close all files
m_fileTrainingImages.Close();
return;
}
}
else
{
// could not open image file
TRACE(_T("%s(%i): Could not open file for training images \n"), __FILE__,__LINE__);
CString msg;
TCHAR szCause[255] = {0};
fe.GetErrorMessage(szCause, 254);
msg.Format( _T("Image file for training images could not be opened\n")
_T("File name: %s\nReason: %s\nError code: %d"),
fe.m_strFileName, szCause, fe.m_cause );
::MessageBox( NULL, msg, _T("Failed to open file for training images"), MB_OK|MB_ICONEXCLAMATION );
return;
}
// (2) Training labels
CFileDialog fd2( TRUE ); // constructs an "open" dialog; we are happy with the remaining defaults
fd2.m_ofn.lpstrFilter = _T("All Files (*.*)\0*.*\0\0");
fd2.m_ofn.lpstrTitle = _T("TRAINING Labels");
fd2.m_ofn.lpstrInitialDir = newPath;
if ( fd2.DoModal() != IDOK )
{
// close the images file too
m_fileTrainingImages.Close();
return;
}
// open the file
if ( m_fileTrainingLabels.Open( (LPCTSTR)fd2.GetPathName(), CFile::modeRead | CFile::shareDenyNone, &fe ) != 0 )
{
// opened successfully
// check for expected magic number and for expected dimensionality
FILEBEGINNING fileBegin;
m_fileTrainingLabels.Read( fileBegin.raw0, sizeof( fileBegin ) );
// convert endian-ness (using winsock functions for convenience)
fileBegin.nMagic = ::ntohl( fileBegin.nMagic );
fileBegin.nItems = ::ntohl( fileBegin.nItems );
// check against expected values
if ( fileBegin.nMagic != ::GetPreferences().m_nMagicTrainingLabels ||
fileBegin.nItems != ::GetPreferences().m_nItemsTrainingLabels )
{
// file is not configured as expected
CString msg;
msg.Format( _T("File header for training labels contains unexpected values as follows\n")
_T("Magic number: got %i, expected %i \n")
_T("Number of items: got %i, expected %i \n")
_T("\nCheck integrity of file for training labels and/or adjust the expected values in the INI file"),
fileBegin.nMagic, ::GetPreferences().m_nMagicTrainingLabels,
fileBegin.nItems, ::GetPreferences().m_nItemsTrainingLabels );
::MessageBox( NULL, msg, _T("Unexpected Format"), MB_OK|MB_ICONEXCLAMATION );
// close all files
m_fileTrainingImages.Close();
m_fileTrainingLabels.Close();
return;
}
}
else
{
// could not open label file
TRACE(_T("%s(%i): Could not open file for training labels \n"), __FILE__,__LINE__);
CString msg;
TCHAR szCause[255] = {0};
fe.GetErrorMessage(szCause, 254);
msg.Format( _T("Label file for training labels could not be opened\n")
_T("File name: %s\nReason: %s\nError code: %d"),
fe.m_strFileName, szCause, fe.m_cause );
::MessageBox( NULL, msg, _T("Failed to open file for training labels"), MB_OK|MB_ICONEXCLAMATION );
// close the already-opened files too
m_fileTrainingImages.Close();
return;
}
// (3) Testing images
CFileDialog fd3( TRUE ); // constructs an "open" dialog; we are happy with the remaining defaults
fd3.m_ofn.lpstrFilter = _T("All Files (*.*)\0*.*\0\0");
fd3.m_ofn.lpstrTitle = _T("TESTING Images");
fd3.m_ofn.lpstrInitialDir = newPath;
if ( fd3.DoModal() != IDOK )
{
// close the already-opened files too
m_fileTrainingLabels.Close();
m_fileTrainingImages.Close();
return;
}
// open the file
if ( m_fileTestingImages.Open( (LPCTSTR)fd3.GetPathName(), CFile::modeRead | CFile::shareDenyNone, &fe ) != 0 )
{
// opened successfully
// check for expected magic number and for expected dimensionality
FILEBEGINNING fileBegin;
m_fileTestingImages.Read( fileBegin.raw0, sizeof( fileBegin ) );
// convert endian-ness (using winsock functions for convenience)
fileBegin.nMagic = ::ntohl( fileBegin.nMagic );
fileBegin.nItems = ::ntohl( fileBegin.nItems );
fileBegin.nRows = ::ntohl( fileBegin.nRows );
fileBegin.nCols = ::ntohl( fileBegin.nCols );
// check against expected values
if ( fileBegin.nMagic != ::GetPreferences().m_nMagicTestingImages ||
fileBegin.nItems != ::GetPreferences().m_nItemsTestingImages ||
fileBegin.nRows != ::GetPreferences().m_nRowsImages ||
fileBegin.nCols != ::GetPreferences().m_nColsImages )
{
// file is not configured as expected
CString msg;
msg.Format( _T("File header for testing images contains unexpected values as follows\n")
_T("Magic number: got %i, expected %i \n")
_T("Number of items: got %i, expected %i \n")
_T("Number of rows: got %i, expected %i \n")
_T("Number of columns: got %i, expected %i \n")
_T("\nCheck integrity of file for testing images and/or adjust the expected values in the INI file"),
fileBegin.nMagic, ::GetPreferences().m_nMagicTestingImages,
fileBegin.nItems, ::GetPreferences().m_nItemsTestingImages,
fileBegin.nRows, ::GetPreferences().m_nRowsImages,
fileBegin.nCols, ::GetPreferences().m_nColsImages );
::MessageBox( NULL, msg, _T("Unexpected Format"), MB_OK|MB_ICONEXCLAMATION );
// close all files
m_fileTestingImages.Close();
m_fileTrainingLabels.Close();
m_fileTrainingImages.Close();
return;
}
}
else
{
// could not open image file
TRACE(_T("%s(%i): Could not open file for testing images \n"), __FILE__,__LINE__);
CString msg;
TCHAR szCause[255] = {0};
fe.GetErrorMessage(szCause, 254);
msg.Format( _T("Image file for testing images could not be opened\n")
_T("File name: %s\nReason: %s\nError code: %d"),
fe.m_strFileName, szCause, fe.m_cause );
::MessageBox( NULL, msg, _T("Failed to open file for testing images"), MB_OK|MB_ICONEXCLAMATION );
// close the already-opened files too
m_fileTrainingLabels.Close();
m_fileTrainingImages.Close();
return;
}
// (4) Testing labels
CFileDialog fd4( TRUE ); // constructs an "open" dialog; we are happy with the remaining defaults
fd4.m_ofn.lpstrFilter = _T("All Files (*.*)\0*.*\0\0");
fd4.m_ofn.lpstrTitle = _T("TESTING Labels");
fd4.m_ofn.lpstrInitialDir = newPath;
if ( fd4.DoModal() != IDOK )
{
// close the already-opened files too
m_fileTestingImages.Close();
m_fileTrainingLabels.Close();
m_fileTrainingImages.Close();
return;
}
// open the file
if ( m_fileTestingLabels.Open( (LPCTSTR)fd4.GetPathName(), CFile::modeRead | CFile::shareDenyNone, &fe ) != 0 )
{
// opened successfully
// check for expected magic number and for expected dimensionality
FILEBEGINNING fileBegin;
m_fileTestingLabels.Read( fileBegin.raw0, sizeof( fileBegin ) );
// convert endian-ness (using winsock functions for convenience)
fileBegin.nMagic = ::ntohl( fileBegin.nMagic );
fileBegin.nItems = ::ntohl( fileBegin.nItems );
// check against expected values
if ( fileBegin.nMagic != ::GetPreferences().m_nMagicTestingLabels ||
fileBegin.nItems != ::GetPreferences().m_nItemsTestingLabels )
{
// file is not configured as expected
CString msg;
msg.Format( _T("File header for testing labels contains unexpected values as follows\n")
_T("Magic number: got %i, expected %i \n")
_T("Number of items: got %i, expected %i \n")
_T("\nCheck integrity of file for testing labels and/or adjust the expected values in the INI file"),
fileBegin.nMagic, ::GetPreferences().m_nMagicTestingLabels,
fileBegin.nItems, ::GetPreferences().m_nItemsTestingLabels );
::MessageBox( NULL, msg, _T("Unexpected Format"), MB_OK|MB_ICONEXCLAMATION );
// close all files
m_fileTestingLabels.Close();
m_fileTestingImages.Close();
m_fileTrainingImages.Close();
m_fileTrainingLabels.Close();
return;
}
}
else
{
// could not open label file
TRACE(_T("%s(%i): Could not open file for testing labels \n"), __FILE__,__LINE__);
CString msg;
TCHAR szCause[255] = {0};
fe.GetErrorMessage(szCause, 254);
msg.Format( _T("Label file for testing labels could not be opened\n")
_T("File name: %s\nReason: %s\nError code: %d"),
fe.m_strFileName, szCause, fe.m_cause );
::MessageBox( NULL, msg, _T("Failed to open file for testing labels"), MB_OK|MB_ICONEXCLAMATION );
// close all already-opened files
m_fileTestingImages.Close();
m_fileTrainingImages.Close();
m_fileTrainingLabels.Close();
return;
}
// all four files are opened, and all four contain expected header information
m_bFilesOpen = TRUE;
ASSERT( g_cImageSize == 28 );
}
void CMNistDoc::OnButtonCloseMnistFiles()
{
CloseMnistFiles();
}
void CMNistDoc::CloseMnistFiles()
{
m_fileTestingImages.Close();
m_fileTestingLabels.Close();
m_fileTrainingImages.Close();
m_fileTrainingLabels.Close();
m_bFilesOpen = FALSE;
}
double CMNistDoc::GetCurrentEta()
{
return m_NN.m_etaLearningRate;
}
double CMNistDoc::GetPreviousEta()
{
// provided because threads might change the current eta before we are able to read it
return m_NN.m_etaLearningRatePrevious;
}
UINT CMNistDoc::GetCurrentTrainingPatternNumber( BOOL bFromRandomizedPatternSequence /* =FALSE */ )
{
// returns the current number of the training pattern, either from the straight sequence, or from
// the randomized sequence
UINT iRet;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -