gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:49:05 26 4
gpt4 key购买 nike

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

我该如何解决?

最佳答案

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

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

关于python - 将列表转换为 numpy 数组时出现内存错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24342161/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com