|
| 1 | +# Copyright 2020 The TensorFlow Authors. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Audio classification demo code of Model Maker for TFLite. |
| 15 | +
|
| 16 | +Example usage: |
| 17 | +python audio_classification_demo.py --export_dir=/tmp |
| 18 | +
|
| 19 | +Sample output: |
| 20 | +Downloading data from |
| 21 | +https://storage.googleapis.com/download.tensorflow.org/data/mini_speech_commands.zip |
| 22 | +182083584/182082353 [==============================] - 4s 0us/step |
| 23 | +182091776/182082353 [==============================] - 4s 0us/step |
| 24 | +Dataset has been downloaded to |
| 25 | +/usr/local/google/home/wangtz/.keras/datasets/mini_speech_commands |
| 26 | +Processing audio files: |
| 27 | +8000/8000 [==============================] - 354s 44ms/file |
| 28 | +Cached 7178 audio samples. |
| 29 | +Training the model |
| 30 | +5742/5742 [==============================] - 29s 5ms/step - loss: 3.2289 - acc: |
| 31 | +0.8029 - val_loss: 0.6229 - val_acc: 0.9638 |
| 32 | +Evaluating the model |
| 33 | +15/15 [==============================] - 2s 12ms/step - loss: 1.3569 - acc: |
| 34 | +0.9270 |
| 35 | +Test accuracy: 0.927039 |
| 36 | +""" |
| 37 | + |
| 38 | +from __future__ import absolute_import |
| 39 | +from __future__ import division |
| 40 | +from __future__ import print_function |
| 41 | + |
| 42 | +from absl import app |
| 43 | +from absl import flags |
| 44 | +from absl import logging |
| 45 | + |
| 46 | +import tensorflow as tf |
| 47 | +from tensorflow_examples.lite.model_maker.core.data_util import audio_dataloader |
| 48 | +from tensorflow_examples.lite.model_maker.core.task import audio_classifier |
| 49 | +from tensorflow_examples.lite.model_maker.core.task import model_spec |
| 50 | + |
| 51 | +FLAGS = flags.FLAGS |
| 52 | + |
| 53 | + |
| 54 | +def define_flags(): |
| 55 | + flags.DEFINE_string('export_dir', None, |
| 56 | + 'The directory to save exported files.') |
| 57 | + flags.DEFINE_string('spec', 'audio_browser_fft', |
| 58 | + 'Name of the model spec to use.') |
| 59 | + flags.mark_flag_as_required('export_dir') |
| 60 | + |
| 61 | + |
| 62 | +def download_dataset(**kwargs): |
| 63 | + """Downloads demo dataset, and returns directory path.""" |
| 64 | + tf.compat.v1.logging.info('Downloading mini speech command dataset.') |
| 65 | + # ${HOME}/.keras/datasets/mini_speech_commands.zip |
| 66 | + filepath = tf.keras.utils.get_file( |
| 67 | + fname='mini_speech_commands.zip', |
| 68 | + origin='https://storage.googleapis.com/download.tensorflow.org/data/mini_speech_commands.zip', |
| 69 | + extract=True, |
| 70 | + **kwargs) |
| 71 | + # ${HOME}/.keras/datasets/mini_speech_commands |
| 72 | + folder_path = filepath.rsplit('.', 1)[0] |
| 73 | + print(f'Dataset has been downloaded to {folder_path}') |
| 74 | + return folder_path |
| 75 | + |
| 76 | + |
| 77 | +def run(data_dir, export_dir, spec='audio_browser_fft', **kwargs): |
| 78 | + """Runs demo.""" |
| 79 | + spec = model_spec.get(spec) |
| 80 | + data = audio_dataloader.DataLoader.from_folder(spec, data_dir) |
| 81 | + |
| 82 | + train_data, rest_data = data.split(0.8) |
| 83 | + validation_data, test_data = rest_data.split(0.5) |
| 84 | + |
| 85 | + print('Training the model') |
| 86 | + model = audio_classifier.create(train_data, spec, validation_data, **kwargs) |
| 87 | + |
| 88 | + print('Evaluating the model') |
| 89 | + _, acc = model.evaluate(test_data) |
| 90 | + print('Test accuracy: %f' % acc) |
| 91 | + |
| 92 | + model.export(export_dir) |
| 93 | + |
| 94 | + |
| 95 | +def main(_): |
| 96 | + logging.set_verbosity(logging.INFO) |
| 97 | + data_dir = download_dataset() |
| 98 | + run(data_dir, FLAGS.export_dir) |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + define_flags() |
| 103 | + app.run(main) |
0 commit comments