比较与torchvision.transforms.RandomSolarize的功能差异

查看源文件

torchvision.transforms.RandomSolarize

class torchvision.transforms.RandomSolarize(
    threshold,
    p=0.5
    )

更多内容详见torchvision.transforms.RandomSolarize

mindspore.dataset.vision.RandomSolarize

class mindspore.dataset.vision.RandomSolarize(
    threshold=(0, 255)
    )

更多内容详见mindspore.dataset.vision.RandomSolarize

使用方式

PyTorch:通过反转高于阈值的所有像素值,以给定的概率随机对图像进行曝光操作。

MindSpore:从指定阈值范围内随机选择一个子范围,并将图像像素值调整在子范围内。

代码示例

from PIL import Image
from pathlib import Path
import numpy as np
import matplotlib.pyplot as plt
import torchvision.transforms as T
import mindspore.dataset.vision as vision

orig_img = Image.open(Path('.') / 'test.jpg')

def show_diff_image(image_original, image_transformed):

    num = 2

    plt.subplot(1, num, 1)
    plt.imshow(image_original)
    plt.title("Original image")

    plt.subplot(1, num, 2)
    plt.imshow(image_transformed)
    plt.title("Random Solaried image")

    plt.show()


# In MindSpore, randomly selects a subrange within the specified threshold range and sets the pixel value within the subrange to (255 - pixel).

solarizer  = vision.RandomSolarize(threshold=(10,100))
rand_sola_img = solarizer(orig_img)
show_diff_image(orig_img, rand_sola_img)

# Out:
# Original image and Solarized image are showed with matplotlib tools


# In torch, the RandomSolarize transform randomly solarizes the image by inverting all pixel values above the threshold.

solarizer = T.RandomSolarize(threshold=192.0)
solarized_imgs = solarizer(orig_img)
show_diff_image(orig_img, solarized_imgs)

# Out:
# Original image and Solarized image are showed with matplotlib tools