tensorflow CNN 卷积神经网络中的卷积层和池化层的代码和效果图
因为很多 demo 都比较复杂,专门抽出这两个函数,写的 demo。
更多教程:http://www.tensorflownews.com#!/usr/bin/python# -*- coding: UTF-8 -*-import matplotlib.pyplot as pltimport tensorflow as tffrom PIL import Imageimport numpyimg = Image.open('szu.jpg')img_ndarray = numpy.asarray(img, dtype='float32')print(img_ndarray.shape)img_ndarray=img_ndarray[:,:,0]plt.figure()plt.subplot(221)plt.imshow(img_ndarray)w=[[-1.0,-1.0,-1.0], [-1.0,9.0,-1.0], [-1.0,-1.0,-1.0]]with tf.Session() as sess: img_ndarray=tf.reshape(img_ndarray,[1,183,276,1]) w=tf.reshape(w,[3,3,1,1]) img_cov=tf.nn.conv2d(img_ndarray, w, strides=[1, 1, 1, 1], padding='SAME') image_data=sess.run(img_cov) print(image_data.shape) plt.subplot(222) plt.imshow(image_data[0,:,:,0]) img_pool=tf.nn.max_pool(img_ndarray, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') image_data = sess.run(img_pool) plt.subplot(223) plt.imshow(image_data[0, :, :, 0]) plt.subplot(224) img_pool = tf.nn.max_pool(img_ndarray, ksize=[1, 4, 4, 1], strides=[1, 4, 4, 1], padding='SAME') image_data = sess.run(img_pool) plt.imshow(image_data[0, :, :, 0]) plt.show()效果图片
继续阅读与本文标签相同的文章
-
SSD:TensorFlow中的单次多重检测器
2026-05-26栏目: 教程
-
YOLO: 3 步实时目标检测安装运行教程 [你看那条狗,好像一条狗!]
2026-05-26栏目: 教程
-
Tensorflow 基于分层注意网络的文件分类器
2026-05-26栏目: 教程
-
StarSpace是用于高效学习实体向量的通用神经模型
2026-05-26栏目: 教程
-
vrn:基于直接体积回归的单幅图像大姿态三维人脸重建
2026-05-26栏目: 教程
