Shilpaj commited on
Commit
ad93eca
·
1 Parent(s): 601860b

Fix: Inferencing issue

Browse files
Files changed (1) hide show
  1. inference.py +52 -50
inference.py CHANGED
@@ -28,63 +28,65 @@ def inference(image, alpha, top_k, target_layer, model=None, classes=None):
28
  model = model.to(device)
29
  model.eval()
30
 
31
- with torch.cuda.amp.autocast(): # Enable automatic mixed precision
32
- with torch.no_grad():
33
- # Save a copy of input img
34
- org_img = image.copy()
35
 
36
- # Calculate mean and std over each channel
37
- mean_r, mean_g, mean_b = np.mean(image[:, :, 0]/255.), np.mean(image[:, :, 1]/255.), np.mean(image[:, :, 2]/255.)
38
- std_r, std_g, std_b = np.std(image[:, :, 0]/255.), np.std(image[:, :, 1]/255.), np.std(image[:, :, 2]/255.)
39
 
40
- # Convert img to tensor and normalize it
41
- _transform = transforms.Compose([
42
- transforms.ToTensor(),
43
- transforms.Normalize((mean_r, mean_g, mean_b), (std_r, std_g, std_b))
44
- ])
45
 
46
- # Preprocess the input image and move to device
47
- input_tensor = _transform(image).to(device)
48
- input_tensor = input_tensor.unsqueeze(0)
49
-
50
- # Get Model Predictions
51
- outputs = model(input_tensor)
52
- probabilities = torch.softmax(outputs, dim=1)[0]
53
- del outputs
54
- confidences = {classes[i]: float(probabilities[i]) for i in range(1000)}
 
55
 
56
- # Select the top classes based on user input
57
- sorted_confidences = sorted(confidences.items(), key=lambda val: val[1], reverse=True)
58
- show_confidences = OrderedDict(sorted_confidences[:top_k])
59
 
60
- # Map layer numbers to meaningful parts of the ResNet architecture
61
- _layers = {
62
- 1: model.conv1, # Initial convolution layer
63
- 2: model.layer1[-1], # Last bottleneck of first residual block
64
- 3: model.layer2[-1], # Last bottleneck of second residual block
65
- 4: model.layer3[-1], # Last bottleneck of third residual block
66
- 5: model.layer4[-1], # Last bottleneck of fourth residual block
67
- 6: model.layer4[-1] # Changed from fc to last conv layer for better visualization
68
- }
69
 
70
- # Ensure valid layer selection
71
- target_layer = min(max(target_layer, 1), 6)
72
- target_layers = [_layers[target_layer]]
73
 
74
- # Get the class activations from the selected layer
75
- cam = GradCAM(model=model, target_layers=target_layers)
76
 
77
- # Get the most probable class index
78
- top_class = max(confidences.items(), key=lambda x: x[1])[0]
79
- class_idx = classes.index(top_class)
80
 
81
- # Generate GradCAM for the top predicted class
82
- grayscale_cam = cam(input_tensor=input_tensor,
83
- targets=[ClassifierOutputTarget(class_idx)],
84
- aug_smooth=True,
85
- eigen_smooth=True)
86
- grayscale_cam = grayscale_cam[0, :]
 
 
 
87
 
88
- # Overlay input image with Class activations
89
- visualization = show_cam_on_image(org_img/255., grayscale_cam, use_rgb=True, image_weight=alpha)
90
- return show_confidences, visualization
 
28
  model = model.to(device)
29
  model.eval()
30
 
31
+ # Save a copy of input img
32
+ org_img = image.copy()
 
 
33
 
34
+ # Calculate mean and std over each channel
35
+ mean_r, mean_g, mean_b = np.mean(image[:, :, 0]/255.), np.mean(image[:, :, 1]/255.), np.mean(image[:, :, 2]/255.)
36
+ std_r, std_g, std_b = np.std(image[:, :, 0]/255.), np.std(image[:, :, 1]/255.), np.std(image[:, :, 2]/255.)
37
 
38
+ # Convert img to tensor and normalize it
39
+ _transform = transforms.Compose([
40
+ transforms.ToTensor(),
41
+ transforms.Normalize((mean_r, mean_g, mean_b), (std_r, std_g, std_b))
42
+ ])
43
 
44
+ # Preprocess the input image and move to device
45
+ input_tensor = _transform(image).to(device)
46
+ input_tensor = input_tensor.unsqueeze(0)
47
+
48
+ # For predictions, we don't need gradients
49
+ with torch.no_grad():
50
+ # Get Model Predictions
51
+ outputs = model(input_tensor)
52
+ probabilities = torch.softmax(outputs, dim=1)[0]
53
+ confidences = {classes[i]: float(probabilities[i]) for i in range(1000)}
54
 
55
+ # Select the top classes based on user input
56
+ sorted_confidences = sorted(confidences.items(), key=lambda val: val[1], reverse=True)
57
+ show_confidences = OrderedDict(sorted_confidences[:top_k])
58
 
59
+ # Map layer numbers to meaningful parts of the ResNet architecture
60
+ _layers = {
61
+ 1: model.conv1, # Initial convolution layer
62
+ 2: model.layer1[-1], # Last bottleneck of first residual block
63
+ 3: model.layer2[-1], # Last bottleneck of second residual block
64
+ 4: model.layer3[-1], # Last bottleneck of third residual block
65
+ 5: model.layer4[-1], # Last bottleneck of fourth residual block
66
+ 6: model.layer4[-1] # Changed from fc to last conv layer for better visualization
67
+ }
68
 
69
+ # Ensure valid layer selection
70
+ target_layer = min(max(target_layer, 1), 6)
71
+ target_layers = [_layers[target_layer]]
72
 
73
+ # Get the class activations from the selected layer
74
+ cam = GradCAM(model=model, target_layers=target_layers)
75
 
76
+ # Get the most probable class index
77
+ top_class = max(confidences.items(), key=lambda x: x[1])[0]
78
+ class_idx = classes.index(top_class)
79
 
80
+ # Enable gradients for GradCAM computation
81
+ input_tensor.requires_grad = True
82
+
83
+ # Generate GradCAM for the top predicted class
84
+ grayscale_cam = cam(input_tensor=input_tensor,
85
+ targets=[ClassifierOutputTarget(class_idx)],
86
+ aug_smooth=True,
87
+ eigen_smooth=True)
88
+ grayscale_cam = grayscale_cam[0, :]
89
 
90
+ # Overlay input image with Class activations
91
+ visualization = show_cam_on_image(org_img/255., grayscale_cam, use_rgb=True, image_weight=alpha)
92
+ return show_confidences, visualization