Spaces:
Runtime error
Runtime error
jaekookang
commited on
Commit
β’
af72b72
1
Parent(s):
f4b95ab
first upload
Browse files- .ipynb_checkpoints/README-checkpoint.md +11 -0
- .ipynb_checkpoints/gradio_artist_classifier-checkpoint.py +23 -0
- .ipynb_checkpoints/requirements-checkpoint.txt +5 -0
- README.md +4 -30
- gradcam_utils.py +141 -0
- gradio_artist_classifier.py +23 -0
- requirements.txt +5 -0
.ipynb_checkpoints/README-checkpoint.md
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
title: Artist Classifier
|
3 |
+
emoji: π¨π¨π»βπ¨
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: pink
|
6 |
+
sdk: gradio
|
7 |
+
app_file: gradio_artist_classifier.py
|
8 |
+
pinned: false
|
9 |
+
---
|
10 |
+
|
11 |
+
# Configuration
|
.ipynb_checkpoints/gradio_artist_classifier-checkpoint.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''Artist Classifier
|
2 |
+
|
3 |
+
prototype
|
4 |
+
|
5 |
+
---
|
6 |
+
- 2022-01-18 jkang first created
|
7 |
+
'''
|
8 |
+
|
9 |
+
import matplotlib.pyplot as plt
|
10 |
+
import matplotlib.image as mpimg
|
11 |
+
import seaborn as sns
|
12 |
+
|
13 |
+
import gradio as gr
|
14 |
+
import tensorflow as tf
|
15 |
+
tfk = tf.keras
|
16 |
+
|
17 |
+
from gradcam_utils import get_img_4d_array, make_gradcam_heatmap, align_image_with_heatmap
|
18 |
+
|
19 |
+
def greet(name):
|
20 |
+
return "Hello " + name + "!!"
|
21 |
+
|
22 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
23 |
+
iface.launch()
|
.ipynb_checkpoints/requirements-checkpoint.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==2.7.0
|
2 |
+
matplotlib==3.5.1
|
3 |
+
numpy==1.22.0
|
4 |
+
seaborn==0.11.2
|
5 |
+
tensorflow==2.7.0
|
README.md
CHANGED
@@ -1,37 +1,11 @@
|
|
1 |
---
|
2 |
title: Artist Classifier
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
app_file:
|
8 |
pinned: false
|
9 |
---
|
10 |
|
11 |
# Configuration
|
12 |
-
|
13 |
-
`title`: _string_
|
14 |
-
Display title for the Space
|
15 |
-
|
16 |
-
`emoji`: _string_
|
17 |
-
Space emoji (emoji-only character allowed)
|
18 |
-
|
19 |
-
`colorFrom`: _string_
|
20 |
-
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
21 |
-
|
22 |
-
`colorTo`: _string_
|
23 |
-
Color for Thumbnail gradient (red, yellow, green, blue, indigo, purple, pink, gray)
|
24 |
-
|
25 |
-
`sdk`: _string_
|
26 |
-
Can be either `gradio`, `streamlit`, or `static`
|
27 |
-
|
28 |
-
`sdk_version` : _string_
|
29 |
-
Only applicable for `streamlit` SDK.
|
30 |
-
See [doc](https://hf.co/docs/hub/spaces) for more info on supported versions.
|
31 |
-
|
32 |
-
`app_file`: _string_
|
33 |
-
Path to your main application file (which contains either `gradio` or `streamlit` Python code, or `static` html code).
|
34 |
-
Path is relative to the root of the repository.
|
35 |
-
|
36 |
-
`pinned`: _boolean_
|
37 |
-
Whether the Space stays on top of your list.
|
|
|
1 |
---
|
2 |
title: Artist Classifier
|
3 |
+
emoji: π¨π¨π»βπ¨
|
4 |
+
colorFrom: red
|
5 |
+
colorTo: pink
|
6 |
sdk: gradio
|
7 |
+
app_file: gradio_artist_classifier.py
|
8 |
pinned: false
|
9 |
---
|
10 |
|
11 |
# Configuration
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gradcam_utils.py
ADDED
@@ -0,0 +1,141 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''
|
2 |
+
Grad-CAM visualization utilities
|
3 |
+
|
4 |
+
- Based on https://keras.io/examples/vision/grad_cam/
|
5 |
+
|
6 |
+
---
|
7 |
+
- 2021-12-18 jkang first created
|
8 |
+
- 2022-01-16
|
9 |
+
- copied from https://huggingface.co/spaces/jkang/demo-gradcam-imagenet/blob/main/utils.py
|
10 |
+
- updated for artis/trend classifier
|
11 |
+
'''
|
12 |
+
import matplotlib.cm as cm
|
13 |
+
|
14 |
+
import os
|
15 |
+
import re
|
16 |
+
from glob import glob
|
17 |
+
import numpy as np
|
18 |
+
import tensorflow as tf
|
19 |
+
tfk = tf.keras
|
20 |
+
K = tfk.backend
|
21 |
+
|
22 |
+
# Disable GPU for testing
|
23 |
+
# os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
|
24 |
+
|
25 |
+
|
26 |
+
def get_imagenet_classes():
|
27 |
+
'''Retrieve all 1000 imagenet classes/labels as dictionaries'''
|
28 |
+
classes = tfk.applications.imagenet_utils.decode_predictions(
|
29 |
+
np.expand_dims(np.arange(1000), 0), top=1000
|
30 |
+
)
|
31 |
+
idx2lab = {cla[2]: cla[1] for cla in classes[0]}
|
32 |
+
lab2idx = {idx2lab[idx]: idx for idx in idx2lab}
|
33 |
+
return idx2lab, lab2idx
|
34 |
+
|
35 |
+
|
36 |
+
def search_by_name(str_part):
|
37 |
+
'''Search imagenet class by partial matching string'''
|
38 |
+
results = [key for key in list(lab2idx.keys()) if re.search(str_part, key)]
|
39 |
+
if len(results) != 0:
|
40 |
+
return [(key, lab2idx[key]) for key in results]
|
41 |
+
else:
|
42 |
+
return []
|
43 |
+
|
44 |
+
|
45 |
+
def get_xception_model():
|
46 |
+
'''Get model to use'''
|
47 |
+
base_model = tfk.applications.xception.Xception
|
48 |
+
preprocessor = tfk.applications.xception.preprocess_input
|
49 |
+
decode_predictions = tfk.applications.xception.decode_predictions
|
50 |
+
last_conv_layer_name = "block14_sepconv2_act"
|
51 |
+
|
52 |
+
model = base_model(weights='imagenet')
|
53 |
+
grad_model = tfk.models.Model(
|
54 |
+
inputs=[model.inputs],
|
55 |
+
outputs=[model.get_layer(last_conv_layer_name).output,
|
56 |
+
model.output]
|
57 |
+
)
|
58 |
+
return model, grad_model, preprocessor, decode_predictions
|
59 |
+
|
60 |
+
|
61 |
+
def get_img_4d_array(image_file, image_size=(299, 299)):
|
62 |
+
'''Load image as 4d array'''
|
63 |
+
img = tfk.preprocessing.image.load_img(
|
64 |
+
image_file, target_size=image_size) # PIL obj
|
65 |
+
img_array = tfk.preprocessing.image.img_to_array(
|
66 |
+
img) # float32 numpy array
|
67 |
+
img_array = np.expand_dims(img_array, axis=0) # 3d -> 4d (1,299,299,3)
|
68 |
+
return img_array
|
69 |
+
|
70 |
+
|
71 |
+
def make_gradcam_heatmap(grad_model, img_array, pred_idx=None):
|
72 |
+
'''Generate heatmap to overlay with
|
73 |
+
- img_array: 4d numpy array
|
74 |
+
- pred_idx: eg. index out of 1000 imagenet classes
|
75 |
+
if None, argmax is chosen from prediction
|
76 |
+
'''
|
77 |
+
# Get gradient of pred class w.r.t. last conv activation
|
78 |
+
with tf.GradientTape() as tape:
|
79 |
+
last_conv_act, predictions = grad_model(img_array)
|
80 |
+
if pred_idx == None:
|
81 |
+
pred_idx = tf.argmax(predictions[0])
|
82 |
+
class_channel = predictions[:, pred_idx] # (1,1000) => (1,)
|
83 |
+
|
84 |
+
# d(class_channel/last_conv_act)
|
85 |
+
grads = tape.gradient(class_channel, last_conv_act)
|
86 |
+
pooled_grads = tf.reduce_mean(grads, axis=(
|
87 |
+
0, 1, 2)) # (1,10,10,2048) => (2048,)
|
88 |
+
|
89 |
+
# (10,10,2048) x (2048,1) => (10,10,1)
|
90 |
+
heatmap = last_conv_act[0] @ pooled_grads[..., tf.newaxis]
|
91 |
+
heatmap = tf.squeeze(heatmap) # (10,10)
|
92 |
+
|
93 |
+
# Normalize heatmap between 0 and 1
|
94 |
+
heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)
|
95 |
+
return heatmap, pred_idx.numpy(), predictions.numpy().squeeze()
|
96 |
+
|
97 |
+
|
98 |
+
def align_image_with_heatmap(img_array, heatmap, alpha=0.3, cmap='jet'):
|
99 |
+
'''Align the image with gradcam heatmap
|
100 |
+
- img_array: 4d numpy array
|
101 |
+
- heatmap: output of `def make_gradcam_heatmap()` as 2d numpy array
|
102 |
+
'''
|
103 |
+
img_array = img_array.squeeze() # 4d => 3d
|
104 |
+
|
105 |
+
# Rescale to 0-255 range
|
106 |
+
heatmap_scaled = np.uint8(255 * heatmap)
|
107 |
+
img_array_scaled = np.uint8(255 * img_array)
|
108 |
+
|
109 |
+
colormap = cm.get_cmap(cmap)
|
110 |
+
colors = colormap(np.arange(256))[:, :3] # mapping RGB to heatmap
|
111 |
+
heatmap_colored = colors[heatmap_scaled] # ? still unclear
|
112 |
+
|
113 |
+
# Make RGB colorized heatmap
|
114 |
+
heatmap_colored = (tfk.preprocessing.image.array_to_img(heatmap_colored) # array => PIL
|
115 |
+
.resize((img_array.shape[1], img_array.shape[0])))
|
116 |
+
heatmap_colored = tfk.preprocessing.image.img_to_array(
|
117 |
+
heatmap_colored) # PIL => array
|
118 |
+
|
119 |
+
# Overlay image with heatmap
|
120 |
+
overlaid_img = heatmap_colored * alpha + img_array_scaled
|
121 |
+
overlaid_img = tfk.preprocessing.image.array_to_img(overlaid_img)
|
122 |
+
return overlaid_img
|
123 |
+
|
124 |
+
|
125 |
+
if __name__ == '__main__':
|
126 |
+
# Test GradCAM
|
127 |
+
examples = sorted(glob(os.path.join('examples', '*.jpg')))
|
128 |
+
idx2lab, lab2idx = get_imagenet_classes()
|
129 |
+
|
130 |
+
model, grad_model, preprocessor, decode_predictions = get_xception_model()
|
131 |
+
|
132 |
+
img_4d_array = get_img_4d_array(examples[0])
|
133 |
+
img_4d_array = preprocessor(img_4d_array)
|
134 |
+
|
135 |
+
heatmap = make_gradcam_heatmap(grad_model, img_4d_array, pred_idx=None)
|
136 |
+
|
137 |
+
img_pil = align_image_with_heatmap(
|
138 |
+
img_4d_array, heatmap, alpha=0.3, cmap='jet')
|
139 |
+
|
140 |
+
img_pil.save('test.jpg')
|
141 |
+
print('done')
|
gradio_artist_classifier.py
ADDED
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
'''Artist Classifier
|
2 |
+
|
3 |
+
prototype
|
4 |
+
|
5 |
+
---
|
6 |
+
- 2022-01-18 jkang first created
|
7 |
+
'''
|
8 |
+
|
9 |
+
import matplotlib.pyplot as plt
|
10 |
+
import matplotlib.image as mpimg
|
11 |
+
import seaborn as sns
|
12 |
+
|
13 |
+
import gradio as gr
|
14 |
+
import tensorflow as tf
|
15 |
+
tfk = tf.keras
|
16 |
+
|
17 |
+
from gradcam_utils import get_img_4d_array, make_gradcam_heatmap, align_image_with_heatmap
|
18 |
+
|
19 |
+
def greet(name):
|
20 |
+
return "Hello " + name + "!!"
|
21 |
+
|
22 |
+
iface = gr.Interface(fn=greet, inputs="text", outputs="text")
|
23 |
+
iface.launch()
|
requirements.txt
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
gradio==2.7.0
|
2 |
+
matplotlib==3.5.1
|
3 |
+
numpy==1.22.0
|
4 |
+
seaborn==0.11.2
|
5 |
+
tensorflow==2.7.0
|