一、代码
import numpy as np
from sklearn.datasets import load_sample_images
import tensorflow as tf
import matplotlib.pyplot as plt
# from tensorflow.compat.v1 import ConfigProto
# config = ConfigProto()
# config.gpu_options.allow_growth = True
# config.gpu_options.per_process_gpu_memory_fraction = 1 # 指定显存分配比例
# 加载数据集
# 输入图片通常是3D,[height, width, channels]
# mini-batch通常是4D,[mini-batch size, height, width, channels]
dataset = np.array(load_sample_images().images, dtype=np.float32)
# 数据集里面两张图片,一个中国庙宇,一个花
# m,h,w,c
batch_size, height, width, channels = dataset.shape
print(batch_size, height, width, channels)
# plt.imshow(load_sample_images().images[0]) # 绘制第一个图
# plt.show()
# plt.imshow(load_sample_images().images[1]) # 绘制第二个图
# plt.show()
# 创建两个filters
# 高,宽,通道数,卷积核的个数
# 7, 7, channels, 2
filters_test = np.zeros(shape=(7, 7, channels, 2), dtype=np.float32)
#切片操作
filters_test[:, 3, :, 0] = 1 # 垂直,3代表中间那一列
filters_test[3, :, :, 1] = 1 # 水平
# filter参数是一个filters的集合
X = tf.placeholder(tf.float32, shape=(None, height, width, channels))
# strides=[1, 2, 2, 1] 中第一最后一个为1,中间对应sh和sw
convolution = tf.nn.conv2d(X, filter=filters_test, strides=[1, 1, 1, 1], padding='SAME')
with tf.Session() as sess:
output = sess.run(convolution, feed_dict={X: dataset})
print(output.shape)
# (2,427,640,2) (多少张照片,高、宽,feature map的数量)
plt.imshow(load_sample_images().images[0]) # 绘制第一个图
plt.show()
plt.imshow(output[0, :, :, 0]) # 绘制第一个图的第一个特征图
plt.show()
plt.imshow(output[0, :, :, 1]) # 绘制第一个图的第二个特征图
plt.show()
plt.imshow(load_sample_images().images[1]) # 绘制第二个图
plt.show()
plt.imshow(output[1, :, :, 0]) # 绘制第二个图的第一个特征图
plt.show()
plt.imshow(output[1, :, :, 1]) # 绘制第二个图的第二个特征图
plt.show()
二、结果
D:\programs\Anaconda3\envs\tf115_py36\python.exe F:/人工智能/11深度学习原理进阶实战代码/04CNN_study/18_convolution.py
2020-08-09 18:07:10.122357: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
WARNING:tensorflow:From F:/人工智能/11深度学习原理进阶实战代码/04CNN_study/18_convolution.py:37: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.
2 427 640 3
WARNING:tensorflow:From F:/人工智能/11深度学习原理进阶实战代码/04CNN_study/18_convolution.py:41: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.
2020-08-09 18:07:12.672410: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library nvcuda.dll
2020-08-09 18:07:13.479728: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
name: GeForce GTX 1050 major: 6 minor: 1 memoryClockRate(GHz): 1.442
pciBusID: 0000:01:00.0
2020-08-09 18:07:13.479951: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
2020-08-09 18:07:13.482875: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_100.dll
2020-08-09 18:07:13.485579: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_100.dll
2020-08-09 18:07:13.487276: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_100.dll
2020-08-09 18:07:13.490839: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_100.dll
2020-08-09 18:07:13.493351: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_100.dll
2020-08-09 18:07:13.500328: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-08-09 18:07:13.501102: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2020-08-09 18:07:13.501456: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
2020-08-09 18:07:13.503587: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1618] Found device 0 with properties:
name: GeForce GTX 1050 major: 6 minor: 1 memoryClockRate(GHz): 1.442
pciBusID: 0000:01:00.0
2020-08-09 18:07:13.503804: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudart64_100.dll
2020-08-09 18:07:13.503946: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cublas64_100.dll
2020-08-09 18:07:13.504088: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cufft64_100.dll
2020-08-09 18:07:13.504231: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library curand64_100.dll
2020-08-09 18:07:13.504378: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusolver64_100.dll
2020-08-09 18:07:13.504523: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cusparse64_100.dll
2020-08-09 18:07:13.504667: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-08-09 18:07:13.505375: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1746] Adding visible gpu devices: 0
2020-08-09 18:07:14.039442: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1159] Device interconnect StreamExecutor with strength 1 edge matrix:
2020-08-09 18:07:14.039598: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1165] 0
2020-08-09 18:07:14.039691: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1178] 0: N
2020-08-09 18:07:14.040333: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1304] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 2127 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1050, pci bus id: 0000:01:00.0, compute capability: 6.1)
2020-08-09 18:07:14.163121: I tensorflow/stream_executor/platform/default/dso_loader.cc:44] Successfully opened dynamic library cudnn64_7.dll
2020-08-09 18:07:14.927996: E tensorflow/stream_executor/cuda/cuda_dnn.cc:319] Loaded runtime CuDNN library: 7.4.1 but source was compiled with: 7.6.0. CuDNN library major and minor version needs to match or have higher minor version in case of CuDNN 7.0 or later version. If using a binary install, upgrade your CuDNN library. If building from sources, make sure the library loaded at runtime is compatible with the version specified during compile configuration.
2020-08-09 18:07:14.930492: E tensorflow/stream_executor/cuda/cuda_dnn.cc:319] Loaded runtime CuDNN library: 7.4.1 but source was compiled with: 7.6.0. CuDNN library major and minor version needs to match or have higher minor version in case of CuDNN 7.0 or later version. If using a binary install, upgrade your CuDNN library. If building from sources, make sure the library loaded at runtime is compatible with the version specified during compile configuration.
Traceback (most recent call last):
File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\client\session.py", line 1365, in _do_call
return fn(*args)
File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\client\session.py", line 1350, in _run_fn
target_list, run_metadata)
File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\client\session.py", line 1443, in _call_tf_sessionrun
run_metadata)
tensorflow.python.framework.errors_impl.UnknownError: 2 root error(s) found.
(0) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[{{node Conv2D}}]]
(1) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[{{node Conv2D}}]]
[[Conv2D/_3]]
0 successful operations.
0 derived errors ignored.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:/人工智能/11深度学习原理进阶实战代码/04CNN_study/18_convolution.py", line 42, in <module>
output = sess.run(convolution, feed_dict={X: dataset})
File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\client\session.py", line 956, in run
run_metadata_ptr)
File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\client\session.py", line 1180, in _run
feed_dict_tensor, options, run_metadata)
File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\client\session.py", line 1359, in _do_run
run_metadata)
File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\client\session.py", line 1384, in _do_call
raise type(e)(node_def, op, message)
tensorflow.python.framework.errors_impl.UnknownError: 2 root error(s) found.
(0) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[node Conv2D (defined at D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\framework\ops.py:1748) ]]
(1) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[node Conv2D (defined at D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\framework\ops.py:1748) ]]
[[Conv2D/_3]]
0 successful operations.
0 derived errors ignored.
Original stack trace for 'Conv2D':
File "F:/人工智能/11深度学习原理进阶实战代码/04CNN_study/18_convolution.py", line 39, in <module>
convolution = tf.nn.conv2d(X, filter=filters_test, strides=[1, 1, 1, 1], padding='SAME')
File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\ops\nn_ops.py", line 2010, in conv2d
name=name)
File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\ops\gen_nn_ops.py", line 1071, in conv2d
data_format=data_format, dilations=dilations, name=name)
File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\framework\op_def_library.py", line 794, in _apply_op_helper
op_def=op_def)
File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\util\deprecation.py", line 507, in new_func
return func(*args, **kwargs)
File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3357, in create_op
attrs, op_def, compute_device)
File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\framework\ops.py", line 3426, in _create_op_internal
op_def=op_def)
File "D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\framework\ops.py", line 1748, in __init__
self._traceback = tf_stack.extract_stack()
Process finished with exit code 1
三、问题
tensorflow.python.framework.errors_impl.UnknownError: 2 root error(s) found.
(0) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[node Conv2D (defined at D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\framework\ops.py:1748) ]]
(1) Unknown: Failed to get convolution algorithm. This is probably because cuDNN failed to initialize, so try looking to see if a warning log message was printed above.
[[node Conv2D (defined at D:\programs\Anaconda3\envs\tf115_py36\lib\site-packages\tensorflow_core\python\framework\ops.py:1748) ]]
[[Conv2D/_3]]
0 successful operations.
0 derived errors ignored.
老师,这里的程序是老师的程序,但是运行会出现上述错误,我的cuda和cudann是按照视频里里的教程安装的
TensorFlow为1.15的版本,也是按照老师教程安装的,为什么会出现“ cuDNN failed to initialize”这个问题。
我上网查了可能是因为显存分配问题,所以加上下述这段代码也不行,麻烦老师帮忙找一下原因?
from tensorflow.compat.v1 import ConfigProto
config = ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.5 # 指定显存分配比例