Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
])),
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