How to read PNG encoded images from LMDB database with python ?

2,930 views
Skip to first unread message

Erogol

unread,
Aug 25, 2015, 9:33:25 AM8/25/15
to Caffe Users
I conerted all ImageNet into PNG encoded LMDB format. However, I suspect to have something wrong for the images and I like to check whether they are in correct format and they have correct labelling. For this purpose, how can I read images from LMDB by their labels. I followed the below code but datum returns 0 width height and channel. I guess this is because the images are encoded.


import caffe
import os
import lmdb
import numpy
import matplotlib.pyplot as plt
%matplotlib inline


LMDB_PATH
= "/imagenet/ilsvrc12_train_lmdb/"


env
= lmdb.open(LMDB_PATH, readonly=True, lock=False)


visualize
= True


datum
= caffe.proto.caffe_pb2.Datum()




with env.begin() as txn:
    cursor
= txn.cursor()
   
for key, value in cursor:
       
# print(key, value)
       
# convert to datum
        datum
.ParseFromString(value)
       
# Read the datum.data
        image
= caffe.io.datum_to_array(datum)
       
#img_data = img_data.reshape(datum.channels, datum.height, datum.width)
       
if visualize:
            plt
.imshow(img_data.transpose([1,2,0]))
            plt
.show(block=False)


       
#print key

Message has been deleted

Mohamed Omran

unread,
Aug 26, 2015, 4:59:11 AM8/26/15
to Caffe Users
as
To get key and value, you want to call cursor.key() and cursor.value() respectively.

Here's some code for inspecting data inside lmdbs:

import os
import sys
import numpy as np
import lmdb
import matplotlib.pyplot as plt

caffe_root = #<CAFFE_ROOT>
sys.path.append(os.path.join(caffe_root, 'python'))
import caffe

def read_images_from_lmdb(db_path) :
    
    """
    Loops over image data in the lmdb, and displays information about each datum
    Assumes that data dimensions are as follows: (channels, height, width)
    """
    ax = plt.subplot(111)
    plt.hold(False)
    lmdb_env = lmdb.open(db_path, readonly=True)    
    with lmdb_env.begin() as lmdb_txn :
        lmdb_cursor = lmdb_txn.cursor() 
        #for it in lmdb_cursor.iternext() :
        while lmdb_cursor.next() :
            value = lmdb_cursor.value()
            key = lmdb_cursor.key()
            
            datum = caffe.proto.caffe_pb2.Datum()
            datum.ParseFromString(value)
            image = np.zeros((datum.channels, datum.height, datum.width))
            image = caffe.io.datum_to_array(datum)   
            image = np.transpose(image, (1, 2, 0))    # -> height, width, channels
            image = image[:,:,::-1]                   # BGR -> RGB
              
            print("key: ", key) 
            print("image shape: " + str(image.shape) + ", data type: " + str(image.dtype) + ", random pixel value: " +  str(image[150,150,0]))
                        
            ax.imshow(np.squeeze(image))
            plt.draw()
            plt.waitforbuttonpress()
            
    plt.show() 
    lmdb_txn.abort()
    lmdb_env.close()

    return
    

def main():

    """
    Set the db_path, and you're good to go.
    """
    
    db_path = #<DB_PATH>
    if not os.path.exists(db_path) :
        raise Exception('db not found')

    read_images_from_lmdb(db_path)
        
    return
 
   
if __name__ == "__main__" :
    main()

Erogol

unread,
Aug 26, 2015, 5:26:14 AM8/26/15
to Caffe Users
I tried your code but the problem is same. Still cannot get any values for channel, width and height. As I pointed, I believe problem is reading the string representation of encoded PNG.
Reply all
Reply to author
Forward
0 new messages