Skip to content

Instantly share code, notes, and snippets.

@Alescontrela
Created June 15, 2018 04:40
Show Gist options
  • Save Alescontrela/1c2ba2e320050d0a8b4e06206e1e279b to your computer and use it in GitHub Desktop.
Save Alescontrela/1c2ba2e320050d0a8b4e06206e1e279b to your computer and use it in GitHub Desktop.
def extract_data(filename, num_images, IMAGE_WIDTH):
'''
Extract images by reading the file bytestream. Reshape the read values into a 3D matrix of dimensions [m, h, w], where m
is the number of training examples.
'''
print('Extracting', filename)
with gzip.open(filename) as bytestream:
bytestream.read(16)
buf = bytestream.read(IMAGE_WIDTH * IMAGE_WIDTH * num_images)
data = np.frombuffer(buf, dtype=np.uint8).astype(np.float32)
data = data.reshape(num_images, IMAGE_WIDTH*IMAGE_WIDTH)
return data
def extract_labels(filename, num_images):
'''
Extract label into vector of integer values of dimensions [m, 1], where m is the number of images.
'''
print('Extracting', filename)
with gzip.open(filename) as bytestream:
bytestream.read(8)
buf = bytestream.read(1 * num_images)
labels = np.frombuffer(buf, dtype=np.uint8).astype(np.int64)
return labels
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment