计算展开的平方差时内存不足

2024-10-02 10:29:34 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图计算图像和模板之间的平方差之和时,我得到了错误:

GPU:

RuntimeError: CUDA out of memory. Tried to allocate 90.73 GiB (GPU 0; 1.96 GiB total capacity; 12.11 MiB already allocated; 1.13 GiB free; 1.89 MiB cached)

中央处理器:

RuntimeError: [enforce fail at CPUAllocator.cpp:64] . DefaultCPUAllocator: can't allocate memory: you tried to allocate 97416120000 bytes. Error code 12 (Cannot allocate memory)

import torch

x = torch.rand(1,3,1000,1000).cuda()
print(x.shape)
t = torch.rand(1,3,100,100).cuda()
print(t.shape)

y = x.unfold(dimension=2, size=t.shape[2], step=1).unfold(dimension=3, size=t.shape[3], step=1)
print(y.shape)

ssd = torch.einsum('xcijkl,xckl->xcij', y, t)
ssd *= - 2
ssd += torch.einsum('xcijkl, xcijkl->xcij', y, y)
ssd += torch.einsum('xcij, xcij', t, t)

我做错什么了吗?有没有别的方法来计算差平方和?你知道吗

opencv的工作原理也一样:

import numpy as np
import cv2

x = np.random.uniform(size=(1000,1000,3))
x = np.array(x*255., dtype=np.uint8)
print(x.shape)
t = np.random.uniform(size=(100,100,3))
t = np.array(t*255., dtype=np.uint8)
print(t.shape)


ssd = cv2.matchTemplate(x, t, cv2.TM_SQDIFF)
print(ssd.shape)

Tags: importsizegpunptorchcv2ssdprint

热门问题