from pytorch_grad_cam import GradCAM, HiResCAM, ScoreCAM, GradCAMPlusPlus, AblationCAM, XGradCAM, EigenCAM, FullGrad
from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
from pytorch_grad_cam.utils.image import show_cam_on_image
from torchvision.models import resnet50, ResNet50_Weights
model = resnet50(weights=ResNet50_Weights.DEFAULT)
target_layers = [model.layer4[-1]]
input_tensor = # Create an input tensor image for your model..
# Note: input_tensor can be a batch tensor with several images!# We have to specify the target we want to generate the CAM for.
targets = [ClassifierOutputTarget(281)]
# Construct the CAM object once, and then re-use it on many images.
with GradCAM(model=model, target_layers=target_layers) as cam:
# You can also pass aug_smooth=True and eigen_smooth=True, to apply smoothing.
grayscale_cam = cam(input_tensor=input_tensor, targets=targ
jacobgil/pytorch-grad-cam 采用 MIT · 允许商用(宽松开源协议,允许商业使用、修改与私有化闭源分发(保留版权声明即可)。);lucidrains/vit-pytorch 采用 MIT · 允许商用(宽松开源协议,允许商业使用、修改与私有化闭源分发(保留版权声明即可)。)。
2026 年 pytorch-grad-cam 与 vit-pytorch 哪个社区增长更快?
按 RepoForAI 实时快照,pytorch-grad-cam 总 Star 13,023(7日 +4),vit-pytorch 总 Star 25,603(7日 +13),近期增速由 vit-pytorch 领先。
团队技术选型时该如何二选一?
适合选用 pytorch-grad-cam(Python):能访问 PyTorch 模型及其中间层,需要定位视觉或图文模型误判,或系统比较归因算法、目标层和指标;适合选用 vit-pytorch(Python):已有 PyTorch 训练流程,需要对照 ViT 架构、试验混合分辨率批处理,或接入掩码预训练和教师蒸馏。