最近稍微看了一下docker的容器和镜像的构建,然后根据网上的教程调通了基于centos的caffe和caffe-ssd的代码,完成了Dockerfile的制作,难度不大,做个记录。
首先贴一下参考的两篇博客,https://blog.csdn.net/zhangtong11111/article/details/78795992,https://blog.csdn.net/kongxx/article/details/79019624。这里我参照的第一个博客,第二个看上去应该更简单一点。直接上代码!
# home下,基于centos的caffe镜像制作
FROM nvidia/cuda:9.0-cudnn7-devel-centos7
# 设置网络,没有网络限制的不需要设置
ARG https_proxy=https://(你使用的机器的网络)
ARG http_proxy=http://(你使用的机器的网络)
# 基本开发工具
RUN yum -y groupinstall "Development Tools" &&
# 安装通用依赖项
yum -y install python-devel &&
yum -y install protobuf-devel &&
yum -y install epel-release &&
yum -y install leveldb-devel &&
yum -y install snappy-devel &&
yum -y install opencv-devel &&
yum -y install boost-devel &&
yum -y install hdf5-devel &&
yum -y install atlas-devel &&
# 安装其他依赖
yum -y install gflags-devel &&
yum -y install glog-devel &&
yum -y install lmdb-devel &&
yum -y install wget &&
# 安装python依赖包
yum -y install python-pip &&
pip install --upgrade pip &&
pip install scikit-image &&
pip install protobuf &&
# 清除缓存包
yum clean all &&
rm -rf /var/cache/yum &&
# 下载caffe
cd /home/ &&
git clone https://github.com/bvlc/caffe.git &&
cd caffe/ &&
# 修改配置文件Makefile.config
# 打开 USE_CUDNN := 1
# 打开BLAS_INCLUDE=/path/to/your/blas 并修改为 BLAS_INCLUDE:=/usr/include
# 打开BLAS_lIB=/path/to/your/blas 并修改为 BLAS_lIB:=/usr/lib64/atlas
# 更改CUDA_ARCH设置,删除前两行
# 修改numpy路径:lib为lib64,dist-packages为site-packages
mv Makefile.config.example Makefile.config &&
sed -i 's/^# USE_CUDNN := 1/USE_CUDNN := 1/' Makefile.config &&
sed -i 's%^# BLAS_INCLUDE := /path/to/your/blas%BLAS_INCLUDE := /usr/include%' Makefile.config &&
sed -i 's%^# BLAS_LIB := /path/to/your/blas%BLAS_LIB := /usr/lib64/atlas%' Makefile.config &&
sed -i 's/^CUDA_ARCH := -gencode arch=compute_20,code=sm_20 //' Makefile.config &&
sed -i 's/-gencode arch=compute_20,code=sm_21 $//' Makefile.config &&
sed -i 's/^[ t]*-gencode arch=compute_30,code=sm_30 $/CUDA_ARCH := -gencode arch=compute_30,code=sm_30 /' Makefile.config &&
sed -i 's%/usr/lib/python2.7/dist-packages/numpy/core/include%/usr/lib64/python2.7/site-packages/numpy/core/include%' Makefile.config &&
# 修改配置文件Makefile
# LIBRARIES += cblas atlas 修改为 LIBRARIES +=satlas tatlas
sed -i 's/LIBRARIES += cblas atlas$/LIBRARIES += satlas tatlas/' Makefile &&
# 编译安装
make all -j32 &&
make test -j32 &&
# make runtest -j32 &&
make pycaffe -j32 &&
# 添加python环境变量
# vim ~/.bashrc
# 加入 export PYTHONPATH=/caffe所在目录/caffe/python
# source ~/.bashrc
# 或者如下将编译好的文件放到系统对应目录
cp -r python/caffe/ /usr/lib/python2.7/site-packages/ &&
cp /home/caffe/.build_release/lib/* /usr/lib64
基本都加了注释,最后一步添加python的环境变量,采用的是将编译好的文件放到系统问价下,这样删除caffe文件夹不影响调用caffe,其他也没有什么区别。
构建好caffe之后,下边是caffe-ssd的安装,本以为有什么不一样,还找了其他的教程,各种问题,结果还是用上边caffe的教程通过的,其实caffe-ssd就是别caffe多了一个写好的ssd的调用文件就是了,安装caffe-ssd就不用在单独安装caffe了,代码基本一致,不一样的地方用红色标出,如下:
# home下,基于centos的caffe-ssd镜像制作
FROM nvidia/cuda:9.0-cudnn7-devel-centos7
# 设置网络
ARG https_proxy=https://
ARG http_proxy=http://
# 基本开发工具
RUN yum -y groupinstall "Development Tools" &&
# 安装通用依赖项
yum -y install python-devel &&
yum -y install protobuf-devel &&
yum -y install epel-release &&
yum -y install leveldb-devel &&
yum -y install snappy-devel &&
yum -y install opencv-devel-3.4.2 &&
yum -y install boost-devel &&
yum -y install hdf5-devel &&
yum -y install atlas-devel &&
yum -y install openblas-devel &&
# 安装其他依赖
yum -y install gflags-devel &&
yum -y install glog-devel &&
yum -y install lmdb-devel &&
yum -y install wget &&
# 安装python依赖包
yum -y install python-pip &&
pip install --upgrade pip &&
pip install scikit-image &&
pip install protobuf &&
# 清除缓存包
yum clean all &&
rm -rf /var/cache/yum &&
# 下载caffe,切换ssd分支
cd /home/ &&
git clone https://github.com/weiliu89/caffe.git &&
cd caffe/ &&
git checkout ssd &&
# 修改配置文件Makefile.config
# 打开 USE_CUDNN := 1
# 打开BLAS_INCLUDE=/path/to/your/blas 并修改为 BLAS_INCLUDE:=/usr/include
# 打开BLAS_lIB=/path/to/your/blas 并修改为 BLAS_lIB:=/usr/lib64/atlas
# 更改CUDA_ARCH设置,删除前两行
# 修改numpy路径:lib为lib64,dist-packages为site-packages
mv Makefile.config.example Makefile.config &&
sed -i 's/^# USE_CUDNN := 1/USE_CUDNN := 1/' Makefile.config &&
sed -i 's%^# BLAS_INCLUDE := /path/to/your/blas%BLAS_INCLUDE := /usr/include%' Makefile.config &&
sed -i 's%^# BLAS_LIB := /path/to/your/blas%BLAS_LIB := /usr/lib64/atlas%' Makefile.config &&
sed -i 's/^CUDA_ARCH := -gencode arch=compute_20,code=sm_20 //' Makefile.config &&
sed -i 's/-gencode arch=compute_20,code=sm_21 $//' Makefile.config &&
sed -i 's/^[ t]*-gencode arch=compute_30,code=sm_30 $/CUDA_ARCH := -gencode arch=compute_30,code=sm_30 /' Makefile.config &&
sed -i 's%/usr/lib/python2.7/dist-packages/numpy/core/include%/usr/lib64/python2.7/site-packages/numpy/core/include%' Makefile.config &&
# 修改配置文件Makefile
# LIBRARIES += cblas atlas 修改为 LIBRARIES +=satlas tatlas
sed -i 's/LIBRARIES += cblas atlas$/LIBRARIES += satlas tatlas/' Makefile &&
# 编译安装
make all -j32 &&
make test -j32 &&
# make runtest -j32 &&
make pycaffe -j32 &&
# 添加python环境变量
# vim ~/.bashrc
# 加入 export PYTHONPATH=/SSD所在目录/caffe/python
# source ~/.bashrc
# 或者如下将编译好的文件放到系统对应目录
cp -r python/caffe/ /usr/lib/python2.7/site-packages/ &&
cp /home/caffe/.build_release/lib/* /usr/lib64
完成了,主要是为了记录,如果有正好需要的同志,可以参考一下