forked from snowzach/doods
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dockerfile.arm32
191 lines (168 loc) · 10.3 KB
/
Dockerfile.arm32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
FROM debian:buster as base
# Install reqs with cross compile support
RUN dpkg --add-architecture armhf && \
apt-get update && apt-get install -y --no-install-recommends \
pkg-config zip zlib1g-dev unzip wget bash-completion git curl \
build-essential patch g++ python python-future python3 \
python-numpy python-six \
cmake ca-certificates \
libc6-dev:armhf libstdc++6:armhf libusb-1.0-0:armhf
# Install protoc
RUN wget https://github.com/protocolbuffers/protobuf/releases/download/v3.12.3/protoc-3.12.3-linux-x86_64.zip && \
unzip protoc-3.12.3-linux-x86_64.zip -d /usr/local && \
rm /usr/local/readme.txt && \
rm protoc-3.12.3-linux-x86_64.zip
# Version Configuration
ARG BAZEL_VERSION="2.0.0"
ARG TF_VERSION="f394a768719a55b5c351ed1ecab2ec6f16f99dd4"
ARG OPENCV_VERSION="4.5.0"
ARG GO_VERSION="1.14.3"
# Install bazel
ENV BAZEL_VERSION $BAZEL_VERSION
RUN wget https://github.com/bazelbuild/bazel/releases/download/${BAZEL_VERSION}/bazel_${BAZEL_VERSION}-linux-x86_64.deb && \
dpkg -i bazel_${BAZEL_VERSION}-linux-x86_64.deb && \
rm bazel_${BAZEL_VERSION}-linux-x86_64.deb
# Download tensorflow sources
ENV TF_VERSION $TF_VERSION
#RUN cd /opt && git clone https://github.com/tensorflow/tensorflow.git --branch $TF_VERSION --single-branch
RUN cd /opt && git clone https://github.com/tensorflow/tensorflow.git && cd /opt/tensorflow && git checkout ${TF_VERSION}
# Download and configure the toolchain and patch tensorflow as needed
ENV CROSSTOOL_COMPILER="yes"
ENV CROSSTOOL_URL="https://releases.linaro.org/components/toolchain/binaries/5.5-2017.10/arm-linux-gnueabihf/gcc-linaro-5.5.0-2017.10-x86_64_arm-linux-gnueabihf.tar.xz"
ENV CROSSTOOL_DIR="gcc-linaro-5.5.0-2017.10-x86_64_arm-linux-gnueabihf"
ENV CROSSTOOL_NAME="arm-linux-gnueabihf"
COPY tf_arm_toolchain_patch.sh /opt/tf_arm_toolchain_patch.sh
RUN cd /opt/tensorflow && /opt/tf_arm_toolchain_patch.sh
# Configure tensorflow
ENV TF_NEED_GDR=0 TF_NEED_AWS=0 TF_NEED_GCP=0 TF_NEED_CUDA=0 TF_NEED_HDFS=0 TF_NEED_OPENCL_SYCL=0 TF_NEED_VERBS=0 TF_NEED_MPI=0 TF_NEED_MKL=0 TF_NEED_JEMALLOC=1 TF_ENABLE_XLA=0 TF_NEED_S3=0 TF_NEED_KAFKA=0 TF_NEED_IGNITE=0 TF_NEED_ROCM=0
RUN cd /opt/tensorflow && yes '' | ./configure
# Tensorflow build flags
ENV BAZEL_COPT_FLAGS="--local_resources 16000,16,1 --copt=-march=armv7-a --copt=-mfpu=neon-vfpv4 --copt=-mfloat-abi=hard --copt=-O3 --copt=-fno-tree-pre --copt=-fpermissive --copt=-std=c++11 --copt=-DS_IREAD=S_IRUSR --copt=-DS_IWRITE=S_IWUSR --copt=-U__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1 --copt=-U__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2 --copt=-U__GCC_HAVE_SYNC_COMPARE_AND_SWAP_8 --config=monolithic --copt=-funsafe-math-optimizations --copt=-ftree-vectorize --copt=-fomit-frame-pointer --copt=-DRASPBERRY_PI --noincompatible_strict_action_env --config=noaws --config=nohdfs --define tensorflow_mkldnn_contraction_kernel=0 --define=raspberry_pi_with_neon=true"
ENV BAZEL_EXTRA_FLAGS="--cpu=armeabi --host_linkopt=-lm --crosstool_top=@local_config_arm_compiler//:toolchain"
# Compile and build tensorflow lite
RUN cd /opt/tensorflow && \
bazel build -c opt $BAZEL_COPT_FLAGS --verbose_failures $BAZEL_EXTRA_FLAGS //tensorflow/lite:libtensorflowlite.so && \
install bazel-bin/tensorflow/lite/libtensorflowlite.so /usr/local/lib/libtensorflowlite.so && \
bazel build -c opt $BAZEL_COPT_FLAGS --verbose_failures $BAZEL_EXTRA_FLAGS //tensorflow/lite/c:libtensorflowlite_c.so && \
install bazel-bin/tensorflow/lite/c/libtensorflowlite_c.so /usr/local/lib/libtensorflowlite_c.so && \
mkdir -p /usr/local/include/flatbuffers && cp bazel-tensorflow/external/flatbuffers/include/flatbuffers/* /usr/local/include/flatbuffers
# Compile and install tensorflow shared library
RUN cd /opt/tensorflow && \
bazel build -c opt $BAZEL_COPT_FLAGS --verbose_failures $BAZEL_EXTRA_FLAGS //tensorflow:libtensorflow.so && \
install bazel-bin/tensorflow/libtensorflow.so /usr/local/lib/libtensorflow.so && \
ln -rs /usr/local/lib/libtensorflow.so /usr/local/lib/libtensorflow.so.1 && \
ln -rs /usr/local/lib/libtensorflow.so /usr/local/lib/libtensorflow.so.2
# cleanup so the cache directory isn't huge
RUN cd /opt/tensorflow && \
bazel clean && rm -Rf /root/.cache
# Download and configure the build environment for gcc 6 which is needed to compile everything else
RUN mkdir -p /tmp/sysroot/lib && mkdir -p /tmp/sysroot/usr/lib && \
cd /tmp && \
wget --no-check-certificate https://releases.linaro.org/components/toolchain/binaries/6.3-2017.05/arm-linux-gnueabihf/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf.tar.xz -O toolchain.tar.xz && \
tar xf toolchain.tar.xz -C /opt/toolchain/ && \
rm toolchain.tar.xz && \
cp -r /opt/toolchain/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/arm-linux-gnueabihf/libc/* /tmp/sysroot/
RUN mkdir -p /tmp/debs && cd /tmp/debs && apt-get update && apt-get download libc6:armhf libc6-dev:armhf && \
ar x libc6_*.deb && tar xvf data.tar.xz && \
ar x libc6-dev*.deb && tar xvf data.tar.xz && \
cp -R usr /tmp/sysroot && cp -R lib /tmp/sysroot && rm -Rf /tmp/debs && \
mkdir -p /tmp/debs && cd /tmp/debs && \
apt-get download libusb-1.0-0:armhf libudev1:armhf zlib1g-dev:armhf zlib1g:armhf && \
ar x libusb-1.0*.deb && tar xvf data.tar.xz && \
ar x libudev1*.deb && tar xvf data.tar.xz && \
ar x zlib1g_*.deb && tar xvf data.tar.xz && \
ar x zlib1g-dev*.deb && tar xvf data.tar.xz && rm usr/lib/arm-linux-gnueabihf/libz.so && \
cp -r lib/arm-linux-gnueabihf/* /tmp/sysroot/lib && \
cp -r usr/lib/arm-linux-gnueabihf/* /tmp/sysroot/usr/lib && \
cp -r usr/include/* /tmp/sysroot/usr/include && \
ln -rs /tmp/sysroot/lib/libusb-1.0.so.0.1.0 /tmp/sysroot/lib/libusb-1.0.so && \
ln -rs /tmp/sysroot/lib/libudev.so.1.6.13 /tmp/sysroot/lib/libudev.so && \
ln -rs /tmp/sysroot/lib/libz.so.1.2.11 /tmp/sysroot/lib/libz.so && \
ln -s /usr/local /tmp/sysroot/usr/local && \
cd /tmp && rm -Rf /tmp/debs
ENV CC="/opt/toolchain/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-gcc"
ENV CXX="/opt/toolchain/gcc-linaro-6.3.1-2017.05-x86_64_arm-linux-gnueabihf/bin/arm-linux-gnueabihf-g++"
ENV LDFLAGS="-v -L /lib -L /usr/lib --sysroot /tmp/sysroot"
ENV CFLAGS="-L /lib -L /usr/lib --sysroot /tmp/sysroot"
ENV CXXFLAGS="-L /lib -L /usr/lib --sysroot /tmp/sysroot"
# Install GOCV
ENV OPENCV_VERSION $OPENCV_VERSION
RUN cd /tmp && \
curl -Lo opencv.zip https://github.com/opencv/opencv/archive/${OPENCV_VERSION}.zip && \
unzip -q opencv.zip && \
curl -Lo opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/${OPENCV_VERSION}.zip && \
unzip -q opencv_contrib.zip && \
rm opencv.zip opencv_contrib.zip && \
cd opencv-${OPENCV_VERSION} && \
mkdir build && cd build && \
cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib-${OPENCV_VERSION}/modules \
-D WITH_JASPER=OFF \
-D WITH_QT=OFF \
-D WITH_GTK=OFF \
-D BUILD_DOCS=OFF \
-D BUILD_EXAMPLES=OFF \
-D BUILD_TESTS=OFF \
-D BUILD_PERF_TESTS=OFF \
-D BUILD_opencv_java=NO \
-D BUILD_opencv_python=NO \
-D BUILD_opencv_python2=NO \
-D BUILD_opencv_python3=NO \
-D OPENCV_GENERATE_PKGCONFIG=ON .. && \
make -j $(nproc --all) && \
make preinstall && make install && \
cd /tmp && rm -rf opencv*
# Fetch the edgetpu library locally
ADD libedgetpu/out/throttled/armv7a/libedgetpu.so.1.0 /tmp/sysroot/usr/lib/libedgetpu.so.1.0
RUN ln -rs /tmp/sysroot/usr/lib/libedgetpu.so.1.0 /tmp/sysroot/usr/lib/libedgetpu.so.1 && \
ln -rs /tmp/sysroot/usr/lib/libedgetpu.so.1.0 /tmp/sysroot/usr/lib/libedgetpu.so && \
mkdir -p /tmp/sysroot/usr/include/libedgetpu
ADD libedgetpu/tflite/public/edgetpu.h /tmp/sysroot/usr/include/libedgetpu/edgetpu.h
ADD libedgetpu/tflite/public/edgetpu_c.h /tmp/sysroot/usr/include/libedgetpu/edgetpu_c.h
# Install Go
ENV GO_VERSION $GO_VERSION
ARG GO_ARCH="amd64"
ENV GOOS=linux
RUN curl -Lo go${GO_VERSION}.linux-$GO_ARCH.tar.gz https://dl.google.com/go/go${GO_VERSION}.linux-$GO_ARCH.tar.gz && \
tar -C /usr/local -xzf go${GO_VERSION}.linux-$GO_ARCH.tar.gz && \
rm go${GO_VERSION}.linux-$GO_ARCH.tar.gz
ENV PATH /usr/local/go/bin:/go/bin:${PATH}
ENV GOPATH /go
# Start compile
WORKDIR /build
ADD . .
# Install/Compile tools
ENV CGO_ENABLED=0
RUN make tools
# Compile DOODS
ENV GOARCH=arm
ENV CGO_ENABLED=1
ENV CGO_LDFLAGS="-v -L /lib -L /usr/lib -L /usr/local/lib --sysroot /tmp/sysroot -ledgetpu"
ENV CGO_CFLAGS="-L /lib -L /usr/lib -L /usr/local/lib -I /opt/tensorflow --sysroot /tmp/sysroot"
ENV CGO_CXXFLAGS="-L /lib -L /usr/lib -L /usr/local/lib -I /opt/tensorflow --sysroot /tmp/sysroot"
RUN make
# Start creating the new root directory
WORKDIR /tmp/newroot
RUN mkdir -p /tmp/newroot/lib && mkdir -p /tmp/newroot/usr/lib && \
cp -r /tmp/sysroot/lib/* /tmp/newroot/lib && \
cp -r /tmp/sysroot/usr/lib/* /tmp/newroot/usr/lib
# Copy doods executable
RUN mkdir -p /tmp/newroot/opt/doods && \
cp /build/doods /tmp/newroot/opt/doods/doods
WORKDIR /tmp/newroot/opt/doods
# Download sample models
RUN mkdir models
RUN wget https://storage.googleapis.com/download.tensorflow.org/models/tflite/coco_ssd_mobilenet_v1_1.0_quant_2018_06_29.zip && unzip coco_ssd_mobilenet_v1_1.0_quant_2018_06_29.zip && rm coco_ssd_mobilenet_v1_1.0_quant_2018_06_29.zip && mv detect.tflite models/coco_ssd_mobilenet_v1_1.0_quant.tflite && rm labelmap.txt
RUN wget https://dl.google.com/coral/canned_models/coco_labels.txt && mv coco_labels.txt models/coco_labels0.txt
RUN wget http://download.tensorflow.org/models/object_detection/faster_rcnn_inception_v2_coco_2018_01_28.tar.gz && tar -zxvf faster_rcnn_inception_v2_coco_2018_01_28.tar.gz faster_rcnn_inception_v2_coco_2018_01_28/frozen_inference_graph.pb --strip=1 --no-same-owner && mv frozen_inference_graph.pb models/faster_rcnn_inception_v2_coco_2018_01_28.pb && rm faster_rcnn_inception_v2_coco_2018_01_28.tar.gz
RUN wget https://raw.githubusercontent.com/amikelive/coco-labels/master/coco-labels-2014_2017.txt && mv coco-labels-2014_2017.txt models/coco_labels1.txt
ADD config.arm.yaml config.yaml
FROM arm32v7/debian:buster-slim
# Copy the pre-built root filesystem
COPY --from=base /tmp/newroot/. /.
COPY --from=base /usr/local/. /usr/local/.
# Needed because we can't run ldconfig
ENV LD_LIBRARY_PATH=/usr/local/lib
WORKDIR /opt/doods
CMD ["/opt/doods/doods", "-c", "/opt/doods/config.yaml", "api"]