MNN icon indicating copy to clipboard operation
MNN copied to clipboard

pip install MNN on Raspberry pi 4 not work

Open nakarin opened this issue 4 years ago • 9 comments

I'd like to install MNN (Python) for RPi 4 by pip install MNN but not work. Have anyone can solved this?

Thanks.

nakarin avatar Aug 12 '20 11:08 nakarin

@nakarin Hi are you able to install it on raspberry pi 4? if yes , please let me know how to do it. -Thank you in advance.

Ratansairohith avatar Jan 26 '21 08:01 Ratansairohith

@Ratansairohith Are you able to install it on rpi 4?

Co2Link avatar Feb 12 '21 05:02 Co2Link

@ling9601 Nope i was unable to install it on rpi 4. If you find a way please let me know.

Ratansairohith avatar Feb 17 '21 15:02 Ratansairohith

yes, I am not at home now. I will share the installation tomorrow

Co2Link avatar Feb 17 '21 16:02 Co2Link

@Ratansairohith The installation was tested on RP4b with the latest RP OS.

Try the following step to build the wheel and install it.

  1. install dependency for compile
sudo apt-get install cmake libprotobuf-dev protobuf-compiler
  1. get the source code
git clone https://github.com/alibaba/MNN

According to [this],(https://github.com/RangiLyu/nanodet/issues/92) use 1.0.0 instead of the latest version (I tested that 1.1.3 is faster and the result is the same as 1.0.0, also mutithread in 1.1.3 can be used but not in 1.1.0)

git checkout 1.1.3
  1. compile preparation
cd /path/to/MNN
./schema/generate.sh
mkdir pymnn_build
  1. Build dependency for python wheel building

If we build MNNConvert, the follow error will show up(can't find any fix about this)

[100%] Linking CXX executable ../../MNNConvert
/usr/bin/ld: /usr/local/lib/libprotobuf.a(arena.o): in function google::protobuf::internal::ArenaImpl::Init(bool)': arena.cc:(.text+0x3ac): undefined reference to __atomic_fetch_add_8'
collect2: error: ld returned 1 exit status

so we dont build the MNNConvert. change the following lines

build_deps.py
...
    elif IS_LINUX:
        os.system('cmake -DMNN_BUILD_CONVERTER=on -DMNN_BUILD_TRAIN=ON -DCMAKE_BUILD_TYPE=Release\
            -DMNN_BUILD_SHARED_LIBS=OFF -DMNN_AAPL_FMWK=OFF -DMNN_SEP_BUILD=OFF\
            -DMNN_USE_THREAD_POOL=OFF .. && make MNN MNNTrain MNNConvert  -j4')
...

to

build_deps.py
...
    elif IS_LINUX:
        os.system('cmake -DMNN_BUILD_CONVERTER=on -DMNN_BUILD_TRAIN=ON -DCMAKE_BUILD_TYPE=Release\
            -DMNN_BUILD_SHARED_LIBS=OFF -DMNN_AAPL_FMWK=OFF -DMNN_SEP_BUILD=OFF\
            -DMNN_USE_THREAD_POOL=OFF .. && make MNN MNNTrain -j4')
...

use build_deps.py to build dependency

cd pymnn/pip_package
python build_deps.py
  1. Build wheel

change platform, modify the following line

pymnn/pip_package/build_wheel.py
...
    if IS_LINUX:
        os.system('python setup.py bdist_wheel --plat-name=manylinux1_x86_64')
...

to

pymnn/pip_package/build_wheel.py
...
    if IS_LINUX:
        os.system('python setup.py bdist_wheel --plat-name=manylinux2014_armv7l')
...

don't build _tools, comment out the following lines

pymnn/pip_package/setup.py
...
    tools = Extension("_tools",\
                    libraries=tools_libraries,\
                    sources=tools_sources,\
                    language='c++',\
                    extra_compile_args=tools_compile_args + extra_compile_args,\
                    include_dirs=tools_include_dirs,\
                    library_dirs=tools_library_dirs,\
                    extra_link_args=tools_extra_link_args +tools_link_args\
                        + [make_relative_rpath('lib')])
    extensions.append(tools)
...

build wheel

python build_wheel.py
  1. Install wheel
pip install pymnn/pip_package/dist/MNN-1.1.3-cp37-cp37m-manylinux2014_armv7l.whl
  1. Test MNN installation
import MNN

Beacuse we did not build the _tools, the following error will show up

>>> import MNN
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pi/.virtualenvs/mnn/lib/python3.7/site-packages/MNN/__init__.py", line 9, in <module>
    from . import tools
  File "/home/pi/.virtualenvs/mnn/lib/python3.7/site-packages/MNN/tools/__init__.py", line 1, in <module>
    from . import mnn, mnnops, mnnquant, mnnconvert 
  File "/home/pi/.virtualenvs/mnn/lib/python3.7/site-packages/MNN/tools/mnnquant.py", line 8, in <module>
    import _tools as Tools
ModuleNotFoundError: No module named '_tools'

in order the silent this error, comment out the following line

/home/pi/.virtualenvs/mnn/lib/python3.7/site-packages/MNN/__init__.py
...
from . import tools
...

then test import MNN again, it should be all done now

Co2Link avatar Feb 18 '21 01:02 Co2Link

@ling9601 thank you very much for your detailed explaination! I will try it out this week and get back to you if i had any trouble.

Ratansairohith avatar Feb 18 '21 16:02 Ratansairohith

modify build_wheel.py on aarch64 os (or openfans) of raspberry pi

# manylinux2014_armv7l to manylinux2014_aarch64
if IS_LINUX:
        comm_args += ' --plat-name=manylinux2014_aarch64'
...

# python to python3
os.system('python3 setup.py bdist_wheel %s' % comm_args)

build wheel as version 1.1.7

python3 build_wheel.py --version 1.1.7

after install MNN-1.1.7-cp37-cp37m-manylinux2014_aarch64.whl

Modify vim /usr/local/lib/python3.7/dist-packages/MNN/__init__.py

comment

# from .version import __version__
...
# from . import tools

fly-studio avatar May 25 '21 15:05 fly-studio

I wonder if you can share the speed you get on the rpi with MNN? FPS for the models you use?

mosheliv avatar Jun 17 '21 22:06 mosheliv

For MNN-2.0.0 in raspberry pi 4 with kernel version 5.10.92-v7l+, only need to modify the build_wheel.py as follows:

pymnn/pip_package/build_wheel.py
...
    if IS_LINUX:
        os.system('python setup.py bdist_wheel --plat-name=manylinux2014_armv7l')
...

GuoJingtao-1997 avatar Sep 03 '22 09:09 GuoJingtao-1997

@Ratansairohith The installation was tested on RP4b with the latest RP OS.

Try the following step to build the wheel and install it.

  1. install dependency for compile
sudo apt-get install cmake libprotobuf-dev protobuf-compiler
  1. get the source code
git clone https://github.com/alibaba/MNN

According to [this],(RangiLyu/nanodet#92) use 1.0.0 instead of the latest version (I tested that 1.1.3 is faster and the result is the same as 1.0.0, also mutithread in 1.1.3 can be used but not in 1.1.0)

git checkout 1.1.3
  1. compile preparation
cd /path/to/MNN
./schema/generate.sh
mkdir pymnn_build
  1. Build dependency for python wheel building

If we build MNNConvert, the follow error will show up(can't find any fix about this)

[100%] Linking CXX executable ../../MNNConvert
/usr/bin/ld: /usr/local/lib/libprotobuf.a(arena.o): in function google::protobuf::internal::ArenaImpl::Init(bool)': arena.cc:(.text+0x3ac): undefined reference to __atomic_fetch_add_8'
collect2: error: ld returned 1 exit status

so we dont build the MNNConvert. change the following lines

build_deps.py
...
    elif IS_LINUX:
        os.system('cmake -DMNN_BUILD_CONVERTER=on -DMNN_BUILD_TRAIN=ON -DCMAKE_BUILD_TYPE=Release\
            -DMNN_BUILD_SHARED_LIBS=OFF -DMNN_AAPL_FMWK=OFF -DMNN_SEP_BUILD=OFF\
            -DMNN_USE_THREAD_POOL=OFF .. && make MNN MNNTrain MNNConvert  -j4')
...

to

build_deps.py
...
    elif IS_LINUX:
        os.system('cmake -DMNN_BUILD_CONVERTER=on -DMNN_BUILD_TRAIN=ON -DCMAKE_BUILD_TYPE=Release\
            -DMNN_BUILD_SHARED_LIBS=OFF -DMNN_AAPL_FMWK=OFF -DMNN_SEP_BUILD=OFF\
            -DMNN_USE_THREAD_POOL=OFF .. && make MNN MNNTrain -j4')
...

use build_deps.py to build dependency

cd pymnn/pip_package
python build_deps.py
  1. Build wheel

change platform, modify the following line

pymnn/pip_package/build_wheel.py
...
    if IS_LINUX:
        os.system('python setup.py bdist_wheel --plat-name=manylinux1_x86_64')
...

to

pymnn/pip_package/build_wheel.py
...
    if IS_LINUX:
        os.system('python setup.py bdist_wheel --plat-name=manylinux2014_armv7l')
...

don't build _tools, comment out the following lines

pymnn/pip_package/setup.py
...
    tools = Extension("_tools",\
                    libraries=tools_libraries,\
                    sources=tools_sources,\
                    language='c++',\
                    extra_compile_args=tools_compile_args + extra_compile_args,\
                    include_dirs=tools_include_dirs,\
                    library_dirs=tools_library_dirs,\
                    extra_link_args=tools_extra_link_args +tools_link_args\
                        + [make_relative_rpath('lib')])
    extensions.append(tools)
...

build wheel

python build_wheel.py
  1. Install wheel
pip install pymnn/pip_package/dist/MNN-1.1.3-cp37-cp37m-manylinux2014_armv7l.whl
  1. Test MNN installation
import MNN

Beacuse we did not build the _tools, the following error will show up

>>> import MNN
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/pi/.virtualenvs/mnn/lib/python3.7/site-packages/MNN/__init__.py", line 9, in <module>
    from . import tools
  File "/home/pi/.virtualenvs/mnn/lib/python3.7/site-packages/MNN/tools/__init__.py", line 1, in <module>
    from . import mnn, mnnops, mnnquant, mnnconvert 
  File "/home/pi/.virtualenvs/mnn/lib/python3.7/site-packages/MNN/tools/mnnquant.py", line 8, in <module>
    import _tools as Tools
ModuleNotFoundError: No module named '_tools'

in order the silent this error, comment out the following line

/home/pi/.virtualenvs/mnn/lib/python3.7/site-packages/MNN/__init__.py
...
from . import tools
...

then test import MNN again, it should be all done now

Don't have pre-build python whl for raspberry. Suggest this method.

wangzhaode avatar Feb 15 '23 07:02 wangzhaode

Hi, Thanks for that. I have moved to other products since this was opened, but would like to comment that this installs 1.1.3 while the cutting edge is 2.3 and this is a pretty awkward process. As rpi is probably the most used SBC in the world, you would think it deserves better support. Just my thoughts.

Best regards, Moshe

mosheliv avatar Feb 15 '23 22:02 mosheliv