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

阅读: 评论:0

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

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

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

系统配置

Software versions
OSWindows, Linux
Python3.8
TensorFlow2.2.0
CUDA Toolkit10.1
CuDNN7.6.5
Anaconda Navigator2.1.1

1. 安装Python

安装Anaconda。
下载地址:
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:Usersxxx>
  • 所有新的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;duce_sum(al([1000, 1000])))"

可以得到类似如下输出

2022-03-03 20:46:47.246800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cudart64_101.dll
2022-03-03 20:46:51.783800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library nvcuda.dll
2022-03-03 20:46:51.909800: I tensorflow/core/common_runtime/gpu/gpu_device: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:44] Successfully opened dynamic library cudart64_101.dll
2022-03-03 20:46:51.929800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cublas64_10.dll
2022-03-03 20:46:51.938800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cufft64_10.dll
2022-03-03 20:46:51.946800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library curand64_10.dll
2022-03-03 20:46:51.956800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cusolver64_10.dll
2022-03-03 20:46:51.967800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cusparse64_10.dll
2022-03-03 20:46:51.987800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cudnn64_7.dll
2022-03-03 20:46:52.003800: I tensorflow/core/common_runtime/gpu/gpu_device:1
703] Adding visible gpu devices: 0
2022-03-03 20:46:52.009800: I tensorflow/core/platform/cpu_feature_guard: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:168] XL
A service 0x69b8bc60 initialized for platform Host (this does not guarantee thatXLA will be used). Devices:
2022-03-03 20:46:52.038800: I tensorflow/compiler/xla/service/service:176]
StreamExecutor device (0): Host, Default Version
2022-03-03 20:46:52.049800: I tensorflow/core/common_runtime/gpu/gpu_device: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:44] Successfully opened dynamic library cudart64_101.dll
2022-03-03 20:46:52.071800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cublas64_10.dll
2022-03-03 20:46:52.078800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cufft64_10.dll
2022-03-03 20:46:52.085800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library curand64_10.dll
2022-03-03 20:46:52.091800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cusolver64_10.dll
2022-03-03 20:46:52.100800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cusparse64_10.dll
2022-03-03 20:46:52.106800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cudnn64_7.dll
2022-03-03 20:46:52.120800: I tensorflow/core/common_runtime/gpu/gpu_device:1
703] Adding visible gpu devices: 0
2022-03-03 20:46:53.766800: I tensorflow/core/common_runtime/gpu/gpu_device: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:1
108]      0
2022-03-03 20:46:53.778800: I tensorflow/core/common_runtime/gpu/gpu_device:1
121] 0:   N
2022-03-03 20:46:53.794800: I tensorflow/core/common_runtime/gpu/gpu_device: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:168] XL
A service 0x94a3aef0 initialized for platform CUDA (this does not guarantee thatXLA will be used). Devices:
2022-03-03 20:46:53.818800: I tensorflow/compiler/xla/service/service: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 Toolkitv10.1
CuDNN7.6.5
2.2.1安装 CUDA Toolkit

选择对应版本 10.1,具体安装教程见 .1/cuda-installation-guide-microsoft-windows/index.html

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

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

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

  • <INSTALL_PATH>NVIDIA GPU Computing ToolkitCUDAv10.1bin

  • <INSTALL_PATH>NVIDIA GPU Computing ToolkitCUDAv10.1libnvvp

  • <INSTALL_PATH>NVIDIA GPU Computing ToolkitCUDAv10.1extrasCUPTIlibx64

  • <INSTALL_PATH>NVIDIA GPU Computing ToolkitCUDAv10.1cudabin

2.1.3更新显卡驱动(N卡)

.aspx下载更新驱动

此时最好重启一下电脑。

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

python -c "import tensorflow as tf;duce_sum(al([1000, 1000])))"

得到类似输出

2022-03-03 21:05:08.385800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cudart64_101.dll
2022-03-03 21:05:12.660800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library nvcuda.dll
2022-03-03 21:05:12.801800: I tensorflow/core/common_runtime/gpu/gpu_device: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:44] Successfully opened dynamic library cudart64_101.dll
2022-03-03 21:05:12.828800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cublas64_10.dll
2022-03-03 21:05:12.842800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cufft64_10.dll
2022-03-03 21:05:12.849800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library curand64_10.dll
2022-03-03 21:05:12.862800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cusolver64_10.dll
2022-03-03 21:05:12.875800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cusparse64_10.dll
2022-03-03 21:05:12.895800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cudnn64_7.dll
2022-03-03 21:05:12.914800: I tensorflow/core/common_runtime/gpu/gpu_device:1
703] Adding visible gpu devices: 0
2022-03-03 21:05:12.921800: I tensorflow/core/platform/cpu_feature_guard: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:168] XL
A service 0x61d0e890 initialized for platform Host (this does not guarantee thatXLA will be used). Devices:
2022-03-03 21:05:12.950800: I tensorflow/compiler/xla/service/service:176]
StreamExecutor device (0): Host, Default Version
2022-03-03 21:05:12.961800: I tensorflow/core/common_runtime/gpu/gpu_device: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:44] Successfully opened dynamic library cudart64_101.dll
2022-03-03 21:05:12.980800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cublas64_10.dll
2022-03-03 21:05:12.987800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cufft64_10.dll
2022-03-03 21:05:12.995800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library curand64_10.dll
2022-03-03 21:05:13.001800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cusolver64_10.dll
2022-03-03 21:05:13.008800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cusparse64_10.dll
2022-03-03 21:05:13.015800: I tensorflow/stream_executor/platform/default/dso_lo
ader:44] Successfully opened dynamic library cudnn64_7.dll
2022-03-03 21:05:13.031800: I tensorflow/core/common_runtime/gpu/gpu_device:1
703] Adding visible gpu devices: 0
2022-03-03 21:05:14.591800: I tensorflow/core/common_runtime/gpu/gpu_device: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:1
108]      0
2022-03-03 21:05:14.605800: I tensorflow/core/common_runtime/gpu/gpu_device:1
121] 0:   N
2022-03-03 21:05:14.622800: I tensorflow/core/common_runtime/gpu/gpu_device: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:168] XL
A service 0x93c96920 initialized for platform CUDA (this does not guarantee thatXLA will be used). Devices:
2022-03-03 21:05:14.652800: I tensorflow/compiler/xla/service/service: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

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

2.4 Protobuf 安装与配置

在 网站中选择windows 版本(最下面),解压后将bin文件夹中的【】放到modelsresearch文件夹

在modelsresearch目录下打开命令行窗口,输入:

# 在目录 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 /?LinkId=691126
然后在tensorflow环境终端输入

pip install cython
pip install git+.git#原博主的这个地址是旧的,请用此链接
pip install pycocotools  #也可以
pip install pycocotools-windows -i  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的文章帮我一个大忙,原文地址

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

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

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

本文发布于:2024-02-02 15:33:56,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170685923944743.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:环境   测试   Object   TensorFlow   Detection
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23