Beli Saya Kopi☕
*Memo:
Pad() boleh menambah padding kepada sifar atau lebih imej seperti yang ditunjukkan di bawah:
*Memo:
from torchvision.datasets import OxfordIIITPet from torchvision.transforms.v2 import Pad pad = Pad(padding=100) pad = Pad(padding=100, fill=0, padding_mode='constant') pad # Pad(padding=100, fill=0, padding_mode=constant) pad.padding # 100 pad.fill # 0 pad.padding_mode # 'constant' origin_data = OxfordIIITPet( root="data", transform=None # transform=Pad(padding=0) ) p50_data = OxfordIIITPet( root="data", transform=Pad(padding=50) ) p100_data = OxfordIIITPet( root="data", transform=Pad(padding=100) ) p150_data = OxfordIIITPet( root="data", transform=Pad(padding=150) ) m50_data = OxfordIIITPet( root="data", transform=Pad(padding=-50) ) m100_data = OxfordIIITPet( root="data", transform=Pad(padding=-100) ) m150_data = OxfordIIITPet( root="data", transform=Pad(padding=-150) ) p100p50_data = OxfordIIITPet( root="data", transform=Pad(padding=[100, 50]) ) m100m50_data = OxfordIIITPet( root="data", transform=Pad(padding=[-100, -50]) ) p100m50_data = OxfordIIITPet( root="data", transform=Pad(padding=[100, -50]) ) p25p50p75p100_data = OxfordIIITPet( root="data", transform=Pad(padding=[25, 50, 75, 100]) ) m25m50m75m100_data = OxfordIIITPet( root="data", transform=Pad(padding=[-25, -50, -75, -100]) ) p25m50p75m100_data = OxfordIIITPet( root="data", transform=Pad(padding=[25, -50, 75, -100]) ) p100fillgray_data = OxfordIIITPet( root="data", transform=Pad(padding=100, fill=150) ) p100fillpurple_data = OxfordIIITPet( root="data", transform=Pad(padding=100, fill=[160, 32, 240]) ) p100edge_data = OxfordIIITPet( root="data", transform=Pad(padding=100, padding_mode="edge") ) p100reflect_data = OxfordIIITPet( root="data", transform=Pad(padding=100, padding_mode="reflect") ) p100symmetric_data = OxfordIIITPet( root="data", transform=Pad(padding=100, padding_mode="symmetric") ) 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=p50_data, main_title='p50_data') show_images1(data=p100_data, main_title='p100_data') show_images1(data=p150_data, main_title='p150_data') print() show_images1(data=origin_data, main_title='origin_data') show_images1(data=m50_data, main_title='m50_data') show_images1(data=m100_data, main_title='m100_data') show_images1(data=m150_data, main_title='m150_data') print() show_images1(data=origin_data, main_title='origin_data') show_images1(data=p100p50_data, main_title='p100p50_data') show_images1(data=m100m50_data, main_title='m100m50_data') show_images1(data=p100m50_data, main_title='p100m50_data') print() show_images1(data=origin_data, main_title='origin_data') show_images1(data=p25p50p75p100_data, main_title='p25p50p75p100_data') show_images1(data=m25m50m75m100_data, main_title='m25m50m75m100_data') show_images1(data=p25m50p75m100_data, main_title='p25m50p75m100_data') print() show_images1(data=p100fillgray_data, main_title='p100fillgray_data') show_images1(data=p100fillpurple_data, main_title='p100fillpurple_data') print() show_images1(data=p100edge_data, main_title='p100edge_data') show_images1(data=p100reflect_data, main_title='p100reflect_data') show_images1(data=p100symmetric_data, main_title='p100symmetric_data') # ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓ def show_images2(data, main_title=None, p=0, f=0, pm='constant'): 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) pad = Pad(padding=p, fill=f, padding_mode=pm) # Here plt.imshow(X=pad(im)) # Here plt.xticks(ticks=[]) plt.yticks(ticks=[]) plt.tight_layout() plt.show() show_images2(data=origin_data, main_title='origin_data') show_images2(data=origin_data, main_title='p50_data', p=50) show_images2(data=origin_data, main_title='p100_data', p=100) show_images2(data=origin_data, main_title='p150_data', p=150) print() show_images2(data=origin_data, main_title='origin_data') show_images2(data=origin_data, main_title='m50_data', p=-50) show_images2(data=origin_data, main_title='m100_data', p=-100) show_images2(data=origin_data, main_title='m150_data', p=-150) print() show_images2(data=origin_data, main_title='origin_data') show_images2(data=origin_data, main_title='p100p50_data', p=[100, 50]) show_images2(data=origin_data, main_title='m100m50_data', p=[-100, -50]) show_images2(data=origin_data, main_title='p100m50_data', p=[100, -50]) print() show_images2(data=origin_data, main_title='origin_data') show_images2(data=origin_data, main_title='p25p50p75p100_data', p=[25, 50, 75, 100]) show_images2(data=origin_data, main_title='m25m50m75m100_data', p=[-25, -50, -75, -100]) show_images2(data=origin_data, main_title='p25m50p75m100_data', p=[25, -50, 75, -100]) print() show_images2(data=origin_data, main_title='p100fillgray_data', p=100, f=[150]) show_images2(data=origin_data, main_title='p100fillpurple_data', p=100, f=[160, 32, 240]) print() show_images2(data=origin_data, main_title='p100edge_data', p=100, pm='edge') show_images2(data=origin_data, main_title='p100reflect_data', p=100, pm='reflect') show_images2(data=origin_data, main_title='p100symmetric_data', p=100, pm='symmetric')
Atas ialah kandungan terperinci Pad dalam PyTorch. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!