PLearn 0.1
NistDB.cc
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 // PLearn (A C++ Machine Learning Library)
00004 // Copyright (C) 1998 Pascal Vincent
00005 // Copyright (C) 1999,2000 Pascal Vincent, Yoshua Bengio and University of Montreal
00006 //
00007 
00008 // Redistribution and use in source and binary forms, with or without
00009 // modification, are permitted provided that the following conditions are met:
00010 // 
00011 //  1. Redistributions of source code must retain the above copyright
00012 //     notice, this list of conditions and the following disclaimer.
00013 // 
00014 //  2. Redistributions in binary form must reproduce the above copyright
00015 //     notice, this list of conditions and the following disclaimer in the
00016 //     documentation and/or other materials provided with the distribution.
00017 // 
00018 //  3. The name of the authors may not be used to endorse or promote
00019 //     products derived from this software without specific prior written
00020 //     permission.
00021 // 
00022 // THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
00023 // IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00024 // OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
00025 // NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00026 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
00027 // TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
00028 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00029 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00030 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00031 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00032 // 
00033 // This file is part of the PLearn library. For more information on the PLearn
00034 // library, go to the PLearn Web site at www.plearn.org
00035 
00036 
00037 
00038 /* *******************************************************      
00039  * $Id: NistDB.cc 6099 2006-07-21 03:54:03Z lamblin $
00040  * AUTHORS: Pascal Vincent
00041  * This file is part of the PLearn library.
00042  ******************************************************* */
00043 
00044 #include "NistDB.h"
00045 
00046 namespace PLearn {
00047 using namespace std;
00048 
00049 
00050 #define DO_RESCALE
00051 
00052 NistDB::NistDB(bool train)
00053     :VMatrix(60000, 28*28+1)
00054 {
00055     if(train)
00056     {
00057         imagef.open(PPath("DBDIR:MNIST/train-images-idx3-ubyte").c_str());
00058         labelf.open(PPath("DBDIR:MNIST/train-labels-idx1-ubyte").c_str());
00059         length_ = 60000;
00060     }
00061     else
00062     {
00063         //      imagef.open(DBDIR"/MNIST/test-images.idx3-ubyte");
00064         //      labelf.open(DBDIR"/MNIST/test-labels.idx1-ubyte");
00065         //      length_ = 60000;
00066         imagef.open(PPath("DBDIR:MNIST/t10k-images-idx3-ubyte").c_str());
00067         labelf.open(PPath("DBDIR:MNIST/t10k-labels-idx1-ubyte").c_str());
00068         length_ = 10000;
00069     }
00070     if(!imagef)
00071         PLERROR("In NistDB constructor could not open imagefile for reading");
00072     if(!labelf)
00073         PLERROR("In NistDB constructor could not open labelfile for reading");
00074 }
00075   
00076 real NistDB::get(int i, int j) const
00077 {
00078 #ifdef BOUNDCHECK
00079     if(i<0 || i>=length() || j<0 || j>=width())
00080         PLERROR("In NistDB::get OUT OF BOUNDS");
00081 #endif
00082     if(j==width()-1) // then read from labelf
00083     {
00084         labelf.seekg(8+i);
00085         return real(labelf.get());
00086     }
00087     else // read from imagef
00088     {
00089         imagef.seekg(16+i*(28*28)+j);
00090 #ifdef DO_RESCALE
00091         return real(imagef.get())/255.0;
00092 #else
00093         return real(imagef.get());
00094 #endif
00095     }
00096 }
00097  
00098 void NistDB::getSubRow(int i, int j, Vec v) const
00099 {
00100 #ifdef BOUNDCHECK
00101     if(i<0 || i>=length() || j<0 || j+v.length()>width())
00102         PLERROR("In NistDB::getSubRow OUT OF BOUNDS");
00103 #endif
00104   
00105     int npixelstoread = v.length();
00106     if(j+v.length()==width())
00107     {
00108         labelf.seekg(8+i);
00109         v[v.length()-1] = real(labelf.get());
00110         npixelstoread--;
00111     }
00112 
00113     if(j<width()-1)
00114     {
00115         imagef.seekg(16+i*(28*28)+j);
00116 #if __GNUC__ < 3 && !defined(WIN32)
00117         imagef.read(buf, npixelstoread);
00118 #else
00119         imagef.read(reinterpret_cast<char*>(buf), npixelstoread);
00120 #endif
00121         for(int k=0; k<npixelstoread; k++)
00122         {
00123 #ifdef DO_RESCALE
00124             v[k] = real(buf[k])/255.0;
00125 #else
00126             v[k] = real(buf[k]);
00127 #endif
00128         }
00129     }
00130 }
00131 
00132 
00133 
00134 
00135 
00136 
00137 
00138 
00139 
00140 } // end of namespace PLearn
00141 
00142 
00143 /*
00144   Local Variables:
00145   mode:c++
00146   c-basic-offset:4
00147   c-file-style:"stroustrup"
00148   c-file-offsets:((innamespace . 0)(inline-open . 0))
00149   indent-tabs-mode:nil
00150   fill-column:79
00151   End:
00152 */
00153 // vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:encoding=utf-8:textwidth=79 :
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines