MCPcopy Index your code
hub / github.com/tensorpack/tensorpack / extract_images

Function extract_images

tensorpack/dataflow/dataset/mnist.py:31–46  ·  view source on GitHub ↗

Extract the images into a 4D uint8 numpy array [index, y, x, depth].

(filename)

Source from the content-addressed store, hash-verified

29
30
31def extract_images(filename):
32 """Extract the images into a 4D uint8 numpy array [index, y, x, depth]."""
33 with gzip.open(filename) as bytestream:
34 magic = _read32(bytestream)
35 if magic != 2051:
36 raise ValueError(
37 'Invalid magic number %d in MNIST image file: %s' %
38 (magic, filename))
39 num_images = _read32(bytestream)
40 rows = _read32(bytestream)
41 cols = _read32(bytestream)
42 buf = bytestream.read(rows * cols * num_images)
43 data = numpy.frombuffer(buf, dtype=numpy.uint8)
44 data = data.reshape(num_images, rows, cols, 1)
45 data = data.astype('float32') / 255.0
46 return data
47
48
49def extract_labels(filename):

Callers 1

get_images_and_labelsMethod · 0.85

Calls 3

_read32Function · 0.85
openMethod · 0.80
readMethod · 0.80

Tested by

no test coverage detected