Download preprocessing.py from xingkungao/resnet50-example: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/xingkungao/resnet50-example/resolve/main/preprocessing.py
- Command line
-
hf download hf://xingkungao/resnet50-example/preprocessing.py
-
curl -L -o preprocessing.py https://huggingface.co/xingkungao/resnet50-example/resolve/main/preprocessing.py
1.06 kB
| from PIL import Image | |
| import numpy as np | |
| import torch | |
| def preprocess_image_with_pil(image, crop_pct=0.875, do_resize=True, do_normalize=True, size=224, image_mean=[0.485, 0.456, 0.406], image_std=[0.229, 0.224, 0.225]): | |
| # 如果需要缩放 | |
| if do_resize: | |
| original_size = min(image.size) | |
| target_size = int(original_size * crop_pct) | |
| image = image.resize((target_size, target_size), Image.Resampling.BILINEAR) | |
| # 中心裁剪 | |
| width, height = image.size | |
| left = (width - size) // 2 | |
| top = (height - size) // 2 | |
| right = left + size | |
| bottom = top + size | |
| image = image.crop((left, top, right, bottom)) | |
| # 转换为 NumPy 数组并归一化 | |
| image = np.array(image).astype(np.float32) / 255.0 | |
| if do_normalize: | |
| image = (image - image_mean) / image_std | |
| # 转换为张量格式并增加批次维度 | |
| image = np.transpose(image, (2, 0, 1)) | |
| tensor_image = torch.tensor(image, dtype=torch.float32) | |
| tensor_image = tensor_image.unsqueeze(0) | |
| return tensor_image | |