当我有 32GRAM 并且 py 是 64 位时,如何解决 python 中的内存错误
How to solve memory error in python when I have 32GRAM and py is 64bit
我正在使用 NumPy 和 Pillow 处理图像,我有 32G 内存,但所有内存都被使用了。我可以做些什么来改进我的程序?这是我的零件代码:
import os
import numpy as np
from PIL import Image
from numpy import *
img_rows,img_cols,img_depth=240,240,30
X_tr=[]
path = '/home/lt/Spyder/data1/brushing teeth/Depth1/'
imlist = os.listdir(path)
imlist.sort(key= lambda x:int(x[:-4]))
im = np.array(Image.open(path+imlist[0]))
m,n = im.shape[0:2]
imbr = len(imlist)
num = imbr-30
i = 0
while i<num :
frames=[]
for k in range(30):
im=np.array(Image.open(path+imlist[k+i]))
frames.append(im)
i=i+5
input=np.array(frames)
ipt = np.rollaxis(np.rollaxis(input,2,0),2,0)
X_tr.append(ipt) #265
X_tr
变量的大小是3751行,我的内存在99%。我应该怎么办?也许我应该处理 100000 张图像,但这只是数据的四分之一!
您正在将所有文件作为图像实例加载到内存中。因此,您的图像太大而无法放入内存。你应该处理更少的图像或者图像应该更小。
但是,您似乎只是在循环它,因此如果您使用生成器应该没问题。
您的代码令人困惑,因为您想处理所有内容,但您只每 5 帧处理一次?无论如何,这里有一些优化:
# drop this, not needed
# i = 0
# better memory data structure
# this should fix your memory problem
frames = collections.deque(maxlen=30)
# process every 5th frame?
# there seems to be a flaw in that you cut the last 30 but you
# when you start you also do not have 30. (can add if len(frames) < 30)
for file_name in files[::5]:
frames.append(np.array(Image.open(path + file_name))
input = np.array(frames)
ipt = np.rollaxis(np.rollaxis(input,2,0),2,0)
X_tr.append(ipt) #265
我正在使用 NumPy 和 Pillow 处理图像,我有 32G 内存,但所有内存都被使用了。我可以做些什么来改进我的程序?这是我的零件代码:
import os
import numpy as np
from PIL import Image
from numpy import *
img_rows,img_cols,img_depth=240,240,30
X_tr=[]
path = '/home/lt/Spyder/data1/brushing teeth/Depth1/'
imlist = os.listdir(path)
imlist.sort(key= lambda x:int(x[:-4]))
im = np.array(Image.open(path+imlist[0]))
m,n = im.shape[0:2]
imbr = len(imlist)
num = imbr-30
i = 0
while i<num :
frames=[]
for k in range(30):
im=np.array(Image.open(path+imlist[k+i]))
frames.append(im)
i=i+5
input=np.array(frames)
ipt = np.rollaxis(np.rollaxis(input,2,0),2,0)
X_tr.append(ipt) #265
X_tr
变量的大小是3751行,我的内存在99%。我应该怎么办?也许我应该处理 100000 张图像,但这只是数据的四分之一!
您正在将所有文件作为图像实例加载到内存中。因此,您的图像太大而无法放入内存。你应该处理更少的图像或者图像应该更小。
但是,您似乎只是在循环它,因此如果您使用生成器应该没问题。
您的代码令人困惑,因为您想处理所有内容,但您只每 5 帧处理一次?无论如何,这里有一些优化:
# drop this, not needed
# i = 0
# better memory data structure
# this should fix your memory problem
frames = collections.deque(maxlen=30)
# process every 5th frame?
# there seems to be a flaw in that you cut the last 30 but you
# when you start you also do not have 30. (can add if len(frames) < 30)
for file_name in files[::5]:
frames.append(np.array(Image.open(path + file_name))
input = np.array(frames)
ipt = np.rollaxis(np.rollaxis(input,2,0),2,0)
X_tr.append(ipt) #265