2.Unknown: Failed to get convolution algorithm......错误总结

源代码

import os 
import cv2
import random
import matplotlib.pyplot as plt

#以下两行是jupyter notebook专有的代码,这两行可以让图像显示的更好
%matplotlib inline
%config InlineBackend.figure_format = 'retina'

path = 'train'
filename = os.listdir(path)
plt.figure(figsize = (12, 10))
for i, filename in enumerate(random.sample(filename, 12)):
    plt.subplot(3, 4, i+1)
    plt.imshow(cv2.imread(os.path.join(path, filename))[:,:,::-1])
    plt.title(filename)


#9.1猫狗大战1.0-用卷积神经网络直接进行训练
#9.1.1导入数据
import cv2
import numpy as np
from tqdm import tqdm

n = 25000
width = 128

X = np.zeros((n, width, width, 3), dtype = np.uint8)
y = np.zeros((n, ), dtype = np.uint8)

for i in tqdm(range(n//2)):
    X[i] = cv2.resize(cv2.imread('train/cat.%d.jpg' % i), (width, width))
    X[i + n//2] = cv2.resize(cv2.imread('train/dog.%d.jpg' % i), (width, width))
    y[n//2] = 1



#9.1.2可视化:为了确定数据是正确的,随机取几张看看
import random 
import matplotlib.pyplot as plt

#以下两行是jupyter notebook专有的代码,这两行可以让图像显示的更好
%matplotlib inline
%config InlineBackend.figure_format = 'retina'

plt.figure(figsize = (12, 10))
for i in range(12):
    random_index = random.randint(0, n - 1)
    plt.subplot(3, 4, i+1)
    plt.imshow(X[random_index][:,:,::-1])
    plt.title(['cat', 'dog'][y[random_index]])



#分割训练集和验证集
from sklearn.model_selection import train_test_split
X_train, X_valid, y_train, y_valid = train_test_split(X, y, test_size = 0.2)




#9.1.4搭建模型
from keras.layers import *
from keras.models import *

inputs = Input((width, width, 3))
x = inputs
for i, layer_num in enumerate([2,2,3,3,3]):
    for j in range(layer_num):
        x = Conv2D(32*2**i, 3, padding = 'same', activation = 'relu')(x)
        x = BatchNormalization()(x)
        x = Activation('relu')(x)
    x = MaxPooling2D(2)(x)
    
x = GlobalAveragePooling2D()(x)
x = Dropout(0.5)(x)
x = Dense(1, activation = 'sigmoid')(x)
model = Model(inputs, x)



#搭建好模型,指定优化器和loss
model.compile(optimizer = 'adam',
                loss = 'binary_crossentropy',
                metrics = ['accuracy'])



#9.1.5模型训练
h = model.fit(X_train, y_train, batch_size=32, epochs=300, validation_data=(X_valid, y_valid))



#用下面的代码画出历史曲线
plt.figure(figsize=(10, 4))
plt.subplot(1, 2, 1)
plt.plot(h.history['loss'])
plt.plot(h.history['val_loss'])
plt.legend(['loss', 'val_loss'])
plt.ylabel('loss')
plt.xlabel('epoch')

#出现  报错Keyerror ‘acc',这是一个keras版本问题,acc和accuracy本意是一样的,
#但是不同keras版本使用不同命名,因此需要更换。val_acc也是如此。将acc换成accuracy即可。
plt.subplot(1, 2, 2)
plt.plot(h.history['acc'])
plt.plot(h.history['val_acc'])
plt.legend(['accuracy', 'val_acc'])
plt.ylabel('accuracy')
plt.xlabel('epoch')

代码出错提示

文字描述:
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/Relu}}]]
[[metrics/acc/Mean_1/_667]]
(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_1/Relu}}]]
0 successful operations.
0 derived errors ignored

在这里插入图片描述
在这里插入图片描述
错误原因
我这里出现这种错误的原因是batch_size =64,batch_size过大,rtx2060显卡6GB的显存不够。出现这种错误。当把batch_size =32时,可顺利运行。

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值