通常,CNN模型具有输入尺寸,该尺寸是固定大小。可以说224x224。因此,在训练阶段和分类阶段,我们需要调整各种大小的图像的大小以匹配输入大小。我的问题是,要使模型更准确,是否需要在调整大小后仍保持图像的长宽比,还是在训练和分类阶段无需担心宽高比?
我无法裁剪像正方形的形状,因为该正方形也可能包含其他类别。谁能建议我准备数据集的最佳方法?
以下是我的调整大小机制;
import cv2
import numpy as np
def resize(image, width, height):
dim = None
(h, w) = image.shape[:2]
if width is None and height is None:
return image
x_off = 0
y_off = 0
if h > w:
r = height / float(h)
dim = (int(w * r), height)
else:
r = width / float(w)
dim = (width, int(h * r))
resized = cv2.resize(image, dim, interpolation = cv2.INTER_AREA)
(h_0, w_0) = resized.shape[:2]
if h_0 > w_0:
x_off = (224 - w_0)/2
else:
y_off = (224 - h_0)/2
blank_image = np.zeros((height,width,3), np.uint8)
blank_image[y_off:y_off+h_0,x_off:x_off+w_0] = resized
return blank_image