Guan-Ting Lin
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Note Insights Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       owned this note    owned this note      
    Published Linked with GitHub
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Hubert training by fairseq library *Prerequisite: fairseq installation, LibriSpeech dataset* For installing fairseq library, please follow the offical installation instruction in fairseq github page. Below tutorial is based on LibriSpeech train-100hr (training set) and dev-clean (validation set). * learned kmeans(hubert large/20 layer/libri-train-100) : https://github.com/voidful/hubert-cluster-code * hubert cluster id asr: https://huggingface.co/voidful/asr_hubert_cluster_bart_base ## Label preparation In `./simple_kmeans`, do below preprocessing steps for training set and validation set. * feature extraction *.tsv files contains a list of audio, where each line is the root, and following lines are the subpath for each audio: ``` <root-dir> <audio-path-1> <audio-path-2> ... ``` by ``` $ python FAIRSEQ_ROOT/examples/wav2vec/wav2vec_manifest.py \ /dir/to/save/audio/files --ext flac \ --dest /path/to/new/train.tsv --valid-percent 0 ``` and then prepare data for kmeans: - nshard: split the list of filenames into nshard segments - rank: rank-th segment, rank = [0, nshard - 1] ``` $ python dump_mfcc_feature.py ${tsv_dir} ${split} ${nshard} ${rank} ${feat_dir} ``` Later on we would merge the those shards, here we have to prepare all the segments, the resulting files would be saved at `${feat_dir}/${split}_${rank}_${nshard}.{npy,len}` * k-means clustering ``` python learn_kmeans.py ${feat_dir} ${split} ${nshard} ${km_path} ${n_cluster} --percent 0.1 ``` Can use more CPUs to speed up, e.g.: in battleship: ``` hrun -c 56 -m 24 python learn_kmeans.py \ /home/daniel094144/data/LibriSpeech/Hubert100/ \ train \ 3 \ /home/daniel094144/data/LibriSpeech/Hubert100/km.pkl \ 100 \ --percent 1 \ ``` * k-means application ``` python dump_km_label.py ${feat_dir} \ ${split} ${km_path} ${nshard} ${rank} ${lab_dir} ``` **(need to run this command for all rank of shards)** - merge all the shards for ${split} by running: ``` for rank in $(seq 0 $((nshard - 1))); do cat $lab_dir/${split}_${rank}_${nshard}.km done > $lab_dir/${split}.km ``` e.g.: nshard = 3; lab_dir = /home/daniel094144/data/LibriSpeech/Hubert100/; split = train ``` for rank in $(seq 0 $((3 - 1))); do cat /home/daniel094144/data/LibriSpeech/Hubert100/train_${rank}_3.km done > /home/daniel094144/data/LibriSpeech/Hubert100/train.km ``` `train.km` is as follow: ![](https://imgur.com/UJxCTht.png) - **prepare dict.km.txt file** ``` python preprocess.py -s km --trainpref /home/daniel094144/data/LibriSpeech/Hubert100/train --destdir /home/daniel094144/data/LibriSpeech/Hubert100/ --only-source ``` https://fairseq.readthedocs.io/en/latest/command_line_tools.html **[NOTE]** You should finish above preparation for both {train} and {valid} split before model pretraining. - Before pretraining, modifying the config file - number of GPU in distributed training: `distributed_world_size` - task.labels: from ??? to ["km"], this refer to `dict.km.txt` dictionary file ![](https://imgur.com/JhDgNuY.png) - Hubert pertraining can be run by the below command: ``` $ python fairseq_cli/hydra_train.py \ --config-dir /path/to/fairseq-py/examples/hubert/config/pretrain \ --config-name hubert_base_librispeech \ task.data=/path/to/data task.label_dir=/path/to/labels model.label_rate=100 ``` e.g.: ``` $ python fairseq_cli/hydra_train.py \ --config-dir /home/daniel094144/fairseq/examples/hubert/config/pretrain \ --config-name hubert_base_librispeech \ task.data=/home/daniel094144/data/LibriSpeech/LS100_fairseq task.label_dir=/home/daniel094144/data/LibriSpeech/Hubert100 model.label_rate=100 ``` e.g.: ``` $ python fairseq_cli/hydra_train.py \ --config-dir /home/daniel094144/fairseq/examples/disentangle_hubert/config/pretrain \ --config-name hubert_base_librispeech \ task.data=/home/daniel094144/data/LibriSpeech/LS100_fairseq task.label_dir=/home/daniel094144/data/LibriSpeech/Hubert100 task.feat_dir=/home/daniel094144/data/LibriSpeech/Hubert100 ``` ## Code Architecture ### Training code flow chart ``` fairseq/fairseq_cli/hydra_train.py': read config by hydra | |__ distributed_utils.call_main(): Assign distributed training setup and call main function |__ 'fairseq/fairseq_cli/train.py' |__ main(): build trainer and training procedure | |__ task: in 'fairseq/fairseq/tasks/', there are lots of tasks in .py, | | specifying the task-dependent code. e.g.: for hubert-pretraining | | is in 'fairseq/fairseq/tasks/hubert_pretraining.py' | | | |__ class HubertPretrainingTask(FairseqTask) | | | |__ HuBertDataset(FairseqDataset): 'fairseq/fairseq/data/audio/hubert_dataset.py' | | |__ FairseqDataset | | |__ load label(label_path) | | |__ load_audio(manifest_path): return root, names, inds, tot, sizes | | |__ get_audio: by soundfile | | |__ get_label | | |__ *get_pitch* | | |__ *get_speaker* | | | |__ *FairseqTask: 'fairseq/fairseq/tasks/fairseq_task.py'. | Tasks store dictionaries and provide helpers for loading/iterating over Datasets, | initializing the Model/Criterion and calculating the loss. | | | |__ build_model | | |__ models: 'fairseq/fairseq/models/' | | |__ Hubert: 'fairseq/fairseq/models/hubert/hubert.py' | | |__ HubertModel(BaseFairseqModel) | | |__ forward: return unnormalized logits from nce loss | | |__ forward_features | | |__ **GRL layer** | | |__ **disentangler output** | | |__ apply_mask | | |__ compute_pred: call compute_nce | | |__ compute_nce: using cosine similarity to calculate NCE loss (retrun unnormalized logit) | | | | | |__ encoder: | | | |__ TransformerEncoder: 'fairseq.models.wav2vec.wav2vec2' | | | |__ TransformerSentenceEncoderLayer | | | | | |__ feature_extractor | | |__ ConvFeatureExtractionModel:'fairseq.models.wav2vec.wav2vec2' | | | |__ build_criterion | | |__ 'fairseq/fairseq/criterions/' | | |__ HubertCriterion(FairseqCriterion):'fairseq/fairseq/criterions/hubert_criterion.py' | | |__ forward: input model, forward model, return loss | | |__ model.get_logits: get masked or unmasked logits | | |__ model.get_targets: return zeros tensor | | |__ F.cross_entropy: (log_softmax + NLL_loss) input is unnormalized logits | |__ train_step: Do forward and backward, and return the loss as computed by *criterion* for the given *model* and *sample*. | |__ valid_step | |__ optimizer_step | |__ AMPOptimizer: support AMP (automatic mixed precision) training | 'fairseq/fairseq/optim/amp_optimizer.py' | |__ trainer(cfg, task, model, criterion): at 'fairseq/fairseq/trainer.py': | handle distributed training and logging, using HubertPretrainingTask to do training | |__ self.task = HubertPretrainingTask | |__ setup lr | |__ lr_scheduler: 'fairseq/fairseq/optim/lr_scheduler/' | |__ train(): 'progress' support for wandb, logging ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully