注意模型IO:
保证输入、输出精度、类型与复现目标一致。
模型推理的代码
from torchvision import transforms
def image_to_tensor(img_path, unsqueeze=True):rgb = transforms.ToTensor()(Image.open(img_path))if unsqueeze:rgb = rgb.unsqueeze(0)return rgbdef disparity_to_tensor(disp_path, unsqueeze=True):disp = cv2.imread(disp_path, -1) / (2 ** 16 - 1)# disp = cv2.imread(disp_path, -1) / (2 ** 8 - 1)disp = torch.from_numpy(disp)[None, ...]if unsqueeze:disp = disp.unsqueeze(0)return disp.float()
输入图像:uint8 960*1280
# load input
try:image = image_to_tensor(img_path).cuda() # [1,3,h,w]
except:image = image_to_tensor(img_path[:-3] + 'jpg').cuda() # [1,3,h,w]# image = image.type(torch.float32)/255/255
if image.shape[1] == 1:image = torch.tile(image, dims=(1, 3, 1, 1))
# image = image.float() / (2 ** 16 - 1)
image = image[:, 0:3, ...]
使用numpy加载测试,必须归一化到【0,1】
image_np = cv2.imread(input_pic, cv2.IMREAD_GRAYSCALE)# if len(image_np.shape) == 2:
# image_np= np.repeat(image_np[:, :, np.newaxis], 3, axis=2)
pic_int = torch.from_numpy(image_np).cuda().unsqueeze(0).unsqueeze(0).float()
if pic_int.shape[1] == 1:pic_int = torch.tile(pic_int , dims=(1, 3, 1, 1))
self.zoe(pic_int )
失败
pic_int = pic_int/255
成功