TensorFlow 2 Object Detection API 环境搭建与测试(Windows)_tensorflow2.x objectdetection maskrcnn api-程序员宅基地

技术标签: 学习  python  tensorflow  Python  

参照博主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安装目录 <INSTALL_PATH>\NVIDIA GPU Computing Toolkit\CUDA\v10.1, <INSTALL_PATH> 默认一般为 C:\Program Files.
2.2.3 设置环境变量

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

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

  • <INSTALL_PATH>\NVIDIA GPU Computing Toolkit\CUDA\v10.1\bin

  • <INSTALL_PATH>\NVIDIA GPU Computing Toolkit\CUDA\v10.1\libnvvp

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

  • <INSTALL_PATH>\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

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

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

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/idfengming/article/details/123262822

智能推荐

react-native如何动态引用本地图片_react native 本地图片-程序员宅基地

文章浏览阅读1.6k次。最近很多人都在问如何去动态的加载本地图片,因为rn自带的require是不能拼接路径的。下面来教大家如何:1.新建一个项目,新建一个图片文件夹,如下所示:2.创建一个js文件,引入图片,输出模块:3.引用文件,并开始拼接:项目地址:GitHub - lvao813/img-demo这个只是一种封装的思想,希望能给大家带来启发。作者:目标全栈链接:https://www.jianshu.com/p/9612b5608183来源:简书著作权归作者所有..._react native 本地图片

SAP Fiori 应用无法根据 contact 字段搜索的原因分析_sap 2000 unable to find-程序员宅基地

文章浏览阅读1.1k次。Issue description: account 4024555 下面挂了一个contact 4098335:但是在Fiori ui上,根据Thompson搜索不出这个contact出来:Debug后台逻辑:Search bar里输入的Thompson作为substringof的参数传到后台,按照fullName搜索:https://sap.corp:4080/sap/opu/od..._sap 2000 unable to find

How to achieve conditional break point in your ABAP program_conditional breakpoint the left-hand side-程序员宅基地

文章浏览阅读243次。Background有不同的同事问我这个问题:例如LOOP要执行1000次,我只对其中的某一次比如第501次循环感兴趣,我肯定不可能按500次F5.或者我只对 LOOP里某个变量为某一个具体值的那一次循环感兴趣。如果避免重复按F5, 而是让断点直接停在我想停的condition上面?其实有三种方法实现。Line 15 will be executed 1000 times. And we ..._conditional breakpoint the left-hand side

干货丨什么是SOA架构?SOA架构有什么作用?-程序员宅基地

文章浏览阅读3k次,点赞3次,收藏3次。针对传统的单体架构存在的问题,后来出现了一种SOA架构。SOA架构是一个面向服务的架构,它是一个组件模型。SOA架构将应用程序的不同功能单元(称为服务)进行拆分,并通过在这些服务之间定义良..._soa架构

OpenCASCADE:建立Body_brepbuilderapi_makeface-程序员宅基地

文章浏览阅读741次。OpenCASCADE:建立BodyOpenCASCADE:建立Body棱镜轮廓应用圆角添加颈部创建空心实体OpenCASCADE:建立Body棱镜轮廓要计算瓶子的主体,您需要创建一个实体形状。最简单的方法是使用先前创建的轮廓并沿一个方向扫描它。Open CASCADE Technology的Prism功能最适合该任务。它接受一个形状和一个方向作为输入,并根据以下规则生成一个新的形状:您当前的个人资料是一条线。参考 Shape/Generates 表,您需要根据其线计算面以生成实体。要创建人脸,_brepbuilderapi_makeface

006 - 编译 QtWebEngine_编译qtwebengine-程序员宅基地

文章浏览阅读4.2k次。编译 QtWebEngine文章目录编译 QtWebEngine前言源码下载编译安装编译过程中可能遇到的问题参考文档前言在之前的文章《Linux 安装 Qt》中,我说了一句 QtWenEngine 模块不好编译,暂时用不到就不管,可没想到苍天饶过谁,这么快就需要用 QtWenEngine 了,能怎么办,编呗,接着编……源码下载之前的 qt 源码里面有 webengine 的源码编译安装根据官方文档,先安装依赖库;根据官方推荐安装的,咱也不知道对不对,主要是这玩意太难编译了,就宁可多装一千,不能_编译qtwebengine

随便推点

软考高级五大证书,哪个更值得考?_软考高级证书有哪些-程序员宅基地

文章浏览阅读1.6w次。软考高级有这五个证书:信息系统项目管理师、系统分析师、系统架构设计师、网络规划设计师、系统规划与管理师。只是专业方向不同,但是产生的作用都是一样的,具体的作用有升职加薪,职称评定,抵扣个税,还有积分落户等。所以没有更值得,只有更适合自己的科目,建议在学习备考有一些疑惑的友,加一些学习群,可以及时解决,也可以看到大家的学习进度,有兴趣的可以来企鹅群:423914346在纠结选什么高级科目的友,一起来看:..._软考高级证书有哪些

【Deep Linking】:从浏览器调起 APP_s.browser_fallback_url 没有作用-程序员宅基地

文章浏览阅读1.9k次。Deep Linking 只是一个概念, 是指通过一个链接进入另一个网站/App,并直接浏览其内部的某个页面。 Deep Linking 给用户带来的是非常顺滑的浏览体验,尤其在 Web 世界中 Deep Linking 的实现非常容易。但如果要进入 App 并定位到对应的页面则较为困难,URI Scheme, Universal Links, Android App Links, 以及 Chro_s.browser_fallback_url 没有作用

UE4学习-AI导航网格的创建及修改_unreal 导航网格编辑-程序员宅基地

文章浏览阅读4.8k次,点赞3次,收藏7次。文章目录_unreal 导航网格编辑

SAP Spartacus的Structured data_set-structured data.-程序员宅基地

文章浏览阅读399次。https://sap.github.io/spartacus-docs/structured-data/Structured Data provides a data structure for the web that makes the web content more understandable for web crawlers. With only HTML structure, a web page provides insufficient understanding of the co_set-structured data.

SAP ABAP BOR type-程序员宅基地

文章浏览阅读517次。Created by Wang, Jerry, last modified on Jan 06, 2017You might have observed that in both settype and relationship metadata table, there are type BUS1178 maintained. What is it?It is BOR type for ..._abap bor

PASCAL VOC Challenge_【pascal voc】ascalvocchallenges-程序员宅基地

文章浏览阅读1.2k次。http://grunt1223.iteye.com/blog/970449在计算视觉的领域中,Pascal VOC Challenge 就好比是数学中的哥德巴赫猜想一样。Pascal的全称是Pattern Analysis, Statical Modeling and Computational Learning。每年,该组织都会提供一系列类别的、带标签的图片,挑战者通过设计各种精妙的算_【pascal voc】ascalvocchallenges

推荐文章

热门文章

相关标签