Spaces:
Sleeping
Sleeping
Fix: Inferencing issue
Browse files- 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 |
-
|
| 32 |
-
|
| 33 |
-
# Save a copy of input img
|
| 34 |
-
org_img = image.copy()
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
|
|
|
| 55 |
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
| 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
|