weixin_41782739
2021-09-24 13:14
采纳率: 0%
浏览 15

RTX3080 10G 训练CNN模型或者小模型爆显存 问题

1、训练CNN模型时爆显存,显存使用率97%,奔溃一瞬间,奔溃一瞬间,GPU使用率100%。
2、第二个采用纯MLP训练,GPU使用率3%,显存使用率100%。实际训练速度比cpu还慢。
按道理说这么小的模型,硬件/代码应该没问题的,问题应该是出现在环境配置或者驱动等问题吧

1、CNN模型代码

#CNN模型代码
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D,MaxPooling2D,Flatten,Dense
#数据归一化
train_datagen=ImageDataGenerator(rescale=1./255)
#导入图片数据
training_set = train_datagen.flow_from_directory('./training_set2',target_size=(50,50),batch_size=1,class_mode='binary')

#建立模型
cnn_model = Sequential()
cnn_model.add(Conv2D(32,(3,3),input_shape=(50,50,3),activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2,2)))
cnn_model.add(Conv2D(32,(3,3),input_shape=(50,50,3),activation='relu'))
cnn_model.add(MaxPooling2D(pool_size=(2,2)))
cnn_model.add(Flatten())
cnn_model.add(Dense(units=128,input_dim=1,activation='relu'))
cnn_model.add(Dense(units=1,activation='sigmoid'))
cnn_model.summary()
cnn_model.compile(optimizer='adam',loss='binary_crossentropy',metrics=['accuracy'])
#训练模型
cnn_model.fit(training_set,epochs=2)

1.1、CNN模型崩溃后报错截图

img


2、MLP模型代码

#MLP模型代码
from tensorflow.keras.datasets import fashion_mnist
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.layers import Dense,Activation
from tensorflow.keras.models import Sequential

#导入数据
(x_train,y_train),(x_test,y_test) = fashion_mnist.load_data()
print(x_train.shape)

#x数据维度转化/数据归一化
feature_size = x_train[0].shape[0]*x_train[0].shape[1]
x_train_format = x_train.reshape(x_train.shape[0],feature_size)
x_test_format = x_test.reshape(x_test.shape[0],feature_size)

x_train_format_norm = x_train_format/255
x_test_format_norm = x_test_format/255

y_train_format = to_categorical(y_train)
y_test_format = to_categorical(y_test)

#建立MLP模型
mlp = Sequential()
mlp.add(Dense(units=392,input_dim=784,activation='relu'))
mlp.add(Dense(units=196,activation='relu'))
mlp.add(Dense(units=10,activation='softmax'))
mlp.summary()
mlp.compile(optimizer='adam',loss='categorical_crossentropy',metrics='categorical_accuracy')

#训练模型
mlp.fit(x_train_format_norm,y_train_format,epochs=10)

2.1、MLP模型训练GPU使用率截图

img

  • 收藏

相关推荐 更多相似问题