【问题标题】:Memory error while converting list to numpy array将列表转换为 numpy 数组时出现内存错误
【发布时间】:2014-08-12 02:10:29
【问题描述】:

我总共有大约 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

我该如何解决?

【问题讨论】:

  • tmp_hogs 的大小是多少?它是一维的吗?
  • 你有多少内存?一头猪占用多少内存?您使用的是 32 位还是 64 位 python?
  • 32 位 python(Anaconda 的 spyder)在 64 位机器上。

标签: python arrays memory numpy


【解决方案1】:

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

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

【讨论】:

  • 另一个改进将是创建一个空数组并将图像直接存储在该数组中,避免中间列表
猜你喜欢
  • 1970-01-01
  • 2014-07-15
  • 1970-01-01
  • 2020-12-10
  • 2018-05-09
  • 1970-01-01
  • 2017-03-08
  • 2017-10-19
  • 2020-01-22
相关资源
最近更新 更多