我对 fastai 非常陌生,我正在尝试调整以前的模型。只有当文件在磁盘上时,我才能加载模型并使用推理。由于我正在尝试将文件处理为视频流,因此我正在保存每个文件并一次处理一个文件,例如。
import cv2, imageio
from fastai.vision import *
# ... load the model here
stream = cv2.VideoCapture('Die.Hard.1988.mkv')
while True:
_, frame = stream.read()
with tempfile.NamedTemporaryFile(suffix='.jpg') as FOUT:
imageio.imwrite(FOUT.name, frame)
FOUT.flush()
x = open_image(FOUT.name)
preds_num = learn.predict(x)[2].numpy()
这似乎可行,但将图像保存到磁盘似乎很浪费。该库似乎使用了一些转换,这意味着我不能直接将图像推送给“学习者”。这是它的加载方式:
from fastai.vision import *
f_model = "shot-type-classifier"
path = "shot_type_classifier/"
data = ImageDataBunch.from_folder(
path,
"train",
"valid",
size=(375, 666),
ds_tfms=get_tfms(),
bs=1,
resize_method=ResizeMethod.SQUISH,
num_workers=0,
).normalize(imagenet_stats)
learn = cnn_learner(data, models.resnet50, metrics=[accuracy], pretrained=True)
learn = learn.to_fp16()
learn.load(f_model)
这里似乎定义了转换。我真的只想将图像直接传送到预处理器,然后传送到模型,而不必将它们保存到磁盘。有没有办法使用fastai做到这一点?