给我买咖啡☕
colorjitter()可以随机更改图像的亮度,对比度,饱和度和色调,如下所示:
*备忘录:
- 初始化的第一个参数是亮度(可选默认:0型:int,float或tuple/tuple/list(int或float)): *备忘录:
- >是亮度[min,max]的范围,因此必须是min 必须为0 元组/列表必须是具有2个元素的1d。
- 初始化的第二个参数是对比度(可选默认:0型:int,float或tuple/tuple/list(int或float)): *备忘录:
- 单个值表示[max(0,1-contrast),1 对比]。
- >
- 初始化的第三个参数是饱和(可选默认:0型:int,float或tuple/tuple/list(int或float)): *备忘录:
- >是饱和度[min,max]的范围,因此必须是min 必须为0 元组/列表必须是具有2个元素的1d。
单个值表示[max(0,1-饱和),1 饱和]。
- 初始化的第四个参数是色调(可选默认:0型:float或tuple/list(float)): *备忘录:
- >这是色调的范围[min,max],因此必须是min >必须为-0.5 元组或列表必须是具有2个元素的1d。
- >
单个值表示[-hue, hue]。
第一个参数是img(必需类型:pil图像或张量(int)): *备忘录:
- 不使用img =。
- 建议根据v1或v2使用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 = transform=ColorJitter(brightness=[1, 1]), contrast=[1, 1], saturation=[1, 1], hue=[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, 1]), # contrast=[1, 1], # saturation=[1, 1], # hue=[0, 0]) ) brightp2_data = OxfordIIITPet( # `bright` is brightness and `p` is plus. root="data", transform=ColorJitter(brightness=2) # transform=ColorJitter(brightness=[0, 3]) ) brightp2p2_data = OxfordIIITPet( root="data", transform=ColorJitter(brightness=[2, 2]) ) brightp05p05_data = OxfordIIITPet( root="data", transform=ColorJitter(brightness=[0.5, 0.5]) ) contrap2_data = OxfordIIITPet( # `contra` is contrast. root="data", transform=ColorJitter(contrast=2) # transform=ColorJitter(contrast=[0, 3]) ) contrap2p2_data = OxfordIIITPet( root="data", transform=ColorJitter(contrast=[2, 2]) ) contrap05p05_data = OxfordIIITPet( root="data", transform=ColorJitter(contrast=[0.5, 0.5]) ) saturap2_data = OxfordIIITPet( # `satura` is saturation. root="data", transform=ColorJitter(saturation=2) # transform=ColorJitter(saturation=[0, 3]) ) saturap2p2_data = OxfordIIITPet( root="data", transform=ColorJitter(saturation=[2, 2]) ) saturap05p05_data = OxfordIIITPet( root="data", transform=ColorJitter(saturation=[0.5, 0.5]) ) huep05_data = OxfordIIITPet( root="data", transform=ColorJitter(hue=0.5) # transform=ColorJitter(hue=[-0.5, 0.5]) ) huep025p025_data = OxfordIIITPet( # `m` is minus. root="data", transform=ColorJitter(hue=[0.25, 0.25]) ) huem025m025_data = OxfordIIITPet( # `m` is minus. root="data", transform=ColorJitter(hue=[-0.25, -0.25]) ) import matplotlib.pyplot as plt def show_images1(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_images1(data=origin_data, main_title="origin_data") show_images1(data=brightp2_data, main_title="brightp2_data") show_images1(data=brightp2p2_data, main_title="brightp2p2_data") show_images1(data=brightp05p05_data, main_title="brightp05p05_data") print() show_images1(data=origin_data, main_title="origin_data") show_images1(data=contrap2_data, main_title="contrap2_data") show_images1(data=contrap2p2_data, main_title="contrap2p2_data") show_images1(data=contrap05p05_data, main_title="contrap05p05_data") print() show_images1(data=origin_data, main_title="origin_data") show_images1(data=saturap2_data, main_title="saturap2_data") show_images1(data=saturap2p2_data, main_title="saturap2p2_data") show_images1(data=saturap05p05_data, main_title="saturap05p05_data") print() show_images1(data=origin_data, main_title="origin_data") show_images1(data=huep05_data, main_title="huep05_data") show_images1(data=huep025p025_data, main_title="huep025p025_data") show_images1(data=huem025m025_data, main_title="huem025m025_data") # ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓ def show_images2(data, main_title=None, b=0, c=0, s=0, h=0): 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) cj = ColorJitter(brightness=b, contrast=c, # Here saturation=s, hue=h) plt.imshow(X=cj(im)) # Here plt.xticks(ticks=[]) plt.yticks(ticks=[]) plt.tight_layout() plt.show() show_images2(data=my_data, main_title="origin_data") show_images2(data=my_data, main_title="brightp2_data", b=2) show_images2(data=my_data, main_title="brightp2p2_data", b=[2, 2]) show_images2(data=my_data, main_title="brightp05p05_data", b=[0.5, 0.5]) print() show_images2(data=my_data, main_title="origin_data") show_images2(data=my_data, main_title="contrap2_data", c=2) show_images2(data=my_data, main_title="contrap2p2_data", c=[2, 2]) show_images2(data=my_data, main_title="contrap05p05_data", c=[0.5, 0.5]) print() show_images2(data=my_data, main_title="origin_data") show_images2(data=my_data, main_title="saturap2_data", s=2) show_images2(data=my_data, main_title="saturap2p2_data", s=[2, 2]) show_images2(data=my_data, main_title="saturap05p05_data", s=[0.5, 0.5]) print() show_images2(data=my_data, main_title="origin_data") show_images2(data=my_data, main_title="huep05_data", h=0.5) show_images2(data=my_data, main_title="huep025p025_data", h=[0.25, 0.25]) show_images2(data=my_data, main_title="huem025m025_data", h=[-0.25, -0.25])
登录后复制
-
单个值表示[max(0,1亮度),1 亮度]。
这是对比度[min,max]的范围,因此必须是min 必须为0 元组/列表必须是具有2个元素的1d。
张量必须为2d或3d。
以上就是pytorch的colorjitter的详细内容,更多请关注php中文网其它相关文章!