mindspore.dataset.vision.TenCrop
- class mindspore.dataset.vision.TenCrop(size, use_vertical_flip=False)[源代码]
在输入PIL图像的中心与四个角处分别裁剪指定尺寸大小的子图,并将其翻转图一并返回。
- 参数:
size (Union[int, Sequence[int, int]]) - 裁剪子图的尺寸大小。若输入int,则以该值为边长裁剪( size , size )尺寸大小的子图;若输入Sequence[int, int],则以2个元素分别为高和宽裁剪子图。
use_vertical_flip (bool,可选) - 若为
True
,将对子图进行垂直翻转;否则进行水平翻转。默认值:False
。
- 异常:
TypeError - 当 size 的类型不为int或Sequence[int, int]。
TypeError - 当 use_vertical_flip 的类型不为bool。
ValueError - 当 size 不为正数。
- 支持平台:
CPU
样例:
>>> import os >>> import numpy as np >>> from PIL import Image, ImageDraw >>> import mindspore.dataset as ds >>> import mindspore.dataset.vision as vision >>> from mindspore.dataset.transforms import Compose >>> >>> # Use the transform in dataset pipeline mode >>> class MyDataset: ... def __init__(self): ... self.data = [] ... img = Image.new("RGB", (300, 300), (255, 255, 255)) ... draw = ImageDraw.Draw(img) ... draw.ellipse(((0, 0), (100, 100)), fill=(255, 0, 0), outline=(255, 0, 0), width=5) ... img.save("./1.jpg") ... data = np.fromfile("./1.jpg", np.uint8) ... self.data.append(data) ... ... def __getitem__(self, index): ... return self.data[0] ... ... def __len__(self): ... return 5 >>> >>> my_dataset = MyDataset() >>> generator_dataset = ds.GeneratorDataset(my_dataset, column_names="image") >>> transforms_list = Compose([vision.Decode(to_pil=True), ... vision.TenCrop(size=200), ... # 4D stack of 10 images ... lambda *images: np.stack([vision.ToTensor()(image) for image in images])]) >>> # apply the transform to dataset through map function >>> generator_dataset = generator_dataset.map(operations=transforms_list, input_columns="image") >>> for item in generator_dataset.create_dict_iterator(num_epochs=1, output_numpy=True): ... print(item["image"].shape, item["image"].dtype) ... break (10, 3, 200, 200) float32 >>> os.remove("./1.jpg") >>> >>> # Use the transform in eager mode >>> img = Image.new("RGB", (300, 300), (255, 255, 255)) >>> draw = ImageDraw.Draw(img) >>> draw.polygon([(50, 50), (150, 50), (100, 150)], fill=(0, 255, 0), outline=(0, 255, 0)) >>> img.save("./2.jpg") >>> data = Image.open("./2.jpg") >>> output = vision.TenCrop(size=200)(data) >>> print(len(output), np.array(output[0]).shape, np.array(output[0]).dtype) 10 (200, 200, 3) uint8 >>> os.remove("./2.jpg")
- 教程样例: