Spaces:
Sleeping
Sleeping
File size: 3,424 Bytes
b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c b5cbaa6 8e6512c |
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 |
import os
import random
import time
import cv2
import numpy
import skimage
import torch
from sklearn.metrics import ( # recall = sensitivity, precision = PPV
accuracy_score,
f1_score,
jaccard_score,
precision_score,
recall_score,
roc_auc_score,
)
""" Seeding the randomness. """
def seeding(seed):
random.seed(seed)
os.environ["PYTHONHASHSEED"] = str(seed)
numpy.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.backends.cudnn.deterministic = True
""" Create a directory. """
def create_dir(path):
if not os.path.exists(path):
os.makedirs(path)
else:
pass
""" Calculate the time taken """
def epoch_time(start_time, end_time):
elapsed_time = end_time - start_time
elapsed_mins = int(elapsed_time / 60)
elapsed_secs = int(elapsed_time - (elapsed_mins * 60))
return elapsed_mins, elapsed_secs
"""METRICS"""
def metricsCalculator(y_true, y_pred):
score_jaccard = jaccard_score(y_true, y_pred, pos_label=255)
score_f1 = f1_score(y_true, y_pred, pos_label=255)
score_recall = recall_score(y_true, y_pred, pos_label=255)
score_precision = precision_score(y_true, y_pred, pos_label=255)
score_acc = accuracy_score(y_true, y_pred)
score_auc = roc_auc_score(y_true, y_pred)
scores = [
score_jaccard,
score_f1,
score_recall,
score_precision,
score_acc,
score_auc,
]
print(
f"\t=>Jaccard: {score_jaccard:1.4f} - F1: {score_f1:1.4f} - Recall: {score_recall:1.4f} - Precision: {score_precision:1.4f} - Acc: {score_acc:1.4f} - AUC: {score_auc:1.4f}\n"
)
return scores
def skelEndpoints(maskArray):
skel = skimage.morphology.skeletonize(maskArray.astype("bool"))
skel = numpy.uint8(skel > 0)
# Apply the convolution.
kernel = numpy.uint8([[1, 1, 1], [1, 10, 1], [1, 1, 1]])
src_depth = -1
filtered = cv2.filter2D(skel, src_depth, kernel)
# Look through to find the value of 11.
# This returns a mask of the endpoints, but if you
# just want the coordinates, you could simply
# return numpy.where(filtered==11)
out = numpy.zeros_like(skel)
out[numpy.where(filtered == 11)] = 1
# endCoords = numpy.where(filtered==11)
# endCoords = list(zip(*endCoords))
# startPoint = endCoords[0]
# endPoint = endCoords[1]
# print(f"Skel starts at {startPoint} and finishes at {endPoint}")
# print(sum(out))
out = out.astype("uint8") * 255
return out
def crudeMaskGenerator(maskArray):
skel = skimage.morphology.skeletonize(maskArray.astype("bool"))
skel = numpy.uint8(skel > 0)
radius = 15
crudeMask = numpy.zeros_like(skel)
skelPoints = numpy.argwhere(skel > 0)
# Create a circular mask to dilate the skel
y, x = numpy.ogrid[-radius : radius + 1, -radius : radius + 1]
circleMask = x**2 + y**2 <= radius**2
for i, point in enumerate(skelPoints[:-1]):
yPos = point[0]
xPos = point[1]
if yPos < skel.shape[0] - radius and xPos < skel.shape[1] - radius:
if yPos > radius and xPos > radius:
crudeMask[
int(yPos - radius) : int(yPos + radius + 1),
int(xPos - radius) : int(xPos + radius + 1),
] += circleMask
crudeMask = crudeMask > 0
return crudeMask.astype("uint8") * 255
|