tensorflow图像识别-1 labelimg
Tensorflow图像识别-2 试一下识别
Tensorflow图像识别-3 训练,笔记本要退休的感觉
Tensorflow图像识别-4 应用
上一部分介绍了如何打标,对这方面的工作感到心累。还是老样子,先按照用TensorFlow训练一个物体检测器(手把手教学版)上面讲的做完,当然不止这些。
创建tensorflow虚拟环境
老样子,输入命令即可
# 之前已经创了,所以 直接列命令吧,创个环境
conda create -n tensorflow
# 这是我当前所有的虚拟环境
(base) PS>conda info --envs
# conda environments:
base * C:\Users\clean\Anaconda3
labelimg C:\Users\clean\Anaconda3\envs\labelimg
tensorflow C:\Users\clean\Anaconda3\envs\tensorflow
# 切到环境里,安装tensorflow gpu版或cpu版,这里需要注意你的gpu是否支持,可以上网查查cuda
conda activate tensorflow
conda install tensorflow-gpu
或者
conda install tensorflow
# 查看安装的库
conda list
然后,让我们看看是否安装成功了,(其实,你上面没报错什么的,应该就好了。)
测试
创个脚本,或命令行里进python也可
# Python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))
(tensorflow) PS>python
Python 3.7.3 (default, Apr 24 2019, 15:29:51) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
2019-06-09 20:21:16.242363: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2019-06-09 20:21:16.531573: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1433] Found device 0 with properties:
name: GeForce GTX 1060 major: 6 minor: 1 memoryClockRate(GHz): 1.733
pciBusID: 0000:01:00.0
totalMemory: 6.00GiB freeMemory: 4.97GiB
2019-06-09 20:21:16.541464: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1512] Adding visible gpu devices: 0
2019-06-09 20:21:17.918230: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-06-09 20:21:17.923506: I tensorflow/core/common_runtime/gpu/gpu_device.cc:990] 0
2019-06-09 20:21:17.926507: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1003] 0: N
2019-06-09 20:21:17.931783: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 4716 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1060, pci bus id: 0000:01:00.0, compute capability: 6.1)
>>> print(sess.run(hello))
b'Hello, TensorFlow!'
看到最后一行,就证明ok了。
安装TensorFlow Object Detection API(对象检测API)
创建能够在单个图像中定位和识别多个对象的精确机器学习模型仍然是计算机视觉中的核心挑战。TensorFlow对象检测API是一个基于TensorFlow构建的开源框架,可以轻松构建,训练和部署对象检测模型。(复制来的,github的readme)
github地址
目录安排
有点大,400多MB,我是整个下了。
喝杯咖啡?
下好文件后,弄清楚目录,或者跟我一样也行。
解压重命名成model
编译protoc
跟linux下不同,windows不能直接*匹配,上网找了一下。
Tensorflow Object Detection API 用 Protobufs 来配置模型和训练参数. 在用这个框架之前,必须先编译Protobuf 库。
(tensorflow) PS>pwd
Path
----
C:\Users\clean\Anaconda3\envs\tensorflow\models\research
(tensorflow) PS>Get-ChildItem object_detection/protos/*.proto |Resolve-Path -Relative | %{protoc $_ --python_out=.}
可以看到,该目录下多了很多.py文件
测试一下
按照原博客的情况,是安装了jupyter notebook做了一个小测试。那就跟着做吧
先加个环境变量
运行这条命令
python object_detection/builders/model_builder_test.py
会提示缺少matplotlib、PIL,安装下即可。PIL就是pillow
最后提示ok就好了。
(tensorflow) PS>python object_detection/builders/model_builder_test.py
WARNING: The TensorFlow contrib module will not be included in TensorFlow 2.0.
For more information, please see:
* https://github.com/tensorflow/community/blob/master/rfcs/20180907-contrib-sunset.md
* https://github.com/tensorflow/addons
If you depend on functionality not listed there, please file an issue.
......C:\Users\clean\Anaconda3\envs\tensorflow\lib\site-packages\numpy\lib\type_check.py:546: DeprecationWarning: np.asscalar(a) is deprecated since NumPy v1.16, use a.item() instead
'a.item() instead', DeprecationWarning, stacklevel=1)
......s...
----------------------------------------------------------------------
Ran 16 tests in 0.120s
OK (skipped=1)
(tensorflow) PS>conda install jupyter notebook
生活的“乐趣”可能就是踩坑,你想不到坑会在哪出现,怎么迈过去,或是迈不过去了。
拿衣服的时候发现没开洗衣机。。
接着,就在当前目录运行jupyter notebook
(tensorflow) PS>jupyter notebook [I 19:33:22.652 NotebookApp] Writing notebook server cookie secret to C:\Users\clean\AppData\Roaming\jupyter\runtime\notebook_cookie_secret
[I 19:33:25.831 NotebookApp] Serving notebooks from local directory: C:\Users\clean\Anaconda3\envs\tensorflow\models\research
[I 19:33:25.832 NotebookApp] The Jupyter Notebook is running at:
[I 19:33:25.833 NotebookApp] http://localhost:8888
[I 19:33:25.834 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 19:33:25.896 NotebookApp]
To access the notebook, open this file in a browser:
file:///C:/Users/clean/AppData/Roaming/jupyter/runtime/nbserver-3000-open.html
Or copy and paste one of these URLs:
http://localhost:8888/?token=f4e67464a9abbf46daa65c28b26609887be52c79198f23a7
打开这个文件
http://localhost:8888/notebooks/object_detection/object_detection_tutorial.ipynb
全部运行就好,shift+enter,或者
这里不知道为什么第一次运行就是没图片,在运行一次就好,另外,第二次可以把
Download Model
隐掉或删掉或变成标记。
结果如图
看完结果再看看上面的代码吧,反正这个坐下来是不懂太多,就一个流程而已,我大概试了5遍,加上看代码两遍吧,
简单的说,就是使用了一个模型,对图片进行目标寻找。
自己弄个图片玩玩吧
把图片加到目录,代码改两部分就好了,一个图片路径,一个循环控制几张图片。
可能不太适合卡通图片,识别成了泰迪熊,/捂脸