How I use output after train lenet ?

639 views
Skip to first unread message

Antonio Paes

unread,
Apr 13, 2015, 5:18:36 PM4/13/15
to caffe...@googlegroups.com
Hy guys,


I'm noob in caffe, I managed to install caffe and compile using the commands "make", train my netwok following tutorial in caffe berkley, but i'm don't understand how I test now, in other words how I use a output after end training?

thanks!

Pastafarianist

unread,
Apr 13, 2015, 5:36:29 PM4/13/15
to caffe...@googlegroups.com
Easiest is to run from Python. I put together a sample which should eliminate all questions: https://gist.github.com/Pastafarianist/68d41ba8abff11c0d61b

вторник, 14 апреля 2015 г., 0:18:36 UTC+3 пользователь Antonio Paes написал:

Antonio Paes

unread,
Apr 13, 2015, 5:58:59 PM4/13/15
to caffe...@googlegroups.com
Hey thanks for answer, you have any example in c++?

J. Yegerlehner

unread,
Apr 13, 2015, 9:27:57 PM4/13/15
to caffe...@googlegroups.com
Caveat: I don't know if any of this is best practices. The code referenced below is a quick hack that seems to work for my purposes but that's it. You've been warned.

If you mean you want to run the net in your own code where the input is provided not from a database, but as a blob object, the process is roughly as follows.

1. Create an instance of a caffe::Net instance, providing the prototxt file path or a NetParameter instance (take your pick) to the constructor.
2. Call Net::CopyTrainedLayersFrom(), passing in the path to the .caffemodel you created when training. and
3. Call Net::Forward, passing in the input blob(s), and it will return the output blobs from the net.
4. Use the output blobs.

But there's a bit more to it. The net's prototxt  (or NetParameter, if you're not providing a prototxt file) needs to be a bit different than what you used for training: Remove the data layers, assuming we're not reading the inputs from a database, but rather providing the inputs as blobs. The name of the input is added at the top of the prototxt, along with the blob's shape (see link to example). The loss layers are removed, and the output of net is left unconnected, so it will be appear in the vector of blobs returned by Net::Forward(). Since the data layers were removed, if they contained transformation such as scaling RGB values 0->255 down to 0->1, then you will need to do that in code. You'll see code where I used caffe::DataTransformer for that.

Here's a link to example code, and partial net prototxt snippet:
https://gist.github.com/jyegerlehner/d269d8b12bc3b0273b4a

The code is more complicated than what you probably need, because the output blob in this case is another image, not category labels. And most of the code is dealing with the blob that comes out and converting it from blob->Datum->CvMat->jpg file. But if you look for the Net::Forward call and see what leads up to it, it should be pretty obvious.

I hope that's more helpful than confusing.

Antonio Paes

unread,
Apr 13, 2015, 11:43:27 PM4/13/15
to caffe...@googlegroups.com
Hi Jim, thanks man, but how you say the code is more complicated than I nedd..rsrsrs

I'm new in caffe, but so I researched the staff are using more python, I'm correct?

If yes you could point me some tutorial?

J. Yegerlehner

unread,
Apr 14, 2015, 1:31:43 AM4/14/15
to caffe...@googlegroups.com
Hi Antonio,




I'm new in caffe, but so I researched the staff are using more python, I'm correct?


Could be. But if so that's their problem :)
 

If yes you could point me some tutorial?

I don't use python, but I guess there's caffe python tutorials here:
http://caffe.berkeleyvision.org/

Scroll down to the links under "Notebook Examples". I think that stuff all looks like python.


Antonio Paes

unread,
Apr 14, 2015, 10:28:13 AM4/14/15
to caffe...@googlegroups.com
You use C++?

I prefer c++, but i do not think any example about how to use a trained network.

J. Yegerlehner

unread,
Apr 14, 2015, 1:00:14 PM4/14/15
to caffe...@googlegroups.com


On Tuesday, April 14, 2015 at 9:28:13 AM UTC-5, Antonio Paes wrote:
You use C++?

Yes
 

I prefer c++, but i do not think any example about how to use a trained network.

I gave you a link to one. Like I said, most of the code you can ignore because it's outputting images. The code for creating the net, loading the .caffemodel and forward propagating is pretty simple.

Antonio Paes

unread,
Apr 14, 2015, 1:43:23 PM4/14/15
to caffe...@googlegroups.com
Ok, i'll try use that code, thanks for now jim !

Antonio Paes

unread,
Apr 14, 2015, 4:12:18 PM4/14/15
to caffe...@googlegroups.com
Ey jim, you could help me compile that code which you send me?

J. Yegerlehner

unread,
Apr 15, 2015, 12:06:44 AM4/15/15
to caffe...@googlegroups.com

Well I wasn't recommending using that code exactly. It was useful only in that contains within it a few lines that show you how to, as I said in the first message, the following in c++:

1. Create an instance of your neural net  from your prototxt file.
 shared_ptr<Net<float> > net( new Net<float>( net_proto, TEST ) ); ```
2. Load your .caffemodel into the neural net:
net->CopyTrainedLayersFrom( model_names[i] );
3. Call Net::Forward, to generate the output from the input:  
std::vector<Blob<float>*> output = net->Forward( input ); ```

Those are the three basic things you need to do to forward prop through an already-trained neural net.

OK if you want to compile it, just put that cpp file in the caffe/tools directory and build caffe. It should build an executable, along with all the other programs in tools. I don't what you'll use it for, because as I've said, it outputs an image and that's probably not what you want to do.





Antonio Paes

unread,
Apr 17, 2015, 11:52:54 AM4/17/15
to caffe...@googlegroups.com
exactly Jim, I'm reading some tutorials to do the training with my own images, I am working with faces.

if you know of to for example take the compiled codes caffe and mount a directory?

For example, I take the necessary files to set up the network and build the network and training files, and play all in one folder. Just to separate a little know.

J. Yegerlehner

unread,
Apr 17, 2015, 7:25:55 PM4/17/15
to caffe...@googlegroups.com


For example, I take the necessary files to set up the network and build the network and training files, and play all in one folder. Just to separate a little know.

That's what I do. Then in your working directory, create a shell script that runs caffe or my own executable. All the paths to the executable and the files are of course specified as relative paths from the working directory. Or maybe I'm not understanding what you are asking.

Perhaps I led you astray. I thought you had expressed a preference for c++ approach, but if it's not obvious how to proceed, then perhaps you are better off using python. As you observed, the tutorials for net surgery and such are all written with python examples. So there's more to guide you there.

Antonio Paes

unread,
Apr 18, 2015, 9:47:03 AM4/18/15
to caffe...@googlegroups.com
Thanks Jim, I'll keep trying with c ++ same, i guess which i need read about framework more.

But you helped a lot, very thanks !
Reply all
Reply to author
Forward
0 new messages