Skip to content

Instantly share code, notes, and snippets.

@twmht
Last active July 21, 2016 15:53
Show Gist options
  • Save twmht/f854453a9b657760535f168965e226ce to your computer and use it in GitHub Desktop.
Save twmht/f854453a9b657760535f168965e226ce to your computer and use it in GitHub Desktop.
from mnist import MNIST
import numpy as np
import lmdb
import caffe
mndata = MNIST('./data')
count = 0
env = lmdb.open('mnist_lmdb', map_size=1000*1000*1000)
txn = env.begin(write=True)
for image, label in mndata.load_training():
datum = caffe.proto.caffe_pb2.Datum()
datum.channels = 1
datum.height = 28
datum.width = 28
datum.data = np.array(image, dtype=np.uint8).tobytes()
datum.label = label
str_id = '{:08}'.format(count)
txn.put(str_id, datum.SerializeToString())
count = count + 1
if count % 1000 == 0:
txn.commit()
txn = env.begin(write=True)
if count % 1000 != 0:
txn.commit()
env.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment