Skip to content

Instantly share code, notes, and snippets.

@joeyism
Last active August 11, 2018 19:00
Show Gist options
  • Save joeyism/6744f18be6349587e53bf5827335627c to your computer and use it in GitHub Desktop.
Save joeyism/6744f18be6349587e53bf5827335627c to your computer and use it in GitHub Desktop.
import numpy as np
import pickle
import os
def __extract_file__(fname):
with open(fname, 'rb') as fo:
d = pickle.load(fo, encoding='bytes')
return d
def __unflatten_image__(img_flat):
img_R = img_flat[0:1024].reshape((32, 32))
img_G = img_flat[1024:2048].reshape((32, 32))
img_B = img_flat[2048:3072].reshape((32, 32))
img = np.dstack((img_R, img_G, img_B))
return img
def __extract_reshape_file__(fname):
res = []
d = __extract_file__(fname)
images = d[b"data"]
labels = d[b"labels"]
for image, label in zip(images, labels):
res.append((__unflatten_image__(image), label))
return res
def get_images_from(dir):
files = [f for f in os.listdir(dir) if f.startswith("data_batch")]
res = []
for f in files:
res = res + __extract_reshape_file__(os.path.join(dir, f))
return res
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment