环境情况:
1、ubuntu 18.04.3(虚拟环境)
2、python 3.6.8(如果有多个python,需要删除一些,并只保留一个)
https://www.tensorflow.org/install/source
1、安装TensorFlow
安装 Python 和 TensorFlow 软件包依赖项
sudo apt install python-dev python-pip # or python3-dev python3-pip
安装 TensorFlow pip 软件包依赖项(如果使用虚拟环境,请省略 --user 参数):
pip install -U --user pip six numpy wheel setuptools mock future>=0.17.1
pip install -U --user keras_applications==1.0.6 --no-deps
pip install -U --user keras_preprocessing==1.0.5 --no-deps
实际:
sudo apt install python3-dev python3-pip
pip3 install -U pip six numpy wheel setuptools mock future>=0.17.1
pip3 install -U --user keras_applications==1.0.6 --no-deps
pip3 install -U --user keras_preprocessing==1.0.5 --no-deps
2、安装Bazel
- 步骤1:将Bazel发行版URI添加为包源
sudo apt install curl
curl https://bazel.build/bazel-release.pub.gpg | sudo apt-key add -
echo "deb [arch=amd64] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list
- 步骤2:安装和更新Bazel
sudo apt update && sudo apt install bazel(无效)
sudo apt update && sudo apt install bazel-1.1.0(变更为此版本)
sudo apt update && sudo apt full-upgrade
sudo apt install openjdk-11-jdk
3、安装tensorflow
image.png1、安装git
sudo apt install git
git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
2、配置编译系统
./configure
选是否的可以用 'n'
3、编译
bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
变更为(上面那句受限于内存大小,会中途终止):
bazel build -c opt --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-msse4.1 --copt=-msse4.2 -k //tensorflow/tools/pip_package:build_pip_package --local_resources 3072,.5,1.0
当提示如下错误,但又明明安装了numpy时,可能是多个python版本造成的。
Traceback (most recent call last):
File "<string>", line 1, in <module>
ModuleNotFoundError: No module named 'numpy'
编译时,可能会出现网络404的错误,多试几次,或尝试翻墙。
./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
pip install /tmp/tensorflow_pkg/tensorflow-2.0.0-cp36-cp36m-linux_x86_64.whl
PS: 1、最后的文件,看自己电脑中“tmp/tensorflow_pkg/”中的文件名
2、第二个命令执行时,需要pip进行在线下载,可能由于网络原因而中断,多试几次
此时会报错:
Could not find a version that satisfies the requirement grpcio>=1.8.6 (from tf-nightly==2.0.0) (from versions: none)
当出现“Successfully installed ....”则属于安装完成。
3 安装tf-faster-rcnn
需要先安装:
// pip安装cython
pip install --user --upgrade cython
// 安装cuda(不用安装,这个是gpu用的,虚拟机装不上)
wget http://developer.download.nvidia.com/compute/cuda/10.2/Prod/local_installers/cuda_10.2.89_440.33.01_linux.run
sudo sh cuda_10.2.89_440.33.01_linux.run
下载并编译
git clone https://github.com/endernewton/tf-faster-rcnn.git
cd ./tf-faster-rcnn/lib
make clean
make
下载下来的版本,默认采用gpu进行编译(如果本身系统带gpu的,直接编译即可),需要做如下调整变更为cpu模式:
- /lib/model/config.py
# Use GPU implementation of non-maximum suppression
__C.USE_GPU_NMS = False
- lib/model/nms_wrapper.py
# --------------------------------------------------------
# Fast R-CNN
# Copyright (c) 2015 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Ross Girshick
# --------------------------------------------------------
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from model.config import cfg
#from nms.gpu_nms import gpu_nms
from nms.cpu_nms import cpu_nms
def nms(dets, thresh, force_cpu=False):
"""Dispatch to either CPU or GPU NMS implementations."""
if dets.shape[0] == 0:
return []
#if cfg.USE_GPU_NMS and not force_cpu:
#return gpu_nms(dets, thresh, device_id=0)
else:
return cpu_nms(dets, thresh)
-
lib/setup.py
image.png
4 安装 Python COCO API,这是为了使用COCO数据库
cd data
git clone https://github.com/pdollar/coco.git
cd coco/PythonAPI
make
cd ../../..
5 下载预训练模型
# Resnet101 for voc在07 + 12上进行了预训练设置
./data/scripts/fetch_faster_rcnn_models.sh
faster_rcnn路径下执行
NET=res101
TRAIN_IMDB=voc_2007_trainval+voc_2012_trainval
mkdir -p output/${NET}/${TRAIN_IMDB}
cd output/${NET}/${TRAIN_IMDB}
ln -s ../../../data/voc_2007_trainval+voc_2012_trainval ./default
cd ../../..
运行示例程序(转到最下面)
GPU_ID=01
CUDA_VISIBLE_DEVICES=${GPU_ID} ./tools/demo.py
其他缺少的安装内容
pip install --user easydict
pip install --user opencv-python
pip install --user matplotlib
安装旧版本的tensorflow,新版本有个内容移除了,但r-cnn并没有变更,会报错
pip uninstall tensorflow
pip install --user tensorflow==1.13.2
6 结束
至此环境搭建完成,demo运行效果如下图:
image.png
参考地址:
1、https://docs.bazel.build/versions/master/install-ubuntu.html
2、https://www.tensorflow.org/install/source#setup_for_linux_and_macos
网友评论