mustapha commited on
Commit
ce57e72
1 Parent(s): 866f275

Saliency maps

Browse files
Files changed (3) hide show
  1. app.py +49 -4
  2. images/real_Farsi.jpg +0 -0
  3. images/real_Ruqaa.jpg +0 -0
app.py CHANGED
@@ -1,5 +1,6 @@
1
 
2
  # %%
 
3
  import gradio as gr
4
  import numpy as np
5
  # import random as rn
@@ -7,8 +8,11 @@ import numpy as np
7
  import tensorflow as tf
8
  import cv2
9
 
10
- # tf.config.experimental.set_visible_devices([], 'GPU')
11
 
 
 
 
12
 
13
  #%%
14
  def parse_image(image):
@@ -53,25 +57,66 @@ model.compile(loss=tf.keras.losses.BinaryCrossentropy(from_logits=False), optimi
53
 
54
  model.load_weights('weights.h5')
55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
  #%%
57
  def segment(image):
 
 
58
  image = parse_image(image)
59
- # print(image.shape)
60
  output = model.predict(image)
61
  # print(output)
62
  labels = {
63
  "farsi" : 1-float(output),
64
  "ruqaa" : float(output)
65
  }
66
- return labels
 
 
 
 
 
 
 
 
 
 
67
 
68
  iface = gr.Interface(fn=segment,
 
 
 
 
 
69
  inputs="image",
70
- outputs="label",
 
 
 
71
  examples=[["images/Farsi_1.jpg"],
72
  ["images/Farsi_2.jpg"],
 
73
  ["images/Ruqaa_1.jpg"],
74
  ["images/Ruqaa_2.jpg"],
75
  ["images/Ruqaa_3.jpg"],
 
76
  ]).launch()
77
  # %%
 
1
 
2
  # %%
3
+ from cProfile import label
4
  import gradio as gr
5
  import numpy as np
6
  # import random as rn
 
8
  import tensorflow as tf
9
  import cv2
10
 
11
+ tf.config.experimental.set_visible_devices([], 'GPU')
12
 
13
+ #%% constantes
14
+ COLOR = np.array([163, 23, 252])/255.0
15
+ ALPHA = 0.8
16
 
17
  #%%
18
  def parse_image(image):
 
57
 
58
  model.load_weights('weights.h5')
59
 
60
+ #%%
61
+
62
+ def saliency_map(img):
63
+ """
64
+ return the normalized gradients overs the image, and also the prediction of the model
65
+ """
66
+ inp = tf.convert_to_tensor(
67
+ img[None, :, :, None],
68
+ dtype = tf.float32
69
+ )
70
+ inp_var = tf.Variable(inp)
71
+
72
+ with tf.GradientTape() as tape:
73
+ pred = model(inp_var, training=False)
74
+ loss = pred[0][0]
75
+ grads = tape.gradient(loss, inp_var)
76
+ grads = tf.math.abs(grads) / (tf.math.reduce_max(tf.math.abs(grads))+1e-14)
77
+ return grads, round(float(model(inp_var, training = False)))
78
+
79
  #%%
80
  def segment(image):
81
+ # c = image
82
+ print(image.shape)
83
  image = parse_image(image)
84
+ print(image.shape)
85
  output = model.predict(image)
86
  # print(output)
87
  labels = {
88
  "farsi" : 1-float(output),
89
  "ruqaa" : float(output)
90
  }
91
+ grads, _ = saliency_map(image[0, :, :, 0])
92
+ s_map = grads.numpy()[0, :, :, 0]
93
+ reconstructed_image = cv2.cvtColor(image.squeeze(0), cv2.COLOR_GRAY2RGB)
94
+ for i in range(reconstructed_image.shape[0]):
95
+ for j in range(reconstructed_image.shape[1]):
96
+ reconstructed_image[i, j, :] = reconstructed_image[i, j, :] * (1-ALPHA) + s_map[i, j]* COLOR * ALPHA
97
+ # reconstructed_image = reconstructed_image.astype(np.uint8)
98
+ V = reconstructed_image
99
+ # print("i shape:", i.shape)
100
+ # print("type(i):", type(i))
101
+ return labels, reconstructed_image
102
 
103
  iface = gr.Interface(fn=segment,
104
+ description="""
105
+ This is an Arab Calligraphy Style Recognition.
106
+ This model predicts the style (binary classification) of the image.
107
+ The model also outputs the Saliency map.
108
+ """,
109
  inputs="image",
110
+ outputs=[
111
+ gr.outputs.Label(num_top_classes=2, label="Style"),
112
+ gr.outputs.Image(label = "Saliency map")
113
+ ],
114
  examples=[["images/Farsi_1.jpg"],
115
  ["images/Farsi_2.jpg"],
116
+ ["images/real_Farsi.jpg"],
117
  ["images/Ruqaa_1.jpg"],
118
  ["images/Ruqaa_2.jpg"],
119
  ["images/Ruqaa_3.jpg"],
120
+ ["images/real_Ruqaa.jpg"],
121
  ]).launch()
122
  # %%
images/real_Farsi.jpg ADDED
images/real_Ruqaa.jpg ADDED