-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutils.py
More file actions
56 lines (42 loc) · 1.58 KB
/
utils.py
File metadata and controls
56 lines (42 loc) · 1.58 KB
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
import os
import random
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import cv2
import matplotlib.pyplot as plt
import seaborn as sns
import tensorflow as tf
from tensorflow.keras import backend as K
plt.style.use("ggplot")
sns.set_theme(style="ticks")
def plot_from_img_path(rows, columns, list_img_path, list_mask_path):
fig = plt.figure(figsize=(12, 12))
for i in range(1, rows * columns + 1):
fig.add_subplot(rows, columns, i)
img_path = list_img_path[i]
mask_path = list_mask_path[i]
image = cv2.imread(img_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
mask = cv2.imread(mask_path)
mask = cv2.cvtColor(mask, cv2.COLOR_BGR2RGB)
plt.imshow(image)
plt.imshow(mask, alpha=0.4)
plt.show()
def dice_coefficients(y_true, y_pred, smooth=100):
y_true_flatten = K.flatten(y_true)
y_pred_flatten = K.flatten(y_pred)
intersection = K.sum(y_true_flatten * y_pred_flatten)
union = K.sum(y_true_flatten) + K.sum(y_pred_flatten)
return (2 * intersection + smooth) / (union + smooth)
def dice_coefficient_loss(y_true, y_pred, smooth=100):
return -(dice_coefficients(y_true, y_pred, smooth))
def iou(y_true, y_pred, smooth=100):
intersection = K.sum(y_true * y_pred)
sum = K.sum(y_true * y_pred)
iou = (intersection + smooth) / (sum - intersection + smooth)
return iou
def jaccard_distance(y_true, y_pred, smooth=100):
y_true_flatten = K.flatten(y_true)
y_pred_flatten = K.flatten(y_pred)
return 1 - iou(y_true_flatten, y_pred_flatten, smooth)