我在 PyTorch 中微调了 resnet50 分类模型。训练期间,火炬。transforms 用于图像的预处理。当我在 OpenCV 中使用相同的预处理执行推理时,我没有得到相同的输出。请帮我解决这个问题。
Torch.transforms 预处理:
transform = transforms.Compose(
[
transforms.Resize((224, 224)),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]
)
OpenCV preprocessing:
def preprocess_image(image):
## swap the color channels from BGR to RGB, resize it, and scale
## the pixel values to [0, 1] range
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (224, 224), interpolation = cv2.INTER_CUBIC)
image = image.astype("float32") / 255.0
## subtract ImageNet mean, divide by ImageNet standard deviation,
## set "channels first" ordering, and add a batch dimension
image -= [0.485, 0.456, 0.406]
image /= [0.229, 0.224, 0.225]
image = np.transpose(image, (2, 0, 1))
image = np.expand_dims(image, 0)
### return the preprocessed image
return image