视频分类数据训练与预测的加载方式探索

加载方式一

将视频数据按顺序保存成图片数据

优点:好像没有啥优点

缺点:繁琐、小文件特别多,索引速度慢

加载方式二

使用python或者pytorch提供的一些接口直接加载视频数据,这里比较推荐pytorch的接口。

优点:简单方便,硬盘空间消耗最少

缺点:加载的数据需要做解码,会偏慢

加载方式三

将视频数据转换成numpy数组,并保存成npy的格式

优点:加载速度快

缺点:步骤多,占用硬盘空间大

使用pytorch官方的API加载范例

代码来源,代码功能:

1.使用pytorch提供的视频加载接口加载数据

2.将加载的数据喂入预训练的R3D模型中

3.获得输出分类和得分

from torchvision.io.video import read_video
from torchvision.models.video import r3d_18, R3D_18_Weights

vid, _, _ = read_video("test/assets/videos/v_SoccerJuggling_g23_c01.avi", output_format="TCHW")
vid = vid[:32]  # optionally shorten duration

# Step 1: Initialize model with the best available weights
weights = R3D_18_Weights.DEFAULT
model = r3d_18(weights=weights)
model.eval()

# Step 2: Initialize the inference transforms
preprocess = weights.transforms()

# Step 3: Apply inference preprocessing transforms
batch = preprocess(vid).unsqueeze(0)

# Step 4: Use the model and print the predicted category
prediction = model(batch).squeeze(0).softmax(0)
label = prediction.argmax().item()
score = prediction[label].item()
category_name = weights.meta["categories"][label]
print(f"{category_name}: {100 * score}%")

结尾

还有没有更多的加载方式呢?有的话请@我去试试的

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值