-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompute_contrib.py
150 lines (104 loc) · 4.55 KB
/
compute_contrib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'''
Descripttion:
version:
Author: QIU Yaowen
Date: 2022-04-21 22:48:08
LastEditors: Andy
LastEditTime: 2022-05-08 12:19:36
'''
import warnings
warnings.filterwarnings("ignore")
import tensorflow as tf
from modified_gradcam import *
import config
import json
import pandas as pd
import shutil
import os
os.environ["CUDA_VISIBLE_DEVICES"] = "2"
imagenet_path = config.train
model = tf.keras.models.load_model("model/SuperClass.h5")
model.compile(loss='categorical_crossentropy', metrics=['acc'])
grad_model = tf.keras.models.Model(
[model.inputs], [model.get_layer("conv5_block3_out").output, model.output]
)
w = model._layers[-1].weights[0].numpy()
last_model = tf.keras.models.Model(
[model.inputs], [model.get_layer("avg_pool").output]
)
# Class json file
class_labels = json.load(open('CWOX/imagenet_class_index.json', 'r'))
# folder : nums_label
folder_to_nums = {class_labels[key][0]: key for key in class_labels.keys()}
#######Process############
def process(x):
# print(x.shape)
i = tf.cast(x, dtype = tf.uint8)
x = tf.cast(i, tf.float32)
x = tf.keras.applications.resnet50.preprocess_input(x)
x = tf.expand_dims(x,axis = 0)
return x
def make_gradcam_heatmap(img_array, grad_model, pred_index):
# First, we create a model that maps the input image to the activations
# of the last conv layer as well as the output predictions
# Then, we compute the gradient of the top predicted class for our input image
# with respect to the activations of the last conv layer
with tf.GradientTape() as tape:
last_conv_layer_output, preds = grad_model(img_array)
# if pred_index is None:
# pred_index = tf.argmax(preds[0])
class_channel = preds[:, pred_index]
# This is the gradient of the output neuron (top predicted or chosen)
# with regard to the output feature map of the last conv layer
grads = tape.gradient(class_channel, last_conv_layer_output)
# This is a vector where each entry is the mean intensity of the gradient
# over a specific feature map channel
pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
# We multiply each channel in the feature map array
# by "how important this channel is" with regard to the top predicted class
# then sum all the channels to obtain the heatmap class activation
last_conv_layer_output = last_conv_layer_output[0]
heatmap = last_conv_layer_output @ pooled_grads[..., tf.newaxis]
heatmap = tf.squeeze(heatmap)
# For visualization purpose, we will also normalize the heatmap between 0 & 1
heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)
return heatmap.numpy()
def save_and_display_gradcam(img_path, heatmap, cam_path):
# plt.figure(figsize=[12,8],dpi=200)
# Load the original image
img = tf.keras.preprocessing.image.load_img(img_path,target_size=(224,224))
img = tf.keras.preprocessing.image.img_to_array(img)
# Rescale heatmap to a range 0-255
heatmap = np.uint8(255 * heatmap)
# Use jet colormap to colorize heatmap
jet = cm.get_cmap("jet")
# Use RGB values of the colormap
jet_colors = jet(np.arange(256))[:, :3]
jet_heatmap = jet_colors[heatmap]
# Create an image with RGB colorized heatmap
jet_heatmap = tf.keras.preprocessing.image.array_to_img(jet_heatmap)
jet_heatmap = jet_heatmap.resize((img.shape[1], img.shape[0]))
jet_heatmap = tf.keras.preprocessing.image.img_to_array(jet_heatmap)
jet_heatmap = jet_heatmap / 255
threshold = np.max(jet_heatmap) * config.delta1_frac
img[jet_heatmap[:,:,0] < threshold] = 0
return img
def transform_gradcam(img_path,processed_img_array, grad_model, pred_index,cam_path):
heatmap = make_gradcam_heatmap(processed_img_array, grad_model,pred_index)
return save_and_display_gradcam(img_path, heatmap,cam_path)
Contribution_Matrix = np.zeros((2048,2))
N = len(os.listdir("/4tssd/imagenet/superclass/car/"))
for img in os.listdir("/4tssd/imagenet/superclass/car/"):
img_path = "/4tssd/imagenet/superclass/car/"+img
try:
img = tf.keras.preprocessing.image.load_img(img_path, target_size=(224,224))
except:
continue
img_array = tf.keras.preprocessing.image.img_to_array(img)
processed_array = process(img_array)
purified_img_array = transform_gradcam(img_path,processed_array,grad_model,0,None)
h_prime = last_model(process(purified_img_array)).numpy().T
contribution = np.multiply(h_prime,w)
Contribution_Matrix += contribution
Contribution_Matrix /= N
np.save("model/contribution_car.npy",Contribution_Matrix)