prabhuomkar / pytorch-cpp

C++ Implementation of PyTorch Tutorials for Everyone
MIT License
1.96k stars 263 forks source link

[bug] Build Problem on Raspberry Pi #37

Closed Nivolsgel closed 4 years ago

Nivolsgel commented 4 years ago

Problem

Scanning dependencies of target pytorch-cpp [ 2%] Building CXX object CMakeFiles/pytorch-cpp.dir/main.cpp.o [ 4%] Linking CXX executable pytorch-cpp /home/pi/Desktop/LibTorch/libtorch/lib/libtorch.so: file not recognized: file format not recognized collect2: error: ld returned 1 exit status make[2]: [CMakeFiles/pytorch-cpp.dir/build.make:87: pytorch-cpp] Fehler 1 make[1]: [CMakeFiles/Makefile2:327: CMakeFiles/pytorch-cpp.dir/all] Fehler 2 make: *** [Makefile:84: all] Fehler 2

Help Me Pls I think this is a Problem of my specific System, but I have no Idea what the issue is

Desktop:

mfl28 commented 4 years ago

I think for this problem it would be best if you'd research/ask in the official PyTorch forum's C++ section. I only did a quick research, and it looks like the Linux libtorch version that you download from the PyTorch website is not compatible with the raspberry's architecture (see for example https://github.com/pytorch/pytorch/issues/15138#issuecomment-448961840).

Some links that might be helpful for you:

Nivolsgel commented 4 years ago

But now there are new Problems showing up:

cmake --build build Scanning dependencies of target pytorch-cpp [ 2%] Building CXX object CMakeFiles/pytorch-cpp.dir/main.cpp.o [ 4%] Linking CXX executable pytorch-cpp [ 4%] Built target pytorch-cpp Scanning dependencies of target feedforward-neural-network [ 7%] Building CXX object tutorials/basics/feedforward_neural_network/CMakeFiles/feedforward-neural-network.dir/src/main.cpp.o /home/pi/Desktop/PytorchC++TestInternet/pytorch-cpp-master/tutorials/basics/feedforward_neural_network/src/main.cpp: In function ‘int main()’: /home/pi/Desktop/PytorchC++TestInternet/pytorch-cpp-master/tutorials/basics/feedforward_neural_network/src/main.cpp:71:48: error: ‘cross_entropy’ is not a member of ‘torch::nn::functional’ auto loss = torch::nn::functional::cross_entropy(output, target); ^~~~~ /home/pi/Desktop/PytorchC++TestInternet/pytorch-cpp-master/tutorials/basics/feedforward_neural_network/src/main.cpp:74:39: error: expected primary-expression before ‘double’ running_loss += loss.item() data.size(0); ^~ /home/pi/Desktop/PytorchC++TestInternet/pytorch-cpp-master/tutorials/basics/feedforward_neural_network/src/main.cpp:111:44: error: ‘cross_entropy’ is not a member of ‘torch::nn::functional’ auto loss = torch::nn::functional::cross_entropy(output, target); ^~~~~ /home/pi/Desktop/PytorchC++TestInternet/pytorch-cpp-master/tutorials/basics/feedforward_neural_network/src/main.cpp:113:35: error: expected primary-expression before ‘double’ running_loss += loss.item() data.size(0); ^~ make[2]: [tutorials/basics/feedforward_neural_network/CMakeFiles/feedforward-neural-network.dir/build.make:63: tutorials/basics/feedforward_neural_network/CMakeFiles/feedforward-neural-network.dir/src/main.cpp.o] Fehler 1 make[1]: [CMakeFiles/Makefile2:354: tutorials/basics/feedforward_neural_network/CMakeFiles/feedforward-neural-network.dir/all] Fehler 2 make: *** [Makefile:84: all] Fehler 2

Is this a Problem of Libtorch 1.3 instead of the leatest Version? Do you know that?

mfl28 commented 4 years ago

Yes, these errors are because of the older version, you need to use v1.4. Someone in the forum has also provided version 1.4 wheels here: https://discuss.pytorch.org/t/installing-pytorch-on-raspberry-pi-3/25215/14.

prabhuomkar commented 4 years ago

@Nivolsgel close this incase it's solved?

Nivolsgel commented 4 years ago

Just tested it. It works with https://discuss.pytorch.org/t/installing-pytorch-on-raspberry-pi-3/25215/14. this. There are just some minor compile Errors that can easily be solved.

Somehow I can't make the Console Window stay open to read the output. Any quick ideas? I already tried stuff like getchar().... Is this a common Problem?

prabhuomkar commented 4 years ago

There are just some minor compile Errors that can easily be solved.

Can you provide some examples of these errors?

Please bare with us as reproducing this will take some time.

Nivolsgel commented 4 years ago

I only compiled the convolutional neural network.

The Error is that torch::nn::functional::cross_entropy(output, target); musst be replaced withtorch::nn::functional::binary_cross_entropy(output, target)` the compiler suggests you to change this.

I don't know if there is acctually a difference between these functions, but with these changes I can compile the programm

Nivolsgel commented 4 years ago

Just tried to compile the rest:

feed forward neural network:

logistic regression:

deep_residual_network:

This is all I got. I don't know what to put instead of the .bias(). In case you have an idea, I could retry it.

prabhuomkar commented 4 years ago

Can you provide following details?

Nivolsgel commented 4 years ago

sorry for the late response

OS: PRETTY_NAME="Raspbian GNU/Linux 10 (buster)" NAME="Raspbian GNU/Linux" VERSION_ID="10" VERSION="10 (buster)" VERSION_CODENAME=buster ID=raspbian ID_LIKE=debian HOME_URL="http://www.raspbian.org/" SUPPORT_URL="http://www.raspbian.org/RaspbianForums" BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

C++ compiler: g++ (Raspbian 8.3.0-6+rpi1) 8.3.0

Screenshots: I can screenshot almost everything, what do you need?

FarukUgurcali commented 4 years ago
* https://discuss.pytorch.org/t/libtorch-for-raspberry-pi/63107
  This actually solved the build problem.

But now there are new Problems showing up:

cmake --build build Scanning dependencies of target pytorch-cpp [ 2%] Building CXX object CMakeFiles/pytorch-cpp.dir/main.cpp.o [ 4%] Linking CXX executable pytorch-cpp [ 4%] Built target pytorch-cpp Scanning dependencies of target feedforward-neural-network [ 7%] Building CXX object tutorials/basics/feedforward_neural_network/CMakeFiles/feedforward-neural-network.dir/src/main.cpp.o /home/pi/Desktop/PytorchC++TestInternet/pytorch-cpp-master/tutorials/basics/feedforward_neural_network/src/main.cpp: In function ‘int main()’: /home/pi/Desktop/PytorchC++TestInternet/pytorch-cpp-master/tutorials/basics/feedforward_neural_network/src/main.cpp:71:48: error: ‘cross_entropy’ is not a member of ‘torch::nn::functional’ auto loss = torch::nn::functional::cross_entropy(output, target); ^~~~~ /home/pi/Desktop/PytorchC++TestInternet/pytorch-cpp-master/tutorials/basics/feedforward_neural_network/src/main.cpp:74:39: error: expected primary-expression before ‘double’ running_loss += loss.item() data.size(0); ^~ /home/pi/Desktop/PytorchC++TestInternet/pytorch-cpp-master/tutorials/basics/feedforward_neural_network/src/main.cpp:111:44: error: ‘cross_entropy’ is not a member of ‘torch::nn::functional’ auto loss = torch::nn::functional::cross_entropy(output, target); ^~~~~ /home/pi/Desktop/PytorchC++TestInternet/pytorch-cpp-master/tutorials/basics/feedforward_neural_network/src/main.cpp:113:35: error: expected primary-expression before ‘double’ running_loss += loss.item() data.size(0); ^~ make[2]: [tutorials/basics/feedforward_neural_network/CMakeFiles/feedforward-neural-network.dir/build.make:63: tutorials/basics/feedforward_neural_network/CMakeFiles/feedforward-neural-network.dir/src/main.cpp.o] Fehler 1 make[1]: [CMakeFiles/Makefile2:354: tutorials/basics/feedforward_neural_network/CMakeFiles/feedforward-neural-network.dir/all] Fehler 2 make: *** [Makefile:84: all] Fehler 2

Is this a Problem of Libtorch 1.3 instead of the leatest Version? Do you know that?

I have a similar issue while trying to build convolutional_neural_network from repo. cmake --build . --config Release Scanning dependencies of target freespace_torch [ 33%] Building CXX object CMakeFiles/freespace_torch.dir/src/convnet.cpp.o In file included from /home/fugurcal/freespace_torch/src/convnet.cpp:2:0: /home/fugurcal/freespace_torch/include/convnet.h:13:20: error: ‘BatchNorm2d’ is not a member of ‘torch::nn’ torch::nn::BatchNorm2d(16), ^~~~~~~~~~~ /home/fugurcal/freespace_torch/include/convnet.h:13:20: note: suggested alternative: ‘BatchNorm’ torch::nn::BatchNorm2d(16), ^~~~~~~~~~~ BatchNorm /home/fugurcal/freespace_torch/include/convnet.h:14:20: error: ‘ReLU’ is not a member of ‘torch::nn’ torch::nn::ReLU(), ^~~~ /home/fugurcal/freespace_torch/include/convnet.h:15:20: error: ‘MaxPool2d’ is not a member of ‘torch::nn’ torch::nn::MaxPool2d(torch::nn::MaxPool2dOptions(2).stride(2)) ^~~~~~~~~ /home/fugurcal/freespace_torch/include/convnet.h:15:41: error: ‘MaxPool2dOptions’ is not a member of ‘torch::nn’ torch::nn::MaxPool2d(torch::nn::MaxPool2dOptions(2).stride(2)) ^~~~~~~~~~~~~~~~ /home/fugurcal/freespace_torch/include/convnet.h:15:41: note: suggested alternative: ‘Conv2dOptions’ torch::nn::MaxPool2d(torch::nn::MaxPool2dOptions(2).stride(2)) ^~~~~~~~~~~~~~~~ Conv2dOptions /home/fugurcal/freespace_torch/include/convnet.h:16:5: error: could not convert ‘{torch::nn::Conv2d((* &(& torch::nn::ConvOptions<2>(1, 16, torch::ExpandingArray<2, long int>(5)).torch::nn::ConvOptions<2>::stride(torch::ExpandingArray<2, long int>(1)))->torch::nn::ConvOptions<2>::padding(torch::ExpandingArray<2, long int>(2)))), <expression error>, <expression error>, <expression error>}’ from ‘<brace-enclosed initializer list>’ to ‘torch::nn::Sequential’ }; ^ /home/fugurcal/freespace_torch/include/convnet.h:20:20: error: ‘BatchNorm2d’ is not a member of ‘torch::nn’ torch::nn::BatchNorm2d(32), ^~~~~~~~~~~ /home/fugurcal/freespace_torch/include/convnet.h:20:20: note: suggested alternative: ‘BatchNorm’ torch::nn::BatchNorm2d(32), ^~~~~~~~~~~ BatchNorm /home/fugurcal/freespace_torch/include/convnet.h:21:20: error: ‘ReLU’ is not a member of ‘torch::nn’ torch::nn::ReLU(), ^~~~ /home/fugurcal/freespace_torch/include/convnet.h:22:20: error: ‘MaxPool2d’ is not a member of ‘torch::nn’ torch::nn::MaxPool2d(torch::nn::MaxPool2dOptions(2).stride(2)) ^~~~~~~~~ /home/fugurcal/freespace_torch/include/convnet.h:22:41: error: ‘MaxPool2dOptions’ is not a member of ‘torch::nn’ torch::nn::MaxPool2d(torch::nn::MaxPool2dOptions(2).stride(2)) ^~~~~~~~~~~~~~~~ /home/fugurcal/freespace_torch/include/convnet.h:22:41: note: suggested alternative: ‘Conv2dOptions’ torch::nn::MaxPool2d(torch::nn::MaxPool2dOptions(2).stride(2)) ^~~~~~~~~~~~~~~~ Conv2dOptions /home/fugurcal/freespace_torch/include/convnet.h:23:5: error: could not convert ‘{torch::nn::Conv2d((* &(& torch::nn::ConvOptions<2>(16, 32, torch::ExpandingArray<2, long int>(5)).torch::nn::ConvOptions<2>::stride(torch::ExpandingArray<2, long int>(1)))->torch::nn::ConvOptions<2>::padding(torch::ExpandingArray<2, long int>(2)))), <expression error>, <expression error>, <expression error>}’ from ‘<brace-enclosed initializer list>’ to ‘torch::nn::Sequential’ }; ^ CMakeFiles/freespace_torch.dir/build.make:62: recipe for target 'CMakeFiles/freespace_torch.dir/src/convnet.cpp.o' failed make[2]: *** [CMakeFiles/freespace_torch.dir/src/convnet.cpp.o] Error 1 CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/freespace_torch.dir/all' failed make[1]: *** [CMakeFiles/freespace_torch.dir/all] Error 2 Makefile:83: recipe for target 'all' failed make: *** [all] Error 2 I am using torch 1.5, any idea how solve this problem?

mfl28 commented 4 years ago

Here is how I got the tutorials to build successfully on a Raspberry 3B+:

  1. Download Pytorch 1.5 wheels for arm architecture to the RPi from this repo: https://github.com/maltequast/pytorch_arm_whl (You may also build them yourself on another system using Docker with qemu-raspbian).
  2. Install on the RPi:
    pip install <wheel_name>.whl
  3. Clone pytorch-cpp repo:
    git clone https://github.com/prabhuomkar/pytorch-cpp.git
    cd pytorch-cpp
  4. Create build-system files with CMake, pointing it to the installed libtorch:
    cmake -B build -D CMAKE_BUILD_TYPE=Release -D CMAKE_PREFIX_PATH=/home/pi/.local/lib/python3.7/site-packages/torch/share/cmake/Torch
  5. Build the tutorials:
    cmake --build build

    Grab a coffee because it takes some time but once it's finished you should be able to run the tutorials on your RPi.

Some additional info to the RPi and installed program versions I used:

Model: Raspberry Pi 3 Model B Plus Rev 1.3
CPU-architecture: armv7l
OS: Raspbian GNU/Linux 10 (buster)

python3 version: 3.7.4
pip version: 20.1.1

CMake version: 3.16.5
C++ compiler: g++ (Raspbian 8.3.0-6+rpi1) 8.3.0
FarukUgurcali commented 4 years ago

Here is how I got the tutorials to build successfully on a Raspberry 3B+:

1. Download Pytorch 1.5 wheels for arm architecture to the RPi from this repo: https://github.com/maltequast/pytorch_arm_whl (You may also build them yourself on another system using Docker with [qemu-raspbian](https://github.com/choonkiatlee/qemu-raspbian#example-build-pytorch-wheels-for-the-raspberry-pi)).

2. Install on the RPi:
   ```shell
   pip install <wheel_name>.whl
   ```

3. Clone pytorch-cpp repo:
   ```shell
   git clone https://github.com/prabhuomkar/pytorch-cpp.git
   cd pytorch-cpp
   ```

4. Create build-system files with CMake, pointing it to the installed libtorch:
   ```shell
   cmake -B build -D CMAKE_BUILD_TYPE=Release -D CMAKE_PREFIX_PATH=/home/pi/.local/lib/python3.7/site-packages/torch/share/cmake/Torch
   ```

5. Build the tutorials:
   ```shell
   cmake --build build
   ```

Grab a coffee because it takes some time but once it's finished you should be able to run the tutorials on your RPi.

Some additional info to the RPi and installed program versions I used:

Model: Raspberry Pi 3 Model B Plus Rev 1.3
CPU-architecture: armv7l
OS: Raspbian GNU/Linux 10 (buster)

python3 version: 3.7.4
pip version: 20.1.1

CMake version: 3.16.5
C++ compiler: g++ (Raspbian 8.3.0-6+rpi1) 8.3.0

Sorry for confusion I am not using RPi. I am using Ubuntu 18.04 laptop with libtorch 1.5.0 (with cuda 10.2). My current g++ vesion is 7.5.0

mfl28 commented 4 years ago

@FarukUgurcali Ok, no problem, I have created a new issue for your problem in #55 . Let's continue discussing a solution there to keep things more organized.

prabhuomkar commented 4 years ago

@Nivolsgel can you try the latest solution provided by markus and let us know if this valid?

mfl28 commented 4 years ago

Closed for now due to inactivity. Will be reopened if there are further questions concerning this problem and the proposed solution.