[acd362]: / Projects / NCS1 / Classes / Data.py

Download this file

232 lines (170 with data), 8.2 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
 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
############################################################################################
#
# Project: Peter Moss Acute Myeloid & Lymphoblastic Leukemia AI Research Project
# Repository: ALL Detection System 2019
# Project: Facial Authentication Server
#
# Author: Adam Milton-Barker (AdamMiltonBarker.com)
# Contributors:
# Title: Data Class
# Description: Data class for the ALL Detection System 2019 NCS1 Classifier.
# License: MIT License
# Last Modified: 2020-07-16
#
############################################################################################
import cv2, glob, json, math, os, pathlib, random, sys, time
import numpy as np
import tensorflow as tf
from datetime import datetime
from PIL import Image
from sys import argv
from Classes.Helpers import Helpers
class Data():
""" Data Helper Class
Core data management class for the ALL Detection System 2019 NCS1 Classifier
"""
def __init__(self):
""" Initializes the Data Class. """
self.Helpers = Helpers("DataProcessor")
self.confs = self.Helpers.confs
self.Helpers.logger.info("Data helper class initialization complete.")
def getLabelsAndDirectories(self):
""" Returns a list of classes/labels and directories. """
labels = [name for name in os.listdir(self.confs["Classifier"]["DatasetDir"]) if os.path.isdir(
os.path.join(self.confs["Classifier"]["DatasetDir"], name)) and name != '.ipynb_checkpoints']
directories = []
for dirName in os.listdir(self.confs["Classifier"]["DatasetDir"]):
if dirName != '.ipynb_checkpoints':
path = os.path.join(
self.confs["Classifier"]["DatasetDir"], dirName)
if os.path.isdir(path):
directories.append(path)
return labels, directories
def processFilesAndClasses(self):
""" Returns a list of filenames and classes/labels. """
labels, directories = self.getLabelsAndDirectories()
data = []
for directory in directories:
for filename in os.listdir(directory):
if filename.endswith('.jpg') or filename.endswith('.jpeg') or filename.endswith('.png') or filename.endswith('.gif'):
data.append(os.path.join(directory, filename))
else:
continue
return data, sorted(labels)
def writeLabels(self, labels_to_labels):
"""
Writes a file with the list of class names.
Args:
labels_to_labels: A map of (integer) labels to class names.
filename: The filename where the class names are written.
"""
labelsFile = os.path.join(
self.confs["Classifier"]["DatasetDir"], self.confs["Classifier"]["Labels"])
classesFile = os.path.join(
self.confs["Classifier"]["DatasetDir"], self.confs["Classifier"]["Classes"])
with tf.gfile.Open(classesFile, 'w') as f:
for label in labels_to_labels:
f.write('%s\n' % (label))
with tf.gfile.Open(labelsFile, 'w') as f:
for label in labels_to_labels:
class_name = labels_to_labels[label]
f.write('%d:%s\n' % (label, class_name))
def convertToTFRecord(self, split_name, filenames, labels_to_ids):
""" Converts the given filenames to a TFRecord dataset. """
assert split_name in ['train', 'validation']
num_per_shard = int(
math.ceil(len(filenames) / float(self.confs["Classifier"]["Shards"])))
self.Helpers.logger.info("Files: " + str(len(filenames)))
self.Helpers.logger.info("Files per shard: " + str(num_per_shard))
with tf.Graph().as_default():
image_reader = ImageReader()
with tf.Session('') as sess:
for shard_id in range(self.confs["Classifier"]["Shards"]):
output_filename = self.getDatasetFilename(
split_name, shard_id)
self.Helpers.logger.info(
"Saving shard: " + output_filename)
with tf.python_io.TFRecordWriter(output_filename) as tfrecord_writer:
start_ndx = shard_id * num_per_shard
end_ndx = min(
(shard_id+1) * num_per_shard, len(filenames))
for i in range(start_ndx, end_ndx):
sys.stdout.write('\r>> Converting image %d/%d shard %d' % (
i+1, len(filenames), shard_id))
sys.stdout.flush()
image_data = tf.gfile.FastGFile(
filenames[i], 'rb').read()
height, width = image_reader.read_image_dims(
sess, image_data)
class_name = os.path.basename(
os.path.dirname(filenames[i]))
class_id = labels_to_ids[class_name]
example = self.imageToTFExample(
image_data, b'jpg', height, width, class_id)
tfrecord_writer.write(example.SerializeToString())
sys.stdout.write('\n')
sys.stdout.flush()
def getDatasetFilename(self, split_name, shard_id):
""" Gets the model TFRecordFile. """
output_filename = '%s_%s_%05d-of-%05d.tfrecord' % (
self.confs["Classifier"]["TFRecordFile"], split_name, shard_id, self.confs["Classifier"]["Shards"])
return os.path.join(self.confs["Classifier"]["DatasetDir"], output_filename)
def int64Feature(self, values):
"""
Returns a TF-Feature of int64s.
Args:
values: A scalar or list of values.
Returns:
a TF-Feature.
"""
if not isinstance(values, (tuple, list)):
values = [values]
return tf.train.Feature(int64_list=tf.train.Int64List(value=values))
def bytesFeature(self, values):
"""
Returns a TF-Feature of bytes.
Args:
values: A string.
Returns:
a TF-Feature.
"""
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[values]))
def imageToTFExample(self, image_data, image_format, height, width, class_id):
return tf.train.Example(features=tf.train.Features(feature={
'image/encoded': self.bytesFeature(image_data),
'image/format': self.bytesFeature(image_format),
'image/class/label': self.int64Feature(class_id),
'image/height': self.int64Feature(height),
'image/width': self.int64Feature(width)
}))
def cropTestDataset(self):
""" Crops the testing dataset. """
data_dir = pathlib.Path(
self.confs["Classifier"]["TestImagePath"])
data = list(data_dir.glob('*.jpg'))
for ipath in data:
fpath = str(ipath)
image = Image.open(fpath)
image = image.resize((600, 600))
image.save(fpath)
self.Helpers.logger.info("Test data resized.")
class ImageReader(object):
""" ImageReader Helper Class
Provides TensorFlow image coding utilities
"""
def __init__(self):
""" Initializes ImageReader Class """
self._decode_jpeg_data = tf.placeholder(dtype=tf.string)
self._decode_jpeg = tf.image.decode_image(
self._decode_jpeg_data, channels=3)
def read_image_dims(self, sess, image_data):
""" Gets the dimensions of image_data """
image = self.decode_jpeg(sess, image_data)
return image.shape[0], image.shape[1]
def decode_jpeg(self, sess, image_data):
""" Decodes image_data (jpeg)"""
image = sess.run(self._decode_jpeg, feed_dict={
self._decode_jpeg_data: image_data})
assert len(image.shape) == 3
assert image.shape[2] == 3
return image