本文整理汇总了Python中torch.utils.data.Subset方法的典型用法代码示例。如果您正苦于以下问题:Python data.Subset方法的具体用法?Python data.Subset怎么用?Python data.Subset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.utils.data
的用法示例。
在下文中一共展示了data.Subset方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_loader
# 需要导入模块: from torch.utils import data [as 别名]
# 或者: from torch.utils.data import Subset [as 别名]
def get_loader(self, force_update=False, override_settings=None, subset_indices=None):
if force_update or self.regime.update(self.epoch, self.steps):
setting = self.get_setting()
if override_settings is not None:
setting.update(override_settings)
self._transform = get_transform(**setting['transform'])
setting['data'].setdefault('transform', self._transform)
self._data = get_dataset(**setting['data'])
if subset_indices is not None:
self._data = Subset(self._data, subset_indices)
if setting['other'].get('distributed', False):
setting['loader']['sampler'] = DistributedSampler(self._data)
setting['loader']['shuffle'] = None
# pin-memory currently broken for distributed
setting['loader']['pin_memory'] = False
self._sampler = setting['loader'].get('sampler', None)
self._loader = torch.utils.data.DataLoader(
self._data, **setting['loader'])
return self._loader
示例2: __next__
# 需要导入模块: from torch.utils import data [as 别名]
# 或者: from torch.utils.data import Subset [as 别名]
def __next__(self):
loader_idx = next(self.idx_cycle)
loader = self.iters[loader_idx]
batch = next(loader)
if isinstance(loader.dataset, Subset):
dataset = loader.dataset.dataset
else:
dataset = loader.dataset
dat_name = dataset.pose_dataset.name
batch["dataset"] = dat_name
if dat_name == "stereohands" or dat_name == "zimsynth":
batch["root"] = "palm"
else:
batch["root"] = "wrist"
if dat_name == "stereohands":
batch["use_stereohands"] = True
else:
batch["use_stereohands"] = False
batch["split"] = dataset.pose_dataset.split
return batch
示例3: step
# 需要导入模块: from torch.utils import data [as 别名]
# 或者: from torch.utils.data import Subset [as 别名]
def step(self) -> bool:
"""Perform an active learning step.
Returns:
boolean, Flag indicating if we continue training.
"""
# High to low
pool = self.dataset.pool
if len(pool) > 0:
# Limit number of samples
if self.max_sample != -1 and self.max_sample < len(pool):
indices = np.random.choice(len(pool), self.max_sample, replace=False)
pool = torchdata.Subset(pool, indices)
else:
indices = np.arange(len(pool))
probs = self.get_probabilities(pool, **self.kwargs)
if probs is not None and (isinstance(probs, types.GeneratorType) or len(probs) > 0):
to_label = self.heuristic(probs)
to_label = indices[np.array(to_label)]
if len(to_label) > 0:
self.dataset.label(to_label[: self.ndata_to_label])
return True
return False
示例4: pool
# 需要导入模块: from torch.utils import data [as 别名]
# 或者: from torch.utils.data import Subset [as 别名]
def pool(self) -> torchdata.Dataset:
"""Returns a new Dataset made from unlabelled samples.
Raises:
ValueError if a pool specific attribute cannot be set.
"""
pool_dataset = copy(self._dataset)
for attr, new_val in self.pool_specifics.items():
if hasattr(pool_dataset, attr):
setattr(pool_dataset, attr, new_val)
else:
raise ValueError(f"{pool_dataset} doesn't have {attr}")
pool_dataset = torchdata.Subset(pool_dataset,
(~self._labelled).nonzero()[0].reshape([-1]))
ald = ActiveLearningPool(pool_dataset, make_unlabelled=self.make_unlabelled)
return ald
示例5: mnist_loaders
# 需要导入模块: from torch.utils import data [as 别名]
# 或者: from torch.utils.data import Subset [as 别名]
def mnist_loaders(dataset, batch_size, shuffle_train = True, shuffle_test = False, normalize_input = False, num_examples = None, test_batch_size=None):
mnist_train = dataset("./data", train=True, download=True, transform=transforms.ToTensor())
mnist_test = dataset("./data", train=False, download=True, transform=transforms.ToTensor())
if num_examples:
indices = list(range(num_examples))
mnist_train = data.Subset(mnist_train, indices)
mnist_test = data.Subset(mnist_test, indices)
train_loader = torch.utils.data.DataLoader(mnist_train, batch_size=batch_size, shuffle=shuffle_train, pin_memory=True, num_workers=min(multiprocessing.cpu_count(),2))
if test_batch_size:
batch_size = test_batch_size
test_loader = torch.utils.data.DataLoader(mnist_test, batch_size=batch_size, shuffle=shuffle_test, pin_memory=True, num_workers=min(multiprocessing.cpu_count(),2))
std = [1.0]
mean = [0.0]
train_loader.std = std
test_loader.std = std
train_loader.mean = mean
test_loader.mean = mean
return train_loader, test_loader
示例6: get_subset
# 需要导入模块: from torch.utils import data [as 别名]
# 或者: from torch.utils.data import Subset [as 别名]
def get_subset(self, indices: Sequence[int]) -> Subset:
"""
Returns a :class:`torch.utils.data.Subset` of the current dataset
based on given indices
Args:
indices: valid indices to extract subset from current dataset
Returns:
Subset: the subset of the current dataset
"""
subset = Subset(self, indices)
subset.__iter__ = self.__iter__
return subset
示例7: get_targets
# 需要导入模块: from torch.utils import data [as 别名]
# 或者: from torch.utils.data import Subset [as 别名]
def get_targets(dataset):
"""Get the targets of a dataset without any target target transforms(!)."""
if isinstance(dataset, TransformedDataset):
return get_targets(dataset.dataset)
if isinstance(dataset, data.Subset):
targets = get_targets(dataset.dataset)
return torch.as_tensor(targets)[dataset.indices]
if isinstance(dataset, data.ConcatDataset):
return torch.cat([get_targets(sub_dataset) for sub_dataset in dataset.datasets])
if isinstance(
dataset, (datasets.MNIST, datasets.ImageFolder,)
):
return torch.as_tensor(dataset.targets)
if isinstance(dataset, datasets.SVHN):
return dataset.labels
raise NotImplementedError(f"Unknown dataset {dataset}!")
示例8: splitDataset
# 需要导入模块: from torch.utils import data [as 别名]
# 或者: from torch.utils.data import Subset [as 别名]
def splitDataset(dataset, groupby):
"""
Split the given dataset into multiple datasets grouped by the given groupby
function. For example::
# Split mnist dataset into 10 datasets, one dataset for each label
splitDataset(mnist, groupby=lambda x: x[1])
# Split mnist dataset into 5 datasets, one dataset for each label pair: [0,1], [2,3],...
splitDataset(mnist, groupby=lambda x: x[1] // 2)
:param dataset: Source dataset to split
:param groupby: Group by function. See :func:`itertools.groupby`
:return: List of datasets
"""
# Split dataset based on the group by function and keep track of indices
indicesByGroup = collections.defaultdict(list)
for k, g in itertools.groupby(enumerate(dataset), key=lambda x: groupby(x[1])):
indicesByGroup[k].extend([i[0] for i in g])
# Sort by group and create a Subset dataset for each of the group indices
_, indices = zip(*(sorted(indicesByGroup.items(), key=lambda x: x[0])))
return [Subset(dataset, indices=i) for i in indices]
示例9: get_data_loaders
# 需要导入模块: from torch.utils import data [as 别名]
# 或者: from torch.utils.data import Subset [as 别名]
def get_data_loaders(dataset, batch_size=32, shuffle=True):
# Creating data indices for training and validation splits:
dataset_size = len(dataset)
indices = list(range(dataset_size))
train_indices, val_indices, test_indices \
= indices[:dataset.mtrain], indices[dataset.mtrain: dataset.mtrain + dataset.mval], \
indices[dataset.mtrain + dataset.mval:]
train_dataset = Subset(dataset, train_indices)
val_dataset = Subset(dataset, val_indices)
test_dataset = Subset(dataset, test_indices)
def collate(batch):
return batch
custom_collate = collate
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=shuffle, collate_fn=custom_collate)
val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=shuffle, collate_fn=custom_collate)
test_loader = DataLoader(test_dataset, batch_size=batch_size, shuffle=shuffle, collate_fn=custom_collate) if len(test_dataset.indices) else None
return train_loader, val_loader, test_loader
示例10: __init__
# 需要导入模块: from torch.utils import data [as 别名]
# 或者: from torch.utils.data import Subset [as 别名]
def __init__(self, root: str, normal_class: int = 5, known_outlier_class: int = 3, n_known_outlier_classes: int = 0,
ratio_known_normal: float = 0.0, ratio_known_outlier: float = 0.0, ratio_pollution: float = 0.0):
super().__init__(root)
# Define normal and outlier classes
self.n_classes = 2 # 0: normal, 1: outlier
self.normal_classes = tuple([normal_class])
self.outlier_classes = list(range(0, 10))
self.outlier_classes.remove(normal_class)
self.outlier_classes = tuple(self.outlier_classes)
if n_known_outlier_classes == 0:
self.known_outlier_classes = ()
elif n_known_outlier_classes == 1:
self.known_outlier_classes = tuple([known_outlier_class])
else:
self.known_outlier_classes = tuple(random.sample(self.outlier_classes, n_known_outlier_classes))
# CIFAR-10 preprocessing: feature scaling to [0, 1]
transform = transforms.ToTensor()
target_transform = transforms.Lambda(lambda x: int(x in self.outlier_classes))
# Get train set
train_set = MyCIFAR10(root=self.root, train=True, transform=transform, target_transform=target_transform,
download=True)
# Create semi-supervised setting
idx, _, semi_targets = create_semisupervised_setting(np.array(train_set.targets), self.normal_classes,
self.outlier_classes, self.known_outlier_classes,
ratio_known_normal, ratio_known_outlier, ratio_pollution)
train_set.semi_targets[idx] = torch.tensor(semi_targets) # set respective semi-supervised labels
# Subset train_set to semi-supervised setup
self.train_set = Subset(train_set, idx)
# Get test set
self.test_set = MyCIFAR10(root=self.root, train=False, transform=transform, target_transform=target_transform,
download=True)
示例11: __init__
# 需要导入模块: from torch.utils import data [as 别名]
# 或者: from torch.utils.data import Subset [as 别名]
def __init__(self, root: str, dataset_name: str, n_known_outlier_classes: int = 0, ratio_known_normal: float = 0.0,
ratio_known_outlier: float = 0.0, ratio_pollution: float = 0.0, random_state=None):
super().__init__(root)
# Define normal and outlier classes
self.n_classes = 2 # 0: normal, 1: outlier
self.normal_classes = (0,)
self.outlier_classes = (1,)
if n_known_outlier_classes == 0:
self.known_outlier_classes = ()
else:
self.known_outlier_classes = (1,)
# Get train set
train_set = ODDSDataset(root=self.root, dataset_name=dataset_name, train=True, random_state=random_state,
download=True)
# Create semi-supervised setting
idx, _, semi_targets = create_semisupervised_setting(train_set.targets.cpu().data.numpy(), self.normal_classes,
self.outlier_classes, self.known_outlier_classes,
ratio_known_normal, ratio_known_outlier, ratio_pollution)
train_set.semi_targets[idx] = torch.tensor(semi_targets) # set respective semi-supervised labels
# Subset train_set to semi-supervised setup
self.train_set = Subset(train_set, idx)
# Get test set
self.test_set = ODDSDataset(root=self.root, dataset_name=dataset_name, train=False, random_state=random_state)
示例12: __init__
# 需要导入模块: from torch.utils import data [as 别名]
# 或者: from torch.utils.data import Subset [as 别名]
def __init__(self, root: str, normal_class: int = 0, known_outlier_class: int = 1, n_known_outlier_classes: int = 0,
ratio_known_normal: float = 0.0, ratio_known_outlier: float = 0.0, ratio_pollution: float = 0.0):
super().__init__(root)
# Define normal and outlier classes
self.n_classes = 2 # 0: normal, 1: outlier
self.normal_classes = tuple([normal_class])
self.outlier_classes = list(range(0, 10))
self.outlier_classes.remove(normal_class)
self.outlier_classes = tuple(self.outlier_classes)
if n_known_outlier_classes == 0:
self.known_outlier_classes = ()
elif n_known_outlier_classes == 1:
self.known_outlier_classes = tuple([known_outlier_class])
else:
self.known_outlier_classes = tuple(random.sample(self.outlier_classes, n_known_outlier_classes))
# FashionMNIST preprocessing: feature scaling to [0, 1]
transform = transforms.ToTensor()
target_transform = transforms.Lambda(lambda x: int(x in self.outlier_classes))
# Get train set
train_set = MyFashionMNIST(root=self.root, train=True, transform=transform, target_transform=target_transform,
download=True)
# Create semi-supervised setting
idx, _, semi_targets = create_semisupervised_setting(train_set.targets.cpu().data.numpy(), self.normal_classes,
self.outlier_classes, self.known_outlier_classes,
ratio_known_normal, ratio_known_outlier, ratio_pollution)
train_set.semi_targets[idx] = torch.tensor(semi_targets) # set respective semi-supervised labels
# Subset train_set to semi-supervised setup
self.train_set = Subset(train_set, idx)
# Get test set
self.test_set = MyFashionMNIST(root=self.root, train=False, transform=transform,
target_transform=target_transform, download=True)
示例13: __init__
# 需要导入模块: from torch.utils import data [as 别名]
# 或者: from torch.utils.data import Subset [as 别名]
def __init__(self, root: str, normal_class: int = 0, known_outlier_class: int = 1, n_known_outlier_classes: int = 0,
ratio_known_normal: float = 0.0, ratio_known_outlier: float = 0.0, ratio_pollution: float = 0.0):
super().__init__(root)
# Define normal and outlier classes
self.n_classes = 2 # 0: normal, 1: outlier
self.normal_classes = tuple([normal_class])
self.outlier_classes = list(range(0, 10))
self.outlier_classes.remove(normal_class)
self.outlier_classes = tuple(self.outlier_classes)
if n_known_outlier_classes == 0:
self.known_outlier_classes = ()
elif n_known_outlier_classes == 1:
self.known_outlier_classes = tuple([known_outlier_class])
else:
self.known_outlier_classes = tuple(random.sample(self.outlier_classes, n_known_outlier_classes))
# MNIST preprocessing: feature scaling to [0, 1]
transform = transforms.ToTensor()
target_transform = transforms.Lambda(lambda x: int(x in self.outlier_classes))
# Get train set
train_set = MyMNIST(root=self.root, train=True, transform=transform, target_transform=target_transform,
download=True)
# Create semi-supervised setting
idx, _, semi_targets = create_semisupervised_setting(train_set.targets.cpu().data.numpy(), self.normal_classes,
self.outlier_classes, self.known_outlier_classes,
ratio_known_normal, ratio_known_outlier, ratio_pollution)
train_set.semi_targets[idx] = torch.tensor(semi_targets) # set respective semi-supervised labels
# Subset train_set to semi-supervised setup
self.train_set = Subset(train_set, idx)
# Get test set
self.test_set = MyMNIST(root=self.root, train=False, transform=transform, target_transform=target_transform,
download=True)
示例14: get_data_loader
# 需要导入模块: from torch.utils import data [as 别名]
# 或者: from torch.utils.data import Subset [as 别名]
def get_data_loader(config):
cfg = config["dataset"]["path"]["test"]
image_dataset = dataset.PairBoneDataset(cfg["pair"], cfg["image"], cfg["bone"], cfg["mask"], cfg["annotation"])
if "generated_limit" in config:
image_dataset = Subset(image_dataset, range(config["generated_limit"]))
image_loader = DataLoader(image_dataset, batch_size=config["train"]["batch_size"],
num_workers=8, pin_memory=True, drop_last=True)
print(image_dataset)
return image_loader
示例15: __init__
# 需要导入模块: from torch.utils import data [as 别名]
# 或者: from torch.utils.data import Subset [as 别名]
def __init__(self, root: str, normal_class=5):
super().__init__(root)
self.n_classes = 2 # 0: normal, 1: outlier
self.normal_classes = tuple([normal_class])
self.outlier_classes = list(range(0, 10))
self.outlier_classes.remove(normal_class)
# Pre-computed min and max values (after applying GCN) from train data per class
min_max = [(-28.94083453598571, 13.802961825439636),
(-6.681770233365245, 9.158067708230273),
(-34.924463588638204, 14.419298165027628),
(-10.599172931391799, 11.093187820377565),
(-11.945022995801637, 10.628045447867583),
(-9.691969487694928, 8.948326776180823),
(-9.174940012342555, 13.847014686472365),
(-6.876682005899029, 12.282371383343161),
(-15.603507135507172, 15.2464923804279),
(-6.132882973622672, 8.046098172351265)]
# CIFAR-10 preprocessing: GCN (with L1 norm) and min-max feature scaling to [0,1]
transform = transforms.Compose([transforms.ToTensor(),
transforms.Lambda(lambda x: global_contrast_normalization(x, scale='l1')),
transforms.Normalize([min_max[normal_class][0]] * 3,
[min_max[normal_class][1] - min_max[normal_class][0]] * 3)])
target_transform = transforms.Lambda(lambda x: int(x in self.outlier_classes))
train_set = MyCIFAR10(root=self.root, train=True, download=True,
transform=transform, target_transform=target_transform)
# Subset train set to normal class
train_idx_normal = get_target_label_idx(train_set.train_labels, self.normal_classes)
self.train_set = Subset(train_set, train_idx_normal)
self.test_set = MyCIFAR10(root=self.root, train=False, download=True,
transform=transform, target_transform=target_transform)