Normalize a given tensor by subtracting the mean and dividing the std. Args: tensor (tensor): tensor to normalize. mean (tensor or list): mean value to subtract. std (tensor or list): std to divide.
(tensor, mean, std)
| 344 | |
| 345 | |
| 346 | def tensor_normalize(tensor, mean, std): |
| 347 | """ |
| 348 | Normalize a given tensor by subtracting the mean and dividing the std. |
| 349 | Args: |
| 350 | tensor (tensor): tensor to normalize. |
| 351 | mean (tensor or list): mean value to subtract. |
| 352 | std (tensor or list): std to divide. |
| 353 | """ |
| 354 | if tensor.dtype == torch.uint8: |
| 355 | tensor = tensor.float() |
| 356 | tensor = tensor / 255.0 |
| 357 | if type(mean) == list: |
| 358 | mean = torch.tensor(mean) |
| 359 | if type(std) == list: |
| 360 | std = torch.tensor(std) |
| 361 | tensor = tensor - mean |
| 362 | tensor = tensor / std |
| 363 | return tensor |