รวมตัวแยกประเภทเสียง

การจัดประเภทเสียงเป็นกรณีการใช้งานทั่วไปของการเรียนรู้ของเครื่องเพื่อจัดประเภทเสียง ตัวอย่างเช่น สามารถระบุชนิดของนกได้จากเพลงของพวกมัน

Task Library AudioClassifier API สามารถใช้เพื่อปรับใช้ตัวแยกประเภทเสียงแบบกำหนดเองหรือตัวแยกประเภทเสียงที่ได้รับการฝึกล่วงหน้าในแอปมือถือของคุณ

คุณสมบัติที่สำคัญของ AudioClassifier API

  • การประมวลผลเสียงอินพุต เช่น การแปลงการเข้ารหัส PCM 16 บิตเป็นการเข้ารหัส PCM Float และการจัดการบัฟเฟอร์วงแหวนเสียง

  • ป้ายชื่อสถานที่แผนที่

  • รองรับโมเดลการจำแนกประเภทหลายหัว

  • รองรับการจำแนกประเภททั้งฉลากเดียวและหลายฉลาก

  • เกณฑ์คะแนนเพื่อกรองผลลัพธ์

  • ผลการจำแนกประเภท Top-k

  • ติดป้ายกำกับรายการที่อนุญาตและรายการที่ปฏิเสธ

รุ่นตัวแยกประเภทเสียงที่รองรับ

รับประกันว่ารุ่นต่อไปนี้จะเข้ากันได้กับ AudioClassifier API

เรียกใช้การอนุมานใน 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.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: ติดตั้งการอ้างอิง

Task Library รองรับการติดตั้งโดยใช้ CocoaPods ตรวจสอบให้แน่ใจว่าติดตั้ง CocoaPods บนระบบของคุณแล้ว โปรดดู คู่มือการติดตั้ง CocoaPods สำหรับคำแนะนำ

โปรดดู คู่มือ CocoaPods สำหรับรายละเอียดเกี่ยวกับการเพิ่มพ็อดในโปรเจ็กต์ Xcode

เพิ่มพ็อด TensorFlowLiteTaskAudio ใน Podfile

target 'MyAppWithTaskAPI' do
  use_frameworks!
  pod 'TensorFlowLiteTaskAudio'
end

ตรวจสอบให้แน่ใจว่าโมเดล .tflite ที่คุณจะใช้ในการอนุมานมีอยู่ใน App Bundle ของคุณ

ขั้นตอนที่ 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

เรียกใช้การอนุมานใน 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 (ถ้ามี) ซึ่งมีโลแคลตรงกับฟิลด์ display_names_locale ของ AudioClassifierOptions ที่ใช้ในเวลาที่สร้าง ("en" ตามค่าเริ่มต้น เช่น ภาษาอังกฤษ) หากไม่มีสิ่งใดเลย ระบบจะกรอกเฉพาะฟิลด์ index ของผลลัพธ์เท่านั้น