= commited on
Commit
d57c931
·
1 Parent(s): b63fd37

adding smooth attention

Browse files
app.py CHANGED
@@ -84,6 +84,13 @@ if file is not None:
84
 
85
  left.markdown("""---""")
86
 
 
 
 
 
 
 
 
87
  # add a side for the scaler and the head number
88
  scale = st.sidebar.slider("Attention scale", min_value=30, max_value =200)
89
 
@@ -122,7 +129,7 @@ if file is not None:
122
  attention = outputs.attentions[-1][0]
123
 
124
  # Let us recuperate the attention image
125
- attention_image = get_attention(image, attention, size = (224, 224), patch_size = (14, 14), scale = scale, head = head)
126
 
127
  # Let us transform the attention image to a opencv image
128
  attention_image = cv2.cvtColor(attention_image.astype('float32'), cv2.COLOR_RGB2BGR)
 
84
 
85
  left.markdown("""---""")
86
 
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)
93
+
94
  # add a side for the scaler and the head number
95
  scale = st.sidebar.slider("Attention scale", min_value=30, max_value =200)
96
 
 
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)
fake_face_detection/metrics/__pycache__/__init__.cpython-310.pyc CHANGED
Binary files a/fake_face_detection/metrics/__pycache__/__init__.cpython-310.pyc and b/fake_face_detection/metrics/__pycache__/__init__.cpython-310.pyc differ
 
fake_face_detection/metrics/__pycache__/compute_metrics.cpython-310.pyc CHANGED
Binary files a/fake_face_detection/metrics/__pycache__/compute_metrics.cpython-310.pyc and b/fake_face_detection/metrics/__pycache__/compute_metrics.cpython-310.pyc differ
 
fake_face_detection/metrics/make_predictions.py CHANGED
@@ -1,6 +1,7 @@
1
 
2
  from fake_face_detection.data.fake_face_dataset import FakeFaceDetectionDataset
3
  from fake_face_detection.metrics.compute_metrics import compute_metrics
 
4
  from torch.utils.tensorboard import SummaryWriter
5
  from PIL.JpegImagePlugin import JpegImageFile
6
  from torch.utils.data import DataLoader
@@ -16,7 +17,7 @@ import numpy as np
16
  import torch
17
  import os
18
 
19
- def get_attention(image: Union[str, JpegImageFile], attention: torch.Tensor, size: tuple, patch_size: tuple, scale: int = 50, head: int = 1):
20
 
21
  # recuperate the image as a numpy array
22
  if isinstance(image, str):
@@ -47,6 +48,9 @@ def get_attention(image: Union[str, JpegImageFile], attention: torch.Tensor, siz
47
  # let us reshape the attention to the right size
48
  attention = attention.reshape(size[0], size[1], 1)
49
 
 
 
 
50
  # recuperate the result
51
  attention_image = img / 255 * attention.numpy() * scale
52
 
@@ -63,7 +67,10 @@ def make_predictions(test_dataset: FakeFaceDetectionDataset,
63
  figsize: tuple = (24, 24),
64
  attention_scale: int = 50,
65
  show: bool = True,
66
- head: int = 1):
 
 
 
67
  """Make predictions with a vision transformer model
68
 
69
  Args:
@@ -78,6 +85,9 @@ def make_predictions(test_dataset: FakeFaceDetectionDataset,
78
  attention_scale (int, optional): The attention scale. Defaults to 50.
79
  show (bool, optional): A boolean value indicating if we want to recuperate the figure. Defaults to True.
80
  head (int, optional): The head number. Defaults to 1.
 
 
 
81
 
82
  Returns:
83
  Union[Tuple[pd.DataFrame, dict], Tuple[pd.DataFame, dict, figure]]: The return prediction and the metrics
 
1
 
2
  from fake_face_detection.data.fake_face_dataset import FakeFaceDetectionDataset
3
  from fake_face_detection.metrics.compute_metrics import compute_metrics
4
+ from fake_face_detection.smoothest_attention import smooth_attention
5
  from torch.utils.tensorboard import SummaryWriter
6
  from PIL.JpegImagePlugin import JpegImageFile
7
  from torch.utils.data import DataLoader
 
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):
 
48
  # let us reshape the attention to the right size
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
56
 
 
67
  figsize: tuple = (24, 24),
68
  attention_scale: int = 50,
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
75
 
76
  Args:
 
85
  attention_scale (int, optional): The attention scale. Defaults to 50.
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
 
92
  Returns:
93
  Union[Tuple[pd.DataFrame, dict], Tuple[pd.DataFame, dict, figure]]: The return prediction and the metrics
fake_face_detection/utils/split_data.py CHANGED
@@ -27,11 +27,11 @@ def split_data_from_dir(path: str, new_path: str, test_size: float = 0.2, valid_
27
  # let us recuperate the files' paths in it
28
  images = os.listdir(dir_path)
29
 
30
- # let us split the data between training and test set
31
- train_set, test_set = train_test_split(images, test_size = test_size)
32
 
33
- # let us split the training set between training and validation set
34
- train_set, valid_set = train_test_split(train_set, test_size = valid_size)
35
 
36
  # let us create the train test and valid directories
37
  if not os.path.exists(os.path.join(os.path.join(new_path, 'train'), dir_)) or\
 
27
  # let us recuperate the files' paths in it
28
  images = os.listdir(dir_path)
29
 
30
+ # let us split the data between training and test + valid sets
31
+ train_set, test_valid_set = train_test_split(images, test_size = test_size + valid_size)
32
 
33
+ # let us split the test + valid sets between test and valid sets
34
+ test_set, valid_set = train_test_split(test_valid_set, test_size = valid_size)
35
 
36
  # let us create the train test and valid directories
37
  if not os.path.exists(os.path.join(os.path.join(new_path, 'train'), dir_)) or\