-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset_extract_features_cnn.py
executable file
·187 lines (152 loc) · 6.91 KB
/
dataset_extract_features_cnn.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
#!/usr/bin/env python
# -*- coding: utf-8 -*
# The MIT License (MIT)
# Copyright (c) 2018 Daniel Koguciuk <daniel.koguciuk@gmail.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
# OR OTHER DEALINGS IN THE SOFTWARE.
'''
@author: Daniel Koguciuk <daniel.koguciuk@gmail.com>
@note: Created on 04.07.2018
'''
import os
import sys
import cv2
import shutil
import argparse
import numpy as np
from tqdm import tqdm
import tensorflow as tf
from utils import data_generator as gen
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
FEAT_DIR = os.path.join(BASE_DIR, 'features')
CNNF_DIR = os.path.join(FEAT_DIR, 'cnn')
MDLS_DIR = os.path.join(BASE_DIR, 'models')
RSRC_DIR = os.path.join(MDLS_DIR, 'research')
SLIM_DIR = os.path.join(RSRC_DIR, 'slim')
sys.path.append(SLIM_DIR)
import nets.inception as inception_model
IMAGE_SIZE = 299
BATCH_SIZE = 4
INCEPTION_MODEL_PATH = 'inception/inception_v3.ckpt'
class InceptionV3(object):
def __init__(self, sess):
# placeholder
self.input_placeholder = tf.placeholder(tf.float32, shape=(None, 299, 299, 3))
# inception features extractor
device = tf.device('device:CPU:0')
if tf.test.is_gpu_available():
device = tf.device('device:GPU:0')
with device:
with tf.contrib.slim.arg_scope(inception_model.inception_v3_arg_scope()):
self.features_extractor, _ = inception_model.inception_v3(self.input_placeholder,
num_classes=0, is_training=False)
# init
init_fn = tf.contrib.slim.assign_from_checkpoint_fn(INCEPTION_MODEL_PATH,
tf.contrib.slim.get_model_variables("InceptionV3"))
init_fn(sess)
def augment_data_mirror(images, labels):
"""
Augment images with mirroring.
Args:
images (list of ndarrays of shape [IMAGE_SIZE, IMAGE_SIZE, 3]): Original images to be augmented.
Returns:
(list of ndarrays of shape [IMAGE_SIZE, IMAGE_SIZE, 3]): Augmented images.
"""
# flip images horizontaly =================================================
images_flipped = []
labels_flipped = []
for idx in range(len(images)):
images_flipped.append(cv2.flip(images[idx], 1))
labels_flipped.append(labels[idx])
images_flipped.append(images[idx])
labels_flipped.append(labels[idx])
# return
return images_flipped, labels_flipped
def calc_features(train, multiplications, augmentation, inception, sess, category='species'):
"""
Calculate cnn features with inception v3.
Args:
train (bool): Am I working with train or test data?
multiplications (int): How many times should I augment data (should be 1 for
test dataset).
augmentation (int): Augmentation level, either 0, 1 or 2.
inception (InceptionV3 object): Inception model.
sess (Tensorflow session): Tensorflow session.
"""
# counter
instances_counter = { str(idx) : 0 for idx in range(10)}
# for every batch
generator = gen.OxfordIIITPets(colorspace='RGB', train_size=0.8)
batches = generator.images_count(train) / BATCH_SIZE
all_featrs = []
all_labels = []
for _ in range(multiplications):
for batch_images, batch_labels in tqdm(generator.generate_batch(train=train, batch_size=BATCH_SIZE, category=category, image_size=IMAGE_SIZE), total=batches):
# augmentation
if augmentation == 1:
images_augmented, labels_augmented = augment_data_mirror(batch_images, batch_labels)
else:
images_augmented, labels_augmented = batch_images, batch_labels
# norm
images_normalized = [cv2.normalize(src=image, dst=None, alpha=-1.0, beta=1.0, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F)
for image in images_augmented]
# to tf format
np_images_data = [np.asarray(image_normalized) for image_normalized in images_normalized]
# stack
tf_images_data = np.stack(np_images_data, axis=0)
# calc features
features = sess.run(inception.features_extractor, feed_dict={inception.input_placeholder : tf_images_data})
all_featrs.append(np.squeeze(features))
all_labels.append(labels_augmented)
# Save it!
all_featrs = np.concatenate(all_featrs, axis=0)
all_labels = np.concatenate(all_labels, axis=0)
return all_featrs, all_labels
def main(argv):
# Parser
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--augmentation_level", help="augment dataset: option 0, 1 or 2, 3", type=int, default=0)
parser.add_argument("-m", "--multiplications", help="how many times should I augment training dataset?", type=int, default=1)
args = vars(parser.parse_args())
# assert aug level
if args['augmentation_level'] != 0 and args['augmentation_level'] != 1:
assert False, "augmentation level can be one of: 0, 1"
# assert features dir
out_dir = CNNF_DIR + "-" + str(args['augmentation_level'])
if os.path.exists(out_dir):
shutil.rmtree(out_dir)
os.mkdir(out_dir)
# tf session & graph
sess = tf.InteractiveSession()
graph = tf.Graph()
graph.as_default()
# InceptionV3
inception = InceptionV3(sess)
# train
print("Calculating features for train dir...")
features_train, labels_train = calc_features(True, args['multiplications'], args['augmentation_level'], inception, sess)
np.save(os.path.join(out_dir, "features_train.npy"), features_train)
np.save(os.path.join(out_dir, "labels_train.npy"), labels_train)
# test
print("Calculating features for test dir...")
features_test, labels_test = calc_features(False, 1, 0, inception, sess)
np.save(os.path.join(out_dir, "features_test.npy"), features_test)
np.save(os.path.join(out_dir, "labels_test.npy"), labels_test)
if __name__ == "__main__":
main(sys.argv[1:])