将列表转换为numpy数组时出现内存错误

时间:2014-06-21 13:39:40

标签: python arrays memory numpy

我总共有大约7000张图片,我从中提取了HoG功能。然后我想将列表转换为np数组以进行进一步处理。但是在转换过程中我遇到了内存错误。

这是我的代码的相关部分:

from skimage import data, io, filter, color, exposure
from skimage.feature import hog
from skimage.transform import resize
import matplotlib.pyplot as plt
import numpy as np

tmp_hogs = [] # this is the list I need to convert into a numpy array
for group in samplegroups:
    for myimg in group:
        curr_img = np.array(myimg, dtype=float)
        imgs.append(curr_img)
        fd, hog_image = hog(curr_img, orientations=8, pixels_per_cell=(4, 4),
                 cells_per_block=(1, 1), visualise=True, normalise=True)
        tmp_hogs.append(fd)

img_hogs = np.array(tmp_hogs, dtype =float) 

我得到的错误是:

Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Users\app\anacondasoftware\lib\threading.py", line 810, in __bootstrap_inner
    self.run()
  File "C:\Users\app\anacondasoftware\lib\site-packages\spyderlib\widgets\externalshell\monitor.py", line 582, in run
    already_pickled=True)
  File "C:\Users\app\anacondasoftware\lib\site-packages\spyderlib\utils\bsdsocket.py", line 45, in write_packet
    nsend -= temp_fail_retry(socket.error, sock.send, sent_data)
  File "C:\Users\app\anacondasoftware\lib\site-packages\spyderlib\utils\bsdsocket.py", line 25, in temp_fail_retry
    return fun(*args)
error: [Errno 10054] An existing connection was forcibly closed by the remote host

Traceback (most recent call last):
  File "C:\Users\app\Documents\Python Scripts\gbc_carclassify.py", line 63, in <module>
    img_hogs = np.array(tmp_hogs, dtype =float) 
MemoryError

我该如何解决?

1 个答案:

答案 0 :(得分:4)

对于RGB或RGBA图像,每个值只需要8位,使用float时,每个值需要64位。请尝试使用np.uint8

img_hogs = np.array(tmp_hogs, dtype=np.uint8)