-
Notifications
You must be signed in to change notification settings - Fork 0
/
tiff2tfrecord.py
171 lines (163 loc) · 6.4 KB
/
tiff2tfrecord.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
162
163
164
165
166
167
168
169
170
import numpy as np
import tensorflow as tf
import cv2 as cv
import os
isize = (512, 512)
lsize = (128, 128)
PATH = "E:\\SC\\segmentation"
CANCERPATH = PATH + '\\cancer'
NONCANCERPATH = PATH + '\\non_cancer'
LABELPATH = PATH + '\\label'
cancer_imgs_f = os.listdir(CANCERPATH)
cancer_imgs_f.sort()
cancer_labels_f = os.listdir(LABELPATH)
cancer_labels_f.sort()
noncancer_imgs_f = os.listdir(NONCANCERPATH)
noncancer_imgs_f.sort()
cwd = "dataset"
tr_writer = tf.python_io.TFRecordWriter(cwd +"\\train.tfrecords")
for i in range(len(cancer_imgs_f)-80):
img = cv.imread(CANCERPATH+'\\'+cancer_imgs_f[i])
label = cv.imread(LABELPATH+'\\'+cancer_labels_f[i])
img = cv.resize(img, isize)
label = np.sum(label, axis=2)
label[label > 0] = 1
label = label.astype(np.uint8)
w, h, = label.shape[0], label.shape[1]
mask = np.zeros([w+2, h+2], dtype=np.uint8)
for sp in [(0, 0), (0, 2047), (2047, 0), (2047, 2047)]:
if label[sp[0]][sp[1]] == 0:
cv.floodFill(label, mask, sp, 1, 0, 0, cv.FLOODFILL_FIXED_RANGE)
tmp = np.zeros([w, h], np.uint8)
tmp[label == 0] = 1
label = tmp
label = cv.resize(label, lsize)
img = np.array(img)
label = np.array(label).astype(np.uint8)
for j in range(4):
img = np.rot90(img)
label = np.rot90(label)
features = tf.train.Features(
feature = {
"data": tf.train.Feature(bytes_list=tf.train.BytesList(value=[img.tostring()])),
"label": tf.train.Feature(bytes_list=tf.train.BytesList(value=[label.tostring()]))
}
)
example = tf.train.Example(features=features)
serialized = example.SerializeToString()
tr_writer.write(serialized)
img = np.fliplr(img)
label = np.fliplr(label)
for j in range(4):
img = np.rot90(img)
label = np.rot90(label)
features = tf.train.Features(
feature = {
"data": tf.train.Feature(bytes_list=tf.train.BytesList(value=[img.tostring()])),
"label": tf.train.Feature(bytes_list=tf.train.BytesList(value=[label.tostring()]))
}
)
example = tf.train.Example(features=features)
serialized = example.SerializeToString()
tr_writer.write(serialized)
for i in range(len(noncancer_imgs_f) - 20):
img = cv.imread(NONCANCERPATH + '\\' + noncancer_imgs_f[i])
img = cv.resize(img, isize)
img = np.array(img)
label = np.zeros(lsize, dtype=np.uint8)
for j in range(4):
img = np.rot90(img)
features = tf.train.Features(
feature = {
"data": tf.train.Feature(bytes_list=tf.train.BytesList(value=[img.tostring()])),
"label": tf.train.Feature(bytes_list=tf.train.BytesList(value=[label.tostring()]))
}
)
example = tf.train.Example(features=features)
serialized = example.SerializeToString()
tr_writer.write(serialized)
img = np.fliplr(img)
for j in range(4):
img = np.rot90(img)
features = tf.train.Features(
feature = {
"data": tf.train.Feature(bytes_list=tf.train.BytesList(value=[img.tostring()])),
"label": tf.train.Feature(bytes_list=tf.train.BytesList(value=[label.tostring()]))
}
)
example = tf.train.Example(features=features)
serialized = example.SerializeToString()
tr_writer.write(serialized)
te_writer = tf.python_io.TFRecordWriter(cwd +"\\test.tfrecords")
for i in range(len(cancer_imgs_f)-80, len(cancer_imgs_f)):
img = cv.imread(CANCERPATH + '\\' + cancer_imgs_f[i])
label = cv.imread(LABELPATH + '\\' + cancer_labels_f[i])
img = cv.resize(img, isize)
label = np.sum(label, axis=2)
label[label > 0] = 1
label = label.astype(np.uint8)
w, h, = label.shape[0], label.shape[1]
mask = np.zeros([w + 2, h + 2], dtype=np.uint8)
for sp in [(0, 0), (0, 2047), (2047, 0), (2047, 2047)]:
if label[sp[0]][sp[1]] == 0:
cv.floodFill(label, mask, sp, 1, 0, 0, cv.FLOODFILL_FIXED_RANGE)
tmp = np.zeros([w, h], np.uint8)
tmp[label == 0] = 1
label = tmp
label = cv.resize(label, lsize)
img = np.array(img)
label = np.array(label).astype(np.uint8)
for j in range(4):
img = np.rot90(img)
label = np.rot90(label)
features = tf.train.Features(
feature={
"data": tf.train.Feature(bytes_list=tf.train.BytesList(value=[img.tostring()])),
"label": tf.train.Feature(bytes_list=tf.train.BytesList(value=[label.tostring()]))
}
)
example = tf.train.Example(features=features)
serialized = example.SerializeToString()
te_writer.write(serialized)
img = np.fliplr(img)
label = np.fliplr(label)
for j in range(4):
img = np.rot90(img)
label = np.rot90(label)
features = tf.train.Features(
feature={
"data": tf.train.Feature(bytes_list=tf.train.BytesList(value=[img.tostring()])),
"label": tf.train.Feature(bytes_list=tf.train.BytesList(value=[label.tostring()]))
}
)
example = tf.train.Example(features=features)
serialized = example.SerializeToString()
te_writer.write(serialized)
for i in range(len(noncancer_imgs_f) - 20, len(noncancer_imgs_f)):
img = cv.imread(NONCANCERPATH + '\\' + noncancer_imgs_f[i])
img = cv.resize(img, isize)
img = np.array(img)
label = np.zeros(lsize, dtype=np.uint8)
for j in range(4):
img = np.rot90(img)
features = tf.train.Features(
feature={
"data": tf.train.Feature(bytes_list=tf.train.BytesList(value=[img.tostring()])),
"label": tf.train.Feature(bytes_list=tf.train.BytesList(value=[label.tostring()]))
}
)
example = tf.train.Example(features=features)
serialized = example.SerializeToString()
te_writer.write(serialized)
img = np.fliplr(img)
for j in range(4):
img = np.rot90(img)
features = tf.train.Features(
feature={
"data": tf.train.Feature(bytes_list=tf.train.BytesList(value=[img.tostring()])),
"label": tf.train.Feature(bytes_list=tf.train.BytesList(value=[label.tostring()]))
}
)
example = tf.train.Example(features=features)
serialized = example.SerializeToString()
te_writer.write(serialized)