-
Notifications
You must be signed in to change notification settings - Fork 42
/
data_train.py
278 lines (211 loc) · 9.97 KB
/
data_train.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
from six.moves import xrange # pylint: disable=redefined-builtin
import tensorflow as tf
import numpy.random
IMAGE_SIZE = 256
MAP_SIZE = 64
# Global constants describing the CIFAR-10 data set.
NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN = 40000
NUM_EXAMPLES_PER_EPOCH_FOR_EVAL = 62957
def readFromFile(filename_queue): #,metaname_queue):
class DataRecord(object):
pass
result = DataRecord()
# Count the bytes for each sample
result.height = 256
result.width = 256
result.depth = 3
result.dmap_height = 64
result.dmap_width = 64
dmap_bytes = result.dmap_height * result.dmap_width
image_bytes = result.height * result.width * result.depth
record_bytes = dmap_bytes + image_bytes + 1
#
# Read a record
data_reader = tf.FixedLengthRecordReader(record_bytes=record_bytes)
result.key, data_value = data_reader.read(filename_queue)
#
# Convert from a string to a vector of uint8 that is record_bytes long.
data_in_bytes = tf.decode_raw(data_value, tf.uint8)
#meta_in_bytes = tf.decode_raw(meta_value, tf.int64)
#
img = tf.reshape(
tf.strided_slice(data_in_bytes, [0],
[0 + image_bytes]),
[result.depth, result.height, result.width])
result.image = tf.cast(tf.transpose(img, [1, 2, 0]), tf.float32) / 256
#
dmap = tf.reshape(
tf.strided_slice(data_in_bytes, [image_bytes],
[image_bytes + dmap_bytes]),
[1, result.dmap_height, result.dmap_width])
result.dmap = tf.cast(tf.transpose(dmap, [1, 2, 0]), tf.float32) / 256
result.label = tf.cast(
tf.strided_slice(data_in_bytes, [image_bytes + dmap_bytes], [image_bytes + dmap_bytes + 1]), tf.int32)
return result
def _generate_image_and_label_batch(image, dmap, label, min_queue_examples,
batch_size, shuffle):
num_preprocess_threads = 16
if shuffle:
images, dmaps, labels = tf.train.shuffle_batch(
[image, dmap, label],
batch_size=batch_size,
num_threads=num_preprocess_threads,
capacity=min_queue_examples + 3 * batch_size,
allow_smaller_final_batch=False,
min_after_dequeue=min_queue_examples)
else:
images, dmaps, labels = tf.train.batch(
[image, dmap, label],
batch_size=batch_size,
num_threads=num_preprocess_threads,
allow_smaller_final_batch=False,
capacity=min_queue_examples + 3 * batch_size)
irgb, ihsv = tf.split(images, num_or_size_splits=2, axis=3)
# Display the training images in the visualizer.
tf.summary.image('input1', irgb)
tf.summary.image('input2', ihsv)
tf.summary.image('input3', dmaps)
return images, dmaps, labels, labels, labels
def distorted_inputs(data_dir, batch_size):
filenames11 = [os.path.join(data_dir, '/research/cvlshare/Databases/Oulu/bin/1s/train_%d.dat' % i)
for i in xrange(1,400)]
filenames = filenames11
metanames11 = [os.path.join(data_dir, '/data/train_demo/bin1/train_meta_%d.dat' % i)
for i in xrange(1,200)]
metanames12 = [os.path.join(data_dir, '/data/train_demo/bin2/train_meta_%d.dat' % i)
for i in xrange(1,200)]
metanames = metanames11 + metanames12 #+ metanames13 + metanames2 #+ metanames21
names = list(zip(filenames, metanames))
numpy.random.shuffle(names)
filenames, metanames = zip(*names)
num_examples_per_epoch = NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN
for f in filenames:
if not tf.gfile.Exists(f):
raise ValueError('Failed to find file: ' + f)
# Create a queue that produces the filenames to read.
filename_queue = tf.train.string_input_producer(filenames)
#metaname_queue = tf.train.string_input_producer(metanames)
# Read examples from files in the filename queue.
read_input = readFromFile(filename_queue)#, metaname_queue)
height = IMAGE_SIZE
width = IMAGE_SIZE
# data augmentation
distorted_image = read_input.image
#distorted_image = tf.image.random_flip_left_right(distorted_image)
hsv_image = tf.image.rgb_to_hsv(distorted_image)
float_image = tf.concat([hsv_image,distorted_image],axis = 2)
#
float_image.set_shape([height, width, 6])
read_input.dmap.set_shape([MAP_SIZE, MAP_SIZE, 1])
read_input.label.set_shape([1])
# Ensure that the random shuffling has good mixing properties.
min_fraction_of_examples_in_queue = 0.1
min_queue_examples = int(num_examples_per_epoch * min_fraction_of_examples_in_queue)
print ('Filling queue with %d CASIA AntiSpoofing images before starting to train. '
'This will take a few minutes.' % min_queue_examples)
# Generate a batch of images and labels by building up a queue of examples.
return _generate_image_and_label_batch(float_image, read_input.dmap, read_input.label,
min_queue_examples, batch_size,
shuffle=True)
def distorted_inputsA(data_dir, batch_size):
filenames21 = [os.path.join(data_dir, '/data/train_demo/bin4/train_%d.dat' % i)
for i in xrange(1,20)]
filenames = filenames21
metanames21 = [os.path.join(data_dir, '/data/train_demo/mix/train_meta_%d.dat' % i)
for i in xrange(1,20)]
metanames = metanames21
names = list(zip(filenames, metanames))
numpy.random.shuffle(names)
filenames, metanames = zip(*names)
num_examples_per_epoch = NUM_EXAMPLES_PER_EPOCH_FOR_TRAIN
for f in filenames:
if not tf.gfile.Exists(f):
raise ValueError('Failed to find file: ' + f)
# Create a queue that produces the filenames to read.
filename_queue = tf.train.string_input_producer(filenames)
metaname_queue = tf.train.string_input_producer(metanames)
# Read examples from files in the filename queue.
read_input = readFromFile(filename_queue, metaname_queue)
height = IMAGE_SIZE
width = IMAGE_SIZE
# data augmentation
distorted_image = read_input.image
#distorted_image = tf.image.random_flip_left_right(distorted_image)
hsv_image = tf.image.rgb_to_hsv(distorted_image)
float_image = tf.concat([hsv_image,distorted_image],axis = 2)
#float_image = distorted_image
# Set the shapes of tensors.
float_image.set_shape([height, width, 6])
read_input.dmap.set_shape([MAP_SIZE, MAP_SIZE, 1])
read_input.label.set_shape([1])
# Ensure that the random shuffling has good mixing properties.
min_fraction_of_examples_in_queue = 0.1
min_queue_examples = int(num_examples_per_epoch * min_fraction_of_examples_in_queue)
print ('Filling queue with %d CASIA AntiSpoofing images before starting to train. '
'This will take a few minutes.' % min_queue_examples)
# Generate a batch of images and labels by building up a queue of examples.
return _generate_image_and_label_batch(float_image, read_input.dmap, read_input.label, read_input.size, read_input.slabel,
min_queue_examples, batch_size,
shuffle=True)
def inputs(testset, data_dir, batch_size):
if testset == 1:
filenames = [os.path.join(data_dir, 'CASIA-FASD/CASIA_test_%d.dat' % i)
for i in xrange(1,11)]
metanames = [os.path.join(data_dir, 'CASIA-FASD/CASIA_test_meta_%d.dat' % i)
for i in xrange(1,11)]
elif testset == 2:
filenames1 = [os.path.join(data_dir, 'CASIA-FASD/CASIA_train_%d.dat' % i)
for i in xrange(1,11)]
filenames2 = [os.path.join(data_dir, 'New_DataSet/BONUS6_train_%d.dat' % i)
for i in xrange(1,11)]
filenames = filenames1
metanames1 = [os.path.join(data_dir, 'CASIA-FASD/CASIA_train_meta_%d.dat' % i)
for i in xrange(1,11)]
metanames2 = [os.path.join(data_dir, 'New_DataSet/BONUS6_train_meta_%d.dat' % i)
for i in xrange(1,11)]
metanames = metanames1
elif testset == 3:
filenames = [os.path.join(data_dir, 'REPLAY-ATTACK/REPLAY-ATTACK/IDIAP128_test_%d.dat' % i)
for i in xrange(1,11)]
metanames = [os.path.join(data_dir, 'REPLAY-ATTACK/REPLAY-ATTACK/IDIAP128_test_meta_%d.dat' % i)
for i in xrange(1,11)]
else:
filenames = [os.path.join(data_dir, 'REPLAY-ATTACK/REPLAY-ATTACK/IDIAP128_test_%d.dat' % i)
for i in xrange(1,11)]
metanames = [os.path.join(data_dir, 'REPLAY-ATTACK/REPLAY-ATTACK/IDIAP128_train_meta_%d.dat' % i)
for i in xrange(1,11)]
num_examples_per_epoch = NUM_EXAMPLES_PER_EPOCH_FOR_EVAL
for f in filenames:
if not tf.gfile.Exists(f):
raise ValueError('Failed to find file: ' + f)
# Create a queue that produces the filenames to read.
filename_queue = tf.train.string_input_producer(filenames)
metaname_queue = tf.train.string_input_producer(metanames)
# Read examples from files in the filename queue.
read_input = readFromFile(filename_queue, metaname_queue)
height = IMAGE_SIZE
width = IMAGE_SIZE
distorted_image = read_input.image
hsv_image = tf.image.rgb_to_hsv(distorted_image)
float_image = tf.concat([hsv_image,distorted_image],axis = 2)
# float_image = distorted_image
# Set the shapes of tensors.
float_image.set_shape([height, width, 6])
read_input.dmap.set_shape([MAP_SIZE, MAP_SIZE, 1])
read_input.label.set_shape([1])
read_input.size.set_shape([1])
read_input.slabel.set_shape([1])
# Ensure that the random shuffling has good mixing properties.
min_fraction_of_examples_in_queue = 0.05
min_queue_examples = int(num_examples_per_epoch *
min_fraction_of_examples_in_queue)
print ('Filling queue with %d CASIA AntiSpoofing images before starting to test. '
'This will take a few minutes.' % min_queue_examples)
# Generate a batch of images and labels by building up a queue of examples.
return _generate_image_and_label_batch(float_image, read_input.dmap, read_input.label, read_input.size, read_input.slabel,
min_queue_examples, batch_size,
shuffle=False)