How to use the saliency.smooth_fullgrad.SmoothFullGrad function in saliency

To help you get started, we’ve selected a few saliency examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github idiap / fullgrad-saliency / dump_images.py View on Github external
])),
    batch_size= batch_size, shuffle=False)

unnormalize = NormalizeInverse(mean = [0.485, 0.456, 0.406],
                           std = [0.229, 0.224, 0.225])

# Use pretrained ResNet-18 provided by PyTorch
model = models.resnet18(pretrained=True)
model = model.to(device)

# Initialize saliency methods
saliency_methods = {
# FullGrad-based methods
'fullgrad': FullGrad(model),
'simple_fullgrad': SimpleFullGrad(model),
'smooth_fullgrad': SmoothFullGrad(model),

# Other saliency methods from literature
'gradcam': GradCAM(model),
'inputgrad': InputGradient(model),
'smoothgrad': SmoothGrad(model)
}

def compute_saliency_and_save():
    for batch_idx, (data, _) in enumerate(sample_loader):
        data = data.to(device).requires_grad_()

        # Compute saliency maps for the input data
        for s in saliency_methods:
            saliency_map = saliency_methods[s].saliency(data)

            # Save saliency maps