Caffe: Is it possible to read LMDB files outside (e.g. use python)?

Created on 24 Sep 2016  路  6Comments  路  Source: weiliu89/caffe

HI,

Would it be possible if I want to read LMDB from outside using a python script?

I tried to use a very basic lmdb_reader (https://gist.github.com/bearpaw/3a07f0e8904ed42f376e), but that doesn't work.

In my understanding, your lmdb stored a 'anno_datum' object therefore we cannot decode it from outside without knowing structure of 'anno_datum'. Am I correct at this point?

Really appreciate for your help.

Most helpful comment

Here is the code that I'm using to debug lmdb databases:

lmdb_file = "D:/image_datasets/CityScapes/CityScapes2016/lmdb/CityScapes2016_train_lmdb/"
lmdb_env = lmdb.open(lmdb_file)
lmdb_txn = lmdb_env.begin()
lmdb_cursor = lmdb_txn.cursor()
datum = caffe_pb2.AnnotatedDatum()

fourcc = cv2.VideoWriter_fourcc('M','P','E','G')
writer = cv2.VideoWriter('D:/dumped_lmdb.avi', fourcc, 30, (2048,1024),isColor=True)
for key, value in lmdb_cursor:
    datum.ParseFromString(value)
    data = datum.datum
    grp = datum.annotation_group

    arr = np.frombuffer(data.data, dtype='uint8')
    img = cv2.imdecode(arr, cv2.IMREAD_COLOR)
    width = img.shape[1]
    height = img.shape[0]
    for annotation in grp:
        for bbox in annotation.annotation:
            cv2.rectangle(img, (int(bbox.bbox.xmin * width), int(bbox.bbox.ymin*height)), (int(bbox.bbox.xmax*width), int(bbox.bbox.ymax*height)), (0,255,0))
    cv2.imshow('decoded image', img)
    cv2.waitKey(1)
    writer.write(img)
writer.release()

All 6 comments

You could change this to caffe_pb2.AnnotatedDatum().

Hi guys,

I did change the Datum part to Annotated Datum,
but when I try to access label or datum members, it gives me this kind of error:

Traceback (most recent call last):
  File "read_lmdb.py", line 26, in <module>
    label = datum.label
AttributeError: 'AnnotatedDatum' object has no attribute 'label'

Could you tell me which attribute of AnnotatedDatum has bounding boxes and image data?
I couldn't find where the AnnotatedDatum class is, unfortunately.
Thank you.

The attribute that you are looking for is annotation_group.
This can be found in src/caffe/proto/caffe.proto

// An extension of Datum which contains "rich" annotations.
message AnnotatedDatum {
enum AnnotationType {
BBOX = 0;
}
optional Datum datum = 1;
// If there are "rich" annotations, specify the type of annotation.
// Currently it only supports bounding box.
// If there are no "rich" annotations, use label in datum instead.
optional AnnotationType type = 2;
// Each group contains annotation for a particular class.
repeated AnnotationGroup annotation_group = 3;
}

Thank you @dtmoodie for your reply :)
Do you possibly know where the image data is stored too?

I tried to access the image data using this kind of code lines:

datum = caffe.proto.caffe_pb2.AnnotatedDatum().datum
print datum.channels, datum.height, datum.height

But it prints 0 0 0.
Is it supposed to be like this?

Here is the code that I'm using to debug lmdb databases:

lmdb_file = "D:/image_datasets/CityScapes/CityScapes2016/lmdb/CityScapes2016_train_lmdb/"
lmdb_env = lmdb.open(lmdb_file)
lmdb_txn = lmdb_env.begin()
lmdb_cursor = lmdb_txn.cursor()
datum = caffe_pb2.AnnotatedDatum()

fourcc = cv2.VideoWriter_fourcc('M','P','E','G')
writer = cv2.VideoWriter('D:/dumped_lmdb.avi', fourcc, 30, (2048,1024),isColor=True)
for key, value in lmdb_cursor:
    datum.ParseFromString(value)
    data = datum.datum
    grp = datum.annotation_group

    arr = np.frombuffer(data.data, dtype='uint8')
    img = cv2.imdecode(arr, cv2.IMREAD_COLOR)
    width = img.shape[1]
    height = img.shape[0]
    for annotation in grp:
        for bbox in annotation.annotation:
            cv2.rectangle(img, (int(bbox.bbox.xmin * width), int(bbox.bbox.ymin*height)), (int(bbox.bbox.xmax*width), int(bbox.bbox.ymax*height)), (0,255,0))
    cv2.imshow('decoded image', img)
    cv2.waitKey(1)
    writer.write(img)
writer.release()

Thank you a lot, @dtmoodie :+1:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ShadowLau picture ShadowLau  路  4Comments

stoneyang picture stoneyang  路  3Comments

Sundrops picture Sundrops  路  4Comments

alenaliu picture alenaliu  路  3Comments

zhenxing9968 picture zhenxing9968  路  7Comments