-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
154 lines (129 loc) · 5.44 KB
/
test.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
import os
os.environ.setdefault('PATH', '')
import numpy as np
import glob
import tensorflow as tf
# path to the images and the text file which holds the scores and ids
base_images_path = 'distorted_images/'
ava_dataset_path = 'class.txt'
IMAGE_SIZE = 224
files = glob.glob(base_images_path + "*.bmp")
files = sorted(files)
train_image_paths = []
train_scores = []
print("Loading training set and val set")
with open(ava_dataset_path, mode='r') as f:
lines = f.readlines()
for i, line in enumerate(lines):
token = line.split()
values = np.array(token[:10], dtype='float32')
file_path = base_images_path + token[10]
if os.path.exists(file_path):
train_image_paths.append(file_path)
train_scores.append(values)
elif os.path.exists(base_images_path + 'I'+token[10][1:]):
train_image_paths.append(base_images_path + 'I'+token[10][1:])
train_scores.append(values)
elif os.path.exists(base_images_path + 'I'+token[10][1:-3]+'BMP'):
train_image_paths.append(base_images_path + 'I'+token[10][1:-3]+'BMP')
train_scores.append(values)
count = 3000 // 20
if i % count == 0 and i != 0:
print('Loaded %d percent of the dataset' % (i / 3000. * 100))
train_image_paths = np.array(train_image_paths)
train_scores = np.array(train_scores, dtype='float32')
val_image_paths = train_image_paths[-300:]
val_scores = train_scores[-300:]
train_image_paths = train_image_paths[:-300]
train_scores = train_scores[:-300]
print('Train set size : ', train_image_paths.shape, train_scores.shape)
print('Val set size : ', val_image_paths.shape, val_scores.shape)
print('Train and validation datasets ready !')
def parse_data(filename, scores):
'''
Loads the image file, and randomly applies crops and flips to each image.
Args:
filename: the filename from the record
scores: the scores from the record
Returns:
an image referred to by the filename and its scores
'''
image = tf.read_file(filename)
image = tf.image.decode_bmp(image, channels=3)
image = tf.image.resize_images(image, (256, 256))
image = tf.random_crop(image, size=(IMAGE_SIZE, IMAGE_SIZE, 3))
image = tf.image.random_flip_left_right(image)
image = (tf.cast(image, tf.float32) - 127.5) / 127.5
return image, scores
def parse_data_without_augmentation(filename, scores):
'''
Loads the image file without any augmentation. Used for validation set.
Args:
filename: the filename from the record
scores: the scores from the record
Returns:
an image referred to by the filename and its scores
'''
image = tf.read_file(filename)
image = tf.image.decode_bmp(image, channels=3)
image = tf.image.resize_images(image, (IMAGE_SIZE, IMAGE_SIZE))
image = (tf.cast(image, tf.float32) - 127.5) / 127.5
return image, scores
def train_generator(batchsize, shuffle=True):
'''
Creates a python generator that loads the AVA dataset images with random data
augmentation and generates numpy arrays to feed into the Keras model for training.
Args:
batchsize: batchsize for training
shuffle: whether to shuffle the dataset
Returns:
a batch of samples (X_images, y_scores)
'''
with tf.Session() as sess:
# create a dataset
train_dataset = tf.data.Dataset().from_tensor_slices((train_image_paths, train_scores))
train_dataset = train_dataset.map(parse_data, num_parallel_calls=2)
train_dataset = train_dataset.batch(batchsize)
train_dataset = train_dataset.repeat()
if shuffle:
train_dataset = train_dataset.shuffle(buffer_size=4)
train_iterator = train_dataset.make_initializable_iterator()
train_batch = train_iterator.get_next()
sess.run(train_iterator.initializer)
while True:
try:
X_batch, y_batch = sess.run(train_batch)
yield (X_batch, y_batch)
except:
train_iterator = train_dataset.make_initializable_iterator()
sess.run(train_iterator.initializer)
train_batch = train_iterator.get_next()
X_batch, y_batch = sess.run(train_batch)
yield (X_batch, y_batch)
def val_generator(batchsize):
'''
Creates a python generator that loads the AVA dataset images without random data
augmentation and generates numpy arrays to feed into the Keras model for training.
Args:
batchsize: batchsize for validation set
Returns:
a batch of samples (X_images, y_scores)
'''
with tf.Session() as sess:
val_dataset = tf.data.Dataset().from_tensor_slices((val_image_paths, val_scores))
val_dataset = val_dataset.map(parse_data_without_augmentation)
val_dataset = val_dataset.batch(batchsize)
val_dataset = val_dataset.repeat()
val_iterator = val_dataset.make_initializable_iterator()
val_batch = val_iterator.get_next()
sess.run(val_iterator.initializer)
while True:
try:
X_batch, y_batch = sess.run(val_batch)
yield (X_batch, y_batch)
except:
val_iterator = val_dataset.make_initializable_iterator()
sess.run(val_iterator.initializer)
val_batch = val_iterator.get_next()
X_batch, y_batch = sess.run(val_batch)
yield (X_batch, y_batch)