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

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

คุณสามารถใช้ 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
    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);

ดูตัวเลือกเพิ่มเติมในการกำหนดค่า AudioClassifier ได้ในซอร์สโค้ดและ javadoc

เรียกใช้การอนุมานใน iOS

ขั้นตอนที่ 1: ติดตั้งการอ้างอิง

คลังงานรองรับการติดตั้งโดยใช้ CocoaPods ตรวจสอบว่าได้ติดตั้ง CocoaPods ในระบบแล้ว โปรดดูวิธีการในคู่มือการติดตั้ง CocoaPods

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

เพิ่มพ็อด 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)
        }
    }
}

Objective-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 โดยอัตโนมัติเมื่อติดตั้ง tflite-support แพ็กเกจ pip

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

โมเดลตัวแยกประเภทเสียงที่เข้ากันได้ควรเป็นไปตามข้อกำหนดต่อไปนี้

  • Tensor เสียงอินพุต (kTfLiteFloat32)

    • คลิปเสียงขนาด [batch x samples]
    • ไม่รองรับการอนุมานแบบกลุ่ม (ต้องตั้งค่า batch เป็น 1)
    • สำหรับโมเดลแบบหลายแชแนล แชแนลต้องสลับกัน
  • Tensor คะแนนเอาต์พุต (kTfLiteFloat32)

    • [1 x N] อาร์เรย์ที่มี N แสดงหมายเลขคลาส
    • แผนที่ป้ายกำกับที่ไม่บังคับ (แต่แนะนำ) เป็น AssociatedFile-s ที่มีประเภท TENSOR_AXIS_LABELS ซึ่งมีป้ายกำกับ 1 รายการต่อบรรทัด ระบบจะใช้ AssociatedFile แรก (หากมี) เพื่อกรอกข้อมูลในช่อง label (ชื่อ class_name ใน C++) ของผลการค้นหา ระบบจะกรอกข้อมูลในช่อง display_name จาก AssociatedFile (หากมี) ซึ่งมีภาษาตรงกับช่อง display_names_locale ของ AudioClassifierOptions ที่ใช้ในเวลาสร้าง ("en" โดยค่าเริ่มต้น ซึ่งก็คือภาษาอังกฤษ) หากไม่มีข้อมูลใดเลย ระบบจะกรอกเฉพาะช่อง index ของผลลัพธ์