无法获取卷积算法 Tensorflow 2.3.0

我最近在Coursera上完成了使用自编码器进行图像超分辨率的课程,当我在笔记本电脑上使用Spyder和Jupyter笔记本运行相同代码时,总是遇到这个错误。我使用的是Nvidia GeForce 1650Ti,以及Tensorflow-gpu=2.3.0, CUDA=10.1, cuDNN=7.6.5 和 python=3.8.5。我之前用相同的配置运行了许多深度神经网络问题,但都没有出现过这个错误。

代码:

# 使用自编码器进行图像超分辨率# 加载图像x_train_n = []x_train_down = []x_train_n2 = []x_train_down2 = []import tensorflow as tfgpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction = 0.95)session = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options))path = 'D:/GPU testing/Image Super Resolution/data/cars_train/'images = os.listdir(path)size = 0for a in images:    try:        img = image.load_img(str(path+a), target_size=(64,64,3))        img_1 = image.img_to_array(img)        img_1 = img_1/255.        x_train_n.append(img_1)        dwn2 = rescale(rescale(img_1, 0.5, multichannel=True),                                    2.0, multichannel=True)        img_2 = image.img_to_array(dwn2)        x_train_down.append(img_2)        size+= 1    except:        print("Error loading image")        size += 1    if size >= 64:        breakx_train_n2 = np.array(x_train_n)print(x_train_n2.shape)x_train_down2 = np.array(x_train_down)print(x_train_down2.shape)# 构建模型from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, Dropout, Conv2DTranspose, UpSampling2D, addfrom tensorflow.keras.models import Modelfrom tensorflow.keras import regularizersfrom tensorflow.keras.callbacks import ModelCheckpoint, ReduceLROnPlateau# 构建编码器input_img = Input(shape=(64, 64, 3))l1 = Conv2D(64, (3, 3), padding='same', activation='relu',            activity_regularizer=regularizers.l1(10e-10))(input_img)l2 = Conv2D(64, (3, 3), padding='same', activation='relu',            activity_regularizer=regularizers.l1(10e-10))(l1)l3 = MaxPooling2D(padding='same')(l2)l3 = Dropout(0.3)(l3)l4 = Conv2D(128, (3, 3),  padding='same', activation='relu',            activity_regularizer=regularizers.l1(10e-10))(l3)l5 = Conv2D(128, (3, 3), padding='same', activation='relu',            activity_regularizer=regularizers.l1(10e-10))(l4)l6 = MaxPooling2D(padding='same')(l5)l7 = Conv2D(256, (3, 3), padding='same', activation='relu',            activity_regularizer=regularizers.l1(10e-10))(l6)# 构建解码器l8 = UpSampling2D()(l7)l9 = Conv2D(128, (3, 3), padding='same', activation='relu',            activity_regularizer=regularizers.l1(10e-10))(l8)l10 = Conv2D(128, (3, 3), padding='same', activation='relu',             activity_regularizer=regularizers.l1(10e-10))(l9)l11 = add([l5, l10])l12 = UpSampling2D()(l11)l13 = Conv2D(64, (3, 3), padding='same', activation='relu',             activity_regularizer=regularizers.l1(10e-10))(l12)l14 = Conv2D(64, (3, 3), padding='same', activation='relu',             activity_regularizer=regularizers.l1(10e-10))(l13)l15 = add([l14, l2])# 通道数为3,用于RGBdecoded = Conv2D(3, (3, 3), padding='same', activation='relu',                 activity_regularizer=regularizers.l1(10e-10))(l15)# 创建我们的网络autoencoder = Model(input_img, decoded)autoencoder_hfenn = Model(input_img, decoded)autoencoder.compile(optimizer='adadelta', loss='mean_squared_error')autoencoder.summary()# 训练模型history = autoencoder.fit(x_train_down2, x_train_n2,                          epochs=20,                          batch_size=16,                          validation_steps=100,                          shuffle=True,                          validation_split=0.15)# 保存模型autoencoder.save('ISR_model_weight.h5')# 将模型表示为JSON字符串autoencoder_json = autoencoder.to_json()with open('ISR_model.json', 'w') as json_file:    json_file.write(autoencoder_json)

错误:

2020-09-18 20:44:23.655077: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1402] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 3891 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1650 Ti, pci bus id: 0000:01:00.0, compute capability: 7.5)2020-09-18 20:44:23.658359: I tensorflow/stream_executor/cuda/cuda_driver.cc:775] failed to allocate 3.80G (4080218880 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory2020-09-18 20:44:23.659070: I tensorflow/stream_executor/cuda/cuda_driver.cc:775] failed to allocate 3.42G (3672196864 bytes) from device: CUDA_ERROR_OUT_OF_MEMORY: out of memory2020-09-18 20:44:25.560185: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library cudnn64_7.dllTraceback (most recent call last):  File "D:\GPU testing\Image Super Resolution\Image Super Resolution using Autoencoders.py", line 126, in <module>    history = autoencoder.fit(x_train_down2, x_train_n2,  File "D:\anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\keras\engine\training.py", line 108, in _method_wrapper    return method(self, *args, **kwargs)  File "D:\anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\keras\engine\training.py", line 1098, in fit    tmp_logs = train_function(iterator)  File "D:\anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\eager\def_function.py", line 780, in __call__    result = self._call(*args, **kwds)  File "D:\anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\eager\def_function.py", line 840, in _call    return self._stateless_fn(*args, **kwds)  File "D:\anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\eager\function.py", line 2829, in __call__    return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access  File "D:\anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\eager\function.py", line 1843, in _filtered_call    return self._call_flat(  File "D:\anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\eager\function.py", line 1923, in _call_flat    return self._build_call_outputs(self._inference_function.call(  File "D:\anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\eager\function.py", line 545, in call    outputs = execute.execute(  File "D:\anaconda3\envs\tensorflow_gpu\lib\site-packages\tensorflow\python\eager\execute.py", line 59, in quick_execute    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,UnknownError:  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 functional_1/conv2d/Relu (defined at D:\GPU testing\Image Super Resolution\Image Super Resolution using Autoencoders.py:126) ]] [Op:__inference_train_function_2246]Function call stack:train_function

我尝试了GPU增长:

config = tf.compat.v1.ConfigProto()config.gpu_options.allow_growth = Truesess = tf.compat.v1.Session(config=config)

以及限制GPU使用:

gpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction = 0.95)session = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options))

但这些方法都没有解决问题。

我最近看到了一篇文章:Analytics Vidya的《什么是自编码器?使用自编码器增强模糊图像》,并尝试了提供的代码,结果遇到了同样的错误。

有人能帮我解决这个问题吗?


回答:

问题出在Tensorflow 2.3.0的GPU增长设置上。正确设置后,我能够解决这个错误。

import tensorflow as tffrom tensorflow.compat.v1.keras.backend import set_sessionconfig = tf.compat.v1.ConfigProto()config.gpu_options.allow_growth = Trueconfig.log_device_placement = Truesess = tf.compat.v1.Session(config=config)set_session(sess)

来源: https://stackoverflow.com/a/59007505/14301371

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注