下述转换因为原来的transform封装和一些情况的考虑,耗时大概24ms,手动的简化版本是1.2ms.
71 self.predict_transform = transforms.Compose([
72 transforms.Resize((256, 256)),
73 transforms.ToTensor(),
74 transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
75 ])
95 def my_predict_transform(self, Xt):
96 #Xt = Xt.transpose(2, 0, 1)
97 Xt = np.array(Xt, dtype=np.float32)
98 Xt = Xt.transpose(2, 0, 1)
99 print("Xt.shape:", Xt.shape)
100 #Xt = cv2.resize(Xt, (256, 256)) #already256,last step
101 Xt = Xt/255
102 print("Xt[0]:", Xt[0].shape)
103 print(Xt[0])
104 Xt -= 0.5
105 Xt = Xt/0.5
106
107 return torch.from_numpy(Xt)
经验证,结果一致