set module guard
Browse files- gradcam.py +51 -45
gradcam.py
CHANGED
@@ -81,48 +81,54 @@ class GradCam():
|
|
81 |
activations = activations.transpose(2, 3).transpose(1, 2)
|
82 |
return activations
|
83 |
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
#
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
81 |
activations = activations.transpose(2, 3).transpose(1, 2)
|
82 |
return activations
|
83 |
|
84 |
+
|
85 |
+
|
86 |
+
if __name__ == "__main__":
|
87 |
+
|
88 |
+
faceGrabber = FaceGrabber()
|
89 |
+
gradCam = GradCam()
|
90 |
+
|
91 |
+
image = Image.open("Feature-Image-74.jpg").convert("RGB")
|
92 |
+
face = faceGrabber.grab_faces(np.array(image))
|
93 |
+
if face is not None:
|
94 |
+
image = Image.fromarray(face)
|
95 |
+
|
96 |
+
img_tensor = transforms.ToTensor()(image)
|
97 |
+
|
98 |
+
model = ViTForImageClassification.from_pretrained("ongkn/attraction-classifier")
|
99 |
+
targets_for_gradcam = [ClassifierOutputTarget(gradCam.category_name_to_index(model, "pos")),
|
100 |
+
ClassifierOutputTarget(gradCam.category_name_to_index(model, "neg"))]
|
101 |
+
target_layer_dff = model.vit.layernorm
|
102 |
+
target_layer_gradcam = model.vit.encoder.layer[-2].output
|
103 |
+
image_resized = image.resize((224, 224))
|
104 |
+
tensor_resized = transforms.ToTensor()(image_resized)
|
105 |
+
|
106 |
+
dff_image = run_dff_on_image(model=model,
|
107 |
+
target_layer=target_layer_dff,
|
108 |
+
classifier=model.classifier,
|
109 |
+
img_pil=image_resized,
|
110 |
+
img_tensor=tensor_resized,
|
111 |
+
reshape_transform=gradCam.reshape_transform_vit_huggingface,
|
112 |
+
n_components=5,
|
113 |
+
top_k=10,
|
114 |
+
threshold=0,
|
115 |
+
output_size=None) #(500, 500))
|
116 |
+
cv.namedWindow("DFF Image", cv.WINDOW_KEEPRATIO)
|
117 |
+
cv.imshow("DFF Image", cv.cvtColor(dff_image, cv.COLOR_BGR2RGB))
|
118 |
+
cv.resizeWindow("DFF Image", 2500, 700)
|
119 |
+
# cv.waitKey(0)
|
120 |
+
# cv.destroyAllWindows()
|
121 |
+
grad_cam_image = run_grad_cam_on_image(model=model,
|
122 |
+
target_layer=target_layer_gradcam,
|
123 |
+
targets_for_gradcam=targets_for_gradcam,
|
124 |
+
input_tensor=tensor_resized,
|
125 |
+
input_image=image_resized,
|
126 |
+
reshape_transform=gradCam.reshape_transform_vit_huggingface,
|
127 |
+
threshold=0)
|
128 |
+
cv.namedWindow("Grad-CAM Image", cv.WINDOW_KEEPRATIO)
|
129 |
+
cv.imshow("Grad-CAM Image", grad_cam_image)
|
130 |
+
cv.resizeWindow("Grad-CAM Image", 2000, 1250)
|
131 |
+
cv.waitKey(0)
|
132 |
+
cv.destroyAllWindows()
|
133 |
+
gradCam.print_top_categories(model, tensor_resized)
|
134 |
+
|