Python MemoryError:无法分配内存。形状为(3000、4000、3)且数据类型为float32的数组的MiB
我在运行一个包含10个正面和10个负面训练数据的模型时遇到了这个问题Python MemoryError:无法分配内存。形状为(3000、4000、3)且数据类型为float32的数组的MiB,python,opencv,machine-learning,jupyter-notebook,Python,Opencv,Machine Learning,Jupyter Notebook,我在运行一个包含10个正面和10个负面训练数据的模型时遇到了这个问题 MemoryError:无法分配内存。形状为(3000、4000、3)且数据类型为float32的数组的MiB 我的代码是: # Define a single function that can extract features using hog sub-sampling and make predictions def find_cars(img, ystart, ystop, scale, svc,orient,
MemoryError:无法分配内存。形状为(3000、4000、3)且数据类型为float32的数组的MiB
我的代码是:
# Define a single function that can extract features using hog sub-sampling and make predictions
def find_cars(img, ystart, ystop, scale, svc,orient,
pix_per_cell, cell_per_block,show_all_rectangles=False):
# array of rectangles where cars were detected
rectangles = []
img = img.astype(np.float32)/255
img_tosearch = img[ystart:ystop,:,:]
# apply color conversion if other than 'RGB'
ctrans_tosearch = cv2.cvtColor(img_tosearch, cv2.COLOR_RGB2YUV)
# rescale image if other than 1.0 scale
imshape = ctrans_tosearch.shape
ctrans_tosearch = cv2.resize(ctrans_tosearch, (np.int(imshape[1]/scale), np.int(imshape[0]/scale)))
ch1 = ctrans_tosearch[:,:,0]
ch2 = ctrans_tosearch[:,:,1]
ch3 = ctrans_tosearch[:,:,2]
# Define blocks and steps as above
nxblocks = (ch1.shape[1] // pix_per_cell)+1 #-1
nyblocks = (ch1.shape[0] // pix_per_cell)+1 #-1
nfeat_per_block = orient*(cell_per_block**2)
# 64 was the orginal sampling rate, with 8 cells and 8 pix per cell
window = 64
nblocks_per_window = (window // pix_per_cell)-1
cells_per_step = 2 # Instead of overlap, define how many cells to step
nxsteps = (nxblocks - nblocks_per_window) // cells_per_step
nysteps = (nyblocks - nblocks_per_window) // cells_per_step
# Compute individual channel HOG features for the entire image
hog1 = get_hog_features(ch1, orient, pix_per_cell, cell_per_block, feature_vec=False)
hog2 = get_hog_features(ch2, orient, pix_per_cell, cell_per_block, feature_vec=False)
hog3 = get_hog_features(ch3, orient, pix_per_cell, cell_per_block, feature_vec=False)
for xb in range(nxsteps):
for yb in range(nysteps):
ypos = yb*cells_per_step
xpos = xb*cells_per_step
# Extract HOG for this patch
hog_feat1 = hog1[ypos:ypos+nblocks_per_window, xpos:xpos+nblocks_per_window].ravel()
hog_feat2 = hog2[ypos:ypos+nblocks_per_window, xpos:xpos+nblocks_per_window].ravel()
hog_feat3 = hog3[ypos:ypos+nblocks_per_window, xpos:xpos+nblocks_per_window].ravel()
hog_features = np.hstack((hog_feat1, hog_feat2, hog_feat3))
xleft = xpos*pix_per_cell
ytop = ypos*pix_per_cell
test_prediction = svc.predict(hog_features.reshape(1,-1))
if test_prediction == 1 or show_all_rectangles:
xbox_left = np.int(xleft*scale)
ytop_draw = np.int(ytop*scale)
win_draw = np.int(window*scale)
rectangles.append(((xbox_left, ytop_draw+ystart),(xbox_left+win_draw,ytop_draw+win_draw+ystart)))
return rectangles
test_img = mpimg.imread('C:/Users/shrey/Desktop/Mod1-IITR/DJI_0314.JPG')
ystart = 20
ystop = 50
scale = 1
orient = 11
pix_per_cell = 16
cell_per_block = 2
rectangles = find_cars(test_img, ystart, ystop, scale, svc,orient, pix_per_cell, cell_per_block)
print(len(rectangles), 'rectangles found in image')
我正在使用Windows-Jupyter笔记本电脑。如何更正此代码问题?错误是,它无法分配内存,因为内存不足
然而,您的系统上的可用内存可能比137MB多得多,jupyter只是限制了系统中可以使用的内存量,因为在您开始运行代码之前,它必须保留一些内存。有关如何在jupyter中增加内存限制的信息,请参见。您在哪里感到困惑?错误信息似乎很清楚:您正在尝试分配137Mb的数据,但您没有可用的数据。虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能无效。-@SimasJoneliunas我真的看不出问题所在,链接不是解决方案,它只是猜测问题的原因。这不是一个“仅链接的答案”。据我们所知,jupyter内存不足,我们没有其他信息,也没有试图解决问题。我们无法修复未描述的问题。很可能这个相关的问题恰好是根本原因。如果是,则整个问题都是重复的,您不应投票删除答案,而应投票赞成关闭该问题。