Buy Me a Coffee☕
*Memos:
CenterCrop() can crop zero or more images, centering on them as shown below:
*Memos:
from torchvision.datasets import OxfordIIITPet from torchvision.transforms.v2 import CenterCrop centercrop = CenterCrop(size=100) centercrop # CenterCrop(size=(100, 100)) centercrop.size # (100, 100) origin_data = OxfordIIITPet( root="data", transform=None ) p600_data = OxfordIIITPet( root="data", transform=CenterCrop(size=600) # transform=CenterCrop(size=[600]) # transform=CenterCrop(size=[600, 600]) ) p400_data = OxfordIIITPet( root="data", transform=CenterCrop(size=400) ) p200_data = OxfordIIITPet( root="data", transform=CenterCrop(size=200) ) p100_data = OxfordIIITPet( root="data", transform=CenterCrop(size=100) ) p50_data = OxfordIIITPet( root="data", transform=CenterCrop(size=50) ) p10_data = OxfordIIITPet( root="data", transform=CenterCrop(size=10) ) p200p300_data = OxfordIIITPet( root="data", transform=CenterCrop(size=[200, 300]) ) p300p200_data = OxfordIIITPet( root="data", transform=CenterCrop(size=[300, 200]) ) 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=p600_data, main_title="p600_data") show_images1(data=p400_data, main_title="p400_data") show_images1(data=p200_data, main_title="p200_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=p200p300_data, main_title="p200p300_data") show_images1(data=p300p200_data, main_title="p300p200_data") # ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓ def show_images2(data, main_title=None, s=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[1], im.size[0]] cc = CenterCrop(size=s) # Here plt.imshow(X=cc(im)) # Here plt.tight_layout() plt.show() show_images2(data=origin_data, main_title="origin_data") show_images2(data=origin_data, main_title="p600_data", s=600) show_images2(data=origin_data, main_title="p400_data", s=400) show_images2(data=origin_data, main_title="p200_data", s=200) 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="p200p300_data", s=[200, 300]) show_images2(data=origin_data, main_title="p300p200_data", s=[300, 200])
The above is the detailed content of CenterCrop in PyTorch. For more information, please follow other related articles on the PHP Chinese website!