= commited on
Commit
3bb44c5
·
1 Parent(s): b39c220

add attention

Browse files
app.py CHANGED
@@ -87,6 +87,8 @@ if file is not None:
87
  # initiliaze the smoothing parameters
88
  smooth_scale = st.sidebar.slider("Smooth scale", min_value=0.1, max_value =1.0, step = 0.1)
89
 
 
 
90
  smooth_size = st.sidebar.slider("Smooth size", min_value=1, max_value =10)
91
 
92
  smooth_iter = st.sidebar.slider("Smooth iter", min_value=1, max_value =10)
@@ -129,7 +131,7 @@ if file is not None:
129
  attention = outputs.attentions[-1][0]
130
 
131
  # Let us recuperate the attention image
132
- attention_image = get_attention(image, attention, size = (224, 224), patch_size = (14, 14), scale = scale, head = head, smooth_scale = smooth_scale, smooth_size = smooth_size, smooth_iter = smooth_iter)
133
 
134
  # Let us transform the attention image to a opencv image
135
  attention_image = cv2.cvtColor(attention_image.astype('float32'), cv2.COLOR_RGB2BGR)
 
87
  # initiliaze the smoothing parameters
88
  smooth_scale = st.sidebar.slider("Smooth scale", min_value=0.1, max_value =1.0, step = 0.1)
89
 
90
+ smooth_thres = st.sidebar.slider("Smooth thres", min_value=0.01, max_value =1.0, step = 0.01)
91
+
92
  smooth_size = st.sidebar.slider("Smooth size", min_value=1, max_value =10)
93
 
94
  smooth_iter = st.sidebar.slider("Smooth iter", min_value=1, max_value =10)
 
131
  attention = outputs.attentions[-1][0]
132
 
133
  # Let us recuperate the attention image
134
+ attention_image = get_attention(image, attention, size = (224, 224), patch_size = (14, 14), scale = scale, head = head, smooth_scale = smooth_scale, smooth_thres=smooth_thres, smooth_size = smooth_size, smooth_iter = smooth_iter)
135
 
136
  # Let us transform the attention image to a opencv image
137
  attention_image = cv2.cvtColor(attention_image.astype('float32'), cv2.COLOR_RGB2BGR)
fake_face_detection/metrics/make_predictions.py CHANGED
@@ -17,7 +17,7 @@ import numpy as np
17
  import torch
18
  import os
19
 
20
- def get_attention(image: Union[str, JpegImageFile], attention: torch.Tensor, size: tuple, patch_size: tuple, scale: int = 50, head: int = 1, smooth_iter: int = 2, smooth_scale: float = 0.2, smooth_size = 5):
21
 
22
  # recuperate the image as a numpy array
23
  if isinstance(image, str):
@@ -49,7 +49,7 @@ def get_attention(image: Union[str, JpegImageFile], attention: torch.Tensor, siz
49
  attention = attention.reshape(size[0], size[1], 1)
50
 
51
  # add the smoothest attention
52
- attention = smooth_attention(attention, smooth_iter, smooth_scale, smooth_size)
53
 
54
  # recuperate the result
55
  attention_image = img / 255 * attention.numpy() * scale
@@ -69,6 +69,7 @@ def make_predictions(test_dataset: FakeFaceDetectionDataset,
69
  show: bool = True,
70
  head: int = 1,
71
  smooth_iter: int = 2,
 
72
  smooth_scale: float = 0.2,
73
  smooth_size = 5):
74
  """Make predictions with a vision transformer model
@@ -86,6 +87,7 @@ def make_predictions(test_dataset: FakeFaceDetectionDataset,
86
  show (bool, optional): A boolean value indicating if we want to recuperate the figure. Defaults to True.
87
  head (int, optional): The head number. Defaults to 1.
88
  smooth_iter (int, optional): The number of iterations for the smoothest attention. Defaults to 2.
 
89
  smooth_scale (float, optional): The scale for the smoothest attention. Defaults to 0.2.
90
  smooth_size ([type], optional): The size for the smoothest attention. Defaults to 5.
91
 
@@ -153,7 +155,7 @@ def make_predictions(test_dataset: FakeFaceDetectionDataset,
153
 
154
  for i in range(len(images)):
155
 
156
- attention_image = get_attention(images[i], predictions['attentions'][i], size, patch_size)
157
 
158
  axes[i].imshow(attention_image)
159
 
 
17
  import torch
18
  import os
19
 
20
+ def get_attention(image: Union[str, JpegImageFile], attention: torch.Tensor, size: tuple, patch_size: tuple, scale: int = 50, head: int = 1, smooth_iter: int = 2, smooth_thres: float = 0.01, smooth_scale: float = 0.2, smooth_size = 5):
21
 
22
  # recuperate the image as a numpy array
23
  if isinstance(image, str):
 
49
  attention = attention.reshape(size[0], size[1], 1)
50
 
51
  # add the smoothest attention
52
+ attention = smooth_attention(attention, smooth_iter, smooth_thres, smooth_scale, smooth_size)
53
 
54
  # recuperate the result
55
  attention_image = img / 255 * attention.numpy() * scale
 
69
  show: bool = True,
70
  head: int = 1,
71
  smooth_iter: int = 2,
72
+ smooth_thres: float = 0.01,
73
  smooth_scale: float = 0.2,
74
  smooth_size = 5):
75
  """Make predictions with a vision transformer model
 
87
  show (bool, optional): A boolean value indicating if we want to recuperate the figure. Defaults to True.
88
  head (int, optional): The head number. Defaults to 1.
89
  smooth_iter (int, optional): The number of iterations for the smoothest attention. Defaults to 2.
90
+ smooth_thres (float, optional): The threshold for the smoothest attention. Defaults to 0.01.
91
  smooth_scale (float, optional): The scale for the smoothest attention. Defaults to 0.2.
92
  smooth_size ([type], optional): The size for the smoothest attention. Defaults to 5.
93
 
 
155
 
156
  for i in range(len(images)):
157
 
158
+ attention_image = get_attention(images[i], predictions['attentions'][i], size, patch_size, attention_scale, head, smooth_iter, smooth_thres, smooth_scale, smooth_size)
159
 
160
  axes[i].imshow(attention_image)
161