class RandomScaleCrop(object):
def __init__(self, base_size, crop_size, fill=0):
self.base_size = base_size
self.crop_size = crop_size
self.fill = fill
def __call__(self, sample):
img = sample['image']
mask = sample['label']
# random scale (short edge)
short_size = random.randint(int(self.base_size * 0.5), int(self.base_size * 2.0))
w, h = img.size
if h > w:
ow = short_size
oh = int(1.0 * h * ow / w)
else:
oh = short_size
ow = int(1.0 * w * oh / h)
img = img.resize((ow, oh), Image.BILINEAR)
mask = mask.resize((ow, oh), Image.NEAREST)
# pad crop
if short_size < self.crop_size:
padh = self.crop_size - oh if oh < self.crop_size else 0
padw = self.crop_size - ow if ow < self.crop_size
Pytorch--RandomScaleCrop操作
最新推荐文章于 2024-05-09 14:22:54 发布
本文详细介绍了在Pytorch中用于语义分割任务的数据增强技术RandomScaleCrop,包括其工作原理、实现步骤和效果,旨在帮助读者更好地理解和应用这一方法来提升模型的泛化能力。
摘要由CSDN通过智能技术生成