Newer
Older
from __future__ import division
import logging
import os
from io import BytesIO
import tensorflow as tf
import numpy as np
from PIL import Image
from sciencebeam_utils.utils.num import (
Daniel Ecer
committed
)
from sciencebeam_gym.utils.tfrecord import (
dict_to_example,
write_examples_to_tfrecord
)
from sciencebeam_gym.tools.calculate_class_weights import (
calculate_sample_frequencies,
iter_calculate_sample_frequencies,
calculate_median_class_weight,
calculate_median_weights_for_frequencies,
calculate_median_class_weights_for_tfrecord_paths_and_colors,
calculate_median_class_weights_for_tfrecord_paths_and_color_map,
calculate_efnet_weights_for_frequencies_by_label,
tf_calculate_efnet_weights_for_frequency_by_label
COLOR_0 = color(0)
COLOR_1 = color(1)
COLOR_2 = color(2)
COLOR_3 = color(3)
class TestCalculateSampleFrequencies(object):
def test_should_return_zero_for_single_not_matching_color(self):
with tf.Session() as session:
assert session.run(calculate_sample_frequencies([[
COLOR_0
]], [COLOR_1])) == [0.0]
def test_should_return_one_for_single_matching_color(self):
with tf.Session() as session:
assert session.run(calculate_sample_frequencies([[
COLOR_1
]], [COLOR_1])) == [1.0]
def test_should_return_total_count_for_multiple_all_matching_color(self):
with tf.Session() as session:
assert session.run(calculate_sample_frequencies([[
COLOR_1, COLOR_1, COLOR_1
]], [COLOR_1])) == [3.0]
def test_should_return_total_count_for_multiple_mixed_color(self):
with tf.Session() as session:
assert session.run(calculate_sample_frequencies([[
COLOR_1, COLOR_1, COLOR_2
]], [COLOR_1, COLOR_2])) == [2.0, 1.0]
def test_should_include_unknown_class_count_if_enabled(self):
with tf.Session() as session:
assert session.run(calculate_sample_frequencies([[
COLOR_1, COLOR_2, COLOR_3
]], [COLOR_1], use_unknown_class=True)) == [1.0, 2.0]
out = BytesIO()
data = np.array(data, dtype=np.uint8)
image_size = data.shape[:-1] # pylint: disable=unsubscriptable-object
get_logger().debug('data type: %s', data.dtype)
get_logger().debug('image_size: %s', image_size)
mode = 'RGB'
image = Image.fromarray(data, mode)
image.save(out, 'png')
image_bytes = out.getvalue()
return image_bytes
class TestIterCalculateSampleFrequencies(object):
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
def test_should_return_zero_for_single_not_matching_color(self):
assert list(iter_calculate_sample_frequencies([
[[
COLOR_0
]]
], [COLOR_1], image_shape=(1, 1, 3))) == [[0.0]]
def test_should_infer_image_shape(self):
assert list(iter_calculate_sample_frequencies([
[[
COLOR_0
]]
], [COLOR_1])) == [[0.0]]
def test_should_include_unknown_class_if_enabled(self):
assert list(iter_calculate_sample_frequencies([
[[
COLOR_0
]]
], [COLOR_1], image_shape=(1, 1, 3), use_unknown_class=True)) == [[0.0, 1.0]]
def test_should_include_unknown_class_if_enabled_and_infer_shape(self):
assert list(iter_calculate_sample_frequencies([
[[
COLOR_0
]]
], [COLOR_1], use_unknown_class=True)) == [[0.0, 1.0]]
def test_should_return_total_count_for_multiple_mixed_color(self):
assert list(iter_calculate_sample_frequencies([
[[
COLOR_0, COLOR_0, COLOR_0
]], [[
COLOR_0, COLOR_1, COLOR_2
]], [[
COLOR_1, COLOR_1, COLOR_2
]]
], [COLOR_1, COLOR_2])) == [
[0.0, 0.0],
[1.0, 1.0],
[2.0, 1.0]
]
def test_should_decode_png(self):
assert list(iter_calculate_sample_frequencies([
encode_png([[
COLOR_1
]])
], [COLOR_1], image_shape=(1, 1, 3), image_format='png')) == [[1.0]]
def test_should_infer_shape_when_decoding_png(self):
assert list(iter_calculate_sample_frequencies([
encode_png([[
COLOR_1
]])
], [COLOR_1], image_format='png')) == [[1.0]]
def test_should_infer_shape_when_decoding_png_and_include_unknown_class(self):
assert list(iter_calculate_sample_frequencies([
encode_png([[
COLOR_1, COLOR_2, COLOR_3
]])
], [COLOR_1], image_format='png', use_unknown_class=True)) == [[1.0, 2.0]]
Daniel Ecer
committed
class TestTfCalculateEfnetForFrequencyByLabel(object):
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
def test_should_return_same_value_for_classes_with_same_frequencies(self):
with tf.Graph().as_default():
with tf.Session():
frequencies = [1, 1]
result = tf_calculate_efnet_weights_for_frequency_by_label(frequencies).eval()
assert result[0] == result[1]
def test_should_return_higher_value_for_less_frequent_occuring_class(self):
with tf.Graph().as_default():
with tf.Session():
frequencies = [2, 1]
result = tf_calculate_efnet_weights_for_frequency_by_label(frequencies).eval()
assert result[0] < result[1]
def test_should_return_zero_value_for_not_occuring_class(self):
with tf.Graph().as_default():
with tf.Session():
frequencies = [1, 0]
result = tf_calculate_efnet_weights_for_frequency_by_label(frequencies).eval()
assert result[-1] == 0.0
class TestCalculateEfnetForFrequenciesByLabel(object):
def test_should_return_same_value_for_classes_with_same_frequencies(self):
frequencies = [
[0, 1],
[0, 1]
]
result = calculate_efnet_weights_for_frequencies_by_label(frequencies)
Daniel Ecer
committed
assert result[0] == result[1]
def test_should_return_higher_value_for_less_frequent_occuring_class(self):
frequencies = [
[1, 1],
[0, 1]
]
result = calculate_efnet_weights_for_frequencies_by_label(frequencies)
Daniel Ecer
committed
assert result[0] < result[1]
def test_should_return_zero_value_for_not_occuring_class(self):
frequencies = [
[1, 1],
[0, 0]
]
result = calculate_efnet_weights_for_frequencies_by_label(frequencies)
Daniel Ecer
committed
assert result[-1] == 0.0
class TestCalculateMedianClassWeight(object):
def test_should_return_median_frequency_balanced_for_same_frequencies(self):
assert calculate_median_class_weight([3, 3, 3]) == 1 / 3
def test_should_return_median_frequence_balanced_for_different_frequencies(self):
assert calculate_median_class_weight([1, 3, 5]) == 1 / 3
def test_should_return_zero_for_all_zero_frequencies(self):
assert calculate_median_class_weight([0, 0, 0]) == 0.0
class TestCalculateWeightsForFrequencies(object):
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
def test_should_return_one_for_single_class(self):
assert calculate_median_weights_for_frequencies([
[3, 3, 3]
]) == [1.0]
def test_should_return_50p_for_classes_with_same_frequencies(self):
assert calculate_median_weights_for_frequencies([
[3, 3, 3],
[3, 3, 3]
]) == [0.5, 0.5]
def test_should_return_higher_value_for_less_frequent_occuring_class(self):
frequencies = [
[1, 1],
[1, 1],
[0, 1]
]
result = calculate_median_weights_for_frequencies(frequencies)
get_logger().debug('result: %s', result)
assert_close(sum(result), 1.0)
assert_all_close(result, [0.25, 0.25, 0.5], atol=0.001)
def test_should_return_zero_value_for_not_occuring_class(self):
frequencies = [
[1, 1],
[1, 1],
[0, 0]
]
result = calculate_median_weights_for_frequencies(frequencies)
get_logger().debug('result: %s', result)
assert_close(sum(result), 1.0)
assert_all_close(result, [0.5, 0.5, 0.0], atol=0.001)
class TestCalculateMedianClassWeightsForFfrecordPathsAndColors(object):
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
def test_should_calculate_median_class_weights_for_single_image_and_single_color(self):
with TemporaryDirectory() as path:
tfrecord_filename = os.path.join(path, 'data.tfrecord')
get_logger().debug('writing to test tfrecord_filename: %s', tfrecord_filename)
write_examples_to_tfrecord(tfrecord_filename, [dict_to_example({
'image': encode_png([[
COLOR_1
]])
})])
class_weights = calculate_median_class_weights_for_tfrecord_paths_and_colors(
[tfrecord_filename], 'image', [COLOR_1]
)
assert class_weights == [1.0]
def test_should_calculate_median_class_weights_for_multiple_image_and_multiple_images(self):
with TemporaryDirectory() as path:
tfrecord_filename = os.path.join(path, 'data.tfrecord')
get_logger().debug('writing to test tfrecord_filename: %s', tfrecord_filename)
write_examples_to_tfrecord(tfrecord_filename, [dict_to_example({
'image': encode_png([[
COLOR_0, COLOR_1, COLOR_2
]])
}), dict_to_example({
'image': encode_png([[
COLOR_1, COLOR_2, COLOR_3
]])
})])
class_weights = calculate_median_class_weights_for_tfrecord_paths_and_colors(
[tfrecord_filename], 'image', [COLOR_1, COLOR_2, COLOR_3]
)
assert class_weights == [0.25, 0.25, 0.5]
def test_should_return_zero_for_non_occuring_class(self):
with TemporaryDirectory() as path:
tfrecord_filename = os.path.join(path, 'data.tfrecord')
get_logger().debug('writing to test tfrecord_filename: %s', tfrecord_filename)
write_examples_to_tfrecord(tfrecord_filename, [dict_to_example({
'image': encode_png([[
COLOR_1
]])
})])
class_weights = calculate_median_class_weights_for_tfrecord_paths_and_colors(
[tfrecord_filename], 'image', [COLOR_1, COLOR_2]
)
assert class_weights == [1.0, 0.0]
class TestCalculateMedianClassWeightsForFfrecordPathsAndColorMap(object):
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
def test_should_calculate_median_class_weights_for_single_image_and_single_color(self):
with TemporaryDirectory() as path:
tfrecord_filename = os.path.join(path, 'data.tfrecord')
get_logger().debug('writing to test tfrecord_filename: %s', tfrecord_filename)
write_examples_to_tfrecord(tfrecord_filename, [dict_to_example({
'image': encode_png([[
COLOR_1, COLOR_2
]])
})])
class_weights_map = calculate_median_class_weights_for_tfrecord_paths_and_color_map(
[tfrecord_filename], 'image', {
'color1': COLOR_1,
'color2': COLOR_2,
'color3': COLOR_3
},
channels=['color1', 'color2']
)
assert class_weights_map == {
'color1': 0.5,
'color2': 0.5
}
def test_should_use_color_map_keys_as_channels_by_default(self):
with TemporaryDirectory() as path:
tfrecord_filename = os.path.join(path, 'data.tfrecord')
get_logger().debug('writing to test tfrecord_filename: %s', tfrecord_filename)
write_examples_to_tfrecord(tfrecord_filename, [dict_to_example({
'image': encode_png([[
COLOR_1, COLOR_2
]])
})])
class_weights_map = calculate_median_class_weights_for_tfrecord_paths_and_color_map(
[tfrecord_filename], 'image', {
'color1': COLOR_1,
'color2': COLOR_2
}
)
assert set(class_weights_map.keys()) == {'color1', 'color2'}
def test_should_include_unknown_class_if_enabled(self):
with TemporaryDirectory() as path:
tfrecord_filename = os.path.join(path, 'data.tfrecord')
get_logger().debug('writing to test tfrecord_filename: %s', tfrecord_filename)
write_examples_to_tfrecord(tfrecord_filename, [dict_to_example({
'image': encode_png([[
COLOR_0, COLOR_1, COLOR_2, COLOR_3
]])
})])
class_weights_map = calculate_median_class_weights_for_tfrecord_paths_and_color_map(
[tfrecord_filename], 'image', {
'color1': COLOR_1,
'color2': COLOR_2
},
use_unknown_class=True,
unknown_class_label='unknown'
)
assert set(class_weights_map.keys()) == {'color1', 'color2', 'unknown'}