| import numpy as np | |
| import torch | |
| class ToTensorNormalize: | |
| def __init__(self, mean, std): | |
| self.mean = torch.tensor(mean, dtype=torch.float32).view(-1, 1, 1) | |
| self.std = torch.tensor(std, dtype=torch.float32).view(-1, 1, 1) | |
| def __call__(self, image): | |
| image_np = np.asarray(image, dtype=np.float32) / 255.0 | |
| tensor = torch.from_numpy(image_np).permute(2, 0, 1).contiguous() | |
| return (tensor - self.mean) / self.std | |