登录
首页精彩阅读Python深度学习之图像识别
Python深度学习之图像识别
2019-11-19
收藏
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别

作者 | 周伟能

来源 | 小叮当讲SAS和Python

Python在机器学习(人工智能,AI)方面有着很大的优势。谈到人工智能,一般也会谈到其实现的语言Python。前面有几讲也是关于机器学习在图像识别中的应用。今天再来讲一个关于运用google的深度学习框架tensorflow和keras进行训练深度神经网络,并对未知图像进行预测。

导入python模块

Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别

导入图像数据

Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别

合并列表数据

Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别

将图片数据转化为数组

Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别

显示一张图片

Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别

训练神经网络

Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别

我们可以看到测试集的准确率达到99.67%

预测一个图像

Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别

预测为汽车的概率为100%。(括号内为真实标签)

Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别

预测为美女的概率为100%。(括号内为真实标签)

Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别

测试集中前15个图像预测完全正确。Nice!

最后我们来识别单张图片。

Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别

结果预测为汽车。Nice!

最后来预测一下外部随便下载的汽车或美女图片

Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别

预测为汽车,不错!

小编这里有10张图片,前5张为汽车图片,后五张为美女图片。

下面进行批量预测:

Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别
Python<a href='/map/shenduxuexi/' style='color:#000;font-size:inherit;'>深度学习</a>之图像识别

结果也是完全正确。

看到这里,感觉神经网络是不是很神奇,要想让神经网络预测得准确,我们就必须给予大量的数据进行训练模型,优化模型,以至于达到准确识别图像的目的,图像识别作为人工智能的一部分,现在已经慢慢走向成熟,虽然机器也有出错的时候,但是进过不断优化,错误率将会越来越小,相信机器智能或者人工智能时代能够创造出更多智能而美好的东西。为社会,为人类的自由做出更大的贡献。

数据分析咨询请扫描二维码

最新资讯
更多
客服在线
立即咨询