将图像从 numpy 数组转换为 fastai 张量 - 内存太大

问题描述

我正在使用 fast.ai 开发预训练模型。在我的第一步中,我使用了一个本地图像,我读取了它,然后变量类型是 vision.image.Image。然后我尝试将网络摄像头中的图像与 cv2.Videocapture 一起使用。但在这里我的问题出现了。因为 frame一个 numpy 数组,首先我需要使用 Image(torch.from_numpy(frame).float()) 将其转换为 fast.ai 张量。但是我遇到了问题。错误

DefaultcpuAllocator:内存不足:您试图分配 167771750400 字节

我想知道为什么通过转换内存变得如此之大。当我使用与以前相同的图像时,只是 cv2.imread() 我遇到了同样的问题。所以我想问题在于将图像转换为张量而不是因为我使用网络摄像头。

这是我之前使用的图片的示例

enter image description here

有什么想法吗?我很欣赏你的回答。谢谢。

from fastai import *
from fastai.vision import *
from fastai.callbacks.hooks import *
from fastai.utils.mem import *
import cv2
import matplotlib as mpl
import matplotlib.pyplot as plt

def acc_camvid(input,target):
    target = target.squeeze(1)
    mask = target != void_code
    #print(mask)
    return (input.argmax(dim=1)[mask]==target[mask]).float().mean()

learn=load_learner(pretrained_model)

cap = cv2.VideoCapture(0)

#frame= cv2.imread(image)
    
_,frame = cap.read()
cap.release()

image = Image(torch.from_numpy(frame).float())
plt.imshow(frame)
  
img = image; img.shape
_,img_hr,b = learn.predict(img)
Image(img_hr).show(figsize=(18,15))

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)