facebookarchive / caffe2

Caffe2 is a lightweight, modular, and scalable deep learning framework.
https://caffe2.ai
Apache License 2.0
8.42k stars 1.95k forks source link

C++ tutorials? #351

Open charlesrwest opened 7 years ago

charlesrwest commented 7 years ago

Hello! First off, I am really impressed by the work that has been done so far and like the changes from caffe 1.

If I may ask, are there any plans to have any tutorials on how to do basic operations in C++ or a section/split in the website for python vs c++ tutorials? Caffe 1 has a C++ API, but it is completely undocumented and there are not really many tutorials for it (https://medium.com/@shiyan/caffe-c-helloworld-example-with-memorydata-input-20c692a82a22).

I am fairly familiar with protobuf and can parse the workflow to hack things together, but it takes a rather large amount of digging through the source code to determine how to do relatively basic things (which are covered in python tutorials). I understand that Python is the dominate language in machine learning, but it seems odd to more or less completely ignore one of the other most popular ones (C/C++ in 2nd/3rd place according to some lists: https://www.techworm.net/2017/03/top-20-popular-programming-languages-2017.html).

I would be happy to help if someone is willing to show me where to post and double check to make sure that I am not leading the user base horribly astray.

Yangqing commented 7 years ago

This is definitely a great idea. Indeed we are short on C++ tutorials right now, while in a lot of products we are using C++ directly instead of Python for apparent reasons. Will start a project and do more C++ examples - something like nvidia's CUDA samples.

bcaine commented 7 years ago

Awesome @Yangqing. I haven't dug completely through the source yet, but I didn't see this on the website. Is there a simple forward pass C++ example somewhere?

Basically just loading a network with pretrained weights, passing data (say images) into memory, and pulling out the result from the last layer?

Thanks!

Yangqing commented 7 years ago

Ah, for that the predictor verifier code might be interesting:

https://github.com/caffe2/caffe2/blob/master/caffe2/binaries/predictor_verifier.cc

and also the predictor test that shows how we do I/Os into and out of the predictor:

https://github.com/caffe2/caffe2/blob/5bd6b21ddfd9c5cc87c528dab1c78e7ab0f49259/caffe2/core/predictor_test.cc

mfojtak commented 7 years ago

+1 for this enhancement

It would also be great to have cling based C++ jupyter notebooks with tutorials. I have no problem creating them but there is no documentation on how to use Caffe2 from C++ in the first place.

crohkohl commented 7 years ago

:+1:

What I always loved about caffe was the "C++ first" style of development. Maybe I am wrong, but for me personally it is really sad to see that it has changed into yet another python driven framework. It would be great to have an example how to train a network using plain old simple C++ without going through all the python hassle.

However, I really like the new library design. Good work!

charlesrwest commented 7 years ago

Agreed. I'm having some build issues with the library on my GPU machine, but I intend to try to reverse engineer the MNIST example. It appears most of what is called has a related C++ class, with the exception of the LENET convenience wrapper. Looking at the resulting prototext from the python example should make it possible to make a functional example without that.

ThatAIGeek commented 7 years ago

When they are available I might also help to create Cling notebooks, so looking forward to them

jasjuang commented 7 years ago

+1 for this

It would so nice to have a C++ tutorial

Soledad89 commented 7 years ago

Great, C++ tutorial will help me a lot.

charlesrwest commented 7 years ago

I did a little work on replicating the MNIST tutorial in C++. The main problem I am running into is that there is a set of Python helper functions used to define the convolutional network architectures which are not replicated in C++. If anyone is able to convert these functions, it would go a long way toward having side by side tutorials.

In the mean time, I have determined (and commented) how to load and do some training on the network architectures that have been previously created (as pbtxt by those same python functions).

I've attached the source code and associated network files. Please tell me what you think.

tutorialCodeAndNetworkDefinitions.zip

leovandriel commented 7 years ago

This might get you up to speed:

https://github.com/leonardvandriel/caffe2_cpp_tutorial

It covers the Intro, Toy, Squeezenet and MNIST tutorials. Let me know if there's any particular tutorial you're interested in getting done as well.

charlesrwest commented 7 years ago

Huzzah! Truly you are a saint among men. Do you think there is any way we could get this on the Caffe2 site?

Seriously, nice work.

leovandriel commented 7 years ago

I would be fine with it moving there.

At this point I find the Caffe2 docs and tutorials fairly messy and incomplete, so perhaps it good to have this grow on the side for a little bit.

charlesrwest commented 7 years ago

I've been pondering the best way to make helper classes in c++ similar to what there are in Python.

What I eventually settled on is a set of virtual classes based on a "ComputeModuleDefinition". At the most basic level, a compute module can stand in for an operator and switch out what is needed depending on the network mode (such as "TRAIN", "TEST" or "DEPLOY"). It also provides one or more initialization operators on demand. A good example is "SoftMax" and "SoftMaxWithLoss". In general, you only want the loss part when you are doing training or testing. Defining a compute module allows which operator ends up in the network to be dynamic on the intention for the network.

A second important piece of the concept is a virtual class derived from a ComputeModuleDefinition that allows multiple compute modules to be grouped together to for a single module (with most of the boiler plate being automatically generated via inheritance). This allows you to define complex building blocks, such a resnet module or a GoogLenet section without having to do a ton of copy and pasting. In my example code, I define a module which allows you to make fully connected networks of arbitrary depth and width as a single module.

Defining a few virtual functions also allows you to do some nice automatic generation, such as automatically generating all of the ADAM operators needed to do training for a network and gradients.

Please let me know what you guys think. I believe this might be useful for working with Caffe2 in C++, but I would be interested to know what others are doing.

Links: Learning a Sine wave using modules: https://github.com/charlesrwest/Caffe2ComputeModules/blob/master/src/executables/trainSineWave/main.cpp

Compute Module Definition: https://github.com/charlesrwest/Caffe2ComputeModules/blob/master/src/library/ComputeModuleDefinition.hpp

Composite Compute Module Definition: https://github.com/charlesrwest/Caffe2ComputeModules/blob/master/src/library/CompositeComputeModuleDefinition.hpp

jwatte commented 6 years ago

@leonardvandriel What is the "NetUtil" class your MNIST C++ example uses? I can't find it in the Caffe2 repo.

leovandriel commented 6 years ago

It's part of the caffe2_cpp_tutorial repo, see net.h and net.cc. I put it in the same namespace and similar include path, which is maybe a bit confusing. It's like an extension to the Caffe2 repo.

ahkarami commented 6 years ago

Really Caffe2 requires a nice C++ tutorial (like PyTorch which has some great ones in python).

Crefeda commented 6 years ago

Hi,

Is there any tutorial on how to use speed_benchmark.cc or caffe2_benchmark.cc. I am specifically looking to pass input images, convert it into the format for the model to ingest and output a class probability. Any pointers would be of great help.