LMDB files and how they are used for caffe deep learning network - matlab

I am quite new in deep learning and I am having some problems in using the caffe deep learning network. Basically, I didn't find any documentation explaining how I can solve a series of questions and problems I am dealing right now.
Please, let me explain my situation first.
I have thousands of images and I must do a series of pre-processing operations on them. For each pre-processing operation, I have to save these pre-processed images as 4D matrices and also store a vector with the images labels. I will store this information as LMDB files that will be used as input for the caffe googlenet deep learning.
I tried to save my images as .HD5 files, but the final file size is 80GB, which is impossible to process with the memory I have.
So, the other option is using LMDB files, right? I am quite newbie in this file format and I appreciate your help in understanding how to create them in Matlab. Basically, my rookie questions are:
1- These LMDB files have extension .MDB, right? is this extension the same used by microsoft access? or the right format is .lmdb and they are different?
2- I find this solution for creating .mdb files (https://github.com/kyamagu/matlab-leveldb), does it create the file format needed by caffe?
3- For caffe, should I have to create one .mdb file for labels and other for images or both can be fields of the same .mdb file?
4- When I create an .mdb file I have to label the database fields. Can I label one field as image and other as label? does caffe understand which field means?
5- what does the function (in https://github.com/kyamagu/matlab-leveldb) database.put('key1', 'value1') and database.put('key2', 'value2') do? Should I have to save my 4-d matrices in one field and the label vector in another?

There is no connection between LMDB files and MS Access files.
As I see it you have two options:
Use the "convert_imageset" tool - it is located in caffe under the tools folder to convert a list of image files and label to lmdb.
Instead of "data layer" use "image data layer" as an input to the network. This type of layer takes a file with a list of image file names and labels as source so you don't have to build a database (another benefit for training - you can use the shuffle option and get slightly better training results)
In order to use an image data layer just replace the layer type from Data to ImageData. The source file is the path to a file containing in each line a path of an image file and the label seperated by space. For example:
/path/to/filnename.png 23
If you want to do some preprocessing of the data without saving the preprocessed file to disk you can use the transformations available by caffe (mirror and cropping) (see here for information http://caffe.berkeleyvision.org/tutorial/data.html) or implement your own DataTransformer.

Caffe doesn't use LevelDB - but it uses LMDB 'Lightning' db from Symas
You can try using this Matlab LMDB wrapper
I personally had no experience with using LMDB with Matlab, but there is nice library for doing this from Python: py-lmdb
LMDB database is a Key/Value db (similar to HashMap in Java or dict in Python). In order to store 4D matrices you need to understand the convention Caffe uses to save images into LMDB format.
This means that the best approach to convert images to LMDB for Caffe would be doing this with Caffe.
There are examples in Caffe on how to convert images into LMDB - I would try to repeat them and then modify scripts to use your images.

Related

How can I import ground truth data into Matlab for the training of a (faster) R-CNN?

I have a large, labelled, dataset which I have created and I would like to provide it to Matlab to train an R-CNN (using the faster R-CNN algorithm).
How can this be done?
The built-in labeller provided by Matlab requires that the user manually load each data sample and label it with a graphical user interface.
This is not practical for me as the set is already labelled and it contains 500,000 samples.
It should be noted, that I can control the format in which the data set is stored. So, I can create .csv files or excel files if needed.
I have tried two directions:
1. Creating a mat file, similar to the one created by the labeller.
2. Looked for ways within Matlab to import the data from .csv or excel files.
I have had no success with either methods.
For Direction 1:
Though there are many libraries that can open mat files, they are not able to open or create files similar to the Matlab ground truths because these are not simple matrices (the cells themselves contain matrices of varying dimensions that represent the bounding boxes of each classified object). Moreover, though the Matlab Level 5 file format is open source I have not been successful in using it to write my own code (C# or C++) to parse and write such files.
For Direction 2:
There are generic methods in Matlab to load .csv and excel files but I do not know how to organize these files in such a way as to produce the structure that the labeller creates and that is consumed by the fasterRCNN trainer.

Caffe is teeing me that the LMDB created by convert_imageset is empty

I am working on a CNN.
For that, I need 2 pairs of LMDB's (one for the testing, and one for training).
Each LMDB in the pair are made out of images, where one LMDB contains the GT.
So I make 4 lists of the images and feed this into convert_imageset. After that I feed the LMDB's into caffe. Caffe constructs the training net without a problem, but when it comes to the testing net, it tells me that the file is empty. Where did I go wrong?

Why do the features extracted with matcaffe_demo.m and matcaffe_batch.m for the same input are different?

I am using Caffe to extract features with matlab wrapper.I have 5011 images as test data set.I chopped all the layers after 'relu7' in 'deploy.prototxt'. I found out if you take the same image as input of matcaffe_demo.m and matcaffe_batch.m, you will get the different 4096-dim features.
Could someone tell me why?
what is the differences between you extract features from all these images one by one with matcaffe_demo.m and extract features by listing all these images with matcaffe_batch.m?
You can find the answer to this question at caffe github.
Basically, matcaffe_demo is used for classification and it averages results of 10 crops of the input image, while matcaffe_bathc uses only a single input.
Moreover, note that these m-files are no longer available in recent caffe versions.

Error using caffe Invalid input size

I tried to train my own neural net using my own imagedatabase as described in
http://caffe.berkeleyvision.org/gathered/examples/imagenet.html
However when I want to check the neural net after training on some standard images using the matlab wrapper I get the following output / error:
Done with init
Using GPU Mode
Done with set_mode
Elapsed time is 3.215971 seconds.
Error using caffe
Invalid input size
I used the matlab wrapper before to extract cnn features based on a pretrained model. It worked. So I don't think the input size of my images is the problem (They are converted to the correct size internally by the function "prepare_image").
Has anyone an idea what could be the error?
Found the solution: I was referencing the wrong ".prototxt" file (Its a little bit confusing because the files are quite similar.
So for computing features using the matlab wrapper one needs to reference the following to files in "matcaffe_demo.m":
models/bvlc_reference_caffenet/deploy.prototxt
models/bvlc_reference_caffenet/MyModel_caffenet_train_iter_450000.caffemodel
where "MyModel_caffenet_train_iter_450000.caffemodel" is the only file needed which is created during training.
In the beginning I was accidently referencing
models/bvlc_reference_caffenet/MyModel_train_val.prototxt
which was the ".prototxt" file used for training.

How can I read a text file of image intensity values and convert to a cv::Mat?

I am working on a project that requires reading intensity values of several images from a text file that has 3 lines of file header, followed by each image. Each image consists of 15 lines of header followed by the intensity values that are arranged in 48 rows, where each row has 144 tab-delimited pixel values.
I have already created a .mat file to read these into Matlab and create a structure array for each image. I'd like to use OpenCV to track features in the image sequence.
Would it make more sense to create a .cpp file that will read the text file or use OpenCV and Matlab mex files in order to accomplish my goal?
I'd recommend writing C++ code to read the file directly independent of Matlab. This way, you don't have to mess with row major vs. column major ordering and all that jazz. Also, are there specs for the image format? If it turns out to be a reasonably common format, you may be able to find an off-the-self reader/library for it.
If you want the visualization and/or other image processing capabilities of Matlab, then mex file might be a reasonable approach.