本文整理匯總了Python中torchvision.transforms.transforms.Compose方法的典型用法代碼示例。如果您正苦於以下問題:Python transforms.Compose方法的具體用法?Python transforms.Compose怎麽用?Python transforms.Compose使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類torchvision.transforms.transforms
的用法示例。
在下文中一共展示了transforms.Compose方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: preprocess
# 需要導入模塊: from torchvision.transforms import transforms [as 別名]
# 或者: from torchvision.transforms.transforms import Compose [as 別名]
def preprocess(image: PIL.Image.Image, image_min_side: float, image_max_side: float) -> Tuple[Tensor, float]:
# resize according to the rules:
# 1. scale shorter side to IMAGE_MIN_SIDE
# 2. after scaling, if longer side > IMAGE_MAX_SIDE, scale longer side to IMAGE_MAX_SIDE
scale_for_shorter_side = image_min_side / min(image.width, image.height)
longer_side_after_scaling = max(image.width, image.height) * scale_for_shorter_side
scale_for_longer_side = (image_max_side / longer_side_after_scaling) if longer_side_after_scaling > image_max_side else 1
scale = scale_for_shorter_side * scale_for_longer_side
transform = transforms.Compose([
transforms.Resize((round(image.height * scale), round(image.width * scale))), # interpolation `BILINEAR` is applied by default
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
image = transform(image)
return image, scale
示例2: preprocess
# 需要導入模塊: from torchvision.transforms import transforms [as 別名]
# 或者: from torchvision.transforms.transforms import Compose [as 別名]
def preprocess(self,image: PIL.Image.Image, image_min_side: float, image_max_side: float) -> Tuple[Tensor, float]:
# resize according to the rules:
# 1. scale shorter side to IMAGE_MIN_SIDE
# 2. after scaling, if longer side > IMAGE_MAX_SIDE, scale longer side to IMAGE_MAX_SIDE
scale_for_shorter_side = image_min_side / min(image.width, image.height)
longer_side_after_scaling = max(image.width, image.height) * scale_for_shorter_side
scale_for_longer_side = (image_max_side / longer_side_after_scaling) if longer_side_after_scaling > image_max_side else 1
scale = scale_for_shorter_side * scale_for_longer_side
transform = transforms.Compose([
transforms.Resize((round(image.height * scale), round(image.width * scale))), # interpolation `BILINEAR` is applied by default
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
image = transform(image)
return image, scale
示例3: pil_to_tensor
# 需要導入模塊: from torchvision.transforms import transforms [as 別名]
# 或者: from torchvision.transforms.transforms import Compose [as 別名]
def pil_to_tensor(img, shape=(64, 64, 3), transform=None):
"""
Convert PIL image to float tensor
:param img: PIL image
:type img: Image.Image
:param shape: image shape in (H, W, C)
:type shape: tuple or list
:param transform: image transform
:return: tensor
:rtype: torch.Tensor
"""
if transform is None:
transform = transforms.Compose((
transforms.Resize(shape[0]),
transforms.ToTensor()
))
return transform(img)
示例4: get_datasets
# 需要導入模塊: from torchvision.transforms import transforms [as 別名]
# 或者: from torchvision.transforms.transforms import Compose [as 別名]
def get_datasets(initial_pool):
transform = transforms.Compose(
[transforms.Resize((224, 224)),
transforms.RandomHorizontalFlip(),
transforms.RandomRotation(30),
transforms.ToTensor(),
transforms.Normalize(3 * [0.5], 3 * [0.5]), ])
test_transform = transforms.Compose(
[
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize(3 * [0.5], 3 * [0.5]),
]
)
# Note: We use the test set here as an example. You should make your own validation set.
train_ds = datasets.CIFAR10('.', train=True,
transform=transform, target_transform=None, download=True)
test_set = datasets.CIFAR10('.', train=False,
transform=test_transform, target_transform=None, download=True)
active_set = ActiveLearningDataset(train_ds, pool_specifics={'transform': test_transform})
# We start labeling randomly.
active_set.label_randomly(initial_pool)
return active_set, test_set
示例5: get_training_set_gt
# 需要導入模塊: from torchvision.transforms import transforms [as 別名]
# 或者: from torchvision.transforms.transforms import Compose [as 別名]
def get_training_set_gt(dataset_path: str, image_size: ImageSize):
num_joints = 15
left_indexes: List[int] = [3, 4, 5, 9, 10, 11]
right_indexes: List[int] = [6, 7, 8, 12, 13, 14]
datasets: List[EhpiLSTMDataset] = [
EhpiLSTMDataset(os.path.join(dataset_path, "JOURNAL_2019_03_GT_30fps"),
transform=transforms.Compose([
RemoveJointsOutsideImgEhpi(image_size),
ScaleEhpi(image_size),
TranslateEhpi(image_size),
FlipEhpi(left_indexes=left_indexes, right_indexes=right_indexes),
NormalizeEhpi(image_size)
]), num_joints=num_joints),
]
for dataset in datasets:
dataset.print_label_statistics()
return ConcatDataset(datasets)
示例6: get_training_posealgo
# 需要導入模塊: from torchvision.transforms import transforms [as 別名]
# 或者: from torchvision.transforms.transforms import Compose [as 別名]
def get_training_posealgo(dataset_path: str, image_size: ImageSize):
num_joints = 15
left_indexes: List[int] = [3, 4, 5, 9, 10, 11]
right_indexes: List[int] = [6, 7, 8, 12, 13, 14]
datasets: List[EhpiLSTMDataset] = [
EhpiLSTMDataset(os.path.join(dataset_path, "JOURNAL_2019_03_POSEALGO_30fps"),
transform=transforms.Compose([
RemoveJointsOutsideImgEhpi(image_size),
ScaleEhpi(image_size),
TranslateEhpi(image_size),
FlipEhpi(left_indexes=left_indexes, right_indexes=right_indexes),
NormalizeEhpi(image_size)
]), num_joints=num_joints),
]
for dataset in datasets:
dataset.print_label_statistics()
return ConcatDataset(datasets)
示例7: get_test_set_lab
# 需要導入模塊: from torchvision.transforms import transforms [as 別名]
# 或者: from torchvision.transforms.transforms import Compose [as 別名]
def get_test_set_lab(dataset_path: str, image_size: ImageSize):
num_joints = 15
datasets = [
EhpiLSTMDataset(os.path.join(dataset_path, "JOURNAL_2019_03_TEST_VUE01_30FPS"),
transform=transforms.Compose([
RemoveJointsOutsideImgEhpi(image_size),
NormalizeEhpi(image_size)
]), num_joints=num_joints, dataset_part=DatasetPart.TEST),
EhpiLSTMDataset(os.path.join(dataset_path, "JOURNAL_2019_03_TEST_VUE02_30FPS"),
transform=transforms.Compose([
RemoveJointsOutsideImgEhpi(image_size),
NormalizeEhpi(image_size)
]), num_joints=num_joints, dataset_part=DatasetPart.TEST),
]
for dataset in datasets:
dataset.print_label_statistics()
return ConcatDataset(datasets)
示例8: prepare_data
# 需要導入模塊: from torchvision.transforms import transforms [as 別名]
# 或者: from torchvision.transforms.transforms import Compose [as 別名]
def prepare_data(images, color_mode='BGR', new_shape=416, color=(127.5, 127.5, 127.5), mode='square'):
images_ok = np.zeros((images.shape[0], new_shape, new_shape, 3), dtype=images[0].dtype)
images_tensor = torch.zeros((images.shape[0], 3, new_shape, new_shape), dtype=torch.float32)
for i in range(len(images)):
if color_mode == 'BGR':
images[i] = cv2.cvtColor(images[i], cv2.COLOR_BGR2RGB)
elif color_mode == 'RGB':
pass
else:
raise NotImplementedError
images_ok[i], _, _, _ = letterbox(images[i], new_shape, color, mode)
images_tensor[i] = transforms.Compose([
transforms.ToPILImage(),
transforms.ToTensor(),
])(images_ok[i])
return images_tensor
示例9: load_images
# 需要導入模塊: from torchvision.transforms import transforms [as 別名]
# 或者: from torchvision.transforms.transforms import Compose [as 別名]
def load_images(img_path):
# imread from img_path
img = cv2.imread(img_path)
img = cv2.resize(img, (224, 224))
# pytorch must normalize the pic by
# mean = [0.485, 0.456, 0.406]
# std = [0.229, 0.224, 0.225]
transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],std=[0.229, 0.224, 0.225])
])
img = transform(img)
img.unsqueeze_(0)
#img_s = img.numpy()
#img_s = np.transpose(img_s, (1, 2, 0))
#cv2.imshow("test img", img_s)
#cv2.waitKey()
return img
示例10: get_transforms
# 需要導入模塊: from torchvision.transforms import transforms [as 別名]
# 或者: from torchvision.transforms.transforms import Compose [as 別名]
def get_transforms(eval=False, aug=None):
trans = []
if aug["randcrop"] and not eval:
trans.append(transforms.RandomCrop(aug["randcrop"]))
if aug["randcrop"] and eval:
trans.append(transforms.CenterCrop(aug["randcrop"]))
if aug["flip"] and not eval:
trans.append(transforms.RandomHorizontalFlip())
if aug["grayscale"]:
trans.append(transforms.Grayscale())
trans.append(transforms.ToTensor())
trans.append(transforms.Normalize(mean=aug["bw_mean"], std=aug["bw_std"]))
elif aug["mean"]:
trans.append(transforms.ToTensor())
trans.append(transforms.Normalize(mean=aug["mean"], std=aug["std"]))
else:
trans.append(transforms.ToTensor())
trans = transforms.Compose(trans)
return trans
示例11: get_test_set
# 需要導入模塊: from torchvision.transforms import transforms [as 別名]
# 或者: from torchvision.transforms.transforms import Compose [as 別名]
def get_test_set(dataset_path: str, image_size: ImageSize):
num_joints = 15
return EhpiDataset(os.path.join(dataset_path, "2019_03_13_Freilichtmuseum_30FPS"),
transform=transforms.Compose([
RemoveJointsOutsideImgEhpi(image_size),
NormalizeEhpi(image_size)
]), dataset_part=DatasetPart.TEST, num_joints=num_joints)
示例12: get_training_set
# 需要導入模塊: from torchvision.transforms import transforms [as 別名]
# 或者: from torchvision.transforms.transforms import Compose [as 別名]
def get_training_set(dataset_path: str, image_size: ImageSize):
num_joints = 15
left_indexes: List[int] = [3, 4, 5, 9, 10, 11]
right_indexes: List[int] = [6, 7, 8, 12, 13, 14]
return EhpiDataset(os.path.join(dataset_path, "JHMDB_ITSC-1/"),
transform=transforms.Compose([
RemoveJointsOutsideImgEhpi(image_size),
RemoveJointsEhpi(indexes_to_remove=foot_indexes, indexes_to_remove_2=knee_indexes,
probability=0.25),
ScaleEhpi(image_size),
TranslateEhpi(image_size),
FlipEhpi(left_indexes=left_indexes, right_indexes=right_indexes),
NormalizeEhpi(image_size)
]), num_joints=num_joints)
示例13: get_sim_pose_algo_only
# 需要導入模塊: from torchvision.transforms import transforms [as 別名]
# 或者: from torchvision.transforms.transforms import Compose [as 別名]
def get_sim_pose_algo_only(dataset_path: str, image_size: ImageSize):
num_joints = 15
left_indexes: List[int] = [3, 4, 5, 9, 10, 11]
right_indexes: List[int] = [6, 7, 8, 12, 13, 14]
datasets: List[EhpiDataset] = [
EhpiDataset(os.path.join(dataset_path, "ofp_sim_pose_algo_equal_30fps"),
transform=transforms.Compose([
RemoveJointsOutsideImgEhpi(image_size),
RemoveJointsEhpi(indexes_to_remove=foot_indexes, indexes_to_remove_2=knee_indexes,
probability=0.25),
ScaleEhpi(image_size),
TranslateEhpi(image_size),
FlipEhpi(left_indexes=left_indexes, right_indexes=right_indexes),
NormalizeEhpi(image_size)
]), num_joints=num_joints),
EhpiDataset(os.path.join(dataset_path, "ofp_from_mocap_pose_algo_30fps"),
transform=transforms.Compose([
RemoveJointsOutsideImgEhpi(image_size),
RemoveJointsEhpi(indexes_to_remove=foot_indexes, indexes_to_remove_2=knee_indexes,
probability=0.25),
ScaleEhpi(image_size),
TranslateEhpi(image_size),
FlipEhpi(left_indexes=left_indexes, right_indexes=right_indexes),
NormalizeEhpi(image_size)
]), num_joints=num_joints),
]
for dataset in datasets:
dataset.print_label_statistics()
return ConcatDataset(datasets)
示例14: get_sim_gt_only
# 需要導入模塊: from torchvision.transforms import transforms [as 別名]
# 或者: from torchvision.transforms.transforms import Compose [as 別名]
def get_sim_gt_only(dataset_path: str, image_size: ImageSize):
num_joints = 15
left_indexes: List[int] = [3, 4, 5, 9, 10, 11]
right_indexes: List[int] = [6, 7, 8, 12, 13, 14]
datasets: List[EhpiDataset] = [
EhpiDataset(os.path.join(dataset_path, "ofp_sim_gt_equal_30fps"),
transform=transforms.Compose([
RemoveJointsOutsideImgEhpi(image_size),
RemoveJointsEhpi(indexes_to_remove=foot_indexes, indexes_to_remove_2=knee_indexes,
probability=0.25),
ScaleEhpi(image_size),
TranslateEhpi(image_size),
FlipEhpi(left_indexes=left_indexes, right_indexes=right_indexes),
NormalizeEhpi(image_size)
]), num_joints=num_joints),
EhpiDataset(os.path.join(dataset_path, "ofp_from_mocap_gt_30fps"),
transform=transforms.Compose([
RemoveJointsOutsideImgEhpi(image_size),
RemoveJointsEhpi(indexes_to_remove=foot_indexes, indexes_to_remove_2=knee_indexes,
probability=0.25),
ScaleEhpi(image_size),
TranslateEhpi(image_size),
FlipEhpi(left_indexes=left_indexes, right_indexes=right_indexes),
NormalizeEhpi(image_size)
]), num_joints=num_joints),
]
for dataset in datasets:
dataset.print_label_statistics()
return ConcatDataset(datasets)
示例15: get_training_set
# 需要導入模塊: from torchvision.transforms import transforms [as 別名]
# 或者: from torchvision.transforms.transforms import Compose [as 別名]
def get_training_set(dataset_path: str, image_size: ImageSize):
num_joints = 15
left_indexes: List[int] = [3, 4, 5, 9, 10, 11]
right_indexes: List[int] = [6, 7, 8, 12, 13, 14]
return EhpiDataset(dataset_path,
transform=transforms.Compose([
RemoveJointsOutsideImgEhpi(image_size),
RemoveJointsEhpi(indexes_to_remove=foot_indexes, indexes_to_remove_2=knee_indexes,
probability=0.25),
ScaleEhpi(image_size),
TranslateEhpi(image_size),
FlipEhpi(left_indexes=left_indexes, right_indexes=right_indexes),
NormalizeEhpi(image_size)
]), num_joints=num_joints)