mindspore.dataset.vision.LinearTransformation

查看源文件
class mindspore.dataset.vision.LinearTransformation(transformation_matrix, mean_vector)[源代码]

使用指定的变换方阵和均值向量对输入numpy.ndarray图像进行线性变换。

先将输入图像展平为一维,从中减去均值向量,然后计算其与变换方阵的点积,最后再变形回原始shape。

参数:
  • transformation_matrix (numpy.ndarray) - shape为(D, D)的变换方阵,其中 \(D = C \times H \times W\)

  • mean_vector (numpy.ndarray) - shape为(D,)的均值向量,其中 \(D = C \times H \times W\)

异常:
  • TypeError - 当 transformation_matrix 的类型不为 numpy.ndarray

  • TypeError - 当 mean_vector 的类型不为 numpy.ndarray

支持平台:

CPU

样例:

>>> import numpy as np
>>> import mindspore.dataset as ds
>>> import mindspore.dataset.vision as vision
>>> from mindspore.dataset.transforms import Compose
>>>
>>> # Use the transform in dataset pipeline mode
>>> height, width = 32, 32
>>> dim = 3 * height * width
>>> transformation_matrix = np.ones([dim, dim])
>>> mean_vector = np.zeros(dim)
>>> transforms_list = Compose([vision.Resize((height,width)),
...                            vision.ToTensor(),
...                            vision.LinearTransformation(transformation_matrix, mean_vector)])
>>> # apply the transform to dataset through map function
>>> data = np.random.randint(0, 255, size=(1, 100, 100, 3)).astype(np.uint8)
>>> numpy_slices_dataset = ds.NumpySlicesDataset(data, ["image"])
>>> numpy_slices_dataset = numpy_slices_dataset.map(operations=transforms_list, input_columns="image")
>>> for item in numpy_slices_dataset.create_dict_iterator(num_epochs=1, output_numpy=True):
...     print(item["image"].shape, item["image"].dtype)
...     break
(3, 32, 32) float64
>>>
>>> # Use the transform in eager mode
>>> data = np.random.randn(10, 10, 3)
>>> transformation_matrix = np.random.randn(300, 300)
>>> mean_vector = np.random.randn(300,)
>>> output = vision.LinearTransformation(transformation_matrix, mean_vector)(data)
>>> print(output.shape, output.dtype)
(10, 10, 3) float64
教程样例: