首頁 >後端開發 >Python教學 >PyTorch 中的 ColorJitter

PyTorch 中的 ColorJitter

Patricia Arquette
Patricia Arquette原創
2024-12-30 09:20:10762瀏覽

請我喝杯咖啡☕

ColorJitter() 可以改變零個或多個影像的亮度、對比、飽和度和色調,如下所示:

*備忘錄:

  • 初始化的第一個參數是亮度(可選-預設:0-類型:float 或 tuple/list(float)): *備註:
    • 亮度範圍[min, max]。
    • 必須是 0
    • 單一值轉換為[max(0, 1-亮度), 1 亮度]。
    • 元組或列表必須是具有 2 個元素的一維。 *第一個元素必須小於或等於第二個元素。
  • 初始化的第二個參數是對比(可選-預設:0-類型:float 或 tuple/list(float)): *備註:
    • 這是對比的範圍[min, max]。
    • 必須是 0
    • 單一值轉換為 [max(0, 1-對比), 1 對比]。
    • 元組或列表必須是具有 2 個元素的一維。 *第一個元素必須小於或等於第二個元素。
  • 初始化的第三個參數是飽和度(可選-預設:0-類型:float 或 tuple/list(float)): *備註:
    • 這是飽和度的範圍[min, max]。
    • 必須是 0
    • 單一值轉換為 [max(0, 1-飽和度), 1 飽和度]。
    • 元組或列表必須是具有 2 個元素的一維。 *第一個元素必須小於或等於第二個元素。
  • 初始化的第四個參數是hue(可選-預設:0-類型:float或tuple/list(float)): *備註:
    • 這是色調的範圍 [min, max]。
    • 必須是 -0.5
    • 單一值轉換為 [-hue, Hue]。
    • 元組或列表必須是具有 2 個元素的一維。 *第一個元素必須小於或等於第二個元素。
  • 第一個參數是img(必需類型:PIL映像或張量/元組/列表(int或float)): *備註:
    • 它必須是 2D 或 3D。對於 3D,最深的 D 必須有一個元素。
    • 不要使用img=。
  • v2建議依照V1還是V2使用?我應該使用哪一個?
from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import ColorJitter

colorjitter = ColorJitter()
colorjitter = ColorJitter(brightness=0,
                          contrast=0,
                          saturation=0,
                          hue=0)
colorjitter = ColorJitter(brightness=(1.0, 2.0),
                          contrast=(1.0, 1.0),
                          saturation=(1.0, 1.0),
                          hue=(0.0, 0.0))
colorjitter
# ColorJitter()

print(colorjitter.brightness)
# None

print(colorjitter.contrast)
# None

print(colorjitter.saturation)
# None

print(colorjitter.hue)
# None

origin_data = OxfordIIITPet(
    root="data",
    transform=None
    # transform=ColorJitter()
    # colorjitter = ColorJitter(brightness=0,
    #                           contrast=0,
    #                           saturation=0,
    #                           hue=0)
    # transform=ColorJitter(brightness=(1.0, 1.0),
    #                       contrast=(1.0, 1.0),
    #                       saturation=(1.0, 1.0),
    #                       hue=(0.0, 0.0))
)

p2bright_data = OxfordIIITPet( # `p` is plus.
    root="data",
    transform=ColorJitter(brightness=2.0)
    # transform=ColorJitter(brightness=(0.0, 3.0))
)

p2p2bright_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(brightness=(2.0, 2.0))
)

p05p05bright_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(brightness=(0.5, 0.5))
)

p2contra_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(contrast=2.0)
    # transform=ColorJitter(contrast=(0.0, 3.0))
)

p2p2contra_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(contrast=(2.0, 2.0))
)

p05p05contra_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(contrast=(0.5, 0.5))
)

p2satura_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(saturation=2.0)
    # transform=ColorJitter(saturation=(0.0, 3.0))
)

p2p2satura_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(saturation=(2.0, 2.0))
)

p05p05satura_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(saturation=(0.5, 0.5))
)

p05hue_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(hue=0.5)
    # transform=ColorJitter(hue=(-0.5, 0.5))
)

p025p025hue_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(hue=(0.25, 0.25))
)

m025m025hue_data = OxfordIIITPet( # `m` is minus.
    root="data",
    transform=ColorJitter(hue=(-0.25, -0.25))
)

import matplotlib.pyplot as plt

def show_images(data, main_title=None):
    plt.figure(figsize=(10, 5))
    plt.suptitle(t=main_title, y=0.8, fontsize=14)
    for i, (im, _) in zip(range(1, 6), data):
        plt.subplot(1, 5, i)
        plt.imshow(X=im)
        plt.xticks(ticks=[])
        plt.yticks(ticks=[])
    plt.tight_layout()
    plt.show()

show_images(data=origin_data, main_title="origin_data")
show_images(data=p2bright_data, main_title="p2bright_data")
show_images(data=p2p2bright_data, main_title="p2p2bright_data")
show_images(data=p05p05bright_data, main_title="p05p05bright_data")

show_images(data=origin_data, main_title="origin_data")
show_images(data=p2contra_data, main_title="p2contra_data")
show_images(data=p2p2contra_data, main_title="p2p2contra_data")
show_images(data=p05p05contra_data, main_title="p05p05contra_data")

show_images(data=origin_data, main_title="origin_data")
show_images(data=p2satura_data, main_title="p2satura_data")
show_images(data=p2p2satura_data, main_title="p2p2satura_data")
show_images(data=p05p05satura_data, main_title="p05p05satura_data")

show_images(data=origin_data, main_title="origin_data")
show_images(data=p05hue_data, main_title="p05hue_data")
show_images(data=p025p025hue_data, main_title="p025p025hue_data")
show_images(data=m025m025hue_data, main_title="m025m025hue_data")

ColorJitter in PyTorch

ColorJitter in PyTorch

ColorJitter in PyTorch

ColorJitter in PyTorch


ColorJitter in PyTorch

ColorJitter in PyTorch

ColorJitter in PyTorch

ColorJitter in PyTorch


ColorJitter in PyTorch

ColorJitter in PyTorch

ColorJitter in PyTorch

ColorJitter in PyTorch


ColorJitter in PyTorch

ColorJitter in PyTorch

ColorJitter in PyTorch

ColorJitter in PyTorch

from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import ColorJitter

colorjitter = ColorJitter()
colorjitter = ColorJitter(brightness=0,
                          contrast=0,
                          saturation=0,
                          hue=0)
colorjitter = ColorJitter(brightness=(1.0, 2.0),
                          contrast=(1.0, 1.0),
                          saturation=(1.0, 1.0),
                          hue=(0.0, 0.0))
colorjitter
# ColorJitter()

print(colorjitter.brightness)
# None

print(colorjitter.contrast)
# None

print(colorjitter.saturation)
# None

print(colorjitter.hue)
# None

origin_data = OxfordIIITPet(
    root="data",
    transform=None
    # transform=ColorJitter()
    # colorjitter = ColorJitter(brightness=0,
    #                           contrast=0,
    #                           saturation=0,
    #                           hue=0)
    # transform=ColorJitter(brightness=(1.0, 1.0),
    #                       contrast=(1.0, 1.0),
    #                       saturation=(1.0, 1.0),
    #                       hue=(0.0, 0.0))
)

p2bright_data = OxfordIIITPet( # `p` is plus.
    root="data",
    transform=ColorJitter(brightness=2.0)
    # transform=ColorJitter(brightness=(0.0, 3.0))
)

p2p2bright_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(brightness=(2.0, 2.0))
)

p05p05bright_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(brightness=(0.5, 0.5))
)

p2contra_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(contrast=2.0)
    # transform=ColorJitter(contrast=(0.0, 3.0))
)

p2p2contra_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(contrast=(2.0, 2.0))
)

p05p05contra_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(contrast=(0.5, 0.5))
)

p2satura_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(saturation=2.0)
    # transform=ColorJitter(saturation=(0.0, 3.0))
)

p2p2satura_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(saturation=(2.0, 2.0))
)

p05p05satura_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(saturation=(0.5, 0.5))
)

p05hue_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(hue=0.5)
    # transform=ColorJitter(hue=(-0.5, 0.5))
)

p025p025hue_data = OxfordIIITPet(
    root="data",
    transform=ColorJitter(hue=(0.25, 0.25))
)

m025m025hue_data = OxfordIIITPet( # `m` is minus.
    root="data",
    transform=ColorJitter(hue=(-0.25, -0.25))
)

import matplotlib.pyplot as plt

def show_images(data, main_title=None):
    plt.figure(figsize=(10, 5))
    plt.suptitle(t=main_title, y=0.8, fontsize=14)
    for i, (im, _) in zip(range(1, 6), data):
        plt.subplot(1, 5, i)
        plt.imshow(X=im)
        plt.xticks(ticks=[])
        plt.yticks(ticks=[])
    plt.tight_layout()
    plt.show()

show_images(data=origin_data, main_title="origin_data")
show_images(data=p2bright_data, main_title="p2bright_data")
show_images(data=p2p2bright_data, main_title="p2p2bright_data")
show_images(data=p05p05bright_data, main_title="p05p05bright_data")

show_images(data=origin_data, main_title="origin_data")
show_images(data=p2contra_data, main_title="p2contra_data")
show_images(data=p2p2contra_data, main_title="p2p2contra_data")
show_images(data=p05p05contra_data, main_title="p05p05contra_data")

show_images(data=origin_data, main_title="origin_data")
show_images(data=p2satura_data, main_title="p2satura_data")
show_images(data=p2p2satura_data, main_title="p2p2satura_data")
show_images(data=p05p05satura_data, main_title="p05p05satura_data")

show_images(data=origin_data, main_title="origin_data")
show_images(data=p05hue_data, main_title="p05hue_data")
show_images(data=p025p025hue_data, main_title="p025p025hue_data")
show_images(data=m025m025hue_data, main_title="m025m025hue_data")

ColorJitter in PyTorch

ColorJitter in PyTorch

ColorJitter in PyTorch

ColorJitter in PyTorch


ColorJitter in PyTorch

ColorJitter in PyTorch

ColorJitter in PyTorch

ColorJitter in PyTorch


ColorJitter in PyTorch

ColorJitter in PyTorch

ColorJitter in PyTorch

ColorJitter in PyTorch


ColorJitter in PyTorch

ColorJitter in PyTorch

ColorJitter in PyTorch

ColorJitter in PyTorch

以上是PyTorch 中的 ColorJitter的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn