TensorFlow 2 Object Detection API 环境搭建与测试(Windows)

参照博主dy_guox的帖子:
原文地址
搭建环境,但是原贴是原博主一年多之前所写,我的搭建过程中有一些弯路。
把更新的搭建过程记录下来。
因为我的旧电脑是win7系统,无法安装最新版的cuda,因此选择和原博主一样的版本,版本虽然旧但不影响学习。

系统配置

Software versions
OS Windows, Linux
Python 3.8
TensorFlow 2.2.0
CUDA Toolkit 10.1
CuDNN 7.6.5
Anaconda Navigator 2.1.1

1. 安装Python

安装Anaconda。
下载地址: https://www.anaconda.com/products/individual
Anaconda安装过程网上有很多,此处略过.
接下来创建一个单独的conda环境

  • 开始菜单 - Anaconda 3- Anaconda Prompt
  • 创建一个名为‘tf’的新环境,Python 3.8版本
    命令行如下:(也可以有 Anaconda Navigator里点击“Environments”创建)
conda create -n tensorflow pip python=3.8
  • 激活此环境
conda activate tf #如果你的环境名称不同,用你的环境名称替换tf
  • 这样在命令前会有环境的名字,如:
(tf) C:\Users\xxx>
  • 所有新的python插件安装都在此环境(Terminal)下进行

2. 安装Tensorflow

2.1安装Tensorflow CPU版

写本文时Tensorflow已经发行到2.8.0版本,如果直接安装Tensorflow将会安装最新版2.8.0,与我们要安装的可以在Win7运行的CUDA Toolkit 10.1不兼容,因此通过命令行安装指定版本

pip install tensorflow==2.2.0 #注意版本号

安装好以后,输入

python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

可以得到类似如下输出

2022-03-03 20:46:47.246800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cudart64_101.dll
2022-03-03 20:46:51.783800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library nvcuda.dll
2022-03-03 20:46:51.909800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1
561] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: Quadro M1000M computeCapability: 5.0
coreClock: 1.0715GHz coreCount: 4 deviceMemorySize: 2.00GiB deviceMemoryBandwidt
h: 74.65GiB/s
2022-03-03 20:46:51.918800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cudart64_101.dll
2022-03-03 20:46:51.929800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cublas64_10.dll
2022-03-03 20:46:51.938800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cufft64_10.dll
2022-03-03 20:46:51.946800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library curand64_10.dll
2022-03-03 20:46:51.956800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2022-03-03 20:46:51.967800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2022-03-03 20:46:51.987800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2022-03-03 20:46:52.003800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1
703] Adding visible gpu devices: 0
2022-03-03 20:46:52.009800: I tensorflow/core/platform/cpu_feature_guard.cc:143]
 Your CPU supports instructions that this TensorFlow binary was not compiled to
use: AVX2
2022-03-03 20:46:52.028800: I tensorflow/compiler/xla/service/service.cc:168] XL
A service 0x69b8bc60 initialized for platform Host (this does not guarantee that
 XLA will be used). Devices:
2022-03-03 20:46:52.038800: I tensorflow/compiler/xla/service/service.cc:176]
StreamExecutor device (0): Host, Default Version
2022-03-03 20:46:52.049800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1
561] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: Quadro M1000M computeCapability: 5.0
coreClock: 1.0715GHz coreCount: 4 deviceMemorySize: 2.00GiB deviceMemoryBandwidt
h: 74.65GiB/s
2022-03-03 20:46:52.065800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cudart64_101.dll
2022-03-03 20:46:52.071800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cublas64_10.dll
2022-03-03 20:46:52.078800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cufft64_10.dll
2022-03-03 20:46:52.085800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library curand64_10.dll
2022-03-03 20:46:52.091800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2022-03-03 20:46:52.100800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2022-03-03 20:46:52.106800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2022-03-03 20:46:52.120800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1
703] Adding visible gpu devices: 0
2022-03-03 20:46:53.766800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1
102] Device interconnect StreamExecutor with strength 1 edge matrix:
2022-03-03 20:46:53.773800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1
108]      0
2022-03-03 20:46:53.778800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1
121] 0:   N
2022-03-03 20:46:53.794800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1
247] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 wit
h 1420 MB memory) -> physical GPU (device: 0, name: Quadro M1000M, pci bus id: 0
000:01:00.0, compute capability: 5.0)
2022-03-03 20:46:53.810800: I tensorflow/compiler/xla/service/service.cc:168] XL
A service 0x94a3aef0 initialized for platform CUDA (this does not guarantee that
 XLA will be used). Devices:
2022-03-03 20:46:53.818800: I tensorflow/compiler/xla/service/service.cc:176]
StreamExecutor device (0): Quadro M1000M, Compute Capability 5.0
tf.Tensor(1110.8943, shape=(), dtype=float32)

以上是CPU版本

2.2安装 GPU 支持项(可选)

若要安装在 GPU 上运行 TensorFlow,还要安装所需的驱动程序和其他软件
(假设使用的3.8版本python)

系统要求
Nvidia GPU (GTX 650 or newer)
CUDA Toolkit v10.1
CuDNN 7.6.5
2.2.1安装 CUDA Toolkit

https://developer.nvidia.com/cuda-toolkit-archive 选择对应版本 10.1,具体安装教程见 https://docs.nvidia.com/cuda/archive/10.1/cuda-installation-guide-microsoft-windows/index.html

2.2.2 安装 CUDNN
  • 进入 https://developer.nvidia.com/rdp/cudnn-download
  • 创建帐号,登录
  • 选择 cuDNN v7.6.5 (Nov 5, 2019), for CUDA 10.1 对应CUDA版本
  • 下载 cuDNN v7.6.5 Library for Windows 10
  • 解压 zip 文件到 CUDA安装目录 \NVIDIA GPU Computing Toolkit\CUDA\v10.1, 默认一般为 C:\Program Files.
2.2.3 设置环境变量

开始菜单搜索 ‘environment variables’ 或者‘系统变量’ , 或者桌面右键‘此电脑’- 属性-高级-环境变量

在系统变量中找到’PATH’,编辑,加入以下路径( 默认一般为 C:\Program Files):

  • \NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin

  • \NVIDIA GPU Computing Toolkit\CUDA\v10.1\libnvvp

  • \NVIDIA GPU Computing Toolkit\CUDA\v10.1\extras\CUPTI\libx64

  • \NVIDIA GPU Computing Toolkit\CUDA\v10.1\cuda\bin

2.1.3更新显卡驱动(N卡)

http://www.nvidia.com/Download/index.aspx下载更新驱动

此时最好重启一下电脑。

再次激活anaconda ‘tf’环境, 输入

python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

得到类似输出

2022-03-03 21:05:08.385800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cudart64_101.dll
2022-03-03 21:05:12.660800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library nvcuda.dll
2022-03-03 21:05:12.801800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1
561] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: Quadro M1000M computeCapability: 5.0
coreClock: 1.0715GHz coreCount: 4 deviceMemorySize: 2.00GiB deviceMemoryBandwidt
h: 74.65GiB/s
2022-03-03 21:05:12.816800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cudart64_101.dll
2022-03-03 21:05:12.828800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cublas64_10.dll
2022-03-03 21:05:12.842800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cufft64_10.dll
2022-03-03 21:05:12.849800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library curand64_10.dll
2022-03-03 21:05:12.862800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2022-03-03 21:05:12.875800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2022-03-03 21:05:12.895800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2022-03-03 21:05:12.914800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1
703] Adding visible gpu devices: 0
2022-03-03 21:05:12.921800: I tensorflow/core/platform/cpu_feature_guard.cc:143]
 Your CPU supports instructions that this TensorFlow binary was not compiled to
use: AVX2
2022-03-03 21:05:12.939800: I tensorflow/compiler/xla/service/service.cc:168] XL
A service 0x61d0e890 initialized for platform Host (this does not guarantee that
 XLA will be used). Devices:
2022-03-03 21:05:12.950800: I tensorflow/compiler/xla/service/service.cc:176]
StreamExecutor device (0): Host, Default Version
2022-03-03 21:05:12.961800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1
561] Found device 0 with properties:
pciBusID: 0000:01:00.0 name: Quadro M1000M computeCapability: 5.0
coreClock: 1.0715GHz coreCount: 4 deviceMemorySize: 2.00GiB deviceMemoryBandwidt
h: 74.65GiB/s
2022-03-03 21:05:12.973800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cudart64_101.dll
2022-03-03 21:05:12.980800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cublas64_10.dll
2022-03-03 21:05:12.987800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cufft64_10.dll
2022-03-03 21:05:12.995800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library curand64_10.dll
2022-03-03 21:05:13.001800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cusolver64_10.dll
2022-03-03 21:05:13.008800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cusparse64_10.dll
2022-03-03 21:05:13.015800: I tensorflow/stream_executor/platform/default/dso_lo
ader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2022-03-03 21:05:13.031800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1
703] Adding visible gpu devices: 0
2022-03-03 21:05:14.591800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1
102] Device interconnect StreamExecutor with strength 1 edge matrix:
2022-03-03 21:05:14.600800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1
108]      0
2022-03-03 21:05:14.605800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1
121] 0:   N
2022-03-03 21:05:14.622800: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1
247] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 wit
h 1419 MB memory) -> physical GPU (device: 0, name: Quadro M1000M, pci bus id: 0
000:01:00.0, compute capability: 5.0)
2022-03-03 21:05:14.641800: I tensorflow/compiler/xla/service/service.cc:168] XL
A service 0x93c96920 initialized for platform CUDA (this does not guarantee that
 XLA will be used). Devices:
2022-03-03 21:05:14.652800: I tensorflow/compiler/xla/service/service.cc:176]
StreamExecutor device (0): Quadro M1000M, Compute Capability 5.0
tf.Tensor(-186.61127, shape=(), dtype=float32)

包含GPU信息,说明GPU版本已经安装成功。

2.3下载Tensorflow object detection API

https://github.com/tensorflow/models

从github上下载项目(右上角“Clone or download”-“DownloadZIP”),下载到本地目录(避免中文和空格),解压.
因为我的工作环境是在anaconda里建立的,因此放到anaconda的tf虚拟环境下的tensorflow文件夹,我的目录位置如下:
C:\ProgramData\Anaconda3\envs\tf\Lib\site-packages\tensorflow

2.4 Protobuf 安装与配置

在 https://github.com/google/protobuf/releases 网站中选择windows 版本(最下面),解压后将bin文件夹中的【protoc.exe】放到models\research文件夹

在models\research\目录下打开命令行窗口,输入:

# 在目录 tensorflow/models/research
protoc object_detection/protos/*.proto --python_out=.

在这一步有时候会出错,可以尝试把/*.proto 这部分改成文件夹下具体的文件名,一个一个试,每运行一个,文件夹下应该出现对应的.py结尾的文件。不报错即可。

2.5 COCO API 安装

TensorFlow 2 需要安装COCO API,而且最好在 object detection api之前安装,不然很有可能报错。

在安装COCO API之前,还需要确认已经安装

Visual C++ 2015 Build Tools https://go.microsoft.com/fwlink/?LinkId=691126
然后在tensorflow环境终端输入

pip install cython
pip install git+https://github.com/cocodataset/cocoapi.git#原博主的这个地址是旧的,请用此链接
pip install pycocotools  #也可以
pip install pycocotools-windows -i https://pypi.tuna.tsinghua.edu.cn/simple#also fine

2.6 安装Tensorflow object detection API

tensorflow环境终端 cd 到对应路径, 此处原博主的方法在我的电脑上无效,也是我花费最多时间摸索的步骤

cd /d TensorFlow/models/research/object_detection/packages/tf2
#将TensorFlow/models改为你的文件夹目录
python setup.py install

2.7检验安装是否成功

博主dy_guox的检验方法在这里就不灵了
感谢博主Jokic_Rn的文章帮我一个大忙,原文地址
https://blog.csdn.net/weixin_44823313/article/details/113115245

  • 打开anaconda prompt激活tensorflow环境
  • 定位到object_detection_tutorial.ipynb所在目录research\object_detection\colab_tutorials
  • 在tf环境内定位到colab_tutorials文件夹,用以下命令打开jupyter notebook(如果没安装 用conda install jupyter notebook安装)
(tf) TensorFlow\models\research\object_detection\colab_tutorials>jupyter notebook
#将TensorFlow/models改为你的文件夹目录
  • 在Jupyter notebook里打开object_detection_tutorial.ipynb,选中命令单元后,点击"Cell"–“Run All Below”
import os
import pathlib


if "models" in pathlib.Path.cwd().parts:
  while "models" in pathlib.Path.cwd().parts:
    os.chdir('..')
elif not pathlib.Path('models').exists():
  !git clone --depth 1 https://github.com/tensorflow/models
  • 如果报错No module named ‘object_detection’ ,解决办法:在导入模块前加入
    !pip install tensorflow-object-detection-api,如下
!pip install tensorflow-object-detection-api
from object_detection.utils import ops as utils_ops
from object_detection.utils import label_map_util
from object_detection.utils import visualization_utils as vis_util

至此,原示例图片的狗、人和风筝就会出现了。

最后,感谢两位原博主的文章。如有问题,到原博主的评论处提问,我是入门小菜鸟,照葫芦画瓢改掉过时内容而已。

你可能感兴趣的:(学习,Python,python,tensorflow)