OmerFarooq commited on
Commit
384c3f4
1 Parent(s): 3fe82d6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -11
app.py CHANGED
@@ -17,10 +17,11 @@ base_model.trainable = False
17
 
18
 
19
  def img_pros(img):
20
- img = tf.keras.preprocessing.image.img_to_array(img)
21
- img = tf.image.resize(img, [160,160])
 
22
  img = tf.expand_dims(img, axis = 0)
23
- return img
24
 
25
  #function for creating model
26
  #returns model, its inputs, Xception's last conv output, the whole model's outputs
@@ -102,7 +103,7 @@ def superimpose_single(heatmap, img, alpha = 0.7):
102
 
103
  # Create an image with RGB colorized heatmap
104
  jet_heatmap = keras.utils.array_to_img(jet_heatmap)
105
- jet_heatmap = jet_heatmap.resize((160,160))
106
  jet_heatmap = keras.utils.img_to_array(jet_heatmap)
107
 
108
  # Superimpose the heatmap on original image
@@ -112,20 +113,20 @@ def superimpose_single(heatmap, img, alpha = 0.7):
112
  return superimposed_img
113
 
114
  #for generating single gradcam image
115
- def gen_grad_img_single(grad_model, img, class_index, alpha = 0.4):
116
  heatmaps, y_pred = create_heatmap(grad_model, img, class_index)
117
 
118
  # for i in range(len(y_pred)):
119
  # if y_pred[i] > 0.5: y_pred[i] = 1
120
  # else: y_pred[i] = 0
121
 
122
- img = superimpose_single(heatmaps, img[0])
123
  return np.array(img).astype('uint8'), y_pred
124
 
125
 
126
- def gen_grad_both(grad_model, img):
127
- img_c, y_pred_c = gen_grad_img_single(grad_model, img, 0)
128
- img_d, y_pred_d = gen_grad_img_single(grad_model, img, 1)
129
  y_pred_c = np.around(y_pred_c,3)
130
  y_pred_d = np.around(y_pred_d,3)
131
  # show_imgs([img_c, img_d], [y_true, y_true], [size[0], size[1]], cols, [y_pred_c, y_pred_d], font_size = font_size)
@@ -139,9 +140,9 @@ def gen_grad_both(grad_model, img):
139
  weights = "weights_nm.h5"
140
 
141
  def get_grad(img):
142
- img = img_pros(img)
143
  grad_model = create_grad_model(weights, 2, "softmax")
144
- grad_img_c, grad_img_d, y_pred, infer = gen_grad_both(grad_model, img)
145
  # pred_class = ""
146
  # if y_pred[0] > 0.5: pred_class = "cat"
147
  # else: pred_class = "dog"
 
17
 
18
 
19
  def img_pros(img):
20
+ original_img = tf.keras.preprocessing.image.img_to_array(img)
21
+ original_img_shape = img.shape
22
+ img = tf.image.resize(original_img, [160,160])
23
  img = tf.expand_dims(img, axis = 0)
24
+ return img, original_img, original_img_shape
25
 
26
  #function for creating model
27
  #returns model, its inputs, Xception's last conv output, the whole model's outputs
 
103
 
104
  # Create an image with RGB colorized heatmap
105
  jet_heatmap = keras.utils.array_to_img(jet_heatmap)
106
+ jet_heatmap = jet_heatmap.resize((original_img_shape[1], original_img_shape[0]))
107
  jet_heatmap = keras.utils.img_to_array(jet_heatmap)
108
 
109
  # Superimpose the heatmap on original image
 
113
  return superimposed_img
114
 
115
  #for generating single gradcam image
116
+ def gen_grad_img_single(grad_model, img, class_index, original_img, original_img_shape, alpha = 0.4):
117
  heatmaps, y_pred = create_heatmap(grad_model, img, class_index)
118
 
119
  # for i in range(len(y_pred)):
120
  # if y_pred[i] > 0.5: y_pred[i] = 1
121
  # else: y_pred[i] = 0
122
 
123
+ img = superimpose_single(heatmaps, original_img, original_img_shape)
124
  return np.array(img).astype('uint8'), y_pred
125
 
126
 
127
+ def gen_grad_both(grad_model, img, original_img, original_img_shape):
128
+ img_c, y_pred_c = gen_grad_img_single(grad_model, img, 0, original_img, original_img_shape)
129
+ img_d, y_pred_d = gen_grad_img_single(grad_model, img, 1, original_img, original_img_shape)
130
  y_pred_c = np.around(y_pred_c,3)
131
  y_pred_d = np.around(y_pred_d,3)
132
  # show_imgs([img_c, img_d], [y_true, y_true], [size[0], size[1]], cols, [y_pred_c, y_pred_d], font_size = font_size)
 
140
  weights = "weights_nm.h5"
141
 
142
  def get_grad(img):
143
+ img, original_img, original_img_shape = img_pros(img)
144
  grad_model = create_grad_model(weights, 2, "softmax")
145
+ grad_img_c, grad_img_d, y_pred, infer = gen_grad_both(grad_model, img, original_img, original_img_shape)
146
  # pred_class = ""
147
  # if y_pred[0] > 0.5: pred_class = "cat"
148
  # else: pred_class = "dog"