【发布时间】:2021-03-11 01:27:25
【问题描述】:
我正在尝试对 github 存储库 (KPConv) 进行 dockerize。该存储库包含用于在tensorflow 中构建和训练神经网络模型的代码。该模型在 C++ 中实现了自己的custom tensorflow operations; KPConv/tf_custom_ops。它提供了一个 bash 文件来编译操作
#!/bin/bash
# Get TF variables
TF_INC=$(python3 -c 'import tensorflow as tf; print(tf.sysconfig.get_include())')
TF_LIB=$(python3 -c 'import tensorflow as tf; print(tf.sysconfig.get_lib())')
# Neighbors op
g++ -std=c++11 -shared tf_neighbors/tf_neighbors.cpp tf_neighbors/neighbors/neighbors.cpp cpp_utils/cloud/cloud.cpp -o tf_neighbors.so -fPIC -I$TF_INC -I$TF_INC/external/nsync/public -L$TF_LIB -ltensorflow_framework -O2 -D_GLIBCXX_USE_CXX11_ABI=0
g++ -std=c++11 -shared tf_neighbors/tf_batch_neighbors.cpp tf_neighbors/neighbors/neighbors.cpp cpp_utils/cloud/cloud.cpp -o tf_batch_neighbors.so -fPIC -I$TF_INC -I$TF_INC/external/nsync/public -L$TF_LIB -ltensorflow_framework -O2 -D_GLIBCXX_USE_CXX11_ABI=0
# Subsampling op
g++ -std=c++11 -shared tf_subsampling/tf_subsampling.cpp tf_subsampling/grid_subsampling/grid_subsampling.cpp cpp_utils/cloud/cloud.cpp -o tf_subsampling.so -fPIC -I$TF_INC -I$TF_INC/external/nsync/public -L$TF_LIB -ltensorflow_framework -O2 -D_GLIBCXX_USE_CXX11_ABI=0
g++ -std=c++11 -shared tf_subsampling/tf_batch_subsampling.cpp tf_subsampling/grid_subsampling/grid_subsampling.cpp cpp_utils/cloud/cloud.cpp -o tf_batch_subsampling.so -fPIC -I$TF_INC -I$TF_INC/external/nsync/public -L$TF_LIB -ltensorflow_framework -O2 -D_GLIBCXX_USE_CXX11_ABI=0
在我的 Dockerfile 中,我尝试在构建映像时调用此脚本来编译操作
FROM tensorflow/tensorflow:1.12.0-devel-gpu-py3
RUN apt-get update && apt install -y --fix-missing --no-install-recommends\
python3-setuptools python3-pip python3-tk
RUN pip install --upgrade pip
RUN pip3 install numpy scikit-learn psutil matplotlib pyqt5 laspy tensorflow-gpu==1.12.0
COPY . /kpconv
WORKDIR /kpconv/tf_custom_ops
RUN sh compile_op.sh
WORKDIR /kpconv/cpp_wrappers
RUN sh compile_wrappers.sh
WORKDIR /kpconv
# Build
# sudo docker build -t kpconv-test .
# Run
# sudo docker run --rm -it --runtime=nvidia kpconv-test /bin/bash
尽管无论我尝试什么(我不会在此问题的内容中添加的详尽列表),它总是无法编译代码。特别是例外(或部分例外)
ImportError: libcuda.so.1: cannot open shared object file: No such file or directory
不过,这不一定是问题。
我决定暂时搁置这个问题,并注释掉 Dockerfile 中 RUN compile_op.sh 的行。这导致了成功的图像构建。然后我启动了容器。经过一些其他工作后,我决定看看如果在容器内运行sh compile_op.sh 会发生什么。这神奇地工作没有问题。
$ sudo docker run --rm -it --runtime=nvidia kpconv-test /bin/bash
root@c36376e71177:/kpconv# cd tf_custom_ops/
root@c36376e71177:/kpconv/tf_custom_ops# sh compile_op.sh
root@c36376e71177:/kpconv/tf_custom_ops#
进一步的证据是通过调用 KPConv 中提供的训练脚本之一来尝试开始神经网络的训练运行。
首先,没有sh compile_op.sh
$ sudo docker run --rm -it --runtime=nvidia kpconv-test /bin/bash
root@64ed930c5aca:/kpconv# python training_ModelNet40.py
Traceback (most recent call last):
<...>
tensorflow.python.framework.errors_impl.NotFoundError: tf_custom_ops/tf_neighbors.so: cannot open shared object file: No such file or directory
root@64ed930c5aca:/kpconv#
虽然编译后编译的.sos可以导入,脚本也可以继续运行(我其实没有数据集)
$ sudo docker run --rm -it --runtime=nvidia kpconv-test /bin/bash
root@938a9d7bc2b0:/kpconv# cd tf_custom_ops/
root@938a9d7bc2b0:/kpconv/tf_custom_ops# sh compile_op.sh
root@938a9d7bc2b0:/kpconv/tf_custom_ops# cd ..
root@938a9d7bc2b0:/kpconv# python training_ModelNet40.py
Dataset Preparation
*******************
<...>
OSError: Data/ModelNet40/modelnet40_normal_resampled/modelnet40_train.txt not found.
root@938a9d7bc2b0:/kpconv#
在 Dockerfile 中调用 sh compile_op.sh 与在容器中调用 sh compile_op.sh 是否存在一些根本区别,导致前者失败而后者成功?
我感觉答案可能在于在 Dockerfile 中使用 RUN sh ...。也许sh 在docker build 过程中无法访问“正确的”$PATH 或其他环境变量...?
【问题讨论】:
-
容器是否已经包含 g++,因为您从未安装 build-essential。我可能对 tensorflow 包了解不够。
-
也许在您尝试运行编译时尚未安装该库,并且 docker 文件中的后续操作安装该库。
标签: c++ docker tensorflow g++