Training
augmentation
AddGlassesAugmentation
Draws glasses on a PIL image, based on face landmarks.
Reference: https://xictus77.medium.com/facial-mask-overlay-with-opencv-dlib-4d948964cc4d
Uses face detector and shape predictor (model for extracting face landmarks) from dlib. The glasses are composed of 3 line segments and 2 ellipses, inside the ellipses a Gaussian blur is applied.
Example usage:
from src.training.augmentation import AddGlassesAugmentation from PIL import Image img = Image.open("image.jpg") add_glasses = AddGlassesAugmentation.with_default_models() img_with_glasses = add_glasses(img)
Source code in src/training/augmentation/glasses.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
with_default_models(width_scale=2.0, height_scale=4.0, blur_radius=1, line_width=3, fill_color='black')
classmethod
Load default models and create an instance of AddGlassesAugmentation.
Source code in src/training/augmentation/glasses.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
AddMaskAugmentation
Image transformation, generates a face mask based on landmark points
Based on https://xictus77.medium.com/facial-mask-overlay-with-opencv-dlib-4d948964cc4d
Source code in src/training/augmentation/mask.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
with_default_models(color='black')
classmethod
Load default models and create an instance of GenerateMask.
Source code in src/training/augmentation/mask.py
19 20 21 22 23 24 25 26 27 28 29 | |
AddRandomBlackRectangle
Bases: BaseAddRandomRectangle
Add a random black rectangle to an image.
Source code in src/training/augmentation/random_rectangle.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
get_color(img, rect)
Get the color black for the rectangle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
The image being modified. |
required | |
rect
|
The rectangle coordinates. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int]
|
The RGB color black (0, 0, 0). |
Source code in src/training/augmentation/random_rectangle.py
93 94 95 96 97 98 99 100 101 102 103 104 | |
AddRandomRectangleAverageColor
Bases: BaseAddRandomRectangle
Add a random rectangle with the average color of the occluded region.
Source code in src/training/augmentation/random_rectangle.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
get_color(img, rect)
Get the average color of the rectangle region in the image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
The image being modified. |
required | |
rect
|
The rectangle coordinates. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int]
|
The average RGB color of the rectangle region. |
Source code in src/training/augmentation/random_rectangle.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
AddRandomRectangleRandomColor
Bases: BaseAddRandomRectangle
Add a random rectangle with a random color to an image.
Source code in src/training/augmentation/random_rectangle.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
get_color(img, rect)
Get a random color for the rectangle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
The image being modified. |
required | |
rect
|
The rectangle coordinates. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int]
|
A random RGB color. |
Source code in src/training/augmentation/random_rectangle.py
75 76 77 78 79 80 81 82 83 84 85 86 87 | |
GaussianNoisePIL
Adapter for applying Gaussian noise to PIL images.
Source code in src/training/augmentation/gaussian_noise_pil.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
__call__(img)
Apply Gaussian noise to the input PIL image.
Source code in src/training/augmentation/gaussian_noise_pil.py
18 19 20 21 22 | |
gaussian_noise_pil
GaussianNoisePIL
Adapter for applying Gaussian noise to PIL images.
Source code in src/training/augmentation/gaussian_noise_pil.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | |
__call__(img)
Apply Gaussian noise to the input PIL image.
Source code in src/training/augmentation/gaussian_noise_pil.py
18 19 20 21 22 | |
glasses
AddGlassesAugmentation
Draws glasses on a PIL image, based on face landmarks.
Reference: https://xictus77.medium.com/facial-mask-overlay-with-opencv-dlib-4d948964cc4d
Uses face detector and shape predictor (model for extracting face landmarks) from dlib. The glasses are composed of 3 line segments and 2 ellipses, inside the ellipses a Gaussian blur is applied.
Example usage:
from src.training.augmentation import AddGlassesAugmentation from PIL import Image img = Image.open("image.jpg") add_glasses = AddGlassesAugmentation.with_default_models() img_with_glasses = add_glasses(img)
Source code in src/training/augmentation/glasses.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
with_default_models(width_scale=2.0, height_scale=4.0, blur_radius=1, line_width=3, fill_color='black')
classmethod
Load default models and create an instance of AddGlassesAugmentation.
Source code in src/training/augmentation/glasses.py
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
mask
AddMaskAugmentation
Image transformation, generates a face mask based on landmark points
Based on https://xictus77.medium.com/facial-mask-overlay-with-opencv-dlib-4d948964cc4d
Source code in src/training/augmentation/mask.py
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
with_default_models(color='black')
classmethod
Load default models and create an instance of GenerateMask.
Source code in src/training/augmentation/mask.py
19 20 21 22 23 24 25 26 27 28 29 | |
random_rectangle
Module for adding random rectangles to images with various color strategies.
This module provides an abstract base class and concrete implementations for adding random rectangles to images. The rectangles can have random colors, a fixed black color, or the average color of the occluded region.
AddRandomBlackRectangle
Bases: BaseAddRandomRectangle
Add a random black rectangle to an image.
Source code in src/training/augmentation/random_rectangle.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | |
get_color(img, rect)
Get the color black for the rectangle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
The image being modified. |
required | |
rect
|
The rectangle coordinates. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int]
|
The RGB color black (0, 0, 0). |
Source code in src/training/augmentation/random_rectangle.py
93 94 95 96 97 98 99 100 101 102 103 104 | |
AddRandomRectangleAverageColor
Bases: BaseAddRandomRectangle
Add a random rectangle with the average color of the occluded region.
Source code in src/training/augmentation/random_rectangle.py
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
get_color(img, rect)
Get the average color of the rectangle region in the image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
The image being modified. |
required | |
rect
|
The rectangle coordinates. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int]
|
The average RGB color of the rectangle region. |
Source code in src/training/augmentation/random_rectangle.py
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | |
AddRandomRectangleRandomColor
Bases: BaseAddRandomRectangle
Add a random rectangle with a random color to an image.
Source code in src/training/augmentation/random_rectangle.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 | |
get_color(img, rect)
Get a random color for the rectangle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
The image being modified. |
required | |
rect
|
The rectangle coordinates. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int]
|
A random RGB color. |
Source code in src/training/augmentation/random_rectangle.py
75 76 77 78 79 80 81 82 83 84 85 86 87 | |
BaseAddRandomRectangle
Bases: ABC
Base class for adding a random rectangle to an image.
Attributes:
| Name | Type | Description |
|---|---|---|
min_size |
Minimum size of the rectangle. |
|
max_size |
Maximum size of the rectangle. |
Source code in src/training/augmentation/random_rectangle.py
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
__call__(img)
Add a random rectangle to the given image.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
The image to modify. |
required |
Returns:
| Type | Description |
|---|---|
|
The modified image with a random rectangle. |
Source code in src/training/augmentation/random_rectangle.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
__init__(min_size=10, max_size=50)
Initialize the base class with rectangle size constraints.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
min_size
|
Minimum size of the rectangle. |
10
|
|
max_size
|
Maximum size of the rectangle. |
50
|
Source code in src/training/augmentation/random_rectangle.py
23 24 25 26 27 28 29 30 31 | |
get_color(img, rect)
abstractmethod
Abstract method to determine the color of the rectangle.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
img
|
The image being modified. |
required | |
rect
|
The rectangle coordinates. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int, int]
|
The color of the rectangle in RGB format. |
Source code in src/training/augmentation/random_rectangle.py
58 59 60 61 62 63 64 65 66 67 68 69 | |
fine_tuning
count_correct(embeddings_1, embeddings_2, labels)
Count correct embedding pairs with respect to labels.
For each triple (e1, e2, label): - label is 1 for the same person and -1 for different people - cosine similarity is used to determine if e1 and e2 are similar enough - similarity is in range [-1, 1], where 1 means very similar - if similarity >= 0, then e1 and e2 are considered similar (label 1) - if similarity < 0, then e1 and e2 are considered different (label -1)
B - batch dimension E - embedding dimension
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
embeddings_1
|
Tensor
|
Embeddings of the first image in the pair, shape (B, E). |
required |
embeddings_2
|
Tensor
|
Embeddings of the second image in the pair, shape (B, E). |
required |
labels
|
Tensor
|
Labels indicating if the pairs are from the same person (1) or different people (-1), shape (B,). |
required |
Source code in src/training/fine_tuning.py
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | |
loss
ContrastiveLoss
Bases: Module
Source code in src/training/loss/contrastive_loss.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
forward(output1, output2, label)
Label is 0 for the same person, 1 for different person
Source code in src/training/loss/contrastive_loss.py
10 11 12 13 14 15 16 17 18 | |
contrastive_loss
ContrastiveLoss
Bases: Module
Source code in src/training/loss/contrastive_loss.py
5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
forward(output1, output2, label)
Label is 0 for the same person, 1 for different person
Source code in src/training/loss/contrastive_loss.py
10 11 12 13 14 15 16 17 18 | |