Caffe image formats

3,908 views
Skip to first unread message
Assigned to tianjin...@gmail.com by me

Patrick Grossmann

unread,
Feb 6, 2015, 2:41:25 PM2/6/15
to caffe...@googlegroups.com
Dear all,

I am trying to learn a deep convolution network with Caffe, and for some reasons my image input data are required to be .tiff files. I tried the IMAGE_DATA layer which failed, so I converted all the images to lmdb using the convert_imageset tool. My training runs without errors on these lmdb folders, but I am unsure whether the convert_imageset file actually understands .tiff files. Could I get an confirmation that Caffe understands my lmdb files correctly, please?

Thank you very much!

Luke Yeager

unread,
Feb 9, 2015, 8:08:03 PM2/9/15
to caffe...@googlegroups.com
You should be able to tell whether it's working or not based on the accuracy that you get during the test phase (look for lines like "Test net output #0: accuracy = 0.0362").

It looks like caffe can handle any image format that OpenCV can handle, since that is the library they use to read images.

Yoann

unread,
Feb 10, 2015, 10:00:18 AM2/10/15
to caffe...@googlegroups.com
As mentioned by Luke you can check the accuracy or check directly in the lmdb file if the data has been written correctly.
Here is a python script that can extract an image (actually the image associated with the key 1 in the lmdb file in my example) and save it as png to be able to visualize one image stored in the lmdb file.
PS: the last lines to reshape the data may be different depending on how you have converted your images in the datum format

import lmdb
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

# Make sure that caffe is on the python path:
caffe_root = '../'  # this file is expected to be in {caffe_root}/examples
import sys
sys.path.insert(0, caffe_root + 'python')

import caffe

db_path = '../data/liris-accede/train_data_lmdb/'

lmdb_env = lmdb.open(db_path)  # equivalent to mdb_env_open()
lmdb_txn = lmdb_env.begin()  # equivalent to mdb_txn_begin()
lmdb_cursor = lmdb_txn.cursor()  # equivalent to mdb_cursor_open()
lmdb_cursor.get('{:0>10d}'.format(1)) #  get the data associated with the 'key' 1, change the value to get other images
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))
image = image[:, :, (2, 1, 0)]
image = image.astype(np.uint8)

mpimg.imsave('out.png', image)

Patrick Grossmann

unread,
Feb 10, 2015, 2:14:26 PM2/10/15
to caffe...@googlegroups.com
Dear Luke and Yoann, thank you very much for pointing out how to confirm everything was read correctly into Caffe! Thank you, Yoann, also for the script, which worked almost out of the box for me!

Matthew Roos

unread,
Mar 1, 2015, 1:17:03 PM3/1/15
to caffe...@googlegroups.com
I tried to do this, using Yoann's code, but either I'm not actually opening the lmdb or something similar is occuring.  My only change to Yoann's code is to define the path of the mnist lmdb:
db_path = '/home/mroos/caffe-master/examples/mnist/mnist_train_lmdb/'

But after trying to open and read from the lmdb, the returned value and key are empty strings:

>>> lmdb_cursor = lmdb_txn.cursor()  # equivalent to mdb_cursor_open()
>>> lmdb_cursor.get('{:0>10d}'.format(1)) #  get the data associated with the 'key' 1, change the value to get other images
>>> value = lmdb_cursor.value()
>>> key = lmdb_cursor.key()
>>> value
''
>>> key
''

I'm not getting any errors. I'm a novice to python and know almost nothing about DBs, so any suggestions would be appreciated!

Yoann

unread,
Mar 2, 2015, 4:39:57 AM3/2/15
to caffe...@googlegroups.com
Maybe the key "{:0>10d}'.format(1)" doesn't exist in MNIST lmdb.
You can try to pick the first key from the lmdb file using something like "lmdb_cursor.first()", at least to understand how the keys have been created for your lmdb file (you can iterate using next()).

Please look at http://lmdb.readthedocs.org/en/release/ for the doc

niey...@gmail.com

unread,
Jul 1, 2015, 11:26:02 PM7/1/15
to caffe...@googlegroups.com
can i extract the image from lmdb file as a string of bytes directly,rather than the image?~

在 2015年2月10日星期二 UTC+8下午11:00:18,Yoann写道:

Jeremy Pinto

unread,
Jun 6, 2016, 4:58:03 PM6/6/16
to Caffe Users
Hello Yoann, is there a reason why you transpose the images twice in a row (context) : 

image = np.transpose(image, (1, 2, 0))
image = image[:, :, (2, 1, 0)]
seems to me like that just undoes the initial transpose?
 
Reply all
Reply to author
Forward
0 new messages