-
Notifications
You must be signed in to change notification settings - Fork 69
/
utils.py
60 lines (51 loc) · 1.53 KB
/
utils.py
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
import os
import numpy as np
import cv2
import json
from glob import glob
from metrics import *
from sklearn.utils import shuffle
from tensorflow.keras.utils import CustomObjectScope
from tensorflow.keras.models import load_model
from model import build_model, Upsample, ASPP
def create_dir(path):
""" Create a directory. """
try:
if not os.path.exists(path):
os.makedirs(path)
except OSError:
print(f"Error: creating directory with name {path}")
def read_data(x, y):
""" Read the image and mask from the given path. """
image = cv2.imread(x, cv2.IMREAD_COLOR)
mask = cv2.imread(y, cv2.IMREAD_COLOR)
return image, mask
def read_params():
""" Reading the parameters from the JSON file."""
with open("params.json", "r") as f:
data = f.read()
params = json.loads(data)
return params
def load_data(path):
""" Loading the data from the given path. """
images_path = os.path.join(path, "image/*")
masks_path = os.path.join(path, "mask/*")
images = glob(images_path)
masks = glob(masks_path)
return images, masks
def shuffling(x, y):
x, y = shuffle(x, y, random_state=42)
return x, y
def load_model_weight(path):
with CustomObjectScope({
'dice_loss': dice_loss,
'dice_coef': dice_coef,
'bce_dice_loss': bce_dice_loss,
'focal_loss': focal_loss,
'iou': iou
}):
model = load_model(path)
return model
# model = build_model(256)
# model.load_weights(path)
# return model