PLearn 0.1
Public Member Functions | Protected Attributes
PLearn::NistDB Class Reference

#include <NistDB.h>

Inheritance diagram for PLearn::NistDB:
Inheritance graph
[legend]
Collaboration diagram for PLearn::NistDB:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 NistDB (bool train)
 Views the train data if train is true, test data otherwise.
virtual real get (int i, int j) const
 This method must be implemented in all subclasses.
virtual void getSubRow (int i, int j, Vec v) const
 It is suggested that this method be implemented in subclasses to speed up accesses (default version repeatedly calls get(i,j) which may have a significant overhead).

Protected Attributes

ifstream imagef
ifstream labelf
unsigned char buf [28 *28]

Detailed Description

Definition at line 56 of file NistDB.h.


Constructor & Destructor Documentation

PLearn::NistDB::NistDB ( bool  train)

Views the train data if train is true, test data otherwise.

Definition at line 52 of file NistDB.cc.

References imagef, labelf, PLearn::VMatrix::length_, and PLERROR.

    :VMatrix(60000, 28*28+1)
{
    if(train)
    {
        imagef.open(PPath("DBDIR:MNIST/train-images-idx3-ubyte").c_str());
        labelf.open(PPath("DBDIR:MNIST/train-labels-idx1-ubyte").c_str());
        length_ = 60000;
    }
    else
    {
        //      imagef.open(DBDIR"/MNIST/test-images.idx3-ubyte");
        //      labelf.open(DBDIR"/MNIST/test-labels.idx1-ubyte");
        //      length_ = 60000;
        imagef.open(PPath("DBDIR:MNIST/t10k-images-idx3-ubyte").c_str());
        labelf.open(PPath("DBDIR:MNIST/t10k-labels-idx1-ubyte").c_str());
        length_ = 10000;
    }
    if(!imagef)
        PLERROR("In NistDB constructor could not open imagefile for reading");
    if(!labelf)
        PLERROR("In NistDB constructor could not open labelfile for reading");
}

Member Function Documentation

real PLearn::NistDB::get ( int  i,
int  j 
) const [virtual]

This method must be implemented in all subclasses.

Returns element (i,j).

Implements PLearn::VMatrix.

Definition at line 76 of file NistDB.cc.

References imagef, labelf, PLearn::VMatrix::length(), PLERROR, and PLearn::VMatrix::width().

{
#ifdef BOUNDCHECK
    if(i<0 || i>=length() || j<0 || j>=width())
        PLERROR("In NistDB::get OUT OF BOUNDS");
#endif
    if(j==width()-1) // then read from labelf
    {
        labelf.seekg(8+i);
        return real(labelf.get());
    }
    else // read from imagef
    {
        imagef.seekg(16+i*(28*28)+j);
#ifdef DO_RESCALE
        return real(imagef.get())/255.0;
#else
        return real(imagef.get());
#endif
    }
}

Here is the call graph for this function:

void PLearn::NistDB::getSubRow ( int  i,
int  j,
Vec  v 
) const [virtual]

It is suggested that this method be implemented in subclasses to speed up accesses (default version repeatedly calls get(i,j) which may have a significant overhead).

Fills v with the subrow i lying between columns j (inclusive) and j+v.length() (exclusive).

Reimplemented from PLearn::VMatrix.

Definition at line 98 of file NistDB.cc.

References buf, imagef, labelf, PLearn::VMatrix::length(), PLearn::TVec< T >::length(), PLERROR, and PLearn::VMatrix::width().

{
#ifdef BOUNDCHECK
    if(i<0 || i>=length() || j<0 || j+v.length()>width())
        PLERROR("In NistDB::getSubRow OUT OF BOUNDS");
#endif
  
    int npixelstoread = v.length();
    if(j+v.length()==width())
    {
        labelf.seekg(8+i);
        v[v.length()-1] = real(labelf.get());
        npixelstoread--;
    }

    if(j<width()-1)
    {
        imagef.seekg(16+i*(28*28)+j);
#if __GNUC__ < 3 && !defined(WIN32)
        imagef.read(buf, npixelstoread);
#else
        imagef.read(reinterpret_cast<char*>(buf), npixelstoread);
#endif
        for(int k=0; k<npixelstoread; k++)
        {
#ifdef DO_RESCALE
            v[k] = real(buf[k])/255.0;
#else
            v[k] = real(buf[k]);
#endif
        }
    }
}

Here is the call graph for this function:


Member Data Documentation

unsigned char PLearn::NistDB::buf[28 *28] [mutable, protected]

Definition at line 62 of file NistDB.h.

Referenced by getSubRow().

ifstream PLearn::NistDB::imagef [mutable, protected]

Definition at line 60 of file NistDB.h.

Referenced by get(), getSubRow(), and NistDB().

ifstream PLearn::NistDB::labelf [mutable, protected]

Definition at line 61 of file NistDB.h.

Referenced by get(), getSubRow(), and NistDB().


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines