New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ tutorials? #351

Open
charlesrwest opened this Issue Apr 21, 2017 · 18 comments

Comments

Projects
None yet
@charlesrwest

charlesrwest commented Apr 21, 2017

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

This comment has been minimized.

Show comment
Hide comment
@Yangqing

Yangqing Apr 21, 2017

Member

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.

Member

Yangqing commented Apr 21, 2017

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

This comment has been minimized.

Show comment
Hide comment
@bcaine

bcaine Apr 21, 2017

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!

bcaine commented Apr 21, 2017

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

This comment has been minimized.

Show comment
Hide comment
@Yangqing

Yangqing Apr 21, 2017

Member

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

Member

Yangqing commented Apr 21, 2017

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

This comment has been minimized.

Show comment
Hide comment
@mfojtak

mfojtak Apr 24, 2017

+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.

mfojtak commented Apr 24, 2017

+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

This comment has been minimized.

Show comment
Hide comment
@crohkohl

crohkohl Apr 24, 2017

👍

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!

crohkohl commented Apr 24, 2017

👍

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

This comment has been minimized.

Show comment
Hide comment
@charlesrwest

charlesrwest Apr 25, 2017

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.

charlesrwest commented Apr 25, 2017

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.

@Liberus

This comment has been minimized.

Show comment
Hide comment
@Liberus

Liberus May 12, 2017

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

Liberus commented May 12, 2017

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

@jasjuang

This comment has been minimized.

Show comment
Hide comment
@jasjuang

jasjuang Jun 5, 2017

Contributor

+1 for this

It would so nice to have a C++ tutorial

Contributor

jasjuang commented Jun 5, 2017

+1 for this

It would so nice to have a C++ tutorial

@Soledad89

This comment has been minimized.

Show comment
Hide comment
@Soledad89

Soledad89 Jun 6, 2017

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

Soledad89 commented Jun 6, 2017

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

@charlesrwest

This comment has been minimized.

Show comment
Hide comment
@charlesrwest

charlesrwest Jun 23, 2017

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

charlesrwest commented Jun 23, 2017

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

@leonardvandriel

This comment has been minimized.

Show comment
Hide comment
@leonardvandriel

leonardvandriel Jun 23, 2017

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.

leonardvandriel commented Jun 23, 2017

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

This comment has been minimized.

Show comment
Hide comment
@charlesrwest

charlesrwest Jun 23, 2017

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.

charlesrwest commented Jun 23, 2017

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.

@leonardvandriel

This comment has been minimized.

Show comment
Hide comment
@leonardvandriel

leonardvandriel Jun 23, 2017

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.

leonardvandriel commented Jun 23, 2017

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

This comment has been minimized.

Show comment
Hide comment
@charlesrwest

charlesrwest Jul 16, 2017

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

charlesrwest commented Jul 16, 2017

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

This comment has been minimized.

Show comment
Hide comment
@jwatte

jwatte Sep 17, 2017

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

jwatte commented Sep 17, 2017

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

@leonardvandriel

This comment has been minimized.

Show comment
Hide comment
@leonardvandriel

leonardvandriel Sep 18, 2017

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.

leonardvandriel commented Sep 18, 2017

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

This comment has been minimized.

Show comment
Hide comment
@ahkarami

ahkarami Mar 2, 2018

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

ahkarami commented Mar 2, 2018

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

@Crefeda

This comment has been minimized.

Show comment
Hide comment
@Crefeda

Crefeda May 26, 2018

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.

Crefeda commented May 26, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment