يعد تصنيف الصوت حالة استخدام شائعة للتعلم الآلي لتصنيف أنواع الصوت. على سبيل المثال، يمكنه التعرف على أنواع الطيور من خلال أغانيها.
يمكن استخدام واجهة برمجة تطبيقات Task Library AudioClassifier
لنشر مصنفات الصوت المخصصة أو المصنفات المدربة مسبقًا في تطبيق الهاتف المحمول الخاص بك.
الميزات الرئيسية لواجهة برمجة تطبيقات AudioClassifier
معالجة صوت الإدخال، على سبيل المثال تحويل تشفير PCM 16 بت إلى تشفير PCM Float ومعالجة المخزن المؤقت للحلقة الصوتية.
تسمية خريطة المنطقة.
دعم نموذج تصنيف متعدد الرؤوس.
دعم التصنيف الفردي والمتعدد التصنيفات.
عتبة النتيجة لتصفية النتائج.
نتائج تصنيف Top-k.
تصنيف القائمة المسموح بها والقائمة المرفوضة.
نماذج مصنف الصوت المدعومة
نضمن أن تكون النماذج التالية متوافقة مع واجهة برمجة تطبيقات AudioClassifier
.
النماذج التي تم إنشاؤها بواسطة TensorFlow Lite Model Maker لتصنيف الصوت .
نماذج تصنيف الأحداث الصوتية المُدربة مسبقًا على TensorFlow Hub .
نماذج مخصصة تلبي متطلبات توافق النموذج .
تشغيل الاستدلال في جافا
راجع التطبيق المرجعي لتصنيف الصوت للحصول على مثال باستخدام AudioClassifier
في تطبيق Android.
الخطوة 1: استيراد تبعية Gradle والإعدادات الأخرى
انسخ ملف نموذج .tflite
إلى دليل الأصول الخاص بوحدة Android حيث سيتم تشغيل النموذج. حدد أنه لا ينبغي ضغط الملف، وأضف مكتبة TensorFlow Lite إلى ملف build.gradle
الخاص بالوحدة:
android {
// Other settings
// Specify that the tflite file should not be compressed when building the APK package.
aaptOptions {
noCompress "tflite"
}
}
dependencies {
// Other dependencies
// Import the Audio Task Library dependency (NNAPI is included)
implementation 'org.tensorflow:tensorflow-lite-task-audio:0.4.4'
// Import the GPU delegate plugin Library for GPU inference
implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin:0.4.4'
}
الخطوة 2: استخدام النموذج
// Initialization
AudioClassifierOptions options =
AudioClassifierOptions.builder()
.setBaseOptions(BaseOptions.builder().useGpu().build())
.setMaxResults(1)
.build();
AudioClassifier classifier =
AudioClassifier.createFromFileAndOptions(context, modelFile, options);
// Start recording
AudioRecord record = classifier.createAudioRecord();
record.startRecording();
// Load latest audio samples
TensorAudio audioTensor = classifier.createInputTensorAudio();
audioTensor.load(record);
// Run inference
List<Classifications> results = audioClassifier.classify(audioTensor);
راجع الكود المصدري وjavadoc لمزيد من الخيارات لتكوين AudioClassifier
.
تشغيل الاستدلال في iOS
الخطوة 1: تثبيت التبعيات
تدعم مكتبة المهام التثبيت باستخدام CocoaPods. تأكد من تثبيت CocoaPods على نظامك. يرجى الاطلاع على دليل تثبيت CocoaPods للحصول على التعليمات.
يرجى الاطلاع على دليل CocoaPods للحصول على تفاصيل حول إضافة البودات إلى مشروع Xcode.
أضف جراب TensorFlowLiteTaskAudio
في ملف Podfile.
target 'MyAppWithTaskAPI' do
use_frameworks!
pod 'TensorFlowLiteTaskAudio'
end
تأكد من أن نموذج .tflite
الذي ستستخدمه للاستدلال موجود في حزمة التطبيق الخاصة بك.
الخطوة 2: استخدام النموذج
سويفت
// Imports
import TensorFlowLiteTaskAudio
import AVFoundation
// Initialization
guard let modelPath = Bundle.main.path(forResource: "sound_classification",
ofType: "tflite") else { return }
let options = AudioClassifierOptions(modelPath: modelPath)
// Configure any additional options:
// options.classificationOptions.maxResults = 3
let classifier = try AudioClassifier.classifier(options: options)
// Create Audio Tensor to hold the input audio samples which are to be classified.
// Created Audio Tensor has audio format matching the requirements of the audio classifier.
// For more details, please see:
// https://github.com/tensorflow/tflite-support/blob/master/tensorflow_lite_support/ios/task/audio/core/audio_tensor/sources/TFLAudioTensor.h
let audioTensor = classifier.createInputAudioTensor()
// Create Audio Record to record the incoming audio samples from the on-device microphone.
// Created Audio Record has audio format matching the requirements of the audio classifier.
// For more details, please see:
https://github.com/tensorflow/tflite-support/blob/master/tensorflow_lite_support/ios/task/audio/core/audio_record/sources/TFLAudioRecord.h
let audioRecord = try classifier.createAudioRecord()
// Request record permissions from AVAudioSession before invoking audioRecord.startRecording().
AVAudioSession.sharedInstance().requestRecordPermission { granted in
if granted {
DispatchQueue.main.async {
// Start recording the incoming audio samples from the on-device microphone.
try audioRecord.startRecording()
// Load the samples currently held by the audio record buffer into the audio tensor.
try audioTensor.load(audioRecord: audioRecord)
// Run inference
let classificationResult = try classifier.classify(audioTensor: audioTensor)
}
}
}
ج موضوعية
// Imports
#import <TensorFlowLiteTaskAudio/TensorFlowLiteTaskAudio.h>
#import <AVFoundation/AVFoundation.h>
// Initialization
NSString *modelPath = [[NSBundle mainBundle] pathForResource:@"sound_classification" ofType:@"tflite"];
TFLAudioClassifierOptions *options =
[[TFLAudioClassifierOptions alloc] initWithModelPath:modelPath];
// Configure any additional options:
// options.classificationOptions.maxResults = 3;
TFLAudioClassifier *classifier = [TFLAudioClassifier audioClassifierWithOptions:options
error:nil];
// Create Audio Tensor to hold the input audio samples which are to be classified.
// Created Audio Tensor has audio format matching the requirements of the audio classifier.
// For more details, please see:
// https://github.com/tensorflow/tflite-support/blob/master/tensorflow_lite_support/ios/task/audio/core/audio_tensor/sources/TFLAudioTensor.h
TFLAudioTensor *audioTensor = [classifier createInputAudioTensor];
// Create Audio Record to record the incoming audio samples from the on-device microphone.
// Created Audio Record has audio format matching the requirements of the audio classifier.
// For more details, please see:
https://github.com/tensorflow/tflite-support/blob/master/tensorflow_lite_support/ios/task/audio/core/audio_record/sources/TFLAudioRecord.h
TFLAudioRecord *audioRecord = [classifier createAudioRecordWithError:nil];
// Request record permissions from AVAudioSession before invoking -[TFLAudioRecord startRecordingWithError:].
[[AVAudioSession sharedInstance] requestRecordPermission:^(BOOL granted) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
// Start recording the incoming audio samples from the on-device microphone.
[audioRecord startRecordingWithError:nil];
// Load the samples currently held by the audio record buffer into the audio tensor.
[audioTensor loadAudioRecord:audioRecord withError:nil];
// Run inference
TFLClassificationResult *classificationResult =
[classifier classifyWithAudioTensor:audioTensor error:nil];
});
}
}];
راجع الكود المصدري لمزيد من الخيارات لتكوين TFLAudioClassifier
.
تشغيل الاستدلال في بيثون
الخطوة 1: تثبيت حزمة النقطة
pip install tflite-support
- Linux: قم بتشغيل
sudo apt-get update && apt-get install libportaudio2
- Mac وWindows: يتم تثبيت PortAudio تلقائيًا عند تثبيت حزمة pip
tflite-support
.
الخطوة 2: استخدام النموذج
# Imports
from tflite_support.task import audio
from tflite_support.task import core
from tflite_support.task import processor
# Initialization
base_options = core.BaseOptions(file_name=model_path)
classification_options = processor.ClassificationOptions(max_results=2)
options = audio.AudioClassifierOptions(base_options=base_options, classification_options=classification_options)
classifier = audio.AudioClassifier.create_from_options(options)
# Alternatively, you can create an audio classifier in the following manner:
# classifier = audio.AudioClassifier.create_from_file(model_path)
# Run inference
audio_file = audio.TensorAudio.create_from_wav_file(audio_path, classifier.required_input_buffer_size)
audio_result = classifier.classify(audio_file)
راجع الكود المصدري لمزيد من الخيارات لتكوين AudioClassifier
.
تشغيل الاستدلال في C++
// Initialization
AudioClassifierOptions options;
options.mutable_base_options()->mutable_model_file()->set_file_name(model_path);
std::unique_ptr<AudioClassifier> audio_classifier = AudioClassifier::CreateFromOptions(options).value();
// Create input audio buffer from your `audio_data` and `audio_format`.
// See more information here: tensorflow_lite_support/cc/task/audio/core/audio_buffer.h
int input_size = audio_classifier->GetRequiredInputBufferSize();
const std::unique_ptr<AudioBuffer> audio_buffer =
AudioBuffer::Create(audio_data, input_size, audio_format).value();
// Run inference
const ClassificationResult result = audio_classifier->Classify(*audio_buffer).value();
راجع الكود المصدري لمزيد من الخيارات لتكوين AudioClassifier
.
متطلبات توافق النموذج
تتوقع واجهة برمجة تطبيقات AudioClassifier
وجود نموذج TFLite مع بيانات تعريف نموذج TFLite الإلزامية. اطلع على أمثلة لإنشاء بيانات تعريف لمصنفات الصوت باستخدام TensorFlow Lite Metadata Writer API .
يجب أن تستوفي نماذج مصنف الصوت المتوافقة المتطلبات التالية:
موتر صوت الإدخال (kTfLiteFloat32)
- مقطع صوتي بحجم
[batch x samples]
. - استنتاج الدُفعة غير مدعوم (يُشترط أن تكون
batch
1). - بالنسبة للنماذج متعددة القنوات، يجب أن تكون القنوات متداخلة.
- مقطع صوتي بحجم
موتر نتيجة الإخراج (kTfLiteFloat32)
- تمثل المصفوفة
[1 x N]
معN
رقم الفئة. - خريطة (خرائط) تسمية اختيارية (لكن موصى بها) مثل AssociatedFile-s مع النوع TENSOR_AXIS_LABELS، والتي تحتوي على تسمية واحدة في كل سطر. يتم استخدام ملف AssociatedFile الأول (إن وجد) لملء حقل
label
(المسمى باسمclass_name
في C++) للنتائج. يتم ملء حقلdisplay_name
من ملف AssociatedFile (إن وجد) الذي تتطابق إعداداته المحلية مع حقلdisplay_names_locale
الخاص بـAudioClassifierOptions
المستخدم في وقت الإنشاء ("en" بشكل افتراضي، أي الإنجليزية). إذا لم يتوفر أي منها، فسيتم ملء حقلindex
النتائج فقط.
- تمثل المصفوفة