-
Notifications
You must be signed in to change notification settings - Fork 7
/
helper.py
80 lines (67 loc) · 2.47 KB
/
helper.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
"""
Version: v1.2
Date: 2021-01-12
Author: Mullissa A.G.
Description: This script contains helper functions that are used in synthesizing data tensors.
"""
import glob
import os
import cv2
import numpy as np
import tifffile
patch_size, stride = 40, 9
aug_times = 1
scales = [1, 0.9, 0.8, 0.7]
batch_size = 128
def gen_patches(file_name):
""" This function prepares patches using the parameters patch_size and stride"""
# read image
img = tifffile.imread(file_name)
img = np.array(img)
h, w, d = img.shape
patches = []
for s in scales:
h_scaled, w_scaled = int(h*s),int(w*s)
img_scaled = cv2.resize(img, (h_scaled,w_scaled), interpolation=cv2.INTER_CUBIC)
# extract patches
for i in range(0, h_scaled-patch_size+1, stride):
for j in range(0, w_scaled-patch_size+1, stride):
x = img_scaled[i:i+patch_size, j:j+patch_size,:]
patches.append(x)
return patches
def make_dataTensor(data_dir,verbose=False):
""" This function prepares the data tensor from a given directory by using gen_patches function"""
file_list = glob.glob(data_dir+'/*.tif') # get name list of all .tif files
# initrialize
data = []
# generate patches
for i in range(len(file_list)):
patch = gen_patches(file_list[i])
data.append(patch)
if verbose:
print(str(i+1)+'/'+ str(len(file_list)) + ' is done ^_^')
data = np.array(data)
data = data.reshape((data.shape[0]*data.shape[1],data.shape[2],data.shape[3],6))
discard_n = len(data)-len(data)//batch_size*batch_size;
data = np.delete(data,range(discard_n),axis = 0)
print('^_^-training data finished-^_^')
return data
def get_steps(data_dir, batch_size=128):
""" This function estimates the step per epoch in training the model from the given data tensor length"""
if os.path.isfile(data_dir):
noisy_files = [data_dir]
else:
noisy_files = glob.glob(data_dir + '/*.tif')
num = 0
#get number of steps per epoch to use in training
for data_file in noisy_files:
xs = make_dataTensor(data_dir)
if xs is not None:
num += len(xs)
print("total number of patches: {}".format(num))
print("steps per epoch: {}".format(num//batch_size))
print("")
return num // batch_size
if __name__ == '__main__':
data = make_dataTensor(data_dir='data/Train2')
label = make_dataTensor(data_dir='data/Label2')