การจัดประเภทเสียงเป็นกรณีการใช้งานทั่วไปของ Machine Learning เพื่อจำแนกประเภทเสียง ตัวอย่างเช่น มันสามารถระบุสายพันธุ์นกด้วยเพลงของพวกเขา
AudioClassifier
API ของไลบรารีงานสามารถใช้เพื่อปรับใช้ตัวแยกประเภทเสียงที่กำหนดเองหรือตัวแยกประเภทที่ฝึกไว้ล่วงหน้าในแอพมือถือของคุณ
คุณสมบัติหลักของ AudioClassifier API
อินพุตการประมวลผลเสียง เช่น การแปลงการเข้ารหัส PCM 16 บิตเป็นการเข้ารหัส PCM Float และการจัดการบัฟเฟอร์เสียงกริ่ง
ป้ายกำกับสถานที่แผนที่
รองรับโมเดลการจำแนกแบบหลายหัว
รองรับการจำแนกประเภทฉลากเดียวและหลายฉลาก
เกณฑ์คะแนนเพื่อกรองผลลัพธ์
ผลการจัดหมวดหมู่ยอดนิยม
ติดป้ายกำกับรายการที่อนุญาตและรายการที่ไม่อนุญาต
รุ่นลักษณนามเสียงที่รองรับ
รุ่นต่อไปนี้รับประกันว่าจะเข้ากันได้กับ AudioClassifier
API
โมเดลที่สร้างโดย TensorFlow Lite Model Maker สำหรับการจำแนกเสียง
โมเดลการจัดประเภทเหตุการณ์เสียงที่ฝึกไว้ล่วงหน้าบน TensorFlow Hub
โมเดลแบบกำหนดเองที่ตรงตาม ข้อกำหนดความเข้ากันได้ ของโมเดล
เรียกใช้การอนุมานใน Java
ดู แอพอ้างอิงการจำแนกเสียง สำหรับตัวอย่างโดยใช้ 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.0'
// Import the GPU delegate plugin Library for GPU inference
implementation 'org.tensorflow:tensorflow-lite-gpu-delegate-plugin:0.4.0'
}
ขั้นตอนที่ 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
ที่คุณจะใช้ในการอนุมานนั้นมีอยู่ใน App Bundle ของคุณ
ขั้นตอนที่ 2: การใช้โมเดล
Swift
// 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)
}
}
}
วัตถุประสงค์ C
// 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
เรียกใช้การอนุมานใน Python
ขั้นตอนที่ 1: ติดตั้งแพ็คเกจ pip
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
API ต้องการโมเดล 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 (ถ้ามี) ซึ่ง locale ตรงกับฟิลด์display_names_locale
ของAudioClassifierOptions
ที่ใช้ในเวลาที่สร้าง ("en" โดยค่าเริ่มต้น เช่น ภาษาอังกฤษ) หากไม่มีสิ่งเหล่านี้ ระบบจะเติมเฉพาะฟิลด์index
ของผลลัพธ์เท่านั้น
-