《深度学习之TensorFlow》(机械工业出版社)第八章 例程8-2Sobel算子
P166-167案例程序:
1 import matplotlib.pyplot as plt 2 import matplotlib.image as mpimg 3 import numpy as np 4 import tensorflow as tf 5 6 myImg = mpimg.imread('F:/JetBrains/Figure/SobelFigure.jpg') 7 plt.imshow(myImg) 8 plt.axis("off") 9 plt.show() 10 print(myImg.shape) 11 12 # [1:一张图, myImg.shape] 13 full = np.reshape(myImg, [1, myImg.shape[0], myImg.shape[1], myImg.shape[2]]) 14 inputfull = tf.Variable(tf.constant(1.0, shape=[1, myImg.shape[0], 15 myImg.shape[1], myImg.shape[2]])) 16 17 # 卷积核:filterInit(注意这里必须使用浮点型(例如:-1.0),整形(tf.uint8)无法通过) 18 filterInit = tf.Variable(tf.constant([[-1.0, -1, -1], [0, 0, 0], [1, 1, 1], 19 [-2, -2, -2], [0, 0, 0], [2, 2, 2], 20 [-1, -1, -1], [0, 0, 0], [1, 1, 1], 21 [-2, -2, -2], [0, 0, 0], [2, 2, 2]], 22 shape=[3, 3, 4, 1])) 23 24 op = tf.nn.conv2d(inputfull, filterInit, strides=[1, 1, 1, 1], padding='SAME') 25 o = tf.cast((op - tf.reduce_min(op)) / (tf.reduce_max(op) - tf.reduce_min(op)) * 255, 26 tf.uint8) 27 28 with tf.Session() as sess: 29 sess.run(tf.global_variables_initializer()) 30 31 t, f = sess.run([o, filterInit], feed_dict={inputfull: full}) 32 33 t = np.reshape(t, [myImg.shape[0], myImg.shape[1]]) 34 35 plt.imshow(t, cmap='Greys_r') 36 plt.axis('off') 37 plt.show()
F:Anaconda3python.exe F:/JetBrains/TensorF/Sobel算子.py
(800, 600, 4)
2020-04-11 13:50:37.711105: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2020-04-11 13:50:39.358279: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1405] Found device 0 with properties:
name: GeForce GTX 1050 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.62
pciBusID: 0000:01:00.0
totalMemory: 4.00GiB freeMemory: 3.30GiB
2020-04-11 13:50:39.375722: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1484] Adding visible gpu devices: 0
2020-04-11 13:50:48.530688: I tensorflow/core/common_runtime/gpu/gpu_device.cc:965] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-04-11 13:50:48.532050: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 0
2020-04-11 13:50:48.532699: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] 0: N
2020-04-11 13:50:48.610300: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1097] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 3010 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1050 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
(800, 600, 4)
2020-04-11 13:50:37.711105: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX AVX2
2020-04-11 13:50:39.358279: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1405] Found device 0 with properties:
name: GeForce GTX 1050 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.62
pciBusID: 0000:01:00.0
totalMemory: 4.00GiB freeMemory: 3.30GiB
2020-04-11 13:50:39.375722: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1484] Adding visible gpu devices: 0
2020-04-11 13:50:48.530688: I tensorflow/core/common_runtime/gpu/gpu_device.cc:965] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-04-11 13:50:48.532050: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 0
2020-04-11 13:50:48.532699: I tensorflow/core/common_runtime/gpu/gpu_device.cc:984] 0: N
2020-04-11 13:50:48.610300: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1097] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 3010 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1050 Ti, pci bus id: 0000:01:00.0, compute capability: 6.1)
进程已结束,退出代码 0