File size: 10,254 Bytes
3757e50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
250
251
252
253
import numpy as np
import matplotlib.pyplot as plt
import cv2
import os
import requests
import torch
from GPUtil import showUtilization as gpu_usage
from prettytable import PrettyTable

## -----------------------------------------------------------------------------------------------------------------##
##                                          CLEAN GPU MEMORY USAGE AND DATASETS                                     ##
## -----------------------------------------------------------------------------------------------------------------##

def free_gpu_cache():
    print("Initial GPU Usage")
    gpu_usage() 
    torch.cuda.synchronize()
    torch.cuda.empty_cache()
    print("GPU Usage after emptying the cache")
    gpu_usage()

# Compute the number of trainable parameters in a model
def count_parameters(model):
    table = PrettyTable(["Modules", "Parameters"])
    total_params = 0
    for name, parameter in model.named_parameters():
        if not parameter.requires_grad:
            continue
        params = parameter.numel()
        table.add_row([name, params])
        total_params += params
    #print(table)
    print(f"Total Trainable Params: {total_params}")
    return table, total_params

## -----------------------------------------------------------------------------------------------------------------##
##                            HEATMAPS GENERATION FROM LANDMARKS POINTS                                             ##
## -----------------------------------------------------------------------------------------------------------------##

def fuse_heatmaps(heatmaps):
    fused_heatmap = np.sum(heatmaps, axis=0)

    # Threshold the heatmap so that values below a certain threshold are set to 0
    binary_fused_heatmap = np.where(fused_heatmap < 0.5, 0, 1)

    assert is_binary_image(binary_fused_heatmap), "Image is not binary"

    return binary_fused_heatmap

def fuse_heatmaps(heatmaps):
    # Use np.maximum.reduce to get the maximum value from each stack of pixels across the heatmaps
    fused_heatmap = np.maximum.reduce(heatmaps)
    return fused_heatmap

def scale_points(points: list, img_size: tuple, orig_size: tuple = None, offset=0):    
    # Scale coordinates according to image size
    if orig_size:
        # Points * Ratio -> Downscaling
        scaled_points = [tuple([round(p*isize/osize)+offset for p, isize, osize in zip(point, img_size, orig_size)]) for point in points]
    else:
        # Points * current Size -> Upscaling (when i use "extract_landmarks" function the points are with size (1,1))
        scaled_points = [tuple([round(r*sz)+offset for sz, r in zip(point, img_size)]) for point in points]

    return scaled_points


def points_to_heatmap(points: list, img_size: tuple, orig_size: tuple = None, sigma=2, fuse=False, offset=0):

    # Scale coordinates according to image size
    if orig_size:
        scaled_points = scale_points(points, img_size, orig_size, offset=offset)
    else:
        scaled_points = scale_points(points, img_size, offset=offset)

    # Generate heatmaps with the scaled points
    heatmaps = [generate_heatmap_from_points(point, img_size, sigma) for point in scaled_points]

    if fuse:
        heatmaps = fuse_heatmaps(heatmaps)

    return np.array(heatmaps)


def generate_heatmap_from_points(point, img_size, sigma):
    # Create a meshgrid of x,y coordinates for the image size
    x, y = np.meshgrid(np.arange(img_size[0]), np.arange(img_size[1]))
    # Calculate the heatmap using a Gaussian function centered at the input point
    heatmap = np.exp(-((x - point[0]) ** 2 + (y - point[1]) ** 2) / (2 * sigma ** 2))
    # Threshold the heatmap so that values below a certain threshold are set to 0
    binary_heatmap = np.where(heatmap < 0.5*heatmap.max(), 0, 1)

    assert is_binary_image(binary_heatmap), "Image is not binary"

    return binary_heatmap

def is_binary_image(image):
    binary_check = np.logical_or(image == 0, image == 1)
    return binary_check.all()
## -----------------------------------------------------------------------------------------------------------------##
##                                   LANDMARKS EXTRACTION FROM HEATMAPS                                             ##
## -----------------------------------------------------------------------------------------------------------------##

def extract_landmarks(heatmaps, num_landmarks):
    landmarks = np.zeros((num_landmarks, 2), dtype=np.float64)
    heatmap_height, heatmap_width = heatmaps.shape[1], heatmaps.shape[2]

    # Loop all the heatmaps (one for each landmark)
    for i in range(num_landmarks):
        heatmap_channel = heatmaps[i]  # get the heatmap number i
        
        # binarize the heatmap channel using a threshold
        binary_img = np.where(heatmap_channel < 0.5 * heatmap_channel.max(), 0, 1)
        binary_img = binary_img.astype(np.uint8)  # convert the binary image to uint8 datatype

        assert is_binary_image(binary_img), "Image is not binary"

        contours, _ = cv2.findContours(binary_img, cv2.RETR_EXTERNAL,
                                       cv2.CHAIN_APPROX_SIMPLE)  # find the contours in the binary image

        if contours:
            max_contour = max(contours, key=cv2.contourArea)  # find the contour with the maximum area
            M = cv2.moments(max_contour)  # calculate the moments of the maximum contour

            if M['m00'] != 0:  # avoid divide by zero error
                centroid_x = M['m10'] / M['m00']  # calculate the x-coordinate of centroid
                centroid_y = M['m01'] / M['m00']  # calculate the y-coordinate of centroid
                landmarks[i, :] = [centroid_x / heatmap_height,
                                   centroid_y / heatmap_width]  # normalize the coordinates

    return landmarks


## -----------------------------------------------------------------------------------------------------------------##
##                                                  PLOT LOSS CURVES                                                ##
## -----------------------------------------------------------------------------------------------------------------##

def plot_loss_curves(results_path: str, save_dir: str = None):
    # Load the results dictionary
    results = torch.load(results_path)['results']
    # Get the loss values of the results dictionary (training and validation)
    train_loss = results['train_loss']
    val_loss = results['val_loss']

    # Figure out how many epochs there were
    epochs = range(1, len(results['train_loss']) + 1)

    # Plot loss
    plt.figure(figsize=(10, 5))
    plt.plot(epochs, train_loss, label='train_loss')
    plt.plot(epochs, val_loss, label='val_loss')
    plt.title('Loss')
    plt.xlabel('Epochs')
    plt.legend()

    if save_dir is not None:
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        plt.savefig(os.path.join(save_dir, f"loss_epochs{epochs}"))

    plt.show()

# -----------------------------------------------------------------------------------------------------------------##
# 
# -----------------------------------------------------------------------------------------------------------------##

# Function to generate the save model path for both custom model and segmentation model.
def generate_save_model_path(PREFIX, model_name, dataset_name, sigma, size, pretrained=None, backbone=None):
    if pretrained is not None:
        pretrained_dir = pretrained
    else:
        pretrained_dir = "no_pretrain"

    if backbone is not None:
        backbone_dir = backbone
    else:
        backbone_dir = "no_backbone"

    save_model_path = f'{PREFIX}/results/models/{model_name}/{pretrained_dir}/{backbone_dir}/{dataset_name}/sigma{sigma}_size{str(size).replace(", ", "x")}'
    return save_model_path

# Generate path if it does not exist
def generate_path(path):
    if not os.path.exists(path):
        os.makedirs(path)
    return path

# Save the: original image with the heatmaps overlayed, single heatmaps and fused heatmap
def save_heatmaps(batch_images, batch_heatmaps, images_name, save_dir):
    for i, sample in enumerate(batch_heatmaps):
        
        original_image = batch_images[i].permute(1, 2, 0).cpu().numpy()
        
        plt.figure(figsize=(10, 10))
        plt.imshow(original_image)
        for j, heatmap in enumerate(sample):
            plt.imshow(heatmap, cmap='viridis', alpha=0.25)
            
            # Save the single heatmaps
            plt.imsave(f"{save_dir}/{images_name[i]}_heatmap_{j}.png", heatmap, cmap='viridis')

        # Save the original image with the heatmaps overlayed
        plt.savefig(f"{save_dir}/{images_name[i]}_overlayed_heatmaps.png")
        plt.close()
        
        # Save the fused heatmap
        fused_heatmap = fuse_heatmaps(sample)
        plt.imsave(f"{save_dir}/{images_name[i]}_fused_heatmap.png", fused_heatmap, cmap='viridis')
        


# -----------------------------------------------------------------------------------------------------------------##

# Load env variables from a .env file
def load_env_variables(env_file):
    with open(env_file, 'r') as f:
        for line in f:
            key, value = line.strip().split('=')
            # trim whitespace
            key = key.strip()
            value = value.strip()
            # remove quotes if present
            if value[0] == value[-1] and value.startswith(("'", '"')):
                value = value[1:-1]

            os.environ[key] = value

def send_telegram_message(text, env_file='~/.env'):
    env_file_path = os.path.expanduser(env_file)

    if os.path.exists(env_file_path):
        load_env_variables(env_file_path)
    else:
        print(f"Error: {env_file_path} does not exist")
        return
    
    token = os.getenv('TELEGRAM_TOKEN')
    chat_id = os.getenv('TELEGRAM_CHAT_ID')
    
    url_req = "https://api.telegram.org/bot" + token + "/sendMessage"
    payload = {
        'chat_id': chat_id,
        'text': text,
        'parse_mode': 'HTML' #or HTML or MarkdownV2
    }
    
    try:
        response = requests.get(url_req, params=payload)
    except Exception as e:
        print(f"Error sending telegram message: {e}")

    return response