-
Notifications
You must be signed in to change notification settings - Fork 1
/
read_from_tfrecords.py
79 lines (60 loc) · 1.81 KB
/
read_from_tfrecords.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
import tensorflow as tf
import os
from data_provider import _parse_image_function
import cv2
SHOW_IMAGE = True
SAVE_IMAGE = False
TF_RECORD_FILES = [
'train.tfrecords',
'test.tfrecords',
]
# Create directories if they do not exist
if (SAVE_IMAGE):
path = TF_RECORD_FILES[0].split('.')[0]
path_exists = os.path.exists(path)
if not path_exists:
os.makedirs(path)
image_path = os.path.join(path, 'image')
image_path_exists = os.path.exists(image_path)
if not image_path_exists:
os.makedirs(image_path)
label_path = os.path.join(path, 'label')
label_path_exists = os.path.exists(label_path)
if not label_path_exists:
os.makedirs(label_path)
dataset = tf.data.TFRecordDataset(
TF_RECORD_FILES
)
dataset = dataset.map(_parse_image_function)
train_set = dataset
train_iterator = train_set.make_initializable_iterator()
next_train_batch = train_iterator.get_next()
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess = tf.Session(config=config)
def show_label(label, cnt):
label = label.astype('uint8')
label = cv2.cvtColor(label, cv2.COLOR_RGB2BGR)
if (SHOW_IMAGE):
cv2.imshow('label', label)
cv2.waitKey(0)
if (SAVE_IMAGE):
cv2.imwrite(label_path + '/' + str(cnt) + '.png', label)
def show_image(image, cnt):
image = image.astype('uint8')
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
if (SHOW_IMAGE):
cv2.imshow('image', image)
cv2.waitKey(0)
if (SAVE_IMAGE):
cv2.imwrite(image_path + '/' + str(cnt) + '.png', image)
sess.run(train_iterator.initializer)
cnt = 0
while True:
cnt += 1
b = sess.run(next_train_batch)
image = b[0]
show_image(image, cnt)
label = b[1]
show_label(label, cnt)
print(cnt)