How to build Caffe dependency - t-kuha/zynq-library GitHub Wiki

CPU only of course...

Get sources


Prerequisite

  • cmake: https://cmake.org/

  • Create directory for output product

    $ mkdir <lib output dir>           # ex. caffe_libs
    $ mkdir <protobuf output for host> # ex. protoc_host
    
  • Source petalinux tools

    $ source <petalinux root>/settings.sh
    

Build dependencies

  • Boost

    $ ./bootstrap.sh 
    
    # Replace the line in project-config.jam:
    #     using gcc ; 
    # with
    #     using gcc : arm : arm-linux-gnueabihf-g++ ;
    $ nano project-config.jam 
    
    $ ./bjam install toolset=gcc-arm \
    > --prefix=<lib output dir> \
    > --with-thread \
    > --with-system \
    > --with-filesystem \
    > -j`nproc` 
    
  • OpenBLAS

    • Using CMake causes compilation error
    $ make CC=arm-linux-gnueabihf-gcc HOSTCC=gcc CROSS=1 TARGET=ARMV7
    $ make -j`nproc` \
    > PREFIX=<lib output dir> \
    > CC=arm-linux-gnueabihf-gcc \
    > HOSTCC=gcc \
    > CROSS=1 \
    > TARGET=ARMV7 \
    > install
    
  • gflags

    $ mkdir _zynq
    $ cd _zynq
    $ cmake .. \
    > -G"Unix Makefiles" \
    > -DCMAKE_TOOLCHAIN_FILE=<path to toolchain.make> \
    > -DCMAKE_INSTALL_PREFIX=<output dir> \
    > -DBUILD_SHARED_LIBS=ON \
    > -DBUILD_STATIC_LIBS=ON \
    $ make -j`nproc`
    $ make install
    
  • glog

    # Configuration
    $ cmake .. -G"Unix Makefiles" -DCMAKE_TOOLCHAIN_FILE=toolchain.make \
    -DCMAKE_INSTALL_PREFIX=<Destination> \
    -DBUILD_TESTING=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON
    
    # Build & install
    $ make -j`nproc` install
    
  • LMDB

    • Modify Makefile

    • Before:

    CC      = gcc
    AR      = ar
       ...
    prefix  = /usr/local
    • After:
    CC      = arm-linux-gnueabihf-gcc
    AR      = arm-linux-gnueabihf-ar
       ...
    prefix  = <lib output dir>
    • Build
    $ make -j`nproc` install
    
  • snappy

    $ mkdir _zynq
    $ cd _zynq
    $ cmake ..\
    > -G"Unix Makefiles" \
    > -DCMAKE_TOOLCHAIN_FILE=<path to toolchain.make> \
    > -DCMAKE_INSTALL_PREFIX=<lib output dir> \
    > -DBUILD_SHARED_LIBS=ON \
    > -DSNAPPY_BUILD_TESTS=OFF
    $ make -j`nproc`
    $ make install
    
  • leveldb

    $ CC=arm-linux-gnueabihf-gcc CXX=arm-linux-gnueabihf-g++ make -j`nproc`
    $ cp -R ./include/leveldb <lib output dir>/include/
    $ cp -R ./out-shared/libleveldb* <lib output dir>/lib
    $ cp -R ./out-static/lib*.a <lib output dir>/lib
    
  • protobuf

    • First, build protoc for host...
    $ mkdir _protoc_host
    $ ./autogen
    $ ./configure --prefix=<protobuf output for host>
    $ make -j`nproc` install
    
    • Then, build for target (Zynq/ARMv7-A)...
    $ ./configure \
    > --prefix=<lib output dir> \
    > --host=arm-linux-gnueabihf \
    > --with-protoc=<protobuf output for host>/bin/protoc
    $ make -j`nproc`
    $ make install
    
  • hdf5

    • Copy H5Tinit.c & H5lib_settings.c into src directory

    • Configure & Build

    $ ./configure \
    > --prefix=<lib output dir> \
    > --host=arm-linux-gnueabihf \
    > hdf5_cv_system_scope_threads=yes \
    > hdf5_cv_ldouble_to_long_special=no \
    > hdf5_cv_long_to_ldouble_special=no \
    > hdf5_cv_ldouble_to_llong_accurate=yes \
    > hdf5_cv_llong_to_ldouble_correct=yes
    $ make -j`nproc`
    $ make install
    
  • OpenCV

    • See this Wiki page...

Build Caffe

  • Edit Makefile.config

  • Edit Makefile

  • Build

    $ PATH=<protobuf output for host>/bin:${PATH} make -j`nproc`
    $ make -j`nproc`
    $ make distribute
    

Test

# ./data/mnist/get_mnist.sh
Downloading...
Connecting to yann.lecun.com (216.165.22.6:80)
train-images-idx3-ub 100% |***************************************************|  9680k  0:00:00 ETA
Connecting to yann.lecun.com (216.165.22.6:80)
train-labels-idx1-ub 100% |***************************************************| 28881   0:00:00 ETA
Connecting to yann.lecun.com (216.165.22.6:80)
t10k-images-idx3-uby 100% |***************************************************|  1610k  0:00:00 ETA
Connecting to yann.lecun.com (216.165.22.6:80)
t10k-labels-idx1-uby 100% |***************************************************|  4542   0:00:00 ETA

# ./examples/mnist/create_mnist.sh
Creating lmdb...
I0804 00:01:04.099035  1156 db_lmdb.cpp:35] Opened lmdb examples/mnist/mnist_train_lmdb
I0804 00:01:04.102541  1156 convert_mnist_data.cpp:88] A total of 60000 items.
I0804 00:01:04.102640  1156 convert_mnist_data.cpp:89] Rows: 28 Cols: 28
I0804 00:02:21.123931  1156 convert_mnist_data.cpp:108] Processed 60000 files.
I0804 00:02:21.246237  1158 db_lmdb.cpp:35] Opened lmdb examples/mnist/mnist_test_lmdb
I0804 00:02:21.247308  1158 convert_mnist_data.cpp:88] A total of 10000 items.
I0804 00:02:21.247402  1158 convert_mnist_data.cpp:89] Rows: 28 Cols: 28
I0804 00:02:34.146692  1158 convert_mnist_data.cpp:108] Processed 10000 files.
Done.

# Do not forget edit examples/mnist/lenet_solver.prototxt
# "solver_mode: GPU" -> "solver_mode: CPU"

# ./examples/mnist/train_lenet.sh
I0804 00:20:36.117604  1169 caffe.cpp:211] Use CPU.
I0804 00:20:36.119038  1169 solver.cpp:44] Initializing solver from parameters:
test_iter: 100
test_interval: 500
base_lr: 0.01

...

I0804 04:16:11.812041  1204 sgd_solver.cpp:105] Iteration 9900, lr = 0.00596843
I0804 04:17:27.156785  1204 solver.cpp:447] Snapshotting to binary proto file examples/mnist/lenet_iter_10000.caffemodel
I0804 04:17:27.213197  1204 sgd_solver.cpp:273] Snapshotting solver state to binary proto file examples/mnist/lenet_iter_10000.solverstate
I0804 04:17:27.518888  1204 solver.cpp:310] Iteration 10000, loss = 0.00437557
I0804 04:17:27.519055  1204 solver.cpp:330] Iteration 10000, Testing net (#0)
I0804 04:18:11.437750  1207 data_layer.cpp:73] Restarting data prefetching from start.
I0804 04:18:13.258384  1204 solver.cpp:397]     Test net output #0: accuracy = 0.9908
I0804 04:18:13.258586  1204 solver.cpp:397]     Test net output #1: loss = 0.0297891 (* 1 = 0.0297891 loss)
I0804 04:18:13.258664  1204 solver.cpp:315] Optimization Done.
I0804 04:18:13.258714  1204 caffe.cpp:259] Optimization Done.
⚠️ **GitHub.com Fallback** ⚠️