In [1]:
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
import time
%matplotlib inline
import lmdb
from caffe.proto import caffe_pb2
# import caffe

In [72]:
train = lmdb.open('./data/48x48/ndsb_train_lmdb/')

image_dims = (48, 48)
with train.begin() as txn:
    c = txn.cursor()
    k0, v0 = iter(c).next()
    
datum = caffe_pb2.Datum()
def bs_to_l(bs):
    datum.ParseFromString(bs)
    return dd.label

def bs_to_im(bs):
    datum.ParseFromString(bs)
    image_dims = (datum.height, datum.width)
    im = np.array(Image.frombytes('L', image_dims, datum.data))[:, :, None]
    return im
print bs_to_l(v0)

# plt.imshow(np.squeeze(bs_to_im(v0)), cmap='gray', interpolation='none')


0

In [66]:
%%timeit
q = bs_to_im(v0)


10000 loops, best of 3: 72.9 µs per loop

In [73]:
tic = time.time()
test = lmdb.open('/data/ndsb/ndsb_test_lmdb/', readonly=True)

with test.begin() as txn:
    c = txn.cursor()
    zz = [(k.split('_')[1], bs_to_im(v), bs_to_l(v)) 
          for k, v in c]
len(zz)
print time.time() - tic


15.9699380398

In [71]:
zz[0]


Out[71]:
('/data/ndsb/test/68720.jpg', array([[[255],
         [255],
         [255],
         ..., 
         [255],
         [255],
         [255]],
 
        [[255],
         [255],
         [255],
         ..., 
         [255],
         [255],
         [255]],
 
        [[255],
         [255],
         [255],
         ..., 
         [255],
         [255],
         [255]],
 
        ..., 
        [[255],
         [255],
         [255],
         ..., 
         [255],
         [255],
         [255]],
 
        [[255],
         [255],
         [255],
         ..., 
         [255],
         [255],
         [255]],
 
        [[255],
         [255],
         [255],
         ..., 
         [255],
         [255],
         [255]]], dtype=uint8), 0)

In [34]:
plt.imshow(img, cmap='gray')


Out[34]:
<matplotlib.image.AxesImage at 0x7efff4512610>

In [36]:
# Scale to standardize input dimensions.
input_ = np.zeros((len(inputs),
    image_dims[0], image_dims[1], inputs[0].shape[2]),
    dtype=np.float32)
for ix, in_ in enumerate(inputs):
    input_[ix] = caffe.io.resize_image(in_, image_dims)

if oversample:
    # Generate center, corner, and mirrored crops.
    input_ = caffe.io.oversample(input_, crop_dims)
else:
    # Take center crop.
    center = np.array(self.image_dims) / 2.0
    crop = np.tile(center, (1, 2))[0] + np.concatenate([
        -self.crop_dims / 2.0,
        self.crop_dims / 2.0
    ])
    input_ = input_[:, crop[0]:crop[2], crop[1]:crop[3], :]


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-36-7d5d4879fadc> in <module>()
      6     input_[ix] = caffe.io.resize_image(in_, image_dims)
      7 
----> 8 if oversample:
      9     # Generate center, corner, and mirrored crops.
     10     input_ = caffe.io.oversample(input_, crop_dims)

NameError: name 'oversample' is not defined

In [20]:
datum = caffe_pb2.Datum()
datum.ParseFromString(v0)
# print datum.label

image_dims = (datum.height, datum.width)
img = Image.frombytes('L', image_dims, datum.data)
plt.imshow(img, cmap='gray', interpolation='none')


Out[20]:
<matplotlib.image.AxesImage at 0x7f9d388e5310>

In [80]:
import tools.my_io as my_io

tic = time.time()
qq = my_io.load_lmdb('./data/48x48/ndsb_train_lmdb/')
print 'Load db time:', time.time()-tic


Load db time: 2.4930639267

In [85]:
p, im, l = qq[0]
p
im
l


Out[85]:
0