def read_mnist_images(fn, dtype=None):
 with open_if_filename(fn, 'rb') as f:
  (magic, number, rows, cols) = struct.unpack('>iiii', f.read(16))
  if (magic != MNIST_IMAGE_MAGIC):
   raise ValueError(('wrong magic number reading MNIST image file: ' + fn))
  array = numpy.fromfile(f, dtype='uint8').reshape((number, rows, cols))
 if dtype:
  dtype = numpy.dtype(dtype)
  if (dtype.kind is 'b'):
   array = (array >= 128)
  else:
   array = array.astype(dtype)
  if (dtype.kind in ('f', 'c')):
   array /= 255.0
 return array

def read_mnist_images ( fn , dtype = None ) : with open_if_filename ( fn , 'rb' ) as f : ( magic , number , rows , cols ) = struct . unpack ( '>iiii' , f . read ( 16 ) ) if ( magic != MNIST_IMAGE_MAGIC ) : raise ValueError ( ( 'wrong magic number reading MNIST image file: ' + fn ) ) array = numpy . fromfile ( f , dtype = 'uint8' ) . reshape ( ( number , rows , cols ) ) if dtype : dtype = numpy . dtype ( dtype ) if ( dtype . kind is 'b' ) : array = ( array >= 128 ) else : array = array . astype ( dtype ) if ( dtype . kind in ( 'f' , 'c' ) ) : array /= 255.0 return array
read mnist images from the original ubyte file format .