Torchvision 数据集是计算机视觉中常用的流行数据集的集合,用于开发和测试机器学习模型。借助 torchvision 数据集,开发人员可以在一系列任务上训练和测试他们的机器学习模型,例如图像分类、对象检测和分割。
要访问此数据集,您可以直接从
import torchvision.datasets as datasets # Load the training dataset train_dataset = datasets.MNIST(root='data/', train=True, transform=None, download=True) # Load the testing dataset test_dataset = datasets.MNIST(root='data/', train=False, transform=None, download=True)
使用 PyTorch torchvision 包加载 MNIST 数据集的代码。从于 20/3/2023 检索。
这个数据集可以从下载
import torch import torchvision import torchvision.transforms as transforms transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]) trainset = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform) testset = torchvision.datasets.CIFAR10(root='./data', train=False, download=True, transform=transform) trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2) testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2)
使用 PyTorch torchvision 包加载 CIFAR-10 数据集的代码。于 20/3/2023 从检索。
要从 Kaggle 下载 torchvision 数据集,请访问 Kaggle
import torchvision.datasets as datasets import torchvision.transforms as transforms # Define transform to normalize data transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]) ]) # Load CIFAR-100 train and test datasets trainset = datasets.CIFAR100(root='./data', train=True, download=True, transform=transform) testset = datasets.CIFAR100(root='./data', train=False, download=True, transform=transform) # Create data loaders for train and test datasets trainloader = torch.utils.data.DataLoader(trainset, batch_size=64, shuffle=True) testloader = torch.utils.data.DataLoader(testset, batch_size=64, shuffle=False)
使用 PyTorch torchvision 包加载 CIFAR-100 数据集的代码。于 20/3/2023 从检索。
要下载此 torchvision 数据集,您必须访问
import torchvision.datasets as datasets import torchvision.transforms as transforms # Set the path to the ImageNet dataset on your machine data_path = "/path/to/imagenet" # Create the ImageNet dataset object with custom options imagenet_train = datasets.ImageNet( root=data_path, split='train', transform=transforms.Compose([ transforms.Resize(256), transforms.RandomCrop(224), transforms.RandomHorizontalFlip(), transforms.ToTensor(), transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]), download=False ) imagenet_val = datasets.ImageNet( root=data_path, split='val', transform=transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize( mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]), download=False ) # Print the number of images in the training and validation sets print("Number of images in the training set:", len(imagenet_train)) print("Number of images in the validation set:", len(imagenet_val))
使用 PyTorch torchvision 包加载 ImageNet 数据集的代码。于 2023 年 3 月 21 日从检索。
要下载此 torchvision 数据集,请访问
import torch from torchvision import datasets, transforms # Define transformation transform = transforms.Compose([ transforms.Resize(256), transforms.CenterCrop(224), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # Load training dataset train_dataset = datasets.CocoDetection(root='/path/to/dataset/train2017', annFile='/path/to/dataset/annotations/instances_train2017.json', transform=transform) train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True) # Load validation dataset val_dataset = datasets.CocoDetection(root='/path/to/dataset/val2017', annFile='/path/to/dataset/annotations/instances_val2017.json', transform=transform) val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=32, shuffle=False)
使用 PyTorch torchvision 包加载 MS Coco 数据集的代码。从于 21/3/2023 检索。
这个 torchvision 数据集可以从下载
import torch import torchvision import torchvision.transforms as transforms # Define transformations transform = transforms.Compose( [transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))]) # Load the dataset trainset = torchvision.datasets.FashionMNIST(root='./data', train=True, download=True, transform=transform) testset = torchvision.datasets.FashionMNIST(root='./data', train=False, download=True, transform=transform) # Create data loaders trainloader = torch.utils.data.DataLoader(trainset, batch_size=4, shuffle=True, num_workers=2) testloader = torch.utils.data.DataLoader(testset, batch_size=4, shuffle=False, num_workers=2)
使用 PyTorch torchvision 包加载 Fashion-MNIST 数据集的代码。于 2023 年 3 月 21 日从检索。
要下载这个 torchvision 数据集,你可以去
import torchvision import torch # Load the train and test sets train_set = torchvision.datasets.SVHN(root='./data', split='train', download=True, transform=torchvision.transforms.ToTensor()) test_set = torchvision.datasets.SVHN(root='./data', split='test', download=True, transform=torchvision.transforms.ToTensor()) # Create data loaders train_loader = torch.utils.data.DataLoader(train_set, batch_size=64, shuffle=True) test_loader = torch.utils.data.DataLoader(test_set, batch_size=64, shuffle=False)
使用 PyTorch torchvision 包加载 SVHN 数据集的代码。于 22/3/2023 从检索。
要访问此数据集,您可以直接从
import torchvision.datasets as datasets import torchvision.transforms as transforms # Define the transformation to apply to the data transform = transforms.Compose([ transforms.ToTensor(), # Convert PIL image to PyTorch tensor transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) # Normalize the data ]) # Load the STL-10 dataset train_dataset = datasets.STL10(root='./data', split='train', download=True, transform=transform) test_dataset = datasets.STL10(root='./data', split='test', download=True, transform=transform)
使用 PyTorch torchvision 包加载 STL-10 数据集的代码。 2023 年 3 月 22 日从检索。
你可以下载这个数据集
import torchvision.datasets as datasets import torchvision.transforms as transforms transform = transforms.Compose([ transforms.CenterCrop(178), transforms.Resize(128), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)) ]) celeba_dataset = datasets.CelebA(root='./data', split='train', transform=transform, download=True)
使用 PyTorch torchvision 包加载 CelebA 数据集的代码。 2023 年 3 月 22 日从检索。
要访问最近的数据集,您可以从
import torch import torchvision from torchvision import transforms # Define transformations to apply to the images transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # Load the train and validation datasets train_dataset = torchvision.datasets.VOCDetection(root='./data', year='2007', image_set='train', transform=transform) val_dataset = torchvision.datasets.VOCDetection(root='./data', year='2007', image_set='val', transform=transform) # Create data loaders train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True) val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=32, shuffle=False)
使用 PyTorch torchvision 包加载 PASCAL VOC 数据集的代码。 2023 年 3 月 22 日从中检索。
要访问此数据集,您可以使用
import torch import torchvision from torchvision import transforms # Define transformations to apply to the images transform = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) # Load the train and validation datasets train_dataset = torchvision.datasets.Places365(root='./data', split='train-standard', transform=transform) val_dataset = torchvision.datasets.Places365(root='./data', split='val', transform=transform) # Create data loaders train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True) val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=32, shuffle=False)
使用 PyTorch torchvision 包加载 Places365 数据集的代码。 2023 年 3 月 22 日从检索。
本文的主图是通过HackerNoon 的 AI Stable Diffusion模型生成的,使用的提示是“数千张图像组织成小帧”。
更多数据集列表: