从 PyTorch DataLoader 获取单个随机示例 [英] Get single random example from PyTorch DataLoader

查看:23
本文介绍了从 PyTorch DataLoader 获取单个随机示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

How do I get a single random example from a PyTorch DataLoader?

If my DataLoader gives minbatches of multiple images and labels, how do I get a single random image and label?

Note that I don't want a single image and label per minibatch, I want a total of one example.

解决方案

If you want to choose specific images from your Trainloader/Testloader, you should check out the Subset function from master:

Here's an example how to use it:

testset = ImageFolderWithPaths(root="path/to/your/Image_Data/Test/", transform=transform)
subset_indices = [0] # select your indices here as a list
subset = torch.utils.data.Subset(testset, subset_indices)
testloader_subset = torch.utils.data.DataLoader(subset, batch_size=1, num_workers=0, shuffle=False)

This way you can use exactly one image and label. However, you can of course use more than just one index in your subset_indices.

If you want to use a specific image from your DataFolder, you can use dataset.sample and build a dictionary to get the index of the image you want to use.

这篇关于从 PyTorch DataLoader 获取单个随机示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆