Python在机器学习中的应用--第十二章深度学习
第十二章深度学习
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
fonts=FontProperties(fname="/Library/Fonts/华文细黑.ttf",size=14)
import seaborn as snsfrom skimage.io import imread
from skimage.color import rgb2gray
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from sklearn import metrics
import tensorflow as tf
import tensorflow.compat.v1 as tf
# import tf.compat.v1 as tf
tf.disable_eager_execution()
import os
path_img=os.path.join(os.getcwd(),"data","chap12","莱娜.tiff")
im=imread(path_img)
imggray=rgb2gray(im)
plt.figure(figsize=(6,6))
plt.imshow(imggray,cmap=plt.cm.gray)
plt.axis("off")
plt.show()
print(imggray.shape)
#整理卷积输入
imggray=tf.convert_to_tensor(imggray,dtype=np.float32)
im_height,im_width=imggray.shape
input_im=tf.reshape(imggray,[1,im_height,im_width,1],name="image")
ker=tf.constant([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],dtype=tf.float32,name="ker")
kernel=tf.reshape(ker,[3,3,1,1],name="kernel")
#卷积运算,输出卷积后的图像
res=tf.squeeze(tf.nn.conv2d(input_im,kernel,strides=[1,1,1,1],padding="SAME"))
#进行运算
with tf.Session() as sess:conv_im1=sess.run(res)
print("卷积后图像的大小",conv_im1.shape)
#显示图像
plt.figure(figsize=(6,6))
plt.imshow(conv_im1,cmap=plt.cm.gray)
plt.axis("off")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
fonts=FontProperties(fname="/Library/Fonts/华文细黑.ttf",size=14)
import seaborn as snsfrom skimage.io import imread
from skimage.color import rgb2gray
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from sklearn import metrics
import tensorflow as tf
import tensorflow.compat.v1 as tf
# import tf.compat.v1 as tf
tf.disable_eager_execution()
import os
path_img=os.path.join(os.getcwd(),"data","chap12","莱娜.tiff")
im=imread(path_img)
imggray=rgb2gray(im)
plt.figure(figsize=(6,6))
plt.imshow(imggray,cmap=plt.cm.gray)
plt.axis("off")
plt.show()
print(imggray.shape)
#整理卷积输入
imggray=tf.convert_to_tensor(imggray,dtype=np.float32)
im_height,im_width=imggray.shape
input_im=tf.reshape(imggray,[1,im_height,im_width,1],name="image")
ker=tf.constant([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],dtype=tf.float32,name="ker")
kernel=tf.reshape(ker,[3,3,1,1],name="kernel")
#卷积运算,输出卷积后的图像
res=tf.squeeze(tf.nn.conv2d(input_im,kernel,strides=[1,1,1,1],padding="SAME"))
#进行运算
with tf.Session() as sess:conv_im1=sess.run(res)
print("卷积后图像的大小",conv_im1.shape)
#显示图像
plt.figure(figsize=(6,6))
plt.imshow(conv_im1,cmap=plt.cm.gray)
plt.axis("off")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
fonts=FontProperties(fname="/Library/Fonts/华文细黑.ttf",size=14)
import seaborn as snsfrom skimage.io import imread
from skimage.color import rgb2gray
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from sklearn import metrics
import tensorflow as tf
import tensorflow.compat.v1 as tf
# import tf.compat.v1 as tf
tf.disable_eager_execution()
import os
path_img=os.path.join(os.getcwd(),"data","chap12","莱娜.tiff")
im=imread(path_img)
imggray=rgb2gray(im)
plt.figure(figsize=(6,6))
plt.imshow(imggray,cmap=plt.cm.gray)
plt.axis("off")
plt.show()
print(imggray.shape)
#整理卷积输入
imggray=tf.convert_to_tensor(imggray,dtype=np.float32)
im_height,im_width=imggray.shape
input_im=tf.reshape(imggray,[1,im_height,im_width,1],name="image")
ker=tf.constant([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],dtype=tf.float32,name="ker")
kernel=tf.reshape(ker,[3,3,1,1],name="kernel")
#卷积运算,输出卷积后的图像
res=tf.squeeze(tf.nn.conv2d(input_im,kernel,strides=[1,1,1,1],padding="SAME"))
#进行运算
with tf.Session() as sess:conv_im1=sess.run(res)
print("卷积后图像的大小",conv_im1.shape)
#显示图像
plt.figure(figsize=(6,6))
plt.imshow(conv_im1,cmap=plt.cm.gray)
plt.axis("off")
plt.show()
im=imread(path_img)
plt.figure(figsize=(6,6))
plt.imshow(im)
plt.axis("off")
plt.show()#整理卷积输入,RGB图像为3通道
im=tf.convert_to_tensor(im,dtype=np.float32)
im_height,im_width,im_channels=im.shape
input_im=tf.reshape(im,[1,im_height,im_width,im_channels])
ker=tf.constant([[[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]]]],dtype=tf.float32,name="ker")print(ker)
kernel=tf.reshape(ker,[3,3,3,2],name="kernel")
##卷积运算,输出卷积后的结果
res=tf.nn.conv2d(input_im,kernel,strides=[1,2,2,1],padding="VALID")
##运算
with tf.Session() as sess:conv_im2=sess.run(res)
print("卷积后输出结果大小:",conv_im2.shape)
##查看卷积后的图像
plt.figure(figsize=(8,4))
plt.subplot(1,2,1)
plt.imshow(conv_im2[0][:,:,0],cmap=plt.cm.gray)
plt.axis("off")
plt.subplot(1,2,2)
plt.imshow(conv_im2[0][:,:,1],cmap=plt.cm.gray)
plt.axis("off")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
fonts=FontProperties(fname="/Library/Fonts/华文细黑.ttf",size=14)
import seaborn as snsfrom skimage.io import imread
from skimage.color import rgb2gray
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from sklearn import metrics
import tensorflow as tf
import tensorflow.compat.v1 as tf
# import tf.compat.v1 as tf
tf.disable_eager_execution()
import os
path_img=os.path.join(os.getcwd(),"data","chap12","莱娜.tiff")
im=imread(path_img)
imggray=rgb2gray(im)
plt.figure(figsize=(6,6))
plt.imshow(imggray,cmap=plt.cm.gray)
plt.axis("off")
plt.show()
print(imggray.shape)
#整理卷积输入
imggray=tf.convert_to_tensor(imggray,dtype=np.float32)
im_height,im_width=imggray.shape
input_im=tf.reshape(imggray,[1,im_height,im_width,1],name="image")
ker=tf.constant([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],dtype=tf.float32,name="ker")
kernel=tf.reshape(ker,[3,3,1,1],name="kernel")
#卷积运算,输出卷积后的图像
res=tf.squeeze(tf.nn.conv2d(input_im,kernel,strides=[1,1,1,1],padding="SAME"))
#进行运算
with tf.Session() as sess:conv_im1=sess.run(res)
print("卷积后图像的大小",conv_im1.shape)
#显示图像
plt.figure(figsize=(6,6))
plt.imshow(conv_im1,cmap=plt.cm.gray)
plt.axis("off")
plt.show()im=imread(path_img)
plt.figure(figsize=(6,6))
plt.imshow(im)
plt.axis("off")
plt.show()#整理卷积输入,RGB图像为3通道
im=tf.convert_to_tensor(im,dtype=np.float32)
im_height,im_width,im_channels=im.shape
input_im=tf.reshape(im,[1,im_height,im_width,im_channels])
ker=tf.constant([[[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]]]],dtype=tf.float32,name="ker")print(ker)
kernel=tf.reshape(ker,[3,3,3,2],name="kernel")
##卷积运算,输出卷积后的结果
res=tf.nn.conv2d(input_im,kernel,strides=[1,2,2,1],padding="VALID")
##运算
with tf.Session() as sess:conv_im2=sess.run(res)
print("卷积后输出结果大小:",conv_im2.shape)
##查看卷积后的图像
plt.figure(figsize=(8,4))
plt.subplot(1,2,1)
plt.imshow(conv_im2[0][:,:,0],cmap=plt.cm.gray)
plt.axis("off")
plt.subplot(1,2,2)
plt.imshow(conv_im2[0][:,:,1],cmap=plt.cm.gray)
plt.axis("off")
plt.show()'''池化'''
max_pool=tf.nn.max_pool(conv_im2,ksize=[1,2,2,1],strides=[1,1,1,1],padding="VALID")
with tf.Session() as sess:max_pool_val=sess.run(max_pool)
print("池化后输出结果大小:",max_pool_val.shape)plt.figure(figsize=(8,4))
plt.subplot(1,2,1)
plt.imshow(max_pool_val[0][:,:,0],cmap=plt.cm.gray)
plt.axis("off")
plt.subplot(1,2,2)
plt.imshow(max_pool_val[0][:,:,1],cmap=plt.cm.gray)
plt.axis("off")
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
fonts=FontProperties(fname="/Library/Fonts/华文细黑.ttf",size=14)
import seaborn as snsfrom skimage.io import imread
from skimage.color import rgb2gray
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from sklearn import metrics
import tensorflow as tf
import tensorflow.compat.v1 as tf
# import tf.compat.v1 as tf
tf.disable_eager_execution()
import os
path_img=os.path.join(os.getcwd(),"data","chap12","莱娜.tiff")
im=imread(path_img)
imggray=rgb2gray(im)
plt.figure(figsize=(6,6))
plt.imshow(imggray,cmap=plt.cm.gray)
plt.axis("off")
plt.show()
print(imggray.shape)
#整理卷积输入
imggray=tf.convert_to_tensor(imggray,dtype=np.float32)
im_height,im_width=imggray.shape
input_im=tf.reshape(imggray,[1,im_height,im_width,1],name="image")
ker=tf.constant([[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],dtype=tf.float32,name="ker")
kernel=tf.reshape(ker,[3,3,1,1],name="kernel")
#卷积运算,输出卷积后的图像
res=tf.squeeze(tf.nn.conv2d(input_im,kernel,strides=[1,1,1,1],padding="SAME"))
#进行运算
with tf.Session() as sess:conv_im1=sess.run(res)
print("卷积后图像的大小",conv_im1.shape)
#显示图像
plt.figure(figsize=(6,6))
plt.imshow(conv_im1,cmap=plt.cm.gray)
plt.axis("off")
plt.show()im=imread(path_img)
plt.figure(figsize=(6,6))
plt.imshow(im)
plt.axis("off")
plt.show()#整理卷积输入,RGB图像为3通道
im=tf.convert_to_tensor(im,dtype=np.float32)
im_height,im_width,im_channels=im.shape
input_im=tf.reshape(im,[1,im_height,im_width,im_channels])
ker=tf.constant([[[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]],[[-1,-1,-1],[-1,8,-1],[-1,-1,-1]]]],dtype=tf.float32,name="ker")print(ker)
kernel=tf.reshape(ker,[3,3,3,2],name="kernel")
##卷积运算,输出卷积后的结果
res=tf.nn.conv2d(input_im,kernel,strides=[1,2,2,1],padding="VALID")
##运算
with tf.Session() as sess:conv_im2=sess.run(res)
print("卷积后输出结果大小:",conv_im2.shape)
##查看卷积后的图像
plt.figure(figsize=(8,4))
plt.subplot(1,2,1)
plt.imshow(conv_im2[0][:,:,0],cmap=plt.cm.gray)
plt.axis("off")
plt.subplot(1,2,2)
plt.imshow(conv_im2[0][:,:,1],cmap=plt.cm.gray)
plt.axis("off")
plt.show()'''池化'''
max_pool=tf.nn.max_pool(conv_im2,ksize=[1,2,2,1],strides=[1,1,1,1],padding="VALID")
with tf.Session() as sess:max_pool_val=sess.run(max_pool)
print("池化后输出结果大小:",max_pool_val.shape)plt.figure(figsize=(8,4))
plt.subplot(1,2,1)
plt.imshow(max_pool_val[0][:,:,0],cmap=plt.cm.gray)
plt.axis("off")
plt.subplot(1,2,2)
plt.imshow(max_pool_val[0][:,:,1],cmap=plt.cm.gray)
plt.axis("off")
plt.show()avg_pool=tf.nn.avg_pool(conv_im2,ksize=[1,3,3,1],strides=[1,2,2,1],padding="VALID")
with tf.Session() as sess:avg_pool_val=sess.run(avg_pool)
print("池化后输出的结果大小:",avg_pool_val.shape)plt.figure(figsize=(8,4))
plt.subplot(1,2,1)
plt.imshow(avg_pool_val[0][:,:,0],cmap=plt.cm.gray)
plt.axis("off")
plt.subplot(1,2,2)
plt.imshow(avg_pool_val[0][:,:,1],cmap=plt.cm.gray)
plt.axis("off")
plt.show()
Python在机器学习中的应用--第十二章深度学习相关推荐
- July博客第十二章参考学习
### July博客第十二章参考学习 ## 第一题:给40亿个不重复的unsigned int 的整数,无序,给一个随机数,快速判断这个是否在40亿个数当中 1. 个人思路: - bitmap,重点在 ...
- 【鸟哥的Linux私房菜】第十二章、学习shell脚本
第十二章.学习shell脚本 以下皆为实践题,请自行编写出程序 请建立一个脚本,当你执行该脚本的时候,该脚本可以显示:(1)你目前的身份(用 whoami) (2)你目前所在的目录(用pwd) #!/ ...
- 机器学习算法平台alink_Alink漫谈(十二) :在线学习算法FTRL 之 整体设计
Alink漫谈(十二) :在线学习算法FTRL 之 整体设计 [Toc] 0x00 摘要 Alink 是阿里巴巴基于实时计算引擎 Flink 研发的新一代机器学习算法平台,是业界首个同时支持批式算法. ...
- Excel在统计分析中的应用—第十二章—回归分析与预测-运用LINEST函数进行多元线性回归分析
发现一个规律:一本书,越到后面,实用性越强. 多元线性回归就是如此,感觉是个很实用的工具. "在现实中,多元回归分析是比一元回归分析更为常用的方法,被大量用来解释各种经济现象,预测多种经济变 ...
- Python 编程从入门到实践 第十二章 飞船开始游戏时靠着边界的问题解决办法
注:图中标识的1,2,3,4 就是所指向的那一行代码,上面是本人的正确代码 解决办法:书上的代码内容顺序是1代码在2代码的前面,其实只要把1代码放到2代码后面就可以解决靠边的问题 飞船初始靠边原因:1 ...
- 【课程作业】西瓜书 机器学习课后习题 : 第十二章
目录 简介 说明 12.1 12.2 12.3 结语 简介 Hello! 非常感谢您阅读海轰的文章,倘若文中有错误的地方,欢迎您指出- ଘ(੭ˊᵕˋ)੭ 昵称:海轰 标签:程序猿|C++选手|学生 ...
- Excel在统计分析中的应用—第十二章—回归分析与预测-应用LINEST函数进行回归分析
这一节,又学到了几个新函数,不错! 具体解释,可参考:http://www.excelfunctions.net/Excel-Linest-Function.html
- 《Python游戏编程快速上手》第十四章----凯撒密码
<Python游戏编程快速上手>的第十二章主要讲了笛卡尔坐标系的基本数学知识,我就不重现了:然后第十三章主要是一个笛卡尔坐标系的小应用,这个小应用也是非常简单的,所以我就不重现了. 今天主 ...
- python 宝典 笔记 第十二章 存储数据和对象 (各种对象转换成字符串)
第十二章 存储数据和对象 12.1数据存储概述 12.1.1文本与二进制对比 文本格式易于阅读和调试,跨平台性能好.二进制格式占用空间小,适于按记录存取. 12.1.2压缩 假如对象的大小开始成为问题 ...
最新文章
- 2018html游戏引擎,跨平台三维游戏引擎Unity Pro 2018.1 Win x64
- [Google Guava] 1.1-使用和避免null
- mybatis里的log适配器模式
- Redis Key过期及清除策略
- UVA10004 Bicoloring【DFS】
- 解压vmlinuz和解压initrd(initramfs)
- JDBC原生连接与连接池介绍
- webstorm 主题 配色
- 微信微页面源码H5页面
- Chromecast电视投屏软件
- 益聚星荣:不打老婆的即时到账”?还呗贷款平台广告词惹争议
- WordPress总裁CeoMax主题模板源码3.9.1无需授权
- 汇兑损益中间科目,系统情况
- 关于new Map()
- Flink1.13学习_第 1 章 初识 Flink
- about云大数据面试宝典 大公司面试一般用不到
- 和孩子斗智斗勇-如何限制只运行上网课的程序(Windows篇)
- windows使用adb命令
- android 7.1 支持哪些 cpu,安卓7.1加骁龙处理器,新一代全民性价比手机
- Couchbase简介
热门文章
- ORA-20000:ORU-10027:buffer overflow,limit of 10000 bytes错误4
- [2006-01-03] 感受《我的校长田长霖》
- 投影仪选购重要10点,当贝X3超亮画质清晰智能满足你
- 微信公众平台测试号的url和token原理
- php实现qq一键登录,Thinkphp 实现QQ一键登录的例子
- 读RedditOs源码
- CResourceException Warning: Uncaught exception in WindowProc (returning 0).
- office2013安装Matytype
- oracle查询的默认排序,oracle 默认排序及认知
- 2021年中国核酸检测产业链、市场规模及主要企业经营分析[图]