caffe icon indicating copy to clipboard operation
caffe copied to clipboard

Any simple example?

Open rmanor opened this issue 12 years ago • 76 comments

Hi,

I started with Caffe and the mnist example ran well. However, I can not understand how am I suppose to use this for my own data for a classification task. What should be the data format? Where should I specify the files? How do I see the results for a test set? All of these are not mentioned at all in the documentation. Any pointers will be appreciated, thanks.

rmanor avatar Jun 27 '14 12:06 rmanor

Hi rmanor,

I'm not very knowledgeable as I just got started using Caffe as well, so folks should feel free to jump in and correct me. The documentation for the general procedure of training with your data is here: http://caffe.berkeleyvision.org/imagenet_training.html , and you will be able to do all your training by copying and modifying the files in CAFFE_ROOT_DIR/examples/imagenet, which we will call the imagenet directory. Using the imagenet architecture should yield decent out of the box results for categorizing images.

To summarize, the steps I followed to train Caffe were:

  1. Group your data into a training folder and a testing folder. Caffe will train on one set of images and test it's accuracy on the other set of images. Your data should be formatted to be 256x256 color jpeg files. For each set, create a text file specifying the categories that the pictures belong to. This text file is formatted like so,

/home/my_test_dir/picture-foo.jpg 0 /home/my_test_dir/picture-foo1.jpg 1

where picture-foo belongs to category 0 and picture-foo1 belongs to category 1.

  1. Now copy and modify create_imagenet.sh from the imagenet directory, changing the arguments to point to your folders and text files. Run create_imagenet.sh and it will generate training and testing leveldb directories. Caffe will work with these leveldb directories from now on.
  2. Copy and modify make_imagenet_mean.sh from the imagenet directory, changing the arguments to point at your spanking new leveldb folders. This will generate mean.prototxt files that caffe uses to normalize images, improving your results. I would recommend specifying absolute paths for everything to minimize headaches.
  3. Copy and modify imagenet_{deploy,solver,train,val}.prototxt. You'll want to change the source and mean_file parameters in imagenet_{train,val} to point to your leveldbs and your mean.prototxt files (again, absolute paths). You may also want to change the batch_size parameter based on the hardware that you'll be running caffe on. Lastly, change the solver.prototxt file to point to your newly modified train and val prototxt files! I believe you can leave deploy.prototxt alone.
  4. Take a step back and make sure you haven't missed anything. You will have deploy, solver, train, and val prototxt files; two image mean binaryproto files; one train_leveldb folder, and one val_leveldb folder. That's two folders and six files in total.
  5. You guessed it- copy and modify train_imagenet.sh! Point it to your solver prototxt file.
  6. Run the modified train_imagenet script. This will periodically spit out solverstate files and data files with names like caffe_train_iter_#.
  7. After training terminates, you can find a script in CAFFE_ROOT_DIR/build/tools called test_net.bin. test_net.bin will take your val.prototxt, a caffe_train_iter_# data file, and the number of testing iterations as arguments. It will tell you how your trained network is doing.

Best of luck!

dennis-chen avatar Jun 27 '14 16:06 dennis-chen

Thanks, I read imagenet example, a bit clearer. I'm trying to write some code now that converts my data into leveldb for caffe. How should I compile it? When I do g++ it doesn't find the caffee include files for some reason. Thanks.

rmanor avatar Jun 28 '14 15:06 rmanor

Hi rmanor, I would recommend against writing your own code to convert your data into a leveldb. convert_imageset.bin in the CAFFE_ROOT_DIR/build/tools directory will do this for you automatically. To see how to use convert_imageset to convert your data, take a look at the contents of create_imagenet.sh in the CAFFE_ROOT_DIR/examples/imagenet directory. If you follow the first two steps in my post above and copy and modify the create_imagenet shell script, you shouldn't have to compile anything and you will save yourself a lot of time. That said, if you truly intend to write your own code to replicate or extend the functionality of convert_imageset, I'm afraid I can't help you because I've only used built in caffe tools so far.

dennis-chen avatar Jun 28 '14 16:06 dennis-chen

I need to write my own code because my data isn't images... but if conver_imageset doesn't do any image processing then maybe I can use it anyway. I'll try, thanks.

rmanor avatar Jun 28 '14 16:06 rmanor

What kind of data do you have? Please disregard all the instructions above if you aren't using images/this isn't for computer vision purposes. I am curious because I was under the impression that convoluted neural nets were designed to recognize visual patterns. Are you re-purposing caffe for something else?

dennis-chen avatar Jun 28 '14 16:06 dennis-chen

I think convnets were designed for images, but they had success in recent years with speech, electroencephalography and more types of data. I'm looking at the source of convert_imageset and indeed it has no image-specific processing. I'll try to use it as is. Thank you. :)

  • Ran

On Sat, Jun 28, 2014 at 7:24 PM, dennis-chen [email protected] wrote:

What kind of data do you have? Please disregard all the instructions above if you aren't using images/this isn't for computer vision purposes. I am curious because I was under the impression that convoluted neural nets were designed to recognize visual patterns. Are you re-purposing caffe for something else?

— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/550#issuecomment-47431804.

rmanor avatar Jun 28 '14 17:06 rmanor

You could also use the HDF5 layer to save and read data.

Sergio

2014-06-28 10:11 GMT-07:00 rmanor [email protected]:

I think convnets were designed for images, but they had success in recent years with speech, electroencephalography and more types of data. I'm looking at the source of convert_imageset and indeed it has no image-specific processing. I'll try to use it as is. Thank you. :)

  • Ran

On Sat, Jun 28, 2014 at 7:24 PM, dennis-chen [email protected] wrote:

What kind of data do you have? Please disregard all the instructions above if you aren't using images/this isn't for computer vision purposes. I am curious because I was under the impression that convoluted neural nets were designed to recognize visual patterns. Are you re-purposing caffe for something else?

— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/550#issuecomment-47431804.

— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/550#issuecomment-47433046.

sguada avatar Jun 28 '14 17:06 sguada

@sguada I see that I create HDF5 files from MATLAB, cool! Any examples on how to use this type of layer? Thanks!

rmanor avatar Jun 28 '14 17:06 rmanor

@sergeyk could you post a simple example using a HDF5 data layer

sguada avatar Jun 30 '14 15:06 sguada

Thanks. Specifically I would like to now how the hdf5 should be built. I looked at the code and it seemed like there should be a dataset "data" and a dataset "label" in the same file, and Matlab wouldn't let me create that. Am I correct? Thanks.

rmanor avatar Jul 01 '14 20:07 rmanor

Okay, will PR an example to master soon.

sergeyk avatar Jul 01 '14 20:07 sergeyk

Thanks. Btw: my data doesn't have to be images, right? Just checking before I invest too much time :) Thanks.

  • Ran

On Tue, Jul 1, 2014 at 11:43 PM, Sergey Karayev [email protected] wrote:

Okay, will PR an example to master soon.

— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/550#issuecomment-47707505.

rmanor avatar Jul 01 '14 20:07 rmanor

Correct, it can be anything.

On Tue, Jul 1, 2014 at 1:46 PM, rmanor [email protected] wrote:

Thanks. Btw: my data doesn't have to be images, right? Just checking before I invest too much time :) Thanks.

  • Ran

On Tue, Jul 1, 2014 at 11:43 PM, Sergey Karayev [email protected]

wrote:

Okay, will PR an example to master soon.

— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/550#issuecomment-47707505.

— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/550#issuecomment-47707759.

sergeyk avatar Jul 01 '14 20:07 sergeyk

Thank you, I appreciate the help from all of you.

  • Ran

On Tue, Jul 1, 2014 at 11:47 PM, Sergey Karayev [email protected] wrote:

Correct, it can be anything.

On Tue, Jul 1, 2014 at 1:46 PM, rmanor [email protected] wrote:

Thanks. Btw: my data doesn't have to be images, right? Just checking before I invest too much time :) Thanks.

  • Ran

On Tue, Jul 1, 2014 at 11:43 PM, Sergey Karayev < [email protected]>

wrote:

Okay, will PR an example to master soon.

— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/550#issuecomment-47707505.

— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/550#issuecomment-47707759.

— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/550#issuecomment-47707937.

rmanor avatar Jul 01 '14 20:07 rmanor

@rmanor sorry i haven't got to packaging up a notebook example, but please consider https://github.com/BVLC/caffe/blob/master/src/caffe/test/test_data/generate_sample_data.py

Running python generate_sample_data.py will generate the test hdf5 files that you're interested in.

This example creates a simple dataset and is used in https://github.com/BVLC/caffe/blob/master/src/caffe/test/test_hdf5data_layer.cpp

I am not sure how to create this example in Matlab, but it should be equally easy.

sergeyk avatar Jul 03 '14 20:07 sergeyk

@sergeyk Thanks!

rmanor avatar Jul 04 '14 12:07 rmanor

Hey, Look at the first post by dennis-chen, I used his method of using test_net.bin . That worked for me... I'm trying now to also classify by using a python script in hope that it will be easier.

  • Ran

On Wed, Jul 9, 2014 at 5:02 AM, wusx11 [email protected] wrote:

@rmanor https://github.com/rmanor Hi rmanor, which step are you reaching now? I get into using caffe recently and have similar confusion... I have already use caffe to train my own data, and I have already got a .solverstate file. But I'm not sure how to use this file to classify a new input? I mean how to use the system I have already trained? Thanks!

— Reply to this email directly or view it on GitHub https://github.com/BVLC/caffe/issues/550#issuecomment-48421716.

rmanor avatar Jul 09 '14 05:07 rmanor

Will be resolved by #691

sergeyk avatar Jul 14 '14 16:07 sergeyk

The documentation link in @dennis-chen's first post is broken. I think it should be http://caffe.berkeleyvision.org/gathered/examples/imagenet.html

roseperrone avatar Nov 17 '14 15:11 roseperrone

Thanks a lot @dennis-chen for your post. It was really helpful! Do you any similar post for testing the data? I want to test an image with the learned model using python wrapper. I am editing the classifier.py file in CAFFE_ROOT/python to classify the test image, but there are some strange errors. Any help in this regard would be really useful.

gpulkitg avatar Nov 19 '14 01:11 gpulkitg