Skip to content

Instantly share code, notes, and snippets.

@MatthiasWinkelmann
Last active March 5, 2024 06:19
Show Gist options
  • Save MatthiasWinkelmann/fbca5503c391c265f958f49b60af6bae to your computer and use it in GitHub Desktop.
Save MatthiasWinkelmann/fbca5503c391c265f958f49b60af6bae to your computer and use it in GitHub Desktop.
Extract PNG images from the cifar-100/cifar-10 pickled dataset
# This extracts png images from the
# packed/pickle'd cifar-100 dataset
# available at http://www.cs.toronto.edu/~kriz/cifar.html
#
# No Rights Reserved/ CC0
# Say thanks @whereismatthi on Twitter if it's useful
#
# probably requires python3
# definitely requires PyPNG: pip3 install pypng
import pickle
import os
import png
for batch in ('test', 'train'):
fpath = os.path.join('cifar-100-python', batch)
f = open(fpath, 'rb')
labels = pickle.load(open(os.path.join('cifar-100-python', 'meta'), 'rb'), encoding="ASCII")
d = pickle.load(f, encoding='bytes')
# decode utf8
d_decoded = {}
for k, v in d.items():
d_decoded[k.decode('utf8')] = v
d = d_decoded
f.close()
for i, filename in enumerate(d['filenames']):
folder = os.path.join(
'data',
'cifar100',
batch,
labels['coarse_label_names'][d['coarse_labels'][i]],
labels['fine_label_names'][d['fine_labels'][i]]
)
os.makedirs(folder, exist_ok=True)
q = d['data'][i]
print(filename)
with open(os.path.join(folder, filename.decode()), 'wb') as outfile:
png.from_array(q.reshape((32, 32, 3), order='F').swapaxes(0,1), mode='RGB').save(outfile)
@Ahmedest61
Copy link

Is there any way to do opposite like if you have number of images and you need to make pickle file containing features and labels?

@sivaji123256
Copy link

@Ahmedest61 ,I am also looking for the same. Can you pls share the code if you have already implemented it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment