/
binary_classification.py
371 lines (279 loc) · 13.5 KB
/
binary_classification.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
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
305
306
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
364
365
366
367
368
369
370
371
"""binary_classification
*************************
Binary classification (ResNetv1-10 with CIFAR10)
Source code: `binary_classification.py <https://github.com/siliconlabs/mltk/blob/master/mltk/models/examples/binary_classification.py>`_
This demonstrates how to classify two images:
- Cat
- Dog
Using the `CIFAR10 <https://www.cs.toronto.edu/~kriz/cifar.html>`_ dataset.
The key points required for binary image classification are:
- ``binary_crossentropy`` loss function
- ``binary`` "class mode" for the data generator, this makes it so the generated "y" vector is a 1D vector
- One dense unit in the final layer to do the actual classification
- ``sigmoid`` activation in the last layer of the model so that the output of the model is between 0 and 1
Commands
--------------
.. code-block:: shell
# Dump some of samples generated by this model
# using a custom command defined at this bottom
# of this model specification file
mltk custom binary_classification datagen_dump
# Do a "dry run" test training of the model
mltk train binary_classification-test
# Train the model
mltk train binary_classification
# Evaluate the trained model .tflite model
mltk evaluate binary_classification --tflite
# Profile the model in the MVP hardware accelerator simulator
mltk profile binary_classification --accelerator MVP
# Profile the model on a physical development board
mltk profile binary_classification --accelerator MVP --device
# Directly invoke the model script
python binary_classification.py
Model Summary
--------------
.. code-block:: shell
mltk summarize binary_classification --tflite
+-------+-----------------+-----------------+-----------------+-----------------------------------------------------+
| Index | OpCode | Input(s) | Output(s) | Config |
+-------+-----------------+-----------------+-----------------+-----------------------------------------------------+
| 0 | conv_2d | 32x32x3 (int8) | 30x30x32 (int8) | Padding:valid stride:1x1 activation:relu |
| | | 3x3x3 (int8) | | |
| | | 32 (int32) | | |
| 1 | max_pool_2d | 30x30x32 (int8) | 15x15x32 (int8) | Padding:valid stride:2x2 filter:2x2 activation:none |
| 2 | conv_2d | 15x15x32 (int8) | 13x13x32 (int8) | Padding:valid stride:1x1 activation:relu |
| | | 3x3x32 (int8) | | |
| | | 32 (int32) | | |
| 3 | max_pool_2d | 13x13x32 (int8) | 6x6x32 (int8) | Padding:valid stride:2x2 filter:2x2 activation:none |
| 4 | conv_2d | 6x6x32 (int8) | 4x4x64 (int8) | Padding:valid stride:1x1 activation:relu |
| | | 3x3x32 (int8) | | |
| | | 64 (int32) | | |
| 5 | max_pool_2d | 4x4x64 (int8) | 2x2x64 (int8) | Padding:valid stride:2x2 filter:2x2 activation:none |
| 6 | reshape | 2x2x64 (int8) | 256 (int8) | BuiltinOptionsType=0 |
| | | 2 (int32) | | |
| 7 | fully_connected | 256 (int8) | 64 (int8) | Activation:relu |
| | | 256 (int8) | | |
| | | 64 (int32) | | |
| 8 | fully_connected | 64 (int8) | 1 (int8) | Activation:none |
| | | 64 (int8) | | |
| | | 1 (int32) | | |
| 9 | logistic | 1 (int8) | 1 (int8) | BuiltinOptionsType=0 |
+-------+-----------------+-----------------+-----------------+-----------------------------------------------------+
Total MACs: 2.646 M
Total OPs: 5.363 M
Name: binary_classification
Version: 1
Description: Example: Binary classification - ResNetv1-10 with CIFAR10
Classes: cat, dog
hash: de33dd53e0afb91a365fd2fff0e4c461
date: 2022-02-11T17:32:37.986Z
runtime_memory_size: 38740
samplewise_norm.rescale: 0.0
samplewise_norm.mean_and_std: False
.tflite file size: 53.8kB
Model Diagram
------------------
.. code-block:: shell
mltk view binary_classification --tflite
.. raw:: html
<div class="model-diagram">
<a href="../../../../_images/models/binary_classification.tflite.png" target="_blank">
<img src="../../../../_images/models/binary_classification.tflite.png" />
<p>Click to enlarge</p>
</a>
</div>
Model Specification
---------------------
.. literalinclude:: ../../../../../../../mltk/models/examples/binary_classification.py
:language: python
:lines: 125-
"""
import functools
import tensorflow as tf
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
from mltk.core.preprocess.image.parallel_generator import ParallelImageDataGenerator
from mltk.datasets.image import cifar10
import mltk.core as mltk_core
# Instantiate the MltkModel object with the following 'mixins':
# - TrainMixin - Provides classifier model training operations and settings
# - ImageDatasetMixin - Provides image data generation operations and settings
# - EvaluateClassifierMixin - Provides classifier evaluation operations and settings
# @mltk_model # NOTE: This tag is required for this model be discoverable
class MyModel(
mltk_core.MltkModel,
mltk_core.TrainMixin,
mltk_core.ImageDatasetMixin,
mltk_core.EvaluateClassifierMixin
):
pass
my_model = MyModel()
# General parameters
my_model.version = 1
my_model.description = 'Example: Binary classification - ResNetv1-10 with CIFAR10'
#################################################
# Training parameters
my_model.epochs = 200
my_model.batch_size = 40
my_model.optimizer = 'adam'
my_model.metrics = ['accuracy']
my_model.loss = 'binary_crossentropy'
#################################################
# TF-Lite converter settings
my_model.tflite_converter['optimizations'] = [tf.lite.Optimize.DEFAULT]
my_model.tflite_converter['supported_ops'] = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
my_model.tflite_converter['inference_input_type'] = tf.int8 # can also be tf.float32
my_model.tflite_converter['inference_output_type'] = tf.int8
# generate a representative dataset from the validation data
my_model.tflite_converter['representative_dataset'] = 'generate'
#################################################
# Image Dataset Settings
# Default size for CIFAR10 dataset
input_height = 32
input_width = 32
input_depth = 3
# The classification type
my_model.class_mode = 'binary'
# The class labels found in your training dataset directory
my_model.classes = ['cat', 'dog']
# The input shape to the model. The dataset samples will be resized if necessary
my_model.input_shape = [input_height, input_width, input_depth]
def my_dataset_loader(model:MyModel):
(cifar10_x_train, cifar10_y_train), (cifar10_x_test, cifar10_y_test) = cifar10.load_data()
# Extract just the cat and dog samples
cats_and_dogs_x_train = []
cats_and_dogs_y_train = []
cats_and_dogs_x_test = []
cats_and_dogs_y_test = []
n_cat = 0
n_dog = 0
for x, y in zip(cifar10_x_train, cifar10_y_train):
if y == 3: # cat label, see https://www.cs.toronto.edu/~kriz/cifar.html
if model.test_mode_enabled and n_cat > 100:
continue
cats_and_dogs_x_train.append(x)
cats_and_dogs_y_train.append(0) # cat maps to id 0, see the my_model.classes above
n_cat += 1
elif y == 5: # dog label
if model.test_mode_enabled and n_dog > 100:
continue
cats_and_dogs_x_train.append(x)
cats_and_dogs_y_train.append(1)
n_dog += 1
n_cat = 0
n_dog = 0
for x, y in zip(cifar10_x_test, cifar10_y_test):
if y == 3: # cat label, see https://www.cs.toronto.edu/~kriz/cifar.html
if model.test_mode_enabled and n_cat > 100:
continue
cats_and_dogs_x_test.append(x)
cats_and_dogs_y_test.append(0) # cat maps to id 0, see the my_model.classes above
n_cat += 1
elif y == 5: # dog label
if model.test_mode_enabled and n_dog > 100:
continue
cats_and_dogs_x_test.append(x)
cats_and_dogs_y_test.append(1)
n_dog += 1
x_train = np.asarray(cats_and_dogs_x_train)
y_train = np.asarray(cats_and_dogs_y_train)
x_test = np.asarray(cats_and_dogs_x_test)
y_test = np.asarray(cats_and_dogs_y_test)
# Convert for training
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# Scale to INT8 range (simple non-adaptive)
x_train = (x_train-128)/128
x_test = (x_test-128)/128
return x_train, y_train, x_test, y_test
my_model.dataset = functools.partial(my_dataset_loader, my_model)
##############################################################
# Training callbacks
#
my_model.datagen = ParallelImageDataGenerator(
cores=.35,
max_batches_pending=32,
rotation_range=15,
width_shift_range=0.1,
height_shift_range=0.1,
horizontal_flip=True,
vertical_flip=True,
validation_augmentation_enabled=False
)
##############################################################
# Model Layout
def my_model_builder(model: MyModel):
keras_model = Sequential()
keras_model.add(layers.Conv2D(32, (3, 3), input_shape=model.input_shape))
keras_model.add(layers.Activation('relu'))
keras_model.add(layers.MaxPooling2D(pool_size=(2, 2)))
keras_model.add(layers.Conv2D(32, (3, 3)))
keras_model.add(layers.Activation('relu'))
keras_model.add(layers.MaxPooling2D(pool_size=(2, 2)))
keras_model.add(layers.Conv2D(64, (3, 3)))
keras_model.add(layers.Activation('relu'))
keras_model.add(layers.MaxPooling2D(pool_size=(2, 2)))
keras_model.add(layers.Flatten()) # this converts our 3D feature maps to 1D feature vectors
keras_model.add(layers.Dense(64))
keras_model.add(layers.Activation('relu'))
keras_model.add(layers.Dropout(0.5))
keras_model.add(layers.Dense(1)) # Binary so we only need 1 unit for this layer
keras_model.add(layers.Activation('sigmoid')) # Binary so we want the activation to be between 0 and 1 which is what the sigmoid function produces
keras_model.compile(
loss=model.loss,
optimizer=model.optimizer,
metrics=model.metrics
)
return keras_model
my_model.build_model_function = my_model_builder
# Register the "datagen_dump" custom command
import typer
@my_model.cli.command('datagen_dump')
def datagen_dump_custom_command(
count:int = typer.Option(100, '--count',
help='Number of samples to dump'
),
):
"""Custom command to dump the augmented samples
\b
Invoke this command with:
mltk custom binary_classification datagen_dump --count 20
"""
my_model.datagen.save_to_dir = my_model.create_log_dir('datagen_dump', delete_existing=True)
my_model.datagen.debug = True
my_model.datagen.cores = 1
my_model.datagen.max_batches_pending = 1
my_model.datagen.batch_size = 1
my_model.load_dataset(subset='training')
for i, _ in enumerate(my_model.x):
if i >= count:
break
my_model.unload_dataset()
print(f'Generated data dump to: {my_model.datagen.save_to_dir}')
##########################################################################################
# The following allows for running this model training script directly, e.g.:
# python binary_classification.py
#
# Note that this has the same functionality as:
# mltk train binary_classification
#
if __name__ == '__main__':
from mltk import cli
# Setup the CLI logger
cli.get_logger(verbose=False)
# If this is true then this will do a "dry run" of the model testing
# If this is false, then the model will be fully trained
test_mode_enabled = True
# Train the model
# This does the same as issuing the command: mltk train binary_classification-test --clean
train_results = mltk_core.train_model(my_model, clean=True, test=test_mode_enabled)
print(train_results)
# Evaluate the model against the quantized .h5 (i.e. float32) model
# This does the same as issuing the command: mltk evaluate binary_classification-test
tflite_eval_results = mltk_core.evaluate_model(my_model, verbose=True, test=test_mode_enabled)
print(tflite_eval_results)
# Profile the model in the simulator
# This does the same as issuing the command: mltk profile binary_classification-test
profiling_results = mltk_core.profile_model(my_model, test=test_mode_enabled)
print(profiling_results)