快捷方式

CocoCaptions

class torchvision.datasets.CocoCaptions(root: Union[str, Path], annFile: str, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None, transforms: Optional[Callable] = None)[source]

MS Coco Captions 数据集。

它需要安装 pycocotools,可以通过 pip install pycocotoolsconda install conda-forge::pycocotools 进行安装。

参数:
  • root (str or pathlib.Path) – 下载图像的根目录。

  • annFile (string) – json 标注文件的路径。

  • transform (callable, optional) – 一个函数/转换,接受 PIL 图像并返回转换后的版本。例如,transforms.PILToTensor

  • target_transform (callable, optional) – 一个函数/转换,接受目标并对其进行转换。

  • transforms (callable, optional) – 一个函数/转换,接受输入样本及其目标作为输入,并返回转换后的版本。

示例

import torchvision.datasets as dset
import torchvision.transforms as transforms
cap = dset.CocoCaptions(root = 'dir where images are',
                        annFile = 'json annotation file',
                        transform=transforms.PILToTensor())

print('Number of samples: ', len(cap))
img, target = cap[3] # load 4th sample

print("Image Size: ", img.size())
print(target)

输出

Number of samples: 82783
Image Size: (3L, 427L, 640L)
[u'A plane emitting smoke stream flying over a mountain.',
u'A plane darts across a bright blue sky behind a mountain covered in snow',
u'A plane leaves a contrail above the snowy mountain top.',
u'A mountain that has a plane flying overheard in the distance.',
u'A mountain view with a plume of smoke in the background']
特殊成员:

__getitem__(index: int) Tuple[Any, Any]
参数:

index (int) – 索引

返回:

样本和元数据,可选地由相应的转换进行转换。

返回类型:

(Any)

文档

访问全面的 PyTorch 开发者文档

查看文档

教程

获取针对初学者和高级开发者的深入教程

查看教程

资源

查找开发资源并解答您的疑问

查看资源