对于长和宽相差很大的图像的一种数据处理方式(pytorch)

当图像的长和宽相差很大的情况,不能使用resize强转和crop。
以下使用的方法是将长边resize到固定大小,短边padding(仅仅应用到testset ,infer 阶段)
INPUT_SIZE = (448,448)
train

img= cv2.cvtColor( cv2.imread(os.path.join(self.root, 'images', train_file)),cv2.COLOR_BGR2RGB)
img = transforms.ToPILImage()(img)
img = transforms.Resize(600, Image.BILINEAR)(img)
img = transforms.RandomCrop(INPUT_SIZE)(img)
img = transforms.RandomHorizontalFlip()(img)
img = transforms.ToTensor()(img)
img = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(img)

test

img= cv2.cvtColor( cv2.imread(os.path.join(self.root, 'images', train_file)),cv2.COLOR_BGR2RGB)
target_size = [INPUT_SIZE[0],INPUT_SIZE[1]]
old_size= img.shape[0:2]
#ratio = min(float(target_size)/(old_size))
ratio = min(float(target_size[i])/(old_size[i]) for i in range(len(old_size)))
new_size = tuple([int(i*ratio) for i in old_size])
img = cv2.resize(img,(new_size[1], new_size[0]))
pad_w = target_size[1] - new_size[1]
pad_h = target_size[0] - new_size[0]
top,bottom = pad_h//2, pad_h-(pad_h//2)
left,right = pad_w//2, pad_w -(pad_w//2)
img = cv2.copyMakeBorder(img,top,bottom,left,right,cv2.BORDER_CONSTANT,None,(0,0,0))

img = transforms.ToPILImage()(img)
img = transforms.ToTensor()(img)
img = transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])(img)

你可能感兴趣的:(笔记,python,pytorch,dataset)