我正在尝试在 pytorch 中训练 CNN,但遇到了一些问题。运行时错误:
运行时错误:CUDA 内存不足。尝试分配 512.00 MiB(GPU 0;2.00 GiB 总容量;已分配 584.97 MiB;13.81 MiB 空闲;PyTorch 总共保留 590.00 MiB)
这是我的代码:
import os
import numpy as np
import cv2
import torch as t
import torch.nn as nn
import torchvision.transforms as transforms
from torch.utils.data import DataLoader,Dataset
import time
import matplotlib.pyplot as plt
%matplotlib inline
root_path='C:/Users/60960/Desktop/recet-task/course_LeeML20/course_LeeML20-datasets/hw3/food-11'
training_path=root_path+'/training'
testing_path=root_path+'/testing'
validation_path=root_path+'/validation'
def readfile(path,has_label):
img_paths=sorted(os.listdir(path))
x=np.zeros((len(img_paths),128,128,3),dtype=np.uint8)
y=np.zeros((len(img_paths)),dtype=np.uint8)
for i,file in enumerate(img_paths):
img=cv2.imread(path+'/'+file)
x[i,:,:]=cv2.resize(img,(128,128))
if has_label:
y[i]=int(file.split('_')[0])
if has_label:
return x,y
else:
return x
def show_img(img_from_cv2):
b,g,r=cv2.split(img_from_cv2)
img=cv2.merge([r,g,b]) …Run Code Online (Sandbox Code Playgroud) nvidia machine-learning deep-learning conv-neural-network pytorch