-
Notifications
You must be signed in to change notification settings - Fork 0
/
TensorflowUtils.py
161 lines (158 loc) · 5.75 KB
/
TensorflowUtils.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
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
# Utils used with tensorflow implemetation
import numpy as np
import scipy.misc as misc
import os, sys
from six.moves import urllib
import tarfile
import zipfile
import scipy.io
import copy
from PIL import Image
from Utils import SeparateStains
import matplotlib.pylab as plt
import scipy.misc as misc
# from torchvision import transforms
def preprocess1(input):
#input = copy.deepcopy(valid_images)
input = copy.deepcopy(np.float64(input))
s = input.shape
if len(s) == 4:
for i in range(s[0]):
input1 = copy.deepcopy(input[i, :, :, :])
for j in range(s[-1]):
b = copy.deepcopy(input1[:, :, j])
b1 = b - np.mean(b)
b = copy.deepcopy(b1)
input1[:, :, j] = copy.deepcopy(b)
input[i, :, :, :] = copy.deepcopy(input1)
else:
input1 = copy.deepcopy(input)
for j in range(s[-1]):
b = copy.deepcopy(input1[:, :, j])
b1 = b - np.mean(b)
b = copy.deepcopy(b1)
input1[:, :, j] = copy.deepcopy(b)
input = copy.deepcopy(input1)
return input
def preprocess(inputi):
#input = copy.deepcopy(valid_images)
s = inputi.shape
if len(s) == 4:
input = np.zeros([s[0], s[1], s[2], s[3]])
# for i in range(s[0]):
# input1 = copy.deepcopy(input[i, :, :, :])
# for j in range(s[-1]):
# b = copy.deepcopy(input1[:, :, j])
# b1 = b - np.mean(b)
# b = copy.deepcopy(b1)
# input1[:, :, j] = copy.deepcopy(b)
# input[i, :, :, :] = copy.deepcopy(input1)
ele = 1
#imageURL = 'D:/redo3/images/training/1972.png'
#b1 = misc.imread('D:/redo3/annotations/training/1972.png')
#imageRGB = misc.imread(imageURL)
He = np.array([0.644211, 0.716556, 0.266844], 'float64')
Eo = np.array([0.092789, 0.954111, 0.283111], 'float64')
Res = np.array([0, 0, 0], 'float64')
for i in range(3):
if He[i]**2+Eo[i]**2 > 1:
Res[i] = 0.001
else:
Res[i] = np.sqrt(1-He[i]**2-Eo[i]**2)
HDABtoRGB = np.matrix([He/np.linalg.norm(He), Eo/np.linalg.norm(Eo), Res/np.linalg.norm(Res)])
RGBtoHDAB = np.linalg.inv(HDABtoRGB)
for j in range(s[0]):
k = 0
k = k + 1
input1 = copy.deepcopy(inputi[j, :, :, :])
imageHDAB = SeparateStains(input1, RGBtoHDAB)
#misc.imsave('D:\\'+str(k)+'.png', imageHDAB[:, :, 0])
cc = np.zeros([s[1], s[2], s[3]])
c = copy.deepcopy(imageHDAB[:, :, 0])
if c.max() != c.min():
c = np.round(255*(c-c.min())/(c.max()-c.min()))
else:
c = np.round(255 + c-c.min())
c = copy.deepcopy(np.array(c, 'uint8'))
for i in range(3):
cc[:, :, i] = copy.deepcopy(c)
#misc.imsave('E:\\1.png', cc)
#cc1 = misc.imread('E:\\1.png')
input[j, :, :, :] = copy.deepcopy(cc)
input = preprocess1(input)
if len(s) == 3:
# for i in range(s[0]):
# input1 = copy.deepcopy(input[i, :, :, :])
# for j in range(s[-1]):
# b = copy.deepcopy(input1[:, :, j])
# b1 = b - np.mean(b)
# b = copy.deepcopy(b1)
# input1[:, :, j] = copy.deepcopy(b)
# input[i, :, :, :] = copy.deepcopy(input1)
ele = 1
# imageURL = 'D:/redo3/images/training/1972.png'
# b1 = misc.imread('D:/redo3/annotations/training/1972.png')
# imageRGB = misc.imread(imageURL)
He = np.array([0.644211, 0.716556, 0.266844], 'float64')
Eo = np.array([0.092789, 0.954111, 0.283111], 'float64')
Res = np.array([0, 0, 0], 'float64')
for i in range(3):
if He[i] ** 2 + Eo[i] ** 2 > 1:
Res[i] = 0.001
else:
Res[i] = np.sqrt(1 - He[i] ** 2 - Eo[i] ** 2)
HDABtoRGB = np.matrix([He / np.linalg.norm(He), Eo / np.linalg.norm(Eo), Res / np.linalg.norm(Res)])
RGBtoHDAB = np.linalg.inv(HDABtoRGB)
input1 = copy.deepcopy(inputi)
imageHDAB = SeparateStains(input1, RGBtoHDAB)
# misc.imsave('D:\\'+str(k)+'.png', imageHDAB[:, :, 0])
cc = np.zeros([s[0], s[1], s[2]])
c = copy.deepcopy(imageHDAB[:, :, 0])
if c.max() != c.min():
c = np.round(255 * (c - c.min()) / (c.max() - c.min()))
else:
c = np.round(255 + c - c.min())
c = copy.deepcopy(np.array(c, 'uint8'))
for i in range(3):
cc[:, :, i] = copy.deepcopy(c)
# misc.imsave('E:\\1.png', cc)
# cc1 = misc.imread('E:\\1.png')
input = preprocess1(cc)
return input
def resize_img(img_pil, scale, type, resize_size):
"""
input: PIL Image
return: PIL Image
"""
w = img_pil.size[0]
h = img_pil.size[1]
if not resize_size:
nw = int(w * scale)
nh = int(h * scale)
else:
nw, nh = resize_size
if type == "image":
img_pil = img_pil.resize((nw, nh), Image.ANTIALIAS)
elif type == "label":
img_pil = img_pil.resize((nw, nh), Image.NEAREST)
else:
img_pil = img_pil.resize((nw, nh), Image.ANTIALIAS)
return img_pil
def crop_img(im, size):
"""
crop image
"""
w = im.size[0]
h = im.size[1]
return im.crop(map(int, [w*(0.5-0.5/size), h*(0.5-0.5/size), w*(0.5+0.5/size), h*(0.5+0.5/size)]))
def trans(image, normalize):
'''
input: numpy.narray
'''
image_out = transforms.ToTensor()(image)
if normalize:
image_out = transforms.Normalize(
normalize["mean"],
normalize["std"]
)(image_out)
return image_out.numpy()