-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdataprocess.py
More file actions
81 lines (62 loc) · 2.7 KB
/
dataprocess.py
File metadata and controls
81 lines (62 loc) · 2.7 KB
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
import tensorflow as tf
import numpy as np
from tensorflow.contrib.data import Dataset
from tensorflow.python.framework import dtypes
from tensorflow.python.framework.ops import convert_to_tensor
MEAN = tf.constant([123.68, 116.779, 103.939], dtype=tf.float32)
class ImageDataGenerator(object):
def __init__(self, txt_file, mode, batch_size, num_classes, shuffle=True,
buffer_size=1000):
self.txt_file = txt_file
self.num_classes = num_classes
self._read_txt_file()
self.data_size = len(self.labels)
if shuffle:
self._shuffle_lists()
self.img_paths = convert_to_tensor(self.img_paths, dtype=dtypes.string)
self.labels = convert_to_tensor(self.labels, dtype=dtypes.int32)
data = Dataset.from_tensor_slices((self.img_paths, self.labels))
if mode == 'training':
data = data.map(self._parse_function_train)
elif mode == 'inference':
data = data.map(self._parse_function_inference)
else:
raise ValueError("Invalid mode '%s'." % (mode))
if shuffle:
data = data.shuffle(buffer_size=buffer_size)
data = data.batch(batch_size)
self.data = data
def _read_txt_file(self):
self.img_paths = []
self.labels = []
with open(self.txt_file, 'r') as f:
lines = f.readlines()
for line in lines:
items = line.split(' ')
self.img_paths.append(items[0])
self.labels.append(int(items[1]))
def _shuffle_lists(self):
path = self.img_paths
labels = self.labels
permutation = np.random.permutation(self.data_size)
self.img_paths = []
self.labels = []
for i in permutation:
self.img_paths.append(path[i])
self.labels.append(labels[i])
def _parse_function_train(self, filename, label):
one_hot = tf.one_hot(label, self.num_classes)
img_string = tf.read_file(filename)
img_decoded = tf.image.decode_png(img_string, channels=3)
img_resized = tf.image.resize_images(img_decoded, [227, 227])
img_centered = tf.subtract(img_resized, MEAN)
img_bgr = img_centered[:, :, ::-1]
return filename, img_bgr, one_hot
def _parse_function_inference(self, filename, label):
one_hot = tf.one_hot(label, self.num_classes)
img_string = tf.read_file(filename)
img_decoded = tf.image.decode_png(img_string, channels=3)
img_resized = tf.image.resize_images(img_decoded, [227, 227])
img_centered = tf.subtract(img_resized, MEAN)
img_bgr = img_centered[:, :, ::-1]
return filename, img_bgr, one_hot