1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Raspberry Pi 4でdockerを使用してtensorflowの環境を作ってみるときに困ったこと

Last updated at Posted at 2024-03-04

やりたいこと

Raspberry Pi 4のマシンにdockerをインストールして、tensorflowを動かしたい。
以前にdockerを使用せずtensorflowをインストールしようとして、raspiの環境を弄り回した結果、OSを初期化するハメになったのでdockerでコンテナ内にtensorflowの環境を作りたい。

で、詰まったことを備忘録がてらメモしていく記事。

環境

Raspberry Pi OS(64-bit)
↓からインストールしたての新品
https://www.raspberrypi.com/software/
にdockerだけインストールしている状態。

やってみる

先駆者の記事を参考にdockerfileを書いてビルドする。

FROM resin/rpi-raspbian:stretch

ENV LANG C.UTF-8

ENV DEBIAN_FRONTEND noninteractive

ENV APP_ROOT /usr/src
WORKDIR $APP_ROOT
COPY src $APP_ROOT

RUN echo "deb http://mirrordirector.raspbian.org/raspbian/ stretch main contrib non-free rpi firmware" > /etc/apt/sources.list

RUN apt-get update -y
RUN apt-get install -y --no-install-recommends \ 
        vim git less wget \
        build-essential \
        libatlas-base-dev \
        python3-pip python3-dev python3-setuptools\
        python3-scipy python3-h5py \
        libraspberrypi-bin \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

RUN pip3 install --upgrade pip setuptools
RUN pip3 install rpi.gpio
RUN pip3 install numpy opencv-python
RUN pip3 install tensorflow
RUN pip3 install keras
RUN pip3 install jupyter

# Jupyter のデフォルトポート
EXPOSE 8888

リポジトリのエラー

ビルドしてみたところ失敗する。
ビルドログに下記のメッセージが出ていた。
(抜粋)

1.650 Err:6 http://ftp.jaist.ac.jp/raspbian stretch/non-free armhf Packages
1.650   404  Not Found
1.665 Ign:7 http://ftp.jaist.ac.jp/raspbian stretch/main armhf Packages
1.680 Ign:8 http://ftp.jaist.ac.jp/raspbian stretch/firmware armhf Packages
1.695 Ign:9 http://ftp.jaist.ac.jp/raspbian stretch/contrib all Packages
1.710 Ign:10 http://ftp.jaist.ac.jp/raspbian stretch/contrib armhf Packages
1.725 Ign:11 http://ftp.jaist.ac.jp/raspbian stretch/rpi armhf Packages
1.740 Ign:12 http://ftp.jaist.ac.jp/raspbian stretch/rpi all Packages
1.812 Get:13 http://archive.raspberrypi.org/debian stretch InRelease [25.3 kB]
3.723 Get:14 http://archive.raspberrypi.org/debian stretch/main armhf Packages [192 kB]
4.661 Fetched 218 kB in 4s (52.6 kB/s)
4.661 Reading package lists...
4.736 W: The repository 'http://ftp.jaist.ac.jp/raspbian stretch Release' does not have a Release file.
4.737 E: Failed to fetch http://ftp.jaist.ac.jp/raspbian/dists/stretch/non-free/binary-armhf/Packages  404  Not Found
4.737 E: Some index files failed to download. They have been ignored, or old ones used instead.

どうやら404エラーが出てライブラリが見つけられないようす(strechのリポジトリがサポート切れになった影響だそう)。

Dockerfile内の記述を修正する。


#修正前
RUN echo "deb http://mirrordirector.raspbian.org/raspbian/ stretch main contrib non-free rpi firmware" > /etc/apt/sources.list

#修正後
RUN echo "deb http://raspbian.raspberrypi.org/raspbian/ bullseye stretch main contrib non-free rpi firmware" > /etc/apt/sources.list

これでライブラリ(リポジトリ)のエラーは解決した。
しかし別の問題が。。。。

numpyのインストールに失敗

ビルドのログを確認すると、numpyのインストールに失敗しているようだった。

 > [10/12] RUN pip3 install numpy opencv-python:
1.764 Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (1.19.5)
2.290 Collecting opencv-python
2.361   Downloading opencv-python-4.9.0.80.tar.gz (92.9 MB)
18.69      qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq 92.9/92.9 MB 1.3 MB/s eta 0:00:00
32.53   Installing build dependencies: started
148.1   Installing build dependencies: still running...
220.2   Installing build dependencies: still running...
284.9   Installing build dependencies: still running...
355.0   Installing build dependencies: still running...
441.3   Installing build dependencies: still running...
474.4   Installing build dependencies: finished with status 'error'
475.1   error: subprocess-exited-with-error

以前初期化するハメになった環境で、numpyのインストールはpipを使用せずに行っていたので同様にやってみる


#修正前
RUN pip3 install numpy opencv-python
#修正後
RUN apt-get install python3-numpy

これで解決したがその後、tensorflowがインストールできなかったので色々調べたところ、pythonのバージョンに問題があるようだったので、raspberrypi-osのpython3.9がセットでビルドされているイメージを使用する形に変更。

以下が最終版のDockerfile

FROM dtcooper/raspberrypi-os:python3.9
ENV LANG C.UTF-8

ENV DEBIAN_FRONTEND noninteractive

ENV APP_ROOT /usr/src
WORKDIR $APP_ROOT
COPY src $APP_ROOT

RUN apt-get update -y
RUN apt-get install -y --no-install-recommends \
        vim git less wget \
        build-essential \
        libatlas-base-dev \
        python3-pip python3-dev python3-setuptools\
        python3-scipy python3-h5py \
        libraspberrypi-bin \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

RUN pip3 install --upgrade pip setuptools
RUN pip3 install rpi.gpio
RUN apt-get install python3-numpy
RUN pip --no-cache-dir install -I -U tensorflow
RUN pip3 install keras
RUN pip3 install jupyter

# Jupyter のデフォルトポート
EXPOSE 8888

あとは適当にビルドしてあげて、

docker build -t rpi-raspbian-stretch .
docker run --name rpi-raspbian-stretch -ti -p 8888:8888 --privileged -d rpi-raspbian-stretch

webブラウザからアクセスできた
(host名:ポート番号をブラウザで入力)

image.png

python3.11がなぜ存在しているのかよく分からないが、とりあえず以上です。
(先駆者に感謝)

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?