[BUG] - Captum 教程中 ResNet18 的预处理不正确
import torch
from torchvision import models, transforms
weights = models.ResNet18_Weights.IMAGENET1K_V1
model = models.resnet18(weights=weights).eval()
tutorial_preprocess = transforms.Compose([
transforms.Resize(224),
transforms.CenterCrop(224),
transforms.ToTensor(),
transforms.Normalize(
mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225],
),
])
official_preprocess = weights.transforms()
x_tutorial = tutorial_preprocess(test_img).unsqueeze(0)
x_official = official_preprocess(test_img).unsqueeze(0)
with torch.inference_mode():
tutorial_probabilities = model(x_tutorial).softmax(dim=1)
official_probabilities = model(x_official).softmax(dim=1)
class_id = official_probabilities.argmax(dim=1).item()
print("Tutorial preprocessing:", tutorial_probabilities[0, class_id].item())
print("Official preprocessing:", official_probabilities[0, class_id].item())
print("Maximum input difference:", (x_tutorial - x_official).abs().max().item())
Expected Result:
The tutorial preprocessing should match the preprocessing associated with the pretrained weights. The tutorial could either use weights.transforms() or change Resize(224) to Resize(256) while retaining the separate normalization step needed by the tutorial.
Actual Result:
The tutorial resizes the shorter image side to 224 instead of
内容来源: pytorch/tutorials