*Memos:
-
My post explains RandomCrop() except
pad_if_needed
argument. - My post explains OxfordIIITPet().
RandomCrop() can crop an image randomly as shown below:
from torchvision.datasets import OxfordIIITPet
from torchvision.transforms.v2 import RandomCrop
origin_data = OxfordIIITPet(
root="data",
transform=None
)
s500_394origin_data = OxfordIIITPet( # `s` is size.
root="data",
transform=RandomCrop(size=[500, 394])
)
s600_494pinTrue_data = OxfordIIITPet( # `pin` is pad_if_needed.
root="data",
transform=RandomCrop(size=[600, 494], pad_if_needed=True)
)
s700_594pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], pad_if_needed=True)
)
s800_694pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[800, 694], pad_if_needed=True)
)
s400_494pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[400, 494], pad_if_needed=True)
)
s600_294pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[600, 294], pad_if_needed=True)
)
s600_494pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[600, 494], pad_if_needed=False)
)
s700_594pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], pad_if_needed=False)
)
s800_694pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[800, 694], pad_if_needed=False)
)
s400_494pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[400, 494], pad_if_needed=False)
)
s600_294pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[600, 294], pad_if_needed=False)
)
s700_594p100origin_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], pad_if_needed=100)
)
s800_694p100pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[800, 694], padding=100, pad_if_needed=True)
)
s900_794p100pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[900, 794], padding=100, pad_if_needed=True)
)
s1000_894p100pinTrue_data = OxfordIIITPet( # `p` is padding.
root="data",
transform=RandomCrop(size=[1000, 894], padding=100, pad_if_needed=True)
)
s600_694p100pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[600, 694], padding=100, pad_if_needed=True)
)
s800_494p100pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[800, 494], padding=100, pad_if_needed=True)
)
s800_694p100pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[800, 694], padding=100, pad_if_needed=False)
)
s900_794p100pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[900, 794], padding=100, pad_if_needed=False)
)
s1000_894p100pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[1000, 894], padding=100, pad_if_needed=False)
)
s600_694p100pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[600, 694], padding=100, pad_if_needed=False)
)
s800_494p100pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[800, 494], padding=100, pad_if_needed=False)
)
s400_294pn50origin_data = OxfordIIITPet( # `n` is negative.
root="data",
transform=RandomCrop(size=[400, 294], padding=-50)
)
s500_394pn50pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[500, 394], padding=-50, pad_if_needed=True)
)
s600_494pn50pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[600, 494], padding=-50, pad_if_needed=True)
)
s700_594pn50pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], padding=-50, pad_if_needed=True)
)
s350_444pn50pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[350, 444], padding=-50, pad_if_needed=True)
)
s450_244pn50pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[450, 244], padding=-50, pad_if_needed=True)
)
s500_394pn50pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[500, 394], padding=-50, pad_if_needed=False)
)
s600_494pn50pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[600, 494], padding=-50, pad_if_needed=False)
)
s700_594pn50pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], padding=-50, pad_if_needed=False)
)
s350_444pn50pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[350, 444], padding=-50, pad_if_needed=False)
)
s450_244pn50pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[450, 244], padding=-50, pad_if_needed=False)
)
s700_594p100origin_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], padding=100)
)
s700_594p50pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], padding=50, pad_if_needed=True)
)
s700_594p0pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], padding=0, pad_if_needed=True)
)
s700_594pn50pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], padding=-50, pad_if_needed=True)
)
s700_594pn100pinTrue_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], padding=-100, pad_if_needed=True)
)
s700_594p50pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], padding=50, pad_if_needed=False)
)
s700_594p0pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], padding=0, pad_if_needed=False)
)
s700_594pn50pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], padding=-50, pad_if_needed=False)
)
s700_594pn100pinFalse_data = OxfordIIITPet(
root="data",
transform=RandomCrop(size=[700, 594], padding=-100, pad_if_needed=False)
)
import matplotlib.pyplot as plt
def show_images1(data, main_title=None):
plt.figure(figsize=(10, 5.5))
plt.suptitle(t=main_title, y=0.8, fontsize=14)
for i in range(1, 6):
plt.subplot(1, 5, i)
plt.imshow(X=data[0][0])
plt.tight_layout()
plt.show()
show_images1(data=s500_394origin_data, main_title="s500_394origin_data")
show_images1(data=s600_494pinTrue_data, main_title="s600_494pinTrue_data")
show_images1(data=s700_594pinTrue_data, main_title="s700_594pinTrue_data")
show_images1(data=s800_694pinTrue_data, main_title="s800_694pinTrue_data")
show_images1(data=s400_494pinTrue_data, main_title="s400_494pinTrue_data")
show_images1(data=s600_294pinTrue_data, main_title="s600_294pinTrue_data")
# show_images1(data=s600_494pinFalse_data,
# main_title="s600_494pinFalse_data") # Error
# show_images1(data=s700_594pinFalse_data,
# main_title="s700_594pinFalse_data") # Error
# show_images1(data=s800_694pinFalse_data,
# main_title="s800_694pinFalse_data") # Error
# show_images1(data=s400_494pinFalse_data,
# main_title="s400_494pinFalse_data") # Error
# show_images1(data=s600_294pinFalse_data,
# main_title="s600_294pinFalse_data") # Error
print()
show_images1(data=s700_594p100origin_data,
main_title="s700_594p100origin_data")
show_images1(data=s800_694p100pinTrue_data,
main_title="s800_694p100pinTrue_data")
show_images1(data=s900_794p100pinTrue_data,
main_title="s900_794p100pinTrue_data")
show_images1(data=s1000_894p100pinTrue_data,
main_title="s1000_894p100pinTrue_data")
show_images1(data=s600_694p100pinTrue_data,
main_title="s600_694p100pinTrue_data")
show_images1(data=s800_494p100pinTrue_data,
main_title="s800_494p100pinTrue_data")
# show_images1(data=s800_694p100pinFalse_data,
# main_title="s800_694p100pinFalse_data") # Error
# show_images1(data=s900_794p100pinFalse_data,
# main_title="s900_794p100pinFalse_data") # Error
# show_images1(data=s1000_894p100pinFalse_data,
# main_title="s1000_894p100pinFalse_data") # Error
# show_images1(data=s600_694p100pinFalse_data,
# main_title="s600_694p100pinFalse_data") # Error
# show_images1(data=s800_494p100pinFalse_data,
# main_title="s800_494p100pinFalse_data") # Error
print()
show_images1(data=s400_294pn50origin_data,
main_title="s400_294pn50origin_data")
show_images1(data=s500_394pn50pinTrue_data,
main_title="s500_394pn50pinTrue_data")
show_images1(data=s600_494pn50pinTrue_data,
main_title="s600_494pn50pinTrue_data")
show_images1(data=s700_594pn50pinTrue_data,
main_title="s700_594pn50pinTrue_data")
show_images1(data=s350_444pn50pinTrue_data,
main_title="s350_444pn50pinTrue_data")
show_images1(data=s450_244pn50pinTrue_data,
main_title="s450_244pn50pinTrue_data")
# show_images1(data=s500_394pn50pinFalse_data,
# main_title="s500_394pn50pinFalse_data") # Error
# show_images1(data=s600_494pn50pinFalse_data,
# main_title="s600_494pn50pinFalse_data") # Error
# show_images1(data=s700_594pn50pinFalse_data,
# main_title="s700_594pn50pinFalse_data") # Error
# show_images1(data=s350_444pn50pinFalse_data,
# main_title="s350_444pn50pinFalse_data") # Error
# show_images1(data=s450_244pn50pinFalse_data,
# main_title="s450_244pn50pinFalse_data") # Error
print()
show_images1(data=s700_594p100origin_data,
main_title="s700_594p100origin_data")
show_images1(data=s700_594p50pinTrue_data,
main_title="s700_594p50pinTrue_data")
show_images1(data=s700_594p0pinTrue_data,
main_title="s700_594p0pinTrue_data")
show_images1(data=s700_594pn50pinTrue_data,
main_title="s700_594pn50pinTrue_data")
show_images1(data=s700_594pn100pinTrue_data,
main_title="s700_594pn100pinTrue_data")
# show_images1(data=s700_594p50pinFalse_data,
# main_title="s700_594p50pinFalse_data") # Error
# show_images1(data=s700_594p0pinFalse_data,
# main_title="s700_594p0pinFalse_data") # Error
# show_images1(data=s700_594pn50pinFalse_data,
# main_title="s700_594pn50pinFalse_data") # Error
# show_images1(data=s700_594pn100pinFalse_data,
# main_title="s700_594pn100pinFalse_data") # Error
# ↓ ↓ ↓ ↓ ↓ ↓ The code below is identical to the code above. ↓ ↓ ↓ ↓ ↓ ↓
def show_images2(data, main_title=None, s=None, p=None,
pin=False, f=0, pm='constant'):
plt.figure(figsize=(10, 5.5))
plt.suptitle(t=main_title, y=0.8, fontsize=14)
temp_s = s
im = data[0][0]
for i in range(1, 6):
plt.subplot(1, 5, i)
if not temp_s:
s = [im.size[1], im.size[0]]
rc = RandomCrop(size=s, padding=p, # Here
pad_if_needed=pin, fill=f, padding_mode=pm)
plt.imshow(X=rc(im)) # Here
plt.tight_layout()
plt.show()
show_images2(data=origin_data, main_title="s500_394origin_data",
s=[500, 394])
show_images2(data=origin_data, main_title="s600_494pinTrue_data",
s=[600, 494], pin=True)
show_images2(data=origin_data, main_title="s700_594pinTrue_data",
s=[700, 594], pin=True)
show_images2(data=origin_data, main_title="s800_694pinTrue_data",
s=[800, 694], pin=True)
show_images2(data=origin_data, main_title="s400_494pinTrue_data",
s=[400, 494], pin=True)
show_images2(data=origin_data, main_title="s600_294pinTrue_data",
s=[600, 294], pin=True)
# show_images2(data=origin_data, main_title="s600_494pinFalse_data",
# s=[600, 494], pin=False) # Error
# show_images2(data=origin_data, main_title="s700_594pinFalse_data",
# s=[700, 594], pin=False) # Error
# show_images2(data=origin_data, main_title="s800_694pinFalse_data",
# s=[800, 694], pin=False) # Error
# show_images2(data=origin_data, main_title="s400_494pinFalse_data",
# s=[400, 494], pin=False) # Error
# show_images2(data=origin_data, main_title="s600_294pinFalse_data",
# s=[600, 294], pin=False) # Error
print()
show_images2(data=origin_data, main_title="s700_594p100origin_data",
s=[700, 594], p=100)
show_images2(data=origin_data, main_title="s800_694p100pinTrue_data",
s=[800, 694], p=100, pin=True)
show_images2(data=origin_data, main_title="s900_794p100pinTrue_data",
s=[900, 794], p=100, pin=True)
show_images2(data=origin_data, main_title="s1000_894p100pinTrue_data",
s=[1000, 894], p=100, pin=True)
show_images2(data=origin_data, main_title="s600_694p100pinTrue_data",
s=[600, 694], p=100, pin=True)
show_images2(data=origin_data, main_title="s800_494p100pinTrue_data",
s=[800, 494], p=100, pin=True)
# show_images2(data=origin_data, main_title="s800_694p100pinFalse_data",
# s=[800, 694], p=100, pin=False) # Error
# show_images2(data=origin_data, main_title="s900_794p100pinFalse_data",
# s=[900, 794], p=100, pin=False) # Error
# show_images2(data=origin_data, main_title="s1000_894p100pinFalse_data",
# s=[1000, 894], p=100, pin=False) # Error
# show_images2(data=origin_data, main_title="s600_694p100pinFalse_data",
# s=[600, 694], p=100, pin=False) # Error
# show_images2(data=origin_data, main_title="s800_494p100pinFalse_data",
# s=[800, 494], p=100, pin=False) # Error
print()
show_images2(data=origin_data, main_title="s400_294pn50origin_data",
s=[400, 294], p=-50)
show_images2(data=origin_data, main_title="s500_394pn50pinTrue_data",
s=[500, 394], p=-50, pin=True)
show_images2(data=origin_data, main_title="s600_494pn50pinTrue_data",
s=[600, 494], p=-50, pin=True)
show_images2(data=origin_data, main_title="s700_594pn50pinTrue_data",
s=[700, 594], p=-50, pin=True)
show_images2(data=origin_data, main_title="s350_444pn50pinTrue_data",
s=[350, 444], p=-50, pin=True)
show_images2(data=origin_data, main_title="s450_244pn50pinTrue_data",
s=[450, 244], p=-50, pin=True)
# show_images2(data=origin_data, main_title="s500_394pn50pinFalse_data",
# s=[500, 394], p=-50, pin=False) # Error
# show_images2(data=origin_data, main_title="s600_494pn50pinFalse_data",
# s=[600, 494], p=-50, pin=False) # Error
# show_images2(data=origin_data, main_title="s700_594pn50pinFalse_data",
# s=[700, 594], p=-50, pin=False) # Error
# show_images2(data=origin_data, main_title="s350_444pn50pinFalse_data",
# s=[350, 444], p=-50, pin=False) # Error
# show_images2(data=origin_data, main_title="s450_244pn50pinFalse_data",
# s=[450, 244], p=-50, pin=False) # Error
print()
show_images2(data=origin_data, main_title="s700_594p100origin_data",
s=[700, 594], p=100)
show_images2(data=origin_data, main_title="s700_594p50pinTrue_data",
s=[700, 594], p=50, pin=True)
show_images2(data=origin_data, main_title="s700_594p0pinTrue_data",
s=[700, 594], p=0, pin=True)
show_images2(data=origin_data, main_title="s700_594pn50pinTrue_data",
s=[700, 594], p=-50, pin=True)
show_images2(data=origin_data, main_title="s700_594pn100pinTrue_data",
s=[700, 594], p=-100, pin=True)
# show_images2(data=origin_data, main_title="s700_594p50pinFalse_data",
# s=[700, 594], p=50, pin=False) # Error
# show_images2(data=origin_data, main_title="s700_594p0pinFalse_data",
# s=[700, 594], p=0, pin=False) # Error
# show_images2(data=origin_data, main_title="s700_594pn50pinFalse_data",
# s=[700, 594], p=-50, pin=False) # Error
# show_images2(data=origin_data, main_title="s700_594pn100pinFalse_data",
# s=[700, 594], p=-100, pin=False) # Error
Top comments (0)