Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scorecam update: nan fix for batch size less than 4 #173

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Binary file added bsize4_after_scorecam_cam.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added bsize4_before_scorecam_cam.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 6 additions & 4 deletions cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ def get_args():
"layercam": LayerCAM,
"fullgrad": FullGrad}

model = models.resnet50(pretrained=True)

#model = models.resnet50(pretrained=True)
model = models.vgg16(pretrained=True)

# Choose the target layer you want to compute the visualization for.
# Usually this will be the last convolutional layer in the model.
# Some common choices can be:
Expand All @@ -86,7 +87,8 @@ def get_args():
# You can also try selecting all layers of a certain type, with e.g:
# from pytorch_grad_cam.utils.find_layers import find_layer_types_recursive
# find_layer_types_recursive(model, [torch.nn.ReLU])
target_layers = [model.layer4[-1]]
#target_layers = [model.layer4[-1]]
target_layers = [model.features[-1]]

rgb_img = cv2.imread(args.image_path, 1)[:, :, ::-1]
rgb_img = np.float32(rgb_img) / 255
Expand All @@ -107,7 +109,7 @@ def get_args():

# AblationCAM and ScoreCAM have batched implementations.
# You can override the internal batch size for faster computation.
cam.batch_size = 32
cam.batch_size = 4

grayscale_cam = cam(input_tensor=input_tensor,
target_category=target_category,
Expand Down
4 changes: 4 additions & 0 deletions pytorch_grad_cam/score_cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def get_cam_weights(self,
maxs, mins = maxs[:, :, None, None], mins[:, :, None, None]
upsampled = (upsampled - mins) / (maxs - mins)

#mask = maxs!=mins
#maxs, mins = maxs[:, :, None, None], mins[:, :, None, None]
#upsampled[mask,:] = (upsampled[mask,:] - mins[mask,:]) / (maxs[mask] - mins[mask])

input_tensors = input_tensor[:, None,
:, :] * upsampled[:, :, None, :, :]

Expand Down