请我喝杯咖啡☕
*备忘录:
Resize() 可以调整零个或多个图像的大小,如下所示:
*备忘录:
from torchvision.datasets import OxfordIIITPet from torchvision.transforms.v2 import Resize from torchvision.transforms.functional import InterpolationMode resize = Resize(size=100) resize = Resize(size=100, interpolation=InterpolationMode.BILINEAR, max_size=None, antialias=True) resize # Resize(size=[100], # interpolation=InterpolationMode.BILINEAR, # antialias=True) resize.size # [100] resize.interpolation # <InterpolationMode.BILINEAR: 'bilinear'> print(resize.max_size) # None resize.antialias # True origin_data = OxfordIIITPet( root="data", transform=None ) p1000_data = OxfordIIITPet( root="data", transform=Resize(size=1000) # transform=Resize(size=[1000]) ) p100_data = OxfordIIITPet( root="data", transform=Resize(size=100) ) p50_data = OxfordIIITPet( root="data", transform=Resize(size=50) ) p10_data = OxfordIIITPet( root="data", transform=Resize(size=10) ) p100p180_data = OxfordIIITPet( root="data", transform=Resize(size=[100, 180]) ) p180p100_data = OxfordIIITPet( root="data", transform=Resize(size=[180, 100]) ) p100ms110_data = OxfordIIITPet( root="data", transform=Resize(size=100, max_size=110) ) 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.tight_layout() plt.show() show_images1(data=origin_data, main_title="origin_data") show_images1(data=p1000_data, main_title="p1000_data") show_images1(data=p100_data, main_title="p100_data") show_images1(data=p50_data, main_title="p50_data") show_images1(data=p10_data, main_title="p10_data") print() show_images1(data=origin_data, main_title="origin_data") show_images1(data=p100p180_data, main_title="p100p180_data") show_images1(data=p180p100_data, main_title="p180p100_data") print() show_images1(data=p100_data, main_title="p100_data") show_images1(data=p100ms110_data, main_title='p100ms110_data') # ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓ def show_images2(data, main_title=None, s=None, ms=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) if not s: s = im.size resize = Resize(size=s, max_size=ms) # Here plt.imshow(X=resize(im)) # Here plt.tight_layout() plt.show() show_images2(data=origin_data, main_title="origin_data") show_images2(data=origin_data, main_title="p1000_data", s=1000) show_images2(data=origin_data, main_title="p100_data", s=100) show_images2(data=origin_data, main_title="p50_data", s=50) show_images2(data=origin_data, main_title="p10_data", s=10) print() show_images2(data=origin_data, main_title="origin_data") show_images2(data=origin_data, main_title="p100p180_data", s=[100, 180]) show_images2(data=origin_data, main_title="p180p100_data", s=[180, 100]) print() show_images2(data=origin_data, main_title="p100_data", s=100) show_images2(data=origin_data, main_title="p100ms110_data", s=100, ms=110)
以上是在 PyTorch 中调整大小的详细内容。更多信息请关注PHP中文网其他相关文章!