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