forked from warmspringwinds/tf-image-segmentation
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtf_records.py
185 lines (141 loc) · 6.36 KB
/
tf_records.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Important: We are using PIL to read .png files later.
# This was done on purpose to read indexed png files
# in a special way -- only indexes and not map the indexes
# to actual rgb values. This is specific to PASCAL VOC
# dataset data. If you don't want thit type of behaviour
# consider using skimage.io.imread()
from PIL import Image
import numpy as np
import skimage.io as io
import tensorflow as tf
# For comparing tf versions for backwards compatibility
from packaging import version
# Helper functions for defining tf types
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
def _int64_feature(value):
return tf.train.Feature(int64_list=tf.train.Int64List(value=[value]))
def write_image_annotation_pairs_to_tfrecord(filename_pairs, tfrecords_filename):
"""Writes given image/annotation pairs to the tfrecords file.
The function reads each image/annotation pair given filenames
of image and respective annotation and writes it to the tfrecord
file.
Parameters
----------
filename_pairs : array of tuples (img_filepath, annotation_filepath)
Array of tuples of image/annotation filenames
tfrecords_filename : string
Tfrecords filename to write the image/annotation pairs
"""
writer = tf.python_io.TFRecordWriter(tfrecords_filename)
i = 0
for img_path, annotation_path in filename_pairs:
img = np.array(Image.open(img_path))
if img.ndim == 2:
img2 = np.zeros((img.shape[0], img.shape[1], 3), dtype=np.uint8 )
img2[:,:,1] = img
img2[:,:,2] = img
img = img2
annotation = np.array(Image.open(annotation_path))
# Unomment this one when working with surgical data
# annotation = annotation[:, :, 0]
# The reason to store image sizes was demonstrated
# in the previous example -- we have to know sizes
# of images to later read raw serialized string,
# convert to 1d array and convert to respective
# shape that image used to have.
height = img.shape[0]
width = img.shape[1]
img_raw = img.tostring()
annotation_raw = annotation.tostring()
example = tf.train.Example(features=tf.train.Features(feature={
'height': _int64_feature(height),
'width': _int64_feature(width),
'image_raw': _bytes_feature(img_raw),
'mask_raw': _bytes_feature(annotation_raw)}))
writer.write(example.SerializeToString())
if i%1000 == 0:
print("Processed " + str(i) + " images...")
i = i+1
print("Processed " + str(i) + " images...")
print("Done!")
writer.close()
def read_image_annotation_pairs_from_tfrecord(tfrecords_filename):
"""Return image/annotation pairs from the tfrecords file.
The function reads the tfrecords file and returns image
and respective annotation matrices pairs.
Parameters
----------
tfrecords_filename : string
filename of .tfrecords file to read from
Returns
-------
image_annotation_pairs : array of tuples (img, annotation)
The image and annotation that were read from the file
"""
image_annotation_pairs = []
record_iterator = tf.python_io.tf_record_iterator(path=tfrecords_filename)
for string_record in record_iterator:
example = tf.train.Example()
example.ParseFromString(string_record)
height = int(example.features.feature['height']
.int64_list
.value[0])
width = int(example.features.feature['width']
.int64_list
.value[0])
img_string = (example.features.feature['image_raw']
.bytes_list
.value[0])
annotation_string = (example.features.feature['mask_raw']
.bytes_list
.value[0])
img_1d = np.fromstring(img_string, dtype=np.uint8)
img = img_1d.reshape((height, width, -1))
annotation_1d = np.fromstring(annotation_string, dtype=np.uint8)
# Annotations don't have depth (3rd dimension)
# TODO: check if it works for other datasets
annotation = annotation_1d.reshape((height, width))
image_annotation_pairs.append((img, annotation))
return image_annotation_pairs
def read_tfrecord_and_decode_into_image_annotation_pair_tensors(tfrecord_filenames_queue):
"""Return image/annotation tensors that are created by reading tfrecord file.
The function accepts tfrecord filenames queue as an input which is usually
can be created using tf.train.string_input_producer() where filename
is specified with desired number of epochs. This function takes queue
produced by aforemention tf.train.string_input_producer() and defines
tensors converted from raw binary representations into
reshaped image/annotation tensors.
Parameters
----------
tfrecord_filenames_queue : tfrecord filename queue
String queue object from tf.train.string_input_producer()
Returns
-------
image, annotation : tuple of tf.int32 (image, annotation)
Tuple of image/annotation tensors
"""
reader = tf.TFRecordReader()
_, serialized_example = reader.read(tfrecord_filenames_queue)
features = tf.parse_single_example(
serialized_example,
features={
'height': tf.FixedLenFeature([], tf.int64),
'width': tf.FixedLenFeature([], tf.int64),
'image_raw': tf.FixedLenFeature([], tf.string),
'mask_raw': tf.FixedLenFeature([], tf.string)
})
image = tf.decode_raw(features['image_raw'], tf.uint8)
annotation = tf.decode_raw(features['mask_raw'], tf.uint8)
height = tf.cast(features['height'], tf.int32)
width = tf.cast(features['width'], tf.int32)
image_shape = tf.stack([height, width, 3])
# The last dimension was added because
# the tf.resize_image_with_crop_or_pad() accepts tensors
# that have depth. We need resize and crop later.
# TODO: See if it is necessary and probably remove third
# dimension
annotation_shape = tf.stack([height, width, 1])
image = tf.reshape(image, image_shape)
annotation = tf.reshape(annotation, annotation_shape)
return image, annotation