ongkn commited on
Commit
0272244
·
unverified ·
1 Parent(s): a5240f9

set module guard

Browse files
Files changed (1) hide show
  1. 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
- faceGrabber = FaceGrabber()
85
-
86
- image = Image.open("Feature-Image-74.jpg").convert("RGB")
87
- face = faceGrabber.grab_faces(np.array(image))
88
- if face is not None:
89
- image = Image.fromarray(face)
90
-
91
- img_tensor = transforms.ToTensor()(image)
92
-
93
- model = ViTForImageClassification.from_pretrained("ongkn/attraction-classifier")
94
- targets_for_gradcam = [ClassifierOutputTarget(category_name_to_index(model, "pos")),
95
- ClassifierOutputTarget(category_name_to_index(model, "neg"))]
96
- target_layer_dff = model.vit.layernorm
97
- target_layer_gradcam = model.vit.encoder.layer[-2].output
98
- image_resized = image.resize((224, 224))
99
- tensor_resized = transforms.ToTensor()(image_resized)
100
-
101
- dff_image = run_dff_on_image(model=model,
102
- target_layer=target_layer_dff,
103
- classifier=model.classifier,
104
- img_pil=image_resized,
105
- img_tensor=tensor_resized,
106
- reshape_transform=reshape_transform_vit_huggingface,
107
- n_components=5,
108
- top_k=10,
109
- threshold=0,
110
- output_size=None) #(500, 500))
111
- cv.namedWindow("DFF Image", cv.WINDOW_KEEPRATIO)
112
- cv.imshow("DFF Image", cv.cvtColor(dff_image, cv.COLOR_BGR2RGB))
113
- cv.resizeWindow("DFF Image", 2500, 700)
114
- # cv.waitKey(0)
115
- # cv.destroyAllWindows()
116
- grad_cam_image = run_grad_cam_on_image(model=model,
117
- target_layer=target_layer_gradcam,
118
- targets_for_gradcam=targets_for_gradcam,
119
- input_tensor=tensor_resized,
120
- input_image=image_resized,
121
- reshape_transform=reshape_transform_vit_huggingface,
122
- threshold=0)
123
- cv.namedWindow("Grad-CAM Image", cv.WINDOW_KEEPRATIO)
124
- cv.imshow("Grad-CAM Image", grad_cam_image)
125
- cv.resizeWindow("Grad-CAM Image", 2000, 1250)
126
- cv.waitKey(0)
127
- cv.destroyAllWindows()
128
- print_top_categories(model, tensor_resized)
 
 
 
 
 
 
 
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
+