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

参照博主dy_guox的帖子:
原文地址
建设环境,但原帖是一年多前原博主写的,在我的建设过程中有一些弯路。

[En]

Build the environment, but the original post was written by the original blogger more than a year ago, and there are some detours in my construction process.

记录更新的施工过程。

[En]

Record the construction process of the update.

因为我的旧电脑是win7系统,无法安装最新版的cuda,因此选择和原博主一样的版本,版本虽然旧但不影响学习。

系统配置

Software versionsOSWindows, LinuxPython3.8TensorFlow2.2.0CUDA Toolkit10.1CuDNN7.6.5Anaconda Navigator2.1.1

安装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
  • 这将在命令之前具有环境名称,例如:
    [En]

    this will have the name of the environment before the command, such as:*

(tf) C:\Users\xxx>
  • 所有新的python插件安装都在此环境(Terminal)下进行

写本文时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版本

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

系统要求Nvidia GPU(GTX 650 or newer)CUDA Toolkitv10.1CuDNN7.6.5

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

  • 进入 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安装目录

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

在系统变量中找到’PATH’,编辑,加入以下路径(

*
*
*

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版本已经安装成功。

https://github.com/tensorflow/models

从github上下载项目(右上角”Clone or download”-“DownloadZIP”),下载到本地目录(避免中文和空格),解压.

因为我的工作环境是在anaconda里建立的,因此放到anaconda的tf虚拟环境下的tensorflow文件夹,我的目录位置如下:
C:\ProgramData\Anaconda3\envs\tf\Lib\site-packages\tensorflow

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

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


protoc object_detection/protos/*.proto --python_out=.

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

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

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

cd /d TensorFlow/models/research/object_detection/packages/tf2

python setup.py install

博主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

  • 在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

至此,原始样图中的狗、人和风筝就会出现。

[En]

At this point, the dogs, people and kites in the original sample picture will appear.

最后,我要感谢两位前博客作者的文章。如果你有任何问题,请到原博主的评论中询问,我是一个初学者,只是修改过时的内容。

[En]

Finally, I would like to thank the two former bloggers for their articles. If you have any questions, go to the comments of the original blogger to ask, I am a beginner, just change the outdated content.

Original: https://blog.csdn.net/idfengming/article/details/123262822
Author: Firmin123456
Title: TensorFlow 2 Object Detection API 环境搭建与测试(Windows)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/509173/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球