在训练好的resnet模型进行红绿灯分类测试时发现效果好差,有40%的图片没有分类正确.这就离谱了,验证集明明95%以上的.分析了好多原因最后发现是测试样本和训练验证集合之间的差距.
训练和验证集合样本尺寸都是正方形,如下所示
BUT,测试的数据集是这样子的
明眼人应该都已经看到差距啦.
下面就是在加载测试集时候加了个图像预处理步骤,在transform.compose里面
单独执行的代码如下cv2版本的.(一开始搞错了我的数据集是PIL打开图像的)
import cv2
import os
import numpy as np
def get_resize_square(input,output):#,output
for filename in os.listdir(input):
image_path=input+'/'+filename
image=cv2.imread(image_path)
x=image.shape[1]##宽度
y=image.shape[0]##高度
target = max(x,y)
BLACK=[0, 0, 0]
a = (target - x) / 2
b = (target - y) / 2