unknown commited on
Commit
f526a64
1 Parent(s): cbc6ea7

Add application file

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. Dockerfile +25 -0
  2. api_link.py +201 -0
  3. main.py +192 -0
  4. model_transfer_batch_2_epoch50.pt +3 -0
  5. pytorch_grad_cam/__init__.py +20 -0
  6. pytorch_grad_cam/__pycache__/__init__.cpython-310.pyc +0 -0
  7. pytorch_grad_cam/__pycache__/__init__.cpython-39.pyc +0 -0
  8. pytorch_grad_cam/__pycache__/ablation_cam.cpython-310.pyc +0 -0
  9. pytorch_grad_cam/__pycache__/ablation_layer.cpython-310.pyc +0 -0
  10. pytorch_grad_cam/__pycache__/activations_and_gradients.cpython-310.pyc +0 -0
  11. pytorch_grad_cam/__pycache__/base_cam.cpython-310.pyc +0 -0
  12. pytorch_grad_cam/__pycache__/base_cam.cpython-39.pyc +0 -0
  13. pytorch_grad_cam/__pycache__/eigen_cam.cpython-310.pyc +0 -0
  14. pytorch_grad_cam/__pycache__/eigen_grad_cam.cpython-310.pyc +0 -0
  15. pytorch_grad_cam/__pycache__/fullgrad_cam.cpython-310.pyc +0 -0
  16. pytorch_grad_cam/__pycache__/grad_cam.cpython-310.pyc +0 -0
  17. pytorch_grad_cam/__pycache__/grad_cam.cpython-39.pyc +0 -0
  18. pytorch_grad_cam/__pycache__/grad_cam_elementwise.cpython-310.pyc +0 -0
  19. pytorch_grad_cam/__pycache__/grad_cam_plusplus.cpython-310.pyc +0 -0
  20. pytorch_grad_cam/__pycache__/guided_backprop.cpython-310.pyc +0 -0
  21. pytorch_grad_cam/__pycache__/hirescam.cpython-310.pyc +0 -0
  22. pytorch_grad_cam/__pycache__/layer_cam.cpython-310.pyc +0 -0
  23. pytorch_grad_cam/__pycache__/random_cam.cpython-310.pyc +0 -0
  24. pytorch_grad_cam/__pycache__/score_cam.cpython-310.pyc +0 -0
  25. pytorch_grad_cam/__pycache__/xgrad_cam.cpython-310.pyc +0 -0
  26. pytorch_grad_cam/ablation_cam.py +148 -0
  27. pytorch_grad_cam/ablation_cam_multilayer.py +136 -0
  28. pytorch_grad_cam/ablation_layer.py +155 -0
  29. pytorch_grad_cam/activations_and_gradients.py +46 -0
  30. pytorch_grad_cam/base_cam.py +203 -0
  31. pytorch_grad_cam/eigen_cam.py +23 -0
  32. pytorch_grad_cam/eigen_grad_cam.py +21 -0
  33. pytorch_grad_cam/feature_factorization/__init__.py +0 -0
  34. pytorch_grad_cam/feature_factorization/__pycache__/__init__.cpython-310.pyc +0 -0
  35. pytorch_grad_cam/feature_factorization/__pycache__/deep_feature_factorization.cpython-310.pyc +0 -0
  36. pytorch_grad_cam/feature_factorization/deep_feature_factorization.py +131 -0
  37. pytorch_grad_cam/fullgrad_cam.py +95 -0
  38. pytorch_grad_cam/grad_cam.py +22 -0
  39. pytorch_grad_cam/grad_cam_elementwise.py +30 -0
  40. pytorch_grad_cam/grad_cam_plusplus.py +32 -0
  41. pytorch_grad_cam/guided_backprop.py +100 -0
  42. pytorch_grad_cam/hirescam.py +32 -0
  43. pytorch_grad_cam/layer_cam.py +36 -0
  44. pytorch_grad_cam/metrics/__init__.py +0 -0
  45. pytorch_grad_cam/metrics/__pycache__/__init__.cpython-310.pyc +0 -0
  46. pytorch_grad_cam/metrics/__pycache__/cam_mult_image.cpython-310.pyc +0 -0
  47. pytorch_grad_cam/metrics/__pycache__/perturbation_confidence.cpython-310.pyc +0 -0
  48. pytorch_grad_cam/metrics/__pycache__/road.cpython-310.pyc +0 -0
  49. pytorch_grad_cam/metrics/cam_mult_image.py +37 -0
  50. pytorch_grad_cam/metrics/perturbation_confidence.py +109 -0
Dockerfile ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # read the doc: https://huggingface.co/docs/hub/spaces-sdks-docker
2
+ # you will also find guides on how best to write your Dockerfile
3
+
4
+ FROM pytorch/pytorch
5
+
6
+ WORKDIR /code
7
+
8
+ COPY ./requirements.txt /code/requirements.txt
9
+
10
+ RUN apt-get update && apt-get install ffmpeg libsm6 libxext6 -y
11
+
12
+ RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
13
+
14
+ RUN mkdir ./.cache
15
+
16
+ RUN mkdir ./code/models
17
+
18
+ RUN chmod -R 777 ./code/models
19
+
20
+ RUN chmod -R 777 ./.cache
21
+
22
+
23
+ COPY . .
24
+
25
+ CMD ["gunicorn", "-b", "0.0.0.0:7860", "main:app"]
api_link.py ADDED
@@ -0,0 +1,201 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask
2
+ from flask_cors import CORS, cross_origin
3
+ from flask import request
4
+ import os
5
+ import cv2
6
+ import json
7
+ import urllib
8
+ import time
9
+
10
+ from flask import request
11
+
12
+ # Khởi tạo Flask Server Backend
13
+ app = Flask(__name__)
14
+
15
+ # Apply Flask CORS
16
+ CORS(app)
17
+ app.config['CORS_HEADERS'] = 'Content-Type'
18
+ app.config['UPLOAD_FOLDER'] = 'static'
19
+
20
+ # yolov6_model = my_yolov6.my_yolov6("weights/yolov6s.pt", 'cpu', 'data/coco.yaml', 640, True)
21
+
22
+ import matplotlib.pyplot as plt
23
+ import numpy as np
24
+ import pandas as pd
25
+ import torch
26
+ from torch import nn, optim
27
+ import torch.nn.functional as F
28
+ import torchvision
29
+ from torchvision import datasets, transforms, models
30
+ from torch.autograd import Variable
31
+ from torch.utils.data.sampler import SubsetRandomSampler
32
+
33
+ import warnings
34
+ warnings.filterwarnings('ignore')
35
+ from pytorch_grad_cam import GradCAM, EigenCAM, LayerCAM, XGradCAM
36
+ from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
37
+ from pytorch_grad_cam.utils.image import show_cam_on_image, \
38
+ deprocess_image, \
39
+ preprocess_image
40
+ from PIL import Image
41
+
42
+ import copy
43
+
44
+ # Load GoogleNet model
45
+ model = models.googlenet(pretrained=True)
46
+
47
+ model.fc= nn.Linear(1024, 4)
48
+ model.load_state_dict(torch.load('./model_transfer_batch_2_epoch50.pt'))
49
+
50
+
51
+ data_transforms ={
52
+ "train_transforms": transforms.Compose([transforms.RandomRotation(30),
53
+ transforms.RandomResizedCrop(224),
54
+ transforms.RandomHorizontalFlip(),
55
+ transforms.ToTensor(),
56
+ transforms.Normalize([0.485, 0.456, 0.406],
57
+ [0.229, 0.224, 0.225])]),
58
+ "valid_transforms": transforms.Compose([transforms.Resize(225),
59
+ transforms.CenterCrop(224),
60
+ transforms.ToTensor(),
61
+ transforms.Normalize([0.485, 0.456, 0.406],
62
+ [0.229, 0.224, 0.225])]),
63
+ "test_transforms": transforms.Compose([transforms.Resize(225),
64
+ transforms.CenterCrop(224),
65
+ transforms.ToTensor(),
66
+ transforms.Normalize([0.485, 0.456, 0.406],
67
+ [0.229, 0.224, 0.225])])
68
+ }
69
+
70
+ transform = transforms.Compose([transforms.Resize(225),
71
+ transforms.CenterCrop(224),
72
+ transforms.ToTensor(),
73
+ transforms.Normalize([0.485, 0.456, 0.406],
74
+ [0.229, 0.224, 0.225])])
75
+
76
+ use_cuda = torch.cuda.is_available()
77
+ classes = ['BrownSpot', 'Healthy', 'Hispa', 'LeafBlast']
78
+
79
+ def yolo_format(x, y, w, h, image_size):
80
+ x_center_norm = (x+w/2)/image_size[1]
81
+ y_center_norm = (y+h/2)/image_size[0]
82
+ w_norm = w/image_size[1]
83
+ h_norm = h/image_size[0]
84
+ return (x_center_norm, y_center_norm, w_norm, h_norm)
85
+
86
+ def predict_image(image_url):
87
+
88
+ img = np.array(Image.open(image_url))
89
+
90
+ img_cp = np.copy(img)
91
+ img_cp = cv2.resize(img_cp, (224, 224))
92
+ img_cp = np.float32(img_cp) / 255
93
+ input_tensor = preprocess_image(img_cp, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
94
+ input_tensor = torch.Tensor(input_tensor)
95
+ input_tensor.cuda()
96
+
97
+ output = model(input_tensor)
98
+ # print(torch.max(output, 1))
99
+ _, preds_tensor = torch.max(output, 1)
100
+ preds = np.squeeze(preds, preds_tensor.numpy()) if not use_cuda else np.squeeze(preds_tensor.cpu().numpy())
101
+ print(preds)
102
+
103
+ class_name = classes[preds]
104
+ if preds == 1:
105
+ grad_bounding_box = (0,0,0,0)
106
+ else:
107
+ img = np.array(Image.open(image_url))
108
+ img = cv2.resize(img, (224, 224))
109
+ img = np.float32(img) / 255
110
+ input_tensor = torch.Tensor(input_tensor)
111
+ input_tensor.cuda()
112
+
113
+ input_tensor = preprocess_image(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
114
+ targets = [ClassifierOutputTarget(0)]
115
+ target_layers = [model.inception5b.branch4[1].conv]
116
+
117
+ with EigenCAM(model=model, target_layers=target_layers) as cam:
118
+ grayscale_cams = cam(input_tensor=input_tensor, targets=targets)
119
+ cam_image = show_cam_on_image(img, grayscale_cams[0, :], use_rgb=True)
120
+ cam = np.uint8(255*grayscale_cams[0, :])
121
+ img = np.uint8(255*img)
122
+ ret, thresh1 = cv2.threshold(cam, 120, 255, cv2.THRESH_BINARY +
123
+ cv2.THRESH_OTSU)
124
+ img_otsu = cam < thresh1
125
+ img_bin = np.multiply(img_otsu, 1)
126
+ img_bin = np.array(img_bin, np.uint8)
127
+ contours, _ = cv2.findContours(img_bin,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
128
+ cnt = contours[0]
129
+ x,y,w,h = cv2.boundingRect(cnt)
130
+ # grad_bounding_box = (x,y,x+w, y+h)
131
+ grad_bounding_box = yolo_format(x, y, w, h, (224, 224))
132
+
133
+ # print(grad_bounding_box)
134
+
135
+ return class_name, grad_bounding_box
136
+
137
+ def yolo2bbox(x, y, w, h, img_size=(224, 224)):
138
+ x = x * img_size[1]
139
+ y = y * img_size[0]
140
+ w = w * img_size[1]
141
+ h = h * img_size[0]
142
+ x1, y1 = x-w/2, y-h/2
143
+ x2, y2 = x+w/2, y+h/2
144
+ return int(x1), int(y1), int(x2), int(y2)
145
+
146
+ def bb_intersection_over_union(boxA, boxB):
147
+ xA = max(boxA[0], boxB[0])
148
+ yA = max(boxA[1], boxB[1])
149
+ xB = min(boxA[2], boxB[2])
150
+ yB = min(boxA[3], boxB[3])
151
+ interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
152
+ boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
153
+ boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
154
+ iou = interArea / float(boxAArea + boxBArea - interArea)
155
+ return iou
156
+
157
+ def read_annot_file(label_file):
158
+ with open(os.path.join(label_file), "r") as file1:
159
+ # Reading from a file
160
+ t = file1.read()
161
+ box = t[t.find(" ")+1:]
162
+ box = list(box.split(" "))
163
+ # list(map(float, box))
164
+ for i in range(len(box)):
165
+ box[i] = float(box[i])
166
+ return box
167
+
168
+
169
+ @app.route('/', methods=['POST'] )
170
+ @cross_origin(origin='*')
171
+ def predict_leaf():
172
+ # image = request.files['file']
173
+ img_url = request.form['url']
174
+ print(img_url)
175
+ image = Image.open(urllib.request.urlopen(img_url))
176
+ date = time.time()
177
+ filename = str(date) + '.jpg'
178
+
179
+ if image:
180
+ # Lưu file
181
+ #save image
182
+ path_to_save = os.path.join(app.config['UPLOAD_FOLDER'], filename)
183
+ image.save(path_to_save)
184
+
185
+ # print("Save= ", path_to_save)
186
+
187
+
188
+ predicted_class, grad_bounding_box = predict_image(path_to_save)
189
+ # print(predicted_class)
190
+ # print(grad_bounding_box)
191
+ result_dict = {'class': predicted_class, 'bounding_box': grad_bounding_box}
192
+ json_object = json.dumps(result_dict)
193
+ print(json_object)
194
+ return json_object
195
+ return 'Upload file to detect: '
196
+
197
+
198
+
199
+ # Start Backend
200
+ if __name__ == '__main__':
201
+ app.run(host='0.0.0.0', port='6868')
main.py ADDED
@@ -0,0 +1,192 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask
2
+ from flask_cors import CORS, cross_origin
3
+ from flask import request
4
+ import os
5
+ import cv2
6
+ import json
7
+
8
+
9
+ from flask import request
10
+
11
+ # Khởi tạo Flask Server Backend
12
+ app = Flask(__name__)
13
+
14
+ # Apply Flask CORS
15
+ CORS(app)
16
+ app.config['CORS_HEADERS'] = 'Content-Type'
17
+ app.config['UPLOAD_FOLDER'] = 'static'
18
+
19
+ # yolov6_model = my_yolov6.my_yolov6("weights/yolov6s.pt", 'cpu', 'data/coco.yaml', 640, True)
20
+
21
+ import matplotlib.pyplot as plt
22
+ import numpy as np
23
+ import pandas as pd
24
+ import torch
25
+ from torch import nn, optim
26
+ import torch.nn.functional as F
27
+ import torchvision
28
+ from torchvision import datasets, transforms, models
29
+ from torch.autograd import Variable
30
+ from torch.utils.data.sampler import SubsetRandomSampler
31
+
32
+ import warnings
33
+ warnings.filterwarnings('ignore')
34
+ from pytorch_grad_cam import GradCAM, EigenCAM, LayerCAM, XGradCAM
35
+ from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
36
+ from pytorch_grad_cam.utils.image import show_cam_on_image, \
37
+ deprocess_image, \
38
+ preprocess_image
39
+ from PIL import Image
40
+
41
+ import copy
42
+
43
+ # Load GoogleNet model
44
+ # os.makedirs('./model', exist_ok=True, mode=0o777)
45
+ os.environ['TORCH_HOME'] = './models'
46
+ model = models.googlenet(pretrained=True)
47
+ model.fc= nn.Linear(1024, 4)
48
+ model.load_state_dict(torch.load('./model_transfer_batch_2_epoch50.pt', map_location=torch.device('cpu')))
49
+
50
+
51
+ data_transforms ={
52
+ "train_transforms": transforms.Compose([transforms.RandomRotation(30),
53
+ transforms.RandomResizedCrop(224),
54
+ transforms.RandomHorizontalFlip(),
55
+ transforms.ToTensor(),
56
+ transforms.Normalize([0.485, 0.456, 0.406],
57
+ [0.229, 0.224, 0.225])]),
58
+ "valid_transforms": transforms.Compose([transforms.Resize(225),
59
+ transforms.CenterCrop(224),
60
+ transforms.ToTensor(),
61
+ transforms.Normalize([0.485, 0.456, 0.406],
62
+ [0.229, 0.224, 0.225])]),
63
+ "test_transforms": transforms.Compose([transforms.Resize(225),
64
+ transforms.CenterCrop(224),
65
+ transforms.ToTensor(),
66
+ transforms.Normalize([0.485, 0.456, 0.406],
67
+ [0.229, 0.224, 0.225])])
68
+ }
69
+
70
+ transform = transforms.Compose([transforms.Resize(225),
71
+ transforms.CenterCrop(224),
72
+ transforms.ToTensor(),
73
+ transforms.Normalize([0.485, 0.456, 0.406],
74
+ [0.229, 0.224, 0.225])])
75
+
76
+ use_cuda = torch.cuda.is_available()
77
+ classes = ['BrownSpot', 'Healthy', 'Hispa', 'LeafBlast']
78
+
79
+ def yolo_format(x, y, w, h, image_size):
80
+ x_center_norm = (x+w/2)/image_size[1]
81
+ y_center_norm = (y+h/2)/image_size[0]
82
+ w_norm = w/image_size[1]
83
+ h_norm = h/image_size[0]
84
+ return (x_center_norm, y_center_norm, w_norm, h_norm)
85
+
86
+ def predict_image(image_url):
87
+
88
+ img = np.array(Image.open(image_url))
89
+
90
+ img_cp = np.copy(img)
91
+ img_cp = cv2.resize(img_cp, (224, 224))
92
+ img_cp = np.float32(img_cp) / 255
93
+ input_tensor = preprocess_image(img_cp, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
94
+ input_tensor = torch.Tensor(input_tensor)
95
+ input_tensor.cuda()
96
+
97
+ output = model(input_tensor)
98
+ # print(torch.max(output, 1))
99
+ _, preds_tensor = torch.max(output, 1)
100
+ preds = np.squeeze(preds, preds_tensor.numpy()) if not use_cuda else np.squeeze(preds_tensor.cpu().numpy())
101
+ print(preds)
102
+
103
+ class_name = classes[preds]
104
+ if preds == 1:
105
+ grad_bounding_box = (0,0,0,0)
106
+ else:
107
+ img = np.array(Image.open(image_url))
108
+ img = cv2.resize(img, (224, 224))
109
+ img = np.float32(img) / 255
110
+ input_tensor = torch.Tensor(input_tensor)
111
+ input_tensor.cuda()
112
+
113
+ input_tensor = preprocess_image(img, mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
114
+ targets = [ClassifierOutputTarget(0)]
115
+ target_layers = [model.inception5b.branch4[1].conv]
116
+
117
+ with EigenCAM(model=model, target_layers=target_layers) as cam:
118
+ grayscale_cams = cam(input_tensor=input_tensor, targets=targets)
119
+ cam_image = show_cam_on_image(img, grayscale_cams[0, :], use_rgb=True)
120
+ cam = np.uint8(255*grayscale_cams[0, :])
121
+ img = np.uint8(255*img)
122
+ ret, thresh1 = cv2.threshold(cam, 120, 255, cv2.THRESH_BINARY +
123
+ cv2.THRESH_OTSU)
124
+ img_otsu = cam < thresh1
125
+ img_bin = np.multiply(img_otsu, 1)
126
+ img_bin = np.array(img_bin, np.uint8)
127
+ contours, _ = cv2.findContours(img_bin,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
128
+ cnt = contours[0]
129
+ x,y,w,h = cv2.boundingRect(cnt)
130
+ # grad_bounding_box = (x,y,x+w, y+h)
131
+ grad_bounding_box = yolo_format(x, y, w, h, (224, 224))
132
+
133
+ # print(grad_bounding_box)
134
+
135
+ return class_name, grad_bounding_box
136
+
137
+ def yolo2bbox(x, y, w, h, img_size=(224, 224)):
138
+ x = x * img_size[1]
139
+ y = y * img_size[0]
140
+ w = w * img_size[1]
141
+ h = h * img_size[0]
142
+ x1, y1 = x-w/2, y-h/2
143
+ x2, y2 = x+w/2, y+h/2
144
+ return int(x1), int(y1), int(x2), int(y2)
145
+
146
+ def bb_intersection_over_union(boxA, boxB):
147
+ xA = max(boxA[0], boxB[0])
148
+ yA = max(boxA[1], boxB[1])
149
+ xB = min(boxA[2], boxB[2])
150
+ yB = min(boxA[3], boxB[3])
151
+ interArea = max(0, xB - xA + 1) * max(0, yB - yA + 1)
152
+ boxAArea = (boxA[2] - boxA[0] + 1) * (boxA[3] - boxA[1] + 1)
153
+ boxBArea = (boxB[2] - boxB[0] + 1) * (boxB[3] - boxB[1] + 1)
154
+ iou = interArea / float(boxAArea + boxBArea - interArea)
155
+ return iou
156
+
157
+ def read_annot_file(label_file):
158
+ with open(os.path.join(label_file), "r") as file1:
159
+ # Reading from a file
160
+ t = file1.read()
161
+ box = t[t.find(" ")+1:]
162
+ box = list(box.split(" "))
163
+ # list(map(float, box))
164
+ for i in range(len(box)):
165
+ box[i] = float(box[i])
166
+ return box
167
+
168
+
169
+ @app.route('/', methods=['POST'] )
170
+ @cross_origin(origin='*')
171
+ def predict_leaf():
172
+ image = request.files['file']
173
+ if image:
174
+ # Lưu file
175
+ path_to_save = os.path.join(app.config['UPLOAD_FOLDER'], image.filename)
176
+ # print("Save= ", path_to_save)
177
+ image.save(path_to_save)
178
+
179
+ predicted_class, grad_bounding_box = predict_image(path_to_save)
180
+ # print(predicted_class)
181
+ # print(grad_bounding_box)
182
+ result_dict = {'class': predicted_class, 'bounding_box': grad_bounding_box}
183
+ json_object = json.dumps(result_dict)
184
+ print(json_object)
185
+ return json_object
186
+ return 'Upload file to detect: '
187
+
188
+
189
+
190
+ # Start Backend
191
+ if __name__ == '__main__':
192
+ app.run(host='0.0.0.0', port='6868')
model_transfer_batch_2_epoch50.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c7eb8ac31ecff2afe28dbd36a08aac278ab5deb032d394a2f530365af697eeed
3
+ size 22596295
pytorch_grad_cam/__init__.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pytorch_grad_cam.grad_cam import GradCAM
2
+ from pytorch_grad_cam.hirescam import HiResCAM
3
+ from pytorch_grad_cam.grad_cam_elementwise import GradCAMElementWise
4
+ from pytorch_grad_cam.ablation_layer import AblationLayer, AblationLayerVit, AblationLayerFasterRCNN
5
+ from pytorch_grad_cam.ablation_cam import AblationCAM
6
+ from pytorch_grad_cam.xgrad_cam import XGradCAM
7
+ from pytorch_grad_cam.grad_cam_plusplus import GradCAMPlusPlus
8
+ from pytorch_grad_cam.score_cam import ScoreCAM
9
+ from pytorch_grad_cam.layer_cam import LayerCAM
10
+ from pytorch_grad_cam.eigen_cam import EigenCAM
11
+ from pytorch_grad_cam.eigen_grad_cam import EigenGradCAM
12
+ from pytorch_grad_cam.random_cam import RandomCAM
13
+ from pytorch_grad_cam.fullgrad_cam import FullGrad
14
+ from pytorch_grad_cam.guided_backprop import GuidedBackpropReLUModel
15
+ from pytorch_grad_cam.activations_and_gradients import ActivationsAndGradients
16
+ from pytorch_grad_cam.feature_factorization.deep_feature_factorization import DeepFeatureFactorization, run_dff_on_image
17
+ import pytorch_grad_cam.utils.model_targets
18
+ import pytorch_grad_cam.utils.reshape_transforms
19
+ import pytorch_grad_cam.metrics.cam_mult_image
20
+ import pytorch_grad_cam.metrics.road
pytorch_grad_cam/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (1.59 kB). View file
 
pytorch_grad_cam/__pycache__/__init__.cpython-39.pyc ADDED
Binary file (1.58 kB). View file
 
pytorch_grad_cam/__pycache__/ablation_cam.cpython-310.pyc ADDED
Binary file (3.78 kB). View file
 
pytorch_grad_cam/__pycache__/ablation_layer.cpython-310.pyc ADDED
Binary file (5.18 kB). View file
 
pytorch_grad_cam/__pycache__/activations_and_gradients.cpython-310.pyc ADDED
Binary file (1.92 kB). View file
 
pytorch_grad_cam/__pycache__/base_cam.cpython-310.pyc ADDED
Binary file (5.88 kB). View file
 
pytorch_grad_cam/__pycache__/base_cam.cpython-39.pyc ADDED
Binary file (5.83 kB). View file
 
pytorch_grad_cam/__pycache__/eigen_cam.cpython-310.pyc ADDED
Binary file (968 Bytes). View file
 
pytorch_grad_cam/__pycache__/eigen_grad_cam.cpython-310.pyc ADDED
Binary file (962 Bytes). View file
 
pytorch_grad_cam/__pycache__/fullgrad_cam.cpython-310.pyc ADDED
Binary file (3.22 kB). View file
 
pytorch_grad_cam/__pycache__/grad_cam.cpython-310.pyc ADDED
Binary file (911 Bytes). View file
 
pytorch_grad_cam/__pycache__/grad_cam.cpython-39.pyc ADDED
Binary file (901 Bytes). View file
 
pytorch_grad_cam/__pycache__/grad_cam_elementwise.cpython-310.pyc ADDED
Binary file (1.13 kB). View file
 
pytorch_grad_cam/__pycache__/grad_cam_plusplus.cpython-310.pyc ADDED
Binary file (1.16 kB). View file
 
pytorch_grad_cam/__pycache__/guided_backprop.cpython-310.pyc ADDED
Binary file (3.46 kB). View file
 
pytorch_grad_cam/__pycache__/hirescam.cpython-310.pyc ADDED
Binary file (1.17 kB). View file
 
pytorch_grad_cam/__pycache__/layer_cam.cpython-310.pyc ADDED
Binary file (1.1 kB). View file
 
pytorch_grad_cam/__pycache__/random_cam.cpython-310.pyc ADDED
Binary file (960 Bytes). View file
 
pytorch_grad_cam/__pycache__/score_cam.cpython-310.pyc ADDED
Binary file (1.99 kB). View file
 
pytorch_grad_cam/__pycache__/xgrad_cam.cpython-310.pyc ADDED
Binary file (1.02 kB). View file
 
pytorch_grad_cam/ablation_cam.py ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ import tqdm
4
+ from typing import Callable, List
5
+ from pytorch_grad_cam.base_cam import BaseCAM
6
+ from pytorch_grad_cam.utils.find_layers import replace_layer_recursive
7
+ from pytorch_grad_cam.ablation_layer import AblationLayer
8
+
9
+
10
+ """ Implementation of AblationCAM
11
+ https://openaccess.thecvf.com/content_WACV_2020/papers/Desai_Ablation-CAM_Visual_Explanations_for_Deep_Convolutional_Network_via_Gradient-free_Localization_WACV_2020_paper.pdf
12
+
13
+ Ablate individual activations, and then measure the drop in the target score.
14
+
15
+ In the current implementation, the target layer activations is cached, so it won't be re-computed.
16
+ However layers before it, if any, will not be cached.
17
+ This means that if the target layer is a large block, for example model.featuers (in vgg), there will
18
+ be a large save in run time.
19
+
20
+ Since we have to go over many channels and ablate them, and every channel ablation requires a forward pass,
21
+ it would be nice if we could avoid doing that for channels that won't contribute anwyay, making it much faster.
22
+ The parameter ratio_channels_to_ablate controls how many channels should be ablated, using an experimental method
23
+ (to be improved). The default 1.0 value means that all channels will be ablated.
24
+ """
25
+
26
+
27
+ class AblationCAM(BaseCAM):
28
+ def __init__(self,
29
+ model: torch.nn.Module,
30
+ target_layers: List[torch.nn.Module],
31
+ use_cuda: bool = False,
32
+ reshape_transform: Callable = None,
33
+ ablation_layer: torch.nn.Module = AblationLayer(),
34
+ batch_size: int = 32,
35
+ ratio_channels_to_ablate: float = 1.0) -> None:
36
+
37
+ super(AblationCAM, self).__init__(model,
38
+ target_layers,
39
+ use_cuda,
40
+ reshape_transform,
41
+ uses_gradients=False)
42
+ self.batch_size = batch_size
43
+ self.ablation_layer = ablation_layer
44
+ self.ratio_channels_to_ablate = ratio_channels_to_ablate
45
+
46
+ def save_activation(self, module, input, output) -> None:
47
+ """ Helper function to save the raw activations from the target layer """
48
+ self.activations = output
49
+
50
+ def assemble_ablation_scores(self,
51
+ new_scores: list,
52
+ original_score: float,
53
+ ablated_channels: np.ndarray,
54
+ number_of_channels: int) -> np.ndarray:
55
+ """ Take the value from the channels that were ablated,
56
+ and just set the original score for the channels that were skipped """
57
+
58
+ index = 0
59
+ result = []
60
+ sorted_indices = np.argsort(ablated_channels)
61
+ ablated_channels = ablated_channels[sorted_indices]
62
+ new_scores = np.float32(new_scores)[sorted_indices]
63
+
64
+ for i in range(number_of_channels):
65
+ if index < len(ablated_channels) and ablated_channels[index] == i:
66
+ weight = new_scores[index]
67
+ index = index + 1
68
+ else:
69
+ weight = original_score
70
+ result.append(weight)
71
+
72
+ return result
73
+
74
+ def get_cam_weights(self,
75
+ input_tensor: torch.Tensor,
76
+ target_layer: torch.nn.Module,
77
+ targets: List[Callable],
78
+ activations: torch.Tensor,
79
+ grads: torch.Tensor) -> np.ndarray:
80
+
81
+ # Do a forward pass, compute the target scores, and cache the
82
+ # activations
83
+ handle = target_layer.register_forward_hook(self.save_activation)
84
+ with torch.no_grad():
85
+ outputs = self.model(input_tensor)
86
+ handle.remove()
87
+ original_scores = np.float32(
88
+ [target(output).cpu().item() for target, output in zip(targets, outputs)])
89
+
90
+ # Replace the layer with the ablation layer.
91
+ # When we finish, we will replace it back, so the original model is
92
+ # unchanged.
93
+ ablation_layer = self.ablation_layer
94
+ replace_layer_recursive(self.model, target_layer, ablation_layer)
95
+
96
+ number_of_channels = activations.shape[1]
97
+ weights = []
98
+ # This is a "gradient free" method, so we don't need gradients here.
99
+ with torch.no_grad():
100
+ # Loop over each of the batch images and ablate activations for it.
101
+ for batch_index, (target, tensor) in enumerate(
102
+ zip(targets, input_tensor)):
103
+ new_scores = []
104
+ batch_tensor = tensor.repeat(self.batch_size, 1, 1, 1)
105
+
106
+ # Check which channels should be ablated. Normally this will be all channels,
107
+ # But we can also try to speed this up by using a low
108
+ # ratio_channels_to_ablate.
109
+ channels_to_ablate = ablation_layer.activations_to_be_ablated(
110
+ activations[batch_index, :], self.ratio_channels_to_ablate)
111
+ number_channels_to_ablate = len(channels_to_ablate)
112
+
113
+ for i in tqdm.tqdm(
114
+ range(
115
+ 0,
116
+ number_channels_to_ablate,
117
+ self.batch_size)):
118
+ if i + self.batch_size > number_channels_to_ablate:
119
+ batch_tensor = batch_tensor[:(
120
+ number_channels_to_ablate - i)]
121
+
122
+ # Change the state of the ablation layer so it ablates the next channels.
123
+ # TBD: Move this into the ablation layer forward pass.
124
+ ablation_layer.set_next_batch(
125
+ input_batch_index=batch_index,
126
+ activations=self.activations,
127
+ num_channels_to_ablate=batch_tensor.size(0))
128
+ score = [target(o).cpu().item()
129
+ for o in self.model(batch_tensor)]
130
+ new_scores.extend(score)
131
+ ablation_layer.indices = ablation_layer.indices[batch_tensor.size(
132
+ 0):]
133
+
134
+ new_scores = self.assemble_ablation_scores(
135
+ new_scores,
136
+ original_scores[batch_index],
137
+ channels_to_ablate,
138
+ number_of_channels)
139
+ weights.extend(new_scores)
140
+
141
+ weights = np.float32(weights)
142
+ weights = weights.reshape(activations.shape[:2])
143
+ original_scores = original_scores[:, None]
144
+ weights = (original_scores - weights) / original_scores
145
+
146
+ # Replace the model back to the original state
147
+ replace_layer_recursive(self.model, ablation_layer, target_layer)
148
+ return weights
pytorch_grad_cam/ablation_cam_multilayer.py ADDED
@@ -0,0 +1,136 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import numpy as np
3
+ import torch
4
+ import tqdm
5
+ from pytorch_grad_cam.base_cam import BaseCAM
6
+
7
+
8
+ class AblationLayer(torch.nn.Module):
9
+ def __init__(self, layer, reshape_transform, indices):
10
+ super(AblationLayer, self).__init__()
11
+
12
+ self.layer = layer
13
+ self.reshape_transform = reshape_transform
14
+ # The channels to zero out:
15
+ self.indices = indices
16
+
17
+ def forward(self, x):
18
+ self.__call__(x)
19
+
20
+ def __call__(self, x):
21
+ output = self.layer(x)
22
+
23
+ # Hack to work with ViT,
24
+ # Since the activation channels are last and not first like in CNNs
25
+ # Probably should remove it?
26
+ if self.reshape_transform is not None:
27
+ output = output.transpose(1, 2)
28
+
29
+ for i in range(output.size(0)):
30
+
31
+ # Commonly the minimum activation will be 0,
32
+ # And then it makes sense to zero it out.
33
+ # However depending on the architecture,
34
+ # If the values can be negative, we use very negative values
35
+ # to perform the ablation, deviating from the paper.
36
+ if torch.min(output) == 0:
37
+ output[i, self.indices[i], :] = 0
38
+ else:
39
+ ABLATION_VALUE = 1e5
40
+ output[i, self.indices[i], :] = torch.min(
41
+ output) - ABLATION_VALUE
42
+
43
+ if self.reshape_transform is not None:
44
+ output = output.transpose(2, 1)
45
+
46
+ return output
47
+
48
+
49
+ def replace_layer_recursive(model, old_layer, new_layer):
50
+ for name, layer in model._modules.items():
51
+ if layer == old_layer:
52
+ model._modules[name] = new_layer
53
+ return True
54
+ elif replace_layer_recursive(layer, old_layer, new_layer):
55
+ return True
56
+ return False
57
+
58
+
59
+ class AblationCAM(BaseCAM):
60
+ def __init__(self, model, target_layers, use_cuda=False,
61
+ reshape_transform=None):
62
+ super(AblationCAM, self).__init__(model, target_layers, use_cuda,
63
+ reshape_transform)
64
+
65
+ if len(target_layers) > 1:
66
+ print(
67
+ "Warning. You are usign Ablation CAM with more than 1 layers. "
68
+ "This is supported only if all layers have the same output shape")
69
+
70
+ def set_ablation_layers(self):
71
+ self.ablation_layers = []
72
+ for target_layer in self.target_layers:
73
+ ablation_layer = AblationLayer(target_layer,
74
+ self.reshape_transform, indices=[])
75
+ self.ablation_layers.append(ablation_layer)
76
+ replace_layer_recursive(self.model, target_layer, ablation_layer)
77
+
78
+ def unset_ablation_layers(self):
79
+ # replace the model back to the original state
80
+ for ablation_layer, target_layer in zip(
81
+ self.ablation_layers, self.target_layers):
82
+ replace_layer_recursive(self.model, ablation_layer, target_layer)
83
+
84
+ def set_ablation_layer_batch_indices(self, indices):
85
+ for ablation_layer in self.ablation_layers:
86
+ ablation_layer.indices = indices
87
+
88
+ def trim_ablation_layer_batch_indices(self, keep):
89
+ for ablation_layer in self.ablation_layers:
90
+ ablation_layer.indices = ablation_layer.indices[:keep]
91
+
92
+ def get_cam_weights(self,
93
+ input_tensor,
94
+ target_category,
95
+ activations,
96
+ grads):
97
+ with torch.no_grad():
98
+ outputs = self.model(input_tensor).cpu().numpy()
99
+ original_scores = []
100
+ for i in range(input_tensor.size(0)):
101
+ original_scores.append(outputs[i, target_category[i]])
102
+ original_scores = np.float32(original_scores)
103
+
104
+ self.set_ablation_layers()
105
+
106
+ if hasattr(self, "batch_size"):
107
+ BATCH_SIZE = self.batch_size
108
+ else:
109
+ BATCH_SIZE = 32
110
+
111
+ number_of_channels = activations.shape[1]
112
+ weights = []
113
+
114
+ with torch.no_grad():
115
+ # Iterate over the input batch
116
+ for tensor, category in zip(input_tensor, target_category):
117
+ batch_tensor = tensor.repeat(BATCH_SIZE, 1, 1, 1)
118
+ for i in tqdm.tqdm(range(0, number_of_channels, BATCH_SIZE)):
119
+ self.set_ablation_layer_batch_indices(
120
+ list(range(i, i + BATCH_SIZE)))
121
+
122
+ if i + BATCH_SIZE > number_of_channels:
123
+ keep = number_of_channels - i
124
+ batch_tensor = batch_tensor[:keep]
125
+ self.trim_ablation_layer_batch_indices(self, keep)
126
+ score = self.model(batch_tensor)[:, category].cpu().numpy()
127
+ weights.extend(score)
128
+
129
+ weights = np.float32(weights)
130
+ weights = weights.reshape(activations.shape[:2])
131
+ original_scores = original_scores[:, None]
132
+ weights = (original_scores - weights) / original_scores
133
+
134
+ # replace the model back to the original state
135
+ self.unset_ablation_layers()
136
+ return weights
pytorch_grad_cam/ablation_layer.py ADDED
@@ -0,0 +1,155 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from collections import OrderedDict
3
+ import numpy as np
4
+ from pytorch_grad_cam.utils.svd_on_activations import get_2d_projection
5
+
6
+
7
+ class AblationLayer(torch.nn.Module):
8
+ def __init__(self):
9
+ super(AblationLayer, self).__init__()
10
+
11
+ def objectiveness_mask_from_svd(self, activations, threshold=0.01):
12
+ """ Experimental method to get a binary mask to compare if the activation is worth ablating.
13
+ The idea is to apply the EigenCAM method by doing PCA on the activations.
14
+ Then we create a binary mask by comparing to a low threshold.
15
+ Areas that are masked out, are probably not interesting anyway.
16
+ """
17
+
18
+ projection = get_2d_projection(activations[None, :])[0, :]
19
+ projection = np.abs(projection)
20
+ projection = projection - projection.min()
21
+ projection = projection / projection.max()
22
+ projection = projection > threshold
23
+ return projection
24
+
25
+ def activations_to_be_ablated(
26
+ self,
27
+ activations,
28
+ ratio_channels_to_ablate=1.0):
29
+ """ Experimental method to get a binary mask to compare if the activation is worth ablating.
30
+ Create a binary CAM mask with objectiveness_mask_from_svd.
31
+ Score each Activation channel, by seeing how much of its values are inside the mask.
32
+ Then keep the top channels.
33
+
34
+ """
35
+ if ratio_channels_to_ablate == 1.0:
36
+ self.indices = np.int32(range(activations.shape[0]))
37
+ return self.indices
38
+
39
+ projection = self.objectiveness_mask_from_svd(activations)
40
+
41
+ scores = []
42
+ for channel in activations:
43
+ normalized = np.abs(channel)
44
+ normalized = normalized - normalized.min()
45
+ normalized = normalized / np.max(normalized)
46
+ score = (projection * normalized).sum() / normalized.sum()
47
+ scores.append(score)
48
+ scores = np.float32(scores)
49
+
50
+ indices = list(np.argsort(scores))
51
+ high_score_indices = indices[::-
52
+ 1][: int(len(indices) *
53
+ ratio_channels_to_ablate)]
54
+ low_score_indices = indices[: int(
55
+ len(indices) * ratio_channels_to_ablate)]
56
+ self.indices = np.int32(high_score_indices + low_score_indices)
57
+ return self.indices
58
+
59
+ def set_next_batch(
60
+ self,
61
+ input_batch_index,
62
+ activations,
63
+ num_channels_to_ablate):
64
+ """ This creates the next batch of activations from the layer.
65
+ Just take corresponding batch member from activations, and repeat it num_channels_to_ablate times.
66
+ """
67
+ self.activations = activations[input_batch_index, :, :, :].clone(
68
+ ).unsqueeze(0).repeat(num_channels_to_ablate, 1, 1, 1)
69
+
70
+ def __call__(self, x):
71
+ output = self.activations
72
+ for i in range(output.size(0)):
73
+ # Commonly the minimum activation will be 0,
74
+ # And then it makes sense to zero it out.
75
+ # However depending on the architecture,
76
+ # If the values can be negative, we use very negative values
77
+ # to perform the ablation, deviating from the paper.
78
+ if torch.min(output) == 0:
79
+ output[i, self.indices[i], :] = 0
80
+ else:
81
+ ABLATION_VALUE = 1e7
82
+ output[i, self.indices[i], :] = torch.min(
83
+ output) - ABLATION_VALUE
84
+
85
+ return output
86
+
87
+
88
+ class AblationLayerVit(AblationLayer):
89
+ def __init__(self):
90
+ super(AblationLayerVit, self).__init__()
91
+
92
+ def __call__(self, x):
93
+ output = self.activations
94
+ output = output.transpose(1, len(output.shape) - 1)
95
+ for i in range(output.size(0)):
96
+
97
+ # Commonly the minimum activation will be 0,
98
+ # And then it makes sense to zero it out.
99
+ # However depending on the architecture,
100
+ # If the values can be negative, we use very negative values
101
+ # to perform the ablation, deviating from the paper.
102
+ if torch.min(output) == 0:
103
+ output[i, self.indices[i], :] = 0
104
+ else:
105
+ ABLATION_VALUE = 1e7
106
+ output[i, self.indices[i], :] = torch.min(
107
+ output) - ABLATION_VALUE
108
+
109
+ output = output.transpose(len(output.shape) - 1, 1)
110
+
111
+ return output
112
+
113
+ def set_next_batch(
114
+ self,
115
+ input_batch_index,
116
+ activations,
117
+ num_channels_to_ablate):
118
+ """ This creates the next batch of activations from the layer.
119
+ Just take corresponding batch member from activations, and repeat it num_channels_to_ablate times.
120
+ """
121
+ repeat_params = [num_channels_to_ablate] + \
122
+ len(activations.shape[:-1]) * [1]
123
+ self.activations = activations[input_batch_index, :, :].clone(
124
+ ).unsqueeze(0).repeat(*repeat_params)
125
+
126
+
127
+ class AblationLayerFasterRCNN(AblationLayer):
128
+ def __init__(self):
129
+ super(AblationLayerFasterRCNN, self).__init__()
130
+
131
+ def set_next_batch(
132
+ self,
133
+ input_batch_index,
134
+ activations,
135
+ num_channels_to_ablate):
136
+ """ Extract the next batch member from activations,
137
+ and repeat it num_channels_to_ablate times.
138
+ """
139
+ self.activations = OrderedDict()
140
+ for key, value in activations.items():
141
+ fpn_activation = value[input_batch_index,
142
+ :, :, :].clone().unsqueeze(0)
143
+ self.activations[key] = fpn_activation.repeat(
144
+ num_channels_to_ablate, 1, 1, 1)
145
+
146
+ def __call__(self, x):
147
+ result = self.activations
148
+ layers = {0: '0', 1: '1', 2: '2', 3: '3', 4: 'pool'}
149
+ num_channels_to_ablate = result['pool'].size(0)
150
+ for i in range(num_channels_to_ablate):
151
+ pyramid_layer = int(self.indices[i] / 256)
152
+ index_in_pyramid_layer = int(self.indices[i] % 256)
153
+ result[layers[pyramid_layer]][i,
154
+ index_in_pyramid_layer, :, :] = -1000
155
+ return result
pytorch_grad_cam/activations_and_gradients.py ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ class ActivationsAndGradients:
2
+ """ Class for extracting activations and
3
+ registering gradients from targetted intermediate layers """
4
+
5
+ def __init__(self, model, target_layers, reshape_transform):
6
+ self.model = model
7
+ self.gradients = []
8
+ self.activations = []
9
+ self.reshape_transform = reshape_transform
10
+ self.handles = []
11
+ for target_layer in target_layers:
12
+ self.handles.append(
13
+ target_layer.register_forward_hook(self.save_activation))
14
+ # Because of https://github.com/pytorch/pytorch/issues/61519,
15
+ # we don't use backward hook to record gradients.
16
+ self.handles.append(
17
+ target_layer.register_forward_hook(self.save_gradient))
18
+
19
+ def save_activation(self, module, input, output):
20
+ activation = output
21
+
22
+ if self.reshape_transform is not None:
23
+ activation = self.reshape_transform(activation)
24
+ self.activations.append(activation.cpu().detach())
25
+
26
+ def save_gradient(self, module, input, output):
27
+ if not hasattr(output, "requires_grad") or not output.requires_grad:
28
+ # You can only register hooks on tensor requires grad.
29
+ return
30
+
31
+ # Gradients are computed in reverse order
32
+ def _store_grad(grad):
33
+ if self.reshape_transform is not None:
34
+ grad = self.reshape_transform(grad)
35
+ self.gradients = [grad.cpu().detach()] + self.gradients
36
+
37
+ output.register_hook(_store_grad)
38
+
39
+ def __call__(self, x):
40
+ self.gradients = []
41
+ self.activations = []
42
+ return self.model(x)
43
+
44
+ def release(self):
45
+ for handle in self.handles:
46
+ handle.remove()
pytorch_grad_cam/base_cam.py ADDED
@@ -0,0 +1,203 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ import ttach as tta
4
+ from typing import Callable, List, Tuple
5
+ from pytorch_grad_cam.activations_and_gradients import ActivationsAndGradients
6
+ from pytorch_grad_cam.utils.svd_on_activations import get_2d_projection
7
+ from pytorch_grad_cam.utils.image import scale_cam_image
8
+ from pytorch_grad_cam.utils.model_targets import ClassifierOutputTarget
9
+
10
+
11
+ class BaseCAM:
12
+ def __init__(self,
13
+ model: torch.nn.Module,
14
+ target_layers: List[torch.nn.Module],
15
+ use_cuda: bool = False,
16
+ reshape_transform: Callable = None,
17
+ compute_input_gradient: bool = False,
18
+ uses_gradients: bool = True) -> None:
19
+ self.model = model.eval()
20
+ self.target_layers = target_layers
21
+ self.cuda = use_cuda
22
+ if self.cuda:
23
+ self.model = model.cuda()
24
+ self.reshape_transform = reshape_transform
25
+ self.compute_input_gradient = compute_input_gradient
26
+ self.uses_gradients = uses_gradients
27
+ self.activations_and_grads = ActivationsAndGradients(
28
+ self.model, target_layers, reshape_transform)
29
+
30
+ """ Get a vector of weights for every channel in the target layer.
31
+ Methods that return weights channels,
32
+ will typically need to only implement this function. """
33
+
34
+ def get_cam_weights(self,
35
+ input_tensor: torch.Tensor,
36
+ target_layers: List[torch.nn.Module],
37
+ targets: List[torch.nn.Module],
38
+ activations: torch.Tensor,
39
+ grads: torch.Tensor) -> np.ndarray:
40
+ raise Exception("Not Implemented")
41
+
42
+ def get_cam_image(self,
43
+ input_tensor: torch.Tensor,
44
+ target_layer: torch.nn.Module,
45
+ targets: List[torch.nn.Module],
46
+ activations: torch.Tensor,
47
+ grads: torch.Tensor,
48
+ eigen_smooth: bool = False) -> np.ndarray:
49
+
50
+ weights = self.get_cam_weights(input_tensor,
51
+ target_layer,
52
+ targets,
53
+ activations,
54
+ grads)
55
+ weighted_activations = weights[:, :, None, None] * activations
56
+ if eigen_smooth:
57
+ cam = get_2d_projection(weighted_activations)
58
+ else:
59
+ cam = weighted_activations.sum(axis=1)
60
+ return cam
61
+
62
+ def forward(self,
63
+ input_tensor: torch.Tensor,
64
+ targets: List[torch.nn.Module],
65
+ eigen_smooth: bool = False) -> np.ndarray:
66
+
67
+ if self.cuda:
68
+ input_tensor = input_tensor.cuda()
69
+
70
+ if self.compute_input_gradient:
71
+ input_tensor = torch.autograd.Variable(input_tensor,
72
+ requires_grad=True)
73
+
74
+ outputs = self.activations_and_grads(input_tensor)
75
+ if targets is None:
76
+ target_categories = np.argmax(outputs.cpu().data.numpy(), axis=-1)
77
+ targets = [ClassifierOutputTarget(
78
+ category) for category in target_categories]
79
+
80
+ if self.uses_gradients:
81
+ self.model.zero_grad()
82
+ loss = sum([target(output)
83
+ for target, output in zip(targets, outputs)])
84
+ loss.backward(retain_graph=True)
85
+
86
+ # In most of the saliency attribution papers, the saliency is
87
+ # computed with a single target layer.
88
+ # Commonly it is the last convolutional layer.
89
+ # Here we support passing a list with multiple target layers.
90
+ # It will compute the saliency image for every image,
91
+ # and then aggregate them (with a default mean aggregation).
92
+ # This gives you more flexibility in case you just want to
93
+ # use all conv layers for example, all Batchnorm layers,
94
+ # or something else.
95
+ cam_per_layer = self.compute_cam_per_layer(input_tensor,
96
+ targets,
97
+ eigen_smooth)
98
+ return self.aggregate_multi_layers(cam_per_layer)
99
+
100
+ def get_target_width_height(self,
101
+ input_tensor: torch.Tensor) -> Tuple[int, int]:
102
+ width, height = input_tensor.size(-1), input_tensor.size(-2)
103
+ return width, height
104
+
105
+ def compute_cam_per_layer(
106
+ self,
107
+ input_tensor: torch.Tensor,
108
+ targets: List[torch.nn.Module],
109
+ eigen_smooth: bool) -> np.ndarray:
110
+ activations_list = [a.cpu().data.numpy()
111
+ for a in self.activations_and_grads.activations]
112
+ grads_list = [g.cpu().data.numpy()
113
+ for g in self.activations_and_grads.gradients]
114
+ target_size = self.get_target_width_height(input_tensor)
115
+
116
+ cam_per_target_layer = []
117
+ # Loop over the saliency image from every layer
118
+ for i in range(len(self.target_layers)):
119
+ target_layer = self.target_layers[i]
120
+ layer_activations = None
121
+ layer_grads = None
122
+ if i < len(activations_list):
123
+ layer_activations = activations_list[i]
124
+ if i < len(grads_list):
125
+ layer_grads = grads_list[i]
126
+
127
+ cam = self.get_cam_image(input_tensor,
128
+ target_layer,
129
+ targets,
130
+ layer_activations,
131
+ layer_grads,
132
+ eigen_smooth)
133
+ cam = np.maximum(cam, 0)
134
+ scaled = scale_cam_image(cam, target_size)
135
+ cam_per_target_layer.append(scaled[:, None, :])
136
+
137
+ return cam_per_target_layer
138
+
139
+ def aggregate_multi_layers(
140
+ self,
141
+ cam_per_target_layer: np.ndarray) -> np.ndarray:
142
+ cam_per_target_layer = np.concatenate(cam_per_target_layer, axis=1)
143
+ cam_per_target_layer = np.maximum(cam_per_target_layer, 0)
144
+ result = np.mean(cam_per_target_layer, axis=1)
145
+ return scale_cam_image(result)
146
+
147
+ def forward_augmentation_smoothing(self,
148
+ input_tensor: torch.Tensor,
149
+ targets: List[torch.nn.Module],
150
+ eigen_smooth: bool = False) -> np.ndarray:
151
+ transforms = tta.Compose(
152
+ [
153
+ tta.HorizontalFlip(),
154
+ tta.Multiply(factors=[0.9, 1, 1.1]),
155
+ ]
156
+ )
157
+ cams = []
158
+ for transform in transforms:
159
+ augmented_tensor = transform.augment_image(input_tensor)
160
+ cam = self.forward(augmented_tensor,
161
+ targets,
162
+ eigen_smooth)
163
+
164
+ # The ttach library expects a tensor of size BxCxHxW
165
+ cam = cam[:, None, :, :]
166
+ cam = torch.from_numpy(cam)
167
+ cam = transform.deaugment_mask(cam)
168
+
169
+ # Back to numpy float32, HxW
170
+ cam = cam.numpy()
171
+ cam = cam[:, 0, :, :]
172
+ cams.append(cam)
173
+
174
+ cam = np.mean(np.float32(cams), axis=0)
175
+ return cam
176
+
177
+ def __call__(self,
178
+ input_tensor: torch.Tensor,
179
+ targets: List[torch.nn.Module] = None,
180
+ aug_smooth: bool = False,
181
+ eigen_smooth: bool = False) -> np.ndarray:
182
+
183
+ # Smooth the CAM result with test time augmentation
184
+ if aug_smooth is True:
185
+ return self.forward_augmentation_smoothing(
186
+ input_tensor, targets, eigen_smooth)
187
+
188
+ return self.forward(input_tensor,
189
+ targets, eigen_smooth)
190
+
191
+ def __del__(self):
192
+ self.activations_and_grads.release()
193
+
194
+ def __enter__(self):
195
+ return self
196
+
197
+ def __exit__(self, exc_type, exc_value, exc_tb):
198
+ self.activations_and_grads.release()
199
+ if isinstance(exc_value, IndexError):
200
+ # Handle IndexError here...
201
+ print(
202
+ f"An exception occurred in CAM with block: {exc_type}. Message: {exc_value}")
203
+ return True
pytorch_grad_cam/eigen_cam.py ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pytorch_grad_cam.base_cam import BaseCAM
2
+ from pytorch_grad_cam.utils.svd_on_activations import get_2d_projection
3
+
4
+ # https://arxiv.org/abs/2008.00299
5
+
6
+
7
+ class EigenCAM(BaseCAM):
8
+ def __init__(self, model, target_layers, use_cuda=False,
9
+ reshape_transform=None):
10
+ super(EigenCAM, self).__init__(model,
11
+ target_layers,
12
+ use_cuda,
13
+ reshape_transform,
14
+ uses_gradients=False)
15
+
16
+ def get_cam_image(self,
17
+ input_tensor,
18
+ target_layer,
19
+ target_category,
20
+ activations,
21
+ grads,
22
+ eigen_smooth):
23
+ return get_2d_projection(activations)
pytorch_grad_cam/eigen_grad_cam.py ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pytorch_grad_cam.base_cam import BaseCAM
2
+ from pytorch_grad_cam.utils.svd_on_activations import get_2d_projection
3
+
4
+ # Like Eigen CAM: https://arxiv.org/abs/2008.00299
5
+ # But multiply the activations x gradients
6
+
7
+
8
+ class EigenGradCAM(BaseCAM):
9
+ def __init__(self, model, target_layers, use_cuda=False,
10
+ reshape_transform=None):
11
+ super(EigenGradCAM, self).__init__(model, target_layers, use_cuda,
12
+ reshape_transform)
13
+
14
+ def get_cam_image(self,
15
+ input_tensor,
16
+ target_layer,
17
+ target_category,
18
+ activations,
19
+ grads,
20
+ eigen_smooth):
21
+ return get_2d_projection(grads * activations)
pytorch_grad_cam/feature_factorization/__init__.py ADDED
File without changes
pytorch_grad_cam/feature_factorization/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (194 Bytes). View file
 
pytorch_grad_cam/feature_factorization/__pycache__/deep_feature_factorization.cpython-310.pyc ADDED
Binary file (4.82 kB). View file
 
pytorch_grad_cam/feature_factorization/deep_feature_factorization.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from PIL import Image
3
+ import torch
4
+ from typing import Callable, List, Tuple, Optional
5
+ from sklearn.decomposition import NMF
6
+ from pytorch_grad_cam.activations_and_gradients import ActivationsAndGradients
7
+ from pytorch_grad_cam.utils.image import scale_cam_image, create_labels_legend, show_factorization_on_image
8
+
9
+
10
+ def dff(activations: np.ndarray, n_components: int = 5):
11
+ """ Compute Deep Feature Factorization on a 2d Activations tensor.
12
+
13
+ :param activations: A numpy array of shape batch x channels x height x width
14
+ :param n_components: The number of components for the non negative matrix factorization
15
+ :returns: A tuple of the concepts (a numpy array with shape channels x components),
16
+ and the explanation heatmaps (a numpy arary with shape batch x height x width)
17
+ """
18
+
19
+ batch_size, channels, h, w = activations.shape
20
+ reshaped_activations = activations.transpose((1, 0, 2, 3))
21
+ reshaped_activations[np.isnan(reshaped_activations)] = 0
22
+ reshaped_activations = reshaped_activations.reshape(
23
+ reshaped_activations.shape[0], -1)
24
+ offset = reshaped_activations.min(axis=-1)
25
+ reshaped_activations = reshaped_activations - offset[:, None]
26
+
27
+ model = NMF(n_components=n_components, init='random', random_state=0)
28
+ W = model.fit_transform(reshaped_activations)
29
+ H = model.components_
30
+ concepts = W + offset[:, None]
31
+ explanations = H.reshape(n_components, batch_size, h, w)
32
+ explanations = explanations.transpose((1, 0, 2, 3))
33
+ return concepts, explanations
34
+
35
+
36
+ class DeepFeatureFactorization:
37
+ """ Deep Feature Factorization: https://arxiv.org/abs/1806.10206
38
+ This gets a model andcomputes the 2D activations for a target layer,
39
+ and computes Non Negative Matrix Factorization on the activations.
40
+
41
+ Optionally it runs a computation on the concept embeddings,
42
+ like running a classifier on them.
43
+
44
+ The explanation heatmaps are scalled to the range [0, 1]
45
+ and to the input tensor width and height.
46
+ """
47
+
48
+ def __init__(self,
49
+ model: torch.nn.Module,
50
+ target_layer: torch.nn.Module,
51
+ reshape_transform: Callable = None,
52
+ computation_on_concepts=None
53
+ ):
54
+ self.model = model
55
+ self.computation_on_concepts = computation_on_concepts
56
+ self.activations_and_grads = ActivationsAndGradients(
57
+ self.model, [target_layer], reshape_transform)
58
+
59
+ def __call__(self,
60
+ input_tensor: torch.Tensor,
61
+ n_components: int = 16):
62
+ batch_size, channels, h, w = input_tensor.size()
63
+ _ = self.activations_and_grads(input_tensor)
64
+
65
+ with torch.no_grad():
66
+ activations = self.activations_and_grads.activations[0].cpu(
67
+ ).numpy()
68
+
69
+ concepts, explanations = dff(activations, n_components=n_components)
70
+
71
+ processed_explanations = []
72
+
73
+ for batch in explanations:
74
+ processed_explanations.append(scale_cam_image(batch, (w, h)))
75
+
76
+ if self.computation_on_concepts:
77
+ with torch.no_grad():
78
+ concept_tensors = torch.from_numpy(
79
+ np.float32(concepts).transpose((1, 0)))
80
+ concept_outputs = self.computation_on_concepts(
81
+ concept_tensors).cpu().numpy()
82
+ return concepts, processed_explanations, concept_outputs
83
+ else:
84
+ return concepts, processed_explanations
85
+
86
+ def __del__(self):
87
+ self.activations_and_grads.release()
88
+
89
+ def __exit__(self, exc_type, exc_value, exc_tb):
90
+ self.activations_and_grads.release()
91
+ if isinstance(exc_value, IndexError):
92
+ # Handle IndexError here...
93
+ print(
94
+ f"An exception occurred in ActivationSummary with block: {exc_type}. Message: {exc_value}")
95
+ return True
96
+
97
+
98
+ def run_dff_on_image(model: torch.nn.Module,
99
+ target_layer: torch.nn.Module,
100
+ classifier: torch.nn.Module,
101
+ img_pil: Image,
102
+ img_tensor: torch.Tensor,
103
+ reshape_transform=Optional[Callable],
104
+ n_components: int = 5,
105
+ top_k: int = 2) -> np.ndarray:
106
+ """ Helper function to create a Deep Feature Factorization visualization for a single image.
107
+ TBD: Run this on a batch with several images.
108
+ """
109
+ rgb_img_float = np.array(img_pil) / 255
110
+ dff = DeepFeatureFactorization(model=model,
111
+ reshape_transform=reshape_transform,
112
+ target_layer=target_layer,
113
+ computation_on_concepts=classifier)
114
+
115
+ concepts, batch_explanations, concept_outputs = dff(
116
+ img_tensor[None, :], n_components)
117
+
118
+ concept_outputs = torch.softmax(
119
+ torch.from_numpy(concept_outputs),
120
+ axis=-1).numpy()
121
+ concept_label_strings = create_labels_legend(concept_outputs,
122
+ labels=model.config.id2label,
123
+ top_k=top_k)
124
+ visualization = show_factorization_on_image(
125
+ rgb_img_float,
126
+ batch_explanations[0],
127
+ image_weight=0.3,
128
+ concept_labels=concept_label_strings)
129
+
130
+ result = np.hstack((np.array(img_pil), visualization))
131
+ return result
pytorch_grad_cam/fullgrad_cam.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ from pytorch_grad_cam.base_cam import BaseCAM
4
+ from pytorch_grad_cam.utils.find_layers import find_layer_predicate_recursive
5
+ from pytorch_grad_cam.utils.svd_on_activations import get_2d_projection
6
+ from pytorch_grad_cam.utils.image import scale_accross_batch_and_channels, scale_cam_image
7
+
8
+ # https://arxiv.org/abs/1905.00780
9
+
10
+
11
+ class FullGrad(BaseCAM):
12
+ def __init__(self, model, target_layers, use_cuda=False,
13
+ reshape_transform=None):
14
+ if len(target_layers) > 0:
15
+ print(
16
+ "Warning: target_layers is ignored in FullGrad. All bias layers will be used instead")
17
+
18
+ def layer_with_2D_bias(layer):
19
+ bias_target_layers = [torch.nn.Conv2d, torch.nn.BatchNorm2d]
20
+ if type(layer) in bias_target_layers and layer.bias is not None:
21
+ return True
22
+ return False
23
+ target_layers = find_layer_predicate_recursive(
24
+ model, layer_with_2D_bias)
25
+ super(
26
+ FullGrad,
27
+ self).__init__(
28
+ model,
29
+ target_layers,
30
+ use_cuda,
31
+ reshape_transform,
32
+ compute_input_gradient=True)
33
+ self.bias_data = [self.get_bias_data(
34
+ layer).cpu().numpy() for layer in target_layers]
35
+
36
+ def get_bias_data(self, layer):
37
+ # Borrowed from official paper impl:
38
+ # https://github.com/idiap/fullgrad-saliency/blob/master/saliency/tensor_extractor.py#L47
39
+ if isinstance(layer, torch.nn.BatchNorm2d):
40
+ bias = - (layer.running_mean * layer.weight
41
+ / torch.sqrt(layer.running_var + layer.eps)) + layer.bias
42
+ return bias.data
43
+ else:
44
+ return layer.bias.data
45
+
46
+ def compute_cam_per_layer(
47
+ self,
48
+ input_tensor,
49
+ target_category,
50
+ eigen_smooth):
51
+ input_grad = input_tensor.grad.data.cpu().numpy()
52
+ grads_list = [g.cpu().data.numpy() for g in
53
+ self.activations_and_grads.gradients]
54
+ cam_per_target_layer = []
55
+ target_size = self.get_target_width_height(input_tensor)
56
+
57
+ gradient_multiplied_input = input_grad * input_tensor.data.cpu().numpy()
58
+ gradient_multiplied_input = np.abs(gradient_multiplied_input)
59
+ gradient_multiplied_input = scale_accross_batch_and_channels(
60
+ gradient_multiplied_input,
61
+ target_size)
62
+ cam_per_target_layer.append(gradient_multiplied_input)
63
+
64
+ # Loop over the saliency image from every layer
65
+ assert(len(self.bias_data) == len(grads_list))
66
+ for bias, grads in zip(self.bias_data, grads_list):
67
+ bias = bias[None, :, None, None]
68
+ # In the paper they take the absolute value,
69
+ # but possibily taking only the positive gradients will work
70
+ # better.
71
+ bias_grad = np.abs(bias * grads)
72
+ result = scale_accross_batch_and_channels(
73
+ bias_grad, target_size)
74
+ result = np.sum(result, axis=1)
75
+ cam_per_target_layer.append(result[:, None, :])
76
+ cam_per_target_layer = np.concatenate(cam_per_target_layer, axis=1)
77
+ if eigen_smooth:
78
+ # Resize to a smaller image, since this method typically has a very large number of channels,
79
+ # and then consumes a lot of memory
80
+ cam_per_target_layer = scale_accross_batch_and_channels(
81
+ cam_per_target_layer, (target_size[0] // 8, target_size[1] // 8))
82
+ cam_per_target_layer = get_2d_projection(cam_per_target_layer)
83
+ cam_per_target_layer = cam_per_target_layer[:, None, :, :]
84
+ cam_per_target_layer = scale_accross_batch_and_channels(
85
+ cam_per_target_layer,
86
+ target_size)
87
+ else:
88
+ cam_per_target_layer = np.sum(
89
+ cam_per_target_layer, axis=1)[:, None, :]
90
+
91
+ return cam_per_target_layer
92
+
93
+ def aggregate_multi_layers(self, cam_per_target_layer):
94
+ result = np.sum(cam_per_target_layer, axis=1)
95
+ return scale_cam_image(result)
pytorch_grad_cam/grad_cam.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from pytorch_grad_cam.base_cam import BaseCAM
3
+
4
+
5
+ class GradCAM(BaseCAM):
6
+ def __init__(self, model, target_layers, use_cuda=False,
7
+ reshape_transform=None):
8
+ super(
9
+ GradCAM,
10
+ self).__init__(
11
+ model,
12
+ target_layers,
13
+ use_cuda,
14
+ reshape_transform)
15
+
16
+ def get_cam_weights(self,
17
+ input_tensor,
18
+ target_layer,
19
+ target_category,
20
+ activations,
21
+ grads):
22
+ return np.mean(grads, axis=(2, 3))
pytorch_grad_cam/grad_cam_elementwise.py ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from pytorch_grad_cam.base_cam import BaseCAM
3
+ from pytorch_grad_cam.utils.svd_on_activations import get_2d_projection
4
+
5
+
6
+ class GradCAMElementWise(BaseCAM):
7
+ def __init__(self, model, target_layers, use_cuda=False,
8
+ reshape_transform=None):
9
+ super(
10
+ GradCAMElementWise,
11
+ self).__init__(
12
+ model,
13
+ target_layers,
14
+ use_cuda,
15
+ reshape_transform)
16
+
17
+ def get_cam_image(self,
18
+ input_tensor,
19
+ target_layer,
20
+ target_category,
21
+ activations,
22
+ grads,
23
+ eigen_smooth):
24
+ elementwise_activations = np.maximum(grads * activations, 0)
25
+
26
+ if eigen_smooth:
27
+ cam = get_2d_projection(elementwise_activations)
28
+ else:
29
+ cam = elementwise_activations.sum(axis=1)
30
+ return cam
pytorch_grad_cam/grad_cam_plusplus.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from pytorch_grad_cam.base_cam import BaseCAM
3
+
4
+ # https://arxiv.org/abs/1710.11063
5
+
6
+
7
+ class GradCAMPlusPlus(BaseCAM):
8
+ def __init__(self, model, target_layers, use_cuda=False,
9
+ reshape_transform=None):
10
+ super(GradCAMPlusPlus, self).__init__(model, target_layers, use_cuda,
11
+ reshape_transform)
12
+
13
+ def get_cam_weights(self,
14
+ input_tensor,
15
+ target_layers,
16
+ target_category,
17
+ activations,
18
+ grads):
19
+ grads_power_2 = grads**2
20
+ grads_power_3 = grads_power_2 * grads
21
+ # Equation 19 in https://arxiv.org/abs/1710.11063
22
+ sum_activations = np.sum(activations, axis=(2, 3))
23
+ eps = 0.000001
24
+ aij = grads_power_2 / (2 * grads_power_2 +
25
+ sum_activations[:, :, None, None] * grads_power_3 + eps)
26
+ # Now bring back the ReLU from eq.7 in the paper,
27
+ # And zero out aijs where the activations are 0
28
+ aij = np.where(grads != 0, aij, 0)
29
+
30
+ weights = np.maximum(grads, 0) * aij
31
+ weights = np.sum(weights, axis=(2, 3))
32
+ return weights
pytorch_grad_cam/guided_backprop.py ADDED
@@ -0,0 +1,100 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import torch
3
+ from torch.autograd import Function
4
+ from pytorch_grad_cam.utils.find_layers import replace_all_layer_type_recursive
5
+
6
+
7
+ class GuidedBackpropReLU(Function):
8
+ @staticmethod
9
+ def forward(self, input_img):
10
+ positive_mask = (input_img > 0).type_as(input_img)
11
+ output = torch.addcmul(
12
+ torch.zeros(
13
+ input_img.size()).type_as(input_img),
14
+ input_img,
15
+ positive_mask)
16
+ self.save_for_backward(input_img, output)
17
+ return output
18
+
19
+ @staticmethod
20
+ def backward(self, grad_output):
21
+ input_img, output = self.saved_tensors
22
+ grad_input = None
23
+
24
+ positive_mask_1 = (input_img > 0).type_as(grad_output)
25
+ positive_mask_2 = (grad_output > 0).type_as(grad_output)
26
+ grad_input = torch.addcmul(
27
+ torch.zeros(
28
+ input_img.size()).type_as(input_img),
29
+ torch.addcmul(
30
+ torch.zeros(
31
+ input_img.size()).type_as(input_img),
32
+ grad_output,
33
+ positive_mask_1),
34
+ positive_mask_2)
35
+ return grad_input
36
+
37
+
38
+ class GuidedBackpropReLUasModule(torch.nn.Module):
39
+ def __init__(self):
40
+ super(GuidedBackpropReLUasModule, self).__init__()
41
+
42
+ def forward(self, input_img):
43
+ return GuidedBackpropReLU.apply(input_img)
44
+
45
+
46
+ class GuidedBackpropReLUModel:
47
+ def __init__(self, model, use_cuda):
48
+ self.model = model
49
+ self.model.eval()
50
+ self.cuda = use_cuda
51
+ if self.cuda:
52
+ self.model = self.model.cuda()
53
+
54
+ def forward(self, input_img):
55
+ return self.model(input_img)
56
+
57
+ def recursive_replace_relu_with_guidedrelu(self, module_top):
58
+
59
+ for idx, module in module_top._modules.items():
60
+ self.recursive_replace_relu_with_guidedrelu(module)
61
+ if module.__class__.__name__ == 'ReLU':
62
+ module_top._modules[idx] = GuidedBackpropReLU.apply
63
+ print("b")
64
+
65
+ def recursive_replace_guidedrelu_with_relu(self, module_top):
66
+ try:
67
+ for idx, module in module_top._modules.items():
68
+ self.recursive_replace_guidedrelu_with_relu(module)
69
+ if module == GuidedBackpropReLU.apply:
70
+ module_top._modules[idx] = torch.nn.ReLU()
71
+ except BaseException:
72
+ pass
73
+
74
+ def __call__(self, input_img, target_category=None):
75
+ replace_all_layer_type_recursive(self.model,
76
+ torch.nn.ReLU,
77
+ GuidedBackpropReLUasModule())
78
+
79
+ if self.cuda:
80
+ input_img = input_img.cuda()
81
+
82
+ input_img = input_img.requires_grad_(True)
83
+
84
+ output = self.forward(input_img)
85
+
86
+ if target_category is None:
87
+ target_category = np.argmax(output.cpu().data.numpy())
88
+
89
+ loss = output[0, target_category]
90
+ loss.backward(retain_graph=True)
91
+
92
+ output = input_img.grad.cpu().data.numpy()
93
+ output = output[0, :, :, :]
94
+ output = output.transpose((1, 2, 0))
95
+
96
+ replace_all_layer_type_recursive(self.model,
97
+ GuidedBackpropReLUasModule,
98
+ torch.nn.ReLU())
99
+
100
+ return output
pytorch_grad_cam/hirescam.py ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from pytorch_grad_cam.base_cam import BaseCAM
3
+ from pytorch_grad_cam.utils.svd_on_activations import get_2d_projection
4
+
5
+
6
+ class HiResCAM(BaseCAM):
7
+ def __init__(self, model, target_layers, use_cuda=False,
8
+ reshape_transform=None):
9
+ super(
10
+ HiResCAM,
11
+ self).__init__(
12
+ model,
13
+ target_layers,
14
+ use_cuda,
15
+ reshape_transform)
16
+
17
+ def get_cam_image(self,
18
+ input_tensor,
19
+ target_layer,
20
+ target_category,
21
+ activations,
22
+ grads,
23
+ eigen_smooth):
24
+ elementwise_activations = grads * activations
25
+
26
+ if eigen_smooth:
27
+ print(
28
+ "Warning: HiResCAM's faithfulness guarantees do not hold if smoothing is applied")
29
+ cam = get_2d_projection(elementwise_activations)
30
+ else:
31
+ cam = elementwise_activations.sum(axis=1)
32
+ return cam
pytorch_grad_cam/layer_cam.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from pytorch_grad_cam.base_cam import BaseCAM
3
+ from pytorch_grad_cam.utils.svd_on_activations import get_2d_projection
4
+
5
+ # https://ieeexplore.ieee.org/document/9462463
6
+
7
+
8
+ class LayerCAM(BaseCAM):
9
+ def __init__(
10
+ self,
11
+ model,
12
+ target_layers,
13
+ use_cuda=False,
14
+ reshape_transform=None):
15
+ super(
16
+ LayerCAM,
17
+ self).__init__(
18
+ model,
19
+ target_layers,
20
+ use_cuda,
21
+ reshape_transform)
22
+
23
+ def get_cam_image(self,
24
+ input_tensor,
25
+ target_layer,
26
+ target_category,
27
+ activations,
28
+ grads,
29
+ eigen_smooth):
30
+ spatial_weighted_activations = np.maximum(grads, 0) * activations
31
+
32
+ if eigen_smooth:
33
+ cam = get_2d_projection(spatial_weighted_activations)
34
+ else:
35
+ cam = spatial_weighted_activations.sum(axis=1)
36
+ return cam
pytorch_grad_cam/metrics/__init__.py ADDED
File without changes
pytorch_grad_cam/metrics/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (180 Bytes). View file
 
pytorch_grad_cam/metrics/__pycache__/cam_mult_image.cpython-310.pyc ADDED
Binary file (1.97 kB). View file
 
pytorch_grad_cam/metrics/__pycache__/perturbation_confidence.cpython-310.pyc ADDED
Binary file (3.81 kB). View file
 
pytorch_grad_cam/metrics/__pycache__/road.cpython-310.pyc ADDED
Binary file (5.71 kB). View file
 
pytorch_grad_cam/metrics/cam_mult_image.py ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+ from typing import List, Callable
4
+ from pytorch_grad_cam.metrics.perturbation_confidence import PerturbationConfidenceMetric
5
+
6
+
7
+ def multiply_tensor_with_cam(input_tensor: torch.Tensor,
8
+ cam: torch.Tensor):
9
+ """ Multiply an input tensor (after normalization)
10
+ with a pixel attribution map
11
+ """
12
+ return input_tensor * cam
13
+
14
+
15
+ class CamMultImageConfidenceChange(PerturbationConfidenceMetric):
16
+ def __init__(self):
17
+ super(CamMultImageConfidenceChange,
18
+ self).__init__(multiply_tensor_with_cam)
19
+
20
+
21
+ class DropInConfidence(CamMultImageConfidenceChange):
22
+ def __init__(self):
23
+ super(DropInConfidence, self).__init__()
24
+
25
+ def __call__(self, *args, **kwargs):
26
+ scores = super(DropInConfidence, self).__call__(*args, **kwargs)
27
+ scores = -scores
28
+ return np.maximum(scores, 0)
29
+
30
+
31
+ class IncreaseInConfidence(CamMultImageConfidenceChange):
32
+ def __init__(self):
33
+ super(IncreaseInConfidence, self).__init__()
34
+
35
+ def __call__(self, *args, **kwargs):
36
+ scores = super(IncreaseInConfidence, self).__call__(*args, **kwargs)
37
+ return np.float32(scores > 0)
pytorch_grad_cam/metrics/perturbation_confidence.py ADDED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import numpy as np
3
+ from typing import List, Callable
4
+
5
+ import numpy as np
6
+ import cv2
7
+
8
+
9
+ class PerturbationConfidenceMetric:
10
+ def __init__(self, perturbation):
11
+ self.perturbation = perturbation
12
+
13
+ def __call__(self, input_tensor: torch.Tensor,
14
+ cams: np.ndarray,
15
+ targets: List[Callable],
16
+ model: torch.nn.Module,
17
+ return_visualization=False,
18
+ return_diff=True):
19
+
20
+ if return_diff:
21
+ with torch.no_grad():
22
+ outputs = model(input_tensor)
23
+ scores = [target(output).cpu().numpy()
24
+ for target, output in zip(targets, outputs)]
25
+ scores = np.float32(scores)
26
+
27
+ batch_size = input_tensor.size(0)
28
+ perturbated_tensors = []
29
+ for i in range(batch_size):
30
+ cam = cams[i]
31
+ tensor = self.perturbation(input_tensor[i, ...].cpu(),
32
+ torch.from_numpy(cam))
33
+ tensor = tensor.to(input_tensor.device)
34
+ perturbated_tensors.append(tensor.unsqueeze(0))
35
+ perturbated_tensors = torch.cat(perturbated_tensors)
36
+
37
+ with torch.no_grad():
38
+ outputs_after_imputation = model(perturbated_tensors)
39
+ scores_after_imputation = [
40
+ target(output).cpu().numpy() for target, output in zip(
41
+ targets, outputs_after_imputation)]
42
+ scores_after_imputation = np.float32(scores_after_imputation)
43
+
44
+ if return_diff:
45
+ result = scores_after_imputation - scores
46
+ else:
47
+ result = scores_after_imputation
48
+
49
+ if return_visualization:
50
+ return result, perturbated_tensors
51
+ else:
52
+ return result
53
+
54
+
55
+ class RemoveMostRelevantFirst:
56
+ def __init__(self, percentile, imputer):
57
+ self.percentile = percentile
58
+ self.imputer = imputer
59
+
60
+ def __call__(self, input_tensor, mask):
61
+ imputer = self.imputer
62
+ if self.percentile != 'auto':
63
+ threshold = np.percentile(mask.cpu().numpy(), self.percentile)
64
+ binary_mask = np.float32(mask < threshold)
65
+ else:
66
+ _, binary_mask = cv2.threshold(
67
+ np.uint8(mask * 255), 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
68
+
69
+ binary_mask = torch.from_numpy(binary_mask)
70
+ binary_mask = binary_mask.to(mask.device)
71
+ return imputer(input_tensor, binary_mask)
72
+
73
+
74
+ class RemoveLeastRelevantFirst(RemoveMostRelevantFirst):
75
+ def __init__(self, percentile, imputer):
76
+ super(RemoveLeastRelevantFirst, self).__init__(percentile, imputer)
77
+
78
+ def __call__(self, input_tensor, mask):
79
+ return super(RemoveLeastRelevantFirst, self).__call__(
80
+ input_tensor, 1 - mask)
81
+
82
+
83
+ class AveragerAcrossThresholds:
84
+ def __init__(
85
+ self,
86
+ imputer,
87
+ percentiles=[
88
+ 10,
89
+ 20,
90
+ 30,
91
+ 40,
92
+ 50,
93
+ 60,
94
+ 70,
95
+ 80,
96
+ 90]):
97
+ self.imputer = imputer
98
+ self.percentiles = percentiles
99
+
100
+ def __call__(self,
101
+ input_tensor: torch.Tensor,
102
+ cams: np.ndarray,
103
+ targets: List[Callable],
104
+ model: torch.nn.Module):
105
+ scores = []
106
+ for percentile in self.percentiles:
107
+ imputer = self.imputer(percentile)
108
+ scores.append(imputer(input_tensor, cams, targets, model))
109
+ return np.mean(np.float32(scores), axis=0)