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

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

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

ฟีเจอร์สำคัญของ AudioClassifier API

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

  • ติดป้ายกำกับภาษาบนแผนที่

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

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

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

  • ผลลัพธ์การจัดประเภทที่สำคัญ

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

รุ่นของเครื่องมือแยกประเภทเสียงที่รองรับ

รุ่นต่อไปนี้ได้รับการรับประกันว่าใช้งานร่วมกับ AudioClassifier ได้ API

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

โปรดดู แอปอ้างอิงการจัดประเภทเสียง ตัวอย่างโดยใช้ AudioClassifier ในแอป Android

ขั้นตอนที่ 1: นำเข้าการอ้างอิง Gradle และการตั้งค่าอื่นๆ

คัดลอกไฟล์โมเดล .tflite ไปยังไดเรกทอรี Asset ของโมดูล 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);

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

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

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

ไลบรารีงานรองรับการติดตั้งโดยใช้ 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)
        }
    }
}

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 ของผลลัพธ์