-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.py
31 lines (23 loc) · 907 Bytes
/
example.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
from tensorflow.keras.applications.vgg16 import preprocess_input
import tensorflow as tf
import numpy as np
from core.score_cam import ScoreCAM
IMAGE_PATH = "./cat.jpg"
gpus = tf.config.list_physical_devices('GPU')
for gpu in gpus:
tf.config.experimental.set_memory_growth(gpu, True)
with tf.device('/GPU:0'):
model = tf.keras.applications.vgg16.VGG16(weights="imagenet", include_top=True)
input_shape = (224, 224)
img = tf.keras.preprocessing.image.load_img(IMAGE_PATH, target_size=input_shape)
img = tf.keras.preprocessing.image.img_to_array(img)
img = np.expand_dims(img, axis=0)
img = preprocess_input(img)
data = (img, None)
class_index = 281
explainer = ScoreCAM()
# Compute ScoreCAM on VGG16
image = explainer.explain(
data, model, class_index, input_shape, display_in_grid=False
)[0]
explainer.save(image, ".", "score_cam.png")