# architparnami/few-shot-kws architecture

> Architecture diagram of the GitHub repository architparnami/few-shot-kws, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-09-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/architparnami/few-shot-kws
- Repository: https://github.com/architparnami/few-shot-kws

## Overview

#### Purpose and execution surface
This is a research Python package for few-shot keyword spotting using prototypical networks. It prepares a speech-command dataset, trains episodic N-way/K-shot models, evaluates saved experiments, and analyzes result tables. Package installation is via `setup.py`; operational entry points are `data/download_prepare_data.py`, `scripts/train/few_shot/run_train.py`, `scripts/train/few_shot/run_trainval.py`, and `scripts/predict/few_shot/eval.py`.

#### Dataset preparation and episodic data boundary
`data/download_prepare_data.py` acquires and prepares the FS-KWS data consumed by the training package. `protonets/data/FewShotSpeechData.py` is the domain-specific dataset layer: it encodes keyword classes and experimental variants including background, silence, and unknown categories. `protonets/data/base.py` provides the base episodic-data abstraction. The key boundary is from prepared audio/features on disk into support/query episodes used by few-shot training and evaluation.

Paths:
- `data/download_prepare_data.py`
- `protonets/data/FewShotSpeechData.py`
- `protonets/data/base.py`

#### Prototypical-learning core
The few-shot model computes embeddings for support and query examples, forms class prototypes from support embeddings, and classifies queries by prototype distance. This logic is centered in `protonets/models/few_shot.py`; shared tensor/model operations are in `protonets/models/utils.py`. The generic training/evaluation orchestration is in `protonets/engine.py`, separating episodic optimization from speech-specific data and encoder choice.

Paths:
- `protonets/models/few_shot.py`
- `protonets/engine.py`
- `protonets/models/utils.py`

#### Pluggable speech embedding encoders
Encoder architectures are selected through a model factory rather than hardwired into the few-shot classifier. `protonets/models/factory.py` constructs configured models. Available embedding backbones include Google-style keyword-spotting CNNs and TC-ResNet variants, with common encoder utilities and defaults under the encoder package.

Paths:
- `protonets/models/factory.py`
- `protonets/models/encoder/GoogleKWS.py`
- `protonets/models/encoder/TCResNet.py`

#### Training control flow and experiment launch
Training wrappers receive `num_ways`, `num_shots`, experiment type, and experiment ID, then invoke Python runners. `run_train.py`/`run_trainval.py` drive training modes, while `train.py`/`trainval.py` implement the corresponding execution paths. Shell wrappers package standard experiments and include PBS/Grid Engine-style submission scripts, indicating optional HPC batch execution.

Paths:
- `scripts/train/few_shot/run_train.py`
- `scripts/train/few_shot/train.py`
- `scripts/train/few_shot/fewshotspeech/qsub_train.sh`

#### Evaluation and persisted outputs
Evaluation is separate from training and operates over saved experiment directories/checkpoints. `scripts/predict/few_shot/eval.py` performs episodic prediction; `eval_results.py` aggregates or reports results, and `run_eval.py` is a launcher. Results are persisted as experiment artifacts and CSV summaries, then explored through notebooks.

Paths:
- `scripts/predict/few_shot/eval.py`
- `scripts/predict/few_shot/eval_results.py`
- `results/Analyze Results.ipynb`

#### Runtime and dependencies
The runtime is Python with a locally installable `protonets` package, dependency set in `requirements.txt`, and likely deep-learning/audio libraries implied by the model and speech pipeline. Infrastructure is filesystem-based datasets, experiment/checkpoint directories, and optional cluster queue submission; no web service, database, or external online inference API is represented.

Paths:
- `setup.py`
- `requirements.txt`
- `protonets/__init__.py`

Main entry points: [`protonets/engine.py`](https://github.com/architparnami/few-shot-kws/blob/master/protonets/engine.py) (Episodic engine), [`protonets/models/factory.py`](https://github.com/architparnami/few-shot-kws/blob/master/protonets/models/factory.py) (Model factory), [`scripts/predict/few_shot/eval.py`](https://github.com/architparnami/few-shot-kws/blob/master/scripts/predict/few_shot/eval.py) (Episodic evaluation), [`protonets/models/few_shot.py`](https://github.com/architparnami/few-shot-kws/blob/master/protonets/models/few_shot.py) (Prototype classifier)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Data pipeline"]
  node_prepare["Dataset preparation<br/>data acquisition"]
  node_speech_data["Speech dataset variants<br/>dataset layer"]
  node_episodic_data["Episodic data base<br/>episode abstraction<br/>[base.py]"]
end

subgraph group_core["Prototypical learning"]
  node_factory["Model factory<br/>model construction<br/>[factory.py]"]
  node_google_kws["Google KWS encoder<br/>CNN backbone<br/>[GoogleKWS.py]"]
  node_tcresnet["TC-ResNet encoder<br/>CNN backbone<br/>[TCResNet.py]"]
  node_few_shot["Prototype classifier<br/>few-shot model<br/>[few_shot.py]"]
  node_model_utils["Model tensor utilities<br/>shared operations<br/>[utils.py]"]
  node_engine["Episodic engine<br/>train/eval orchestration<br/>[engine.py]"]
end

subgraph group_train["Training"]
  node_train_runner["Training launcher<br/>CLI runner<br/>[run_train.py]"]
  node_train_exec["Training execution<br/>training workflow<br/>[train.py]"]
  node_hpc_submit["HPC submission<br/>batch wrapper<br/>[qsub_train.sh]"]
end

subgraph group_eval["Evaluation &amp; analysis"]
  node_artifacts[("Experiment artifacts<br/>filesystem outputs")]
  node_eval_exec["Episodic evaluation<br/>prediction workflow<br/>[eval.py]"]
  node_result_report["Result aggregation<br/>reporting workflow<br/>[eval_results.py]"]
  node_notebook["Results analysis<br/>notebook"]
end

node_setup["Package installation<br/>Python packaging<br/>[setup.py]"]
node_requirements["Dependencies<br/>Python requirements<br/>[requirements.txt]"]

node_setup -->|"installs with"| node_requirements
node_prepare -->|"prepared audio/features"| node_speech_data
node_speech_data -->|"specializes"| node_episodic_data
node_factory -->|"constructs"| node_google_kws
node_factory -->|"constructs"| node_tcresnet
node_factory -->|"supplies encoder"| node_few_shot
node_model_utils -->|"shared operations"| node_few_shot
node_episodic_data -->|"support/query episodes"| node_engine
node_few_shot -->|"episodic model"| node_engine
node_train_runner -->|"launches"| node_train_exec
node_hpc_submit -.->|"submits"| node_train_runner
node_train_exec -->|"trains through"| node_engine
node_engine -->|"writes checkpoints/results"| node_artifacts
node_artifacts -->|"saved experiments"| node_eval_exec
node_eval_exec -->|"evaluates through"| node_engine
node_eval_exec -->|"evaluation results"| node_result_report
node_result_report -->|"writes CSV summaries"| node_artifacts
node_artifacts -->|"explored in"| node_notebook

click node_setup "https://github.com/architparnami/few-shot-kws/blob/master/setup.py"
click node_requirements "https://github.com/architparnami/few-shot-kws/blob/master/requirements.txt"
click node_prepare "https://github.com/architparnami/few-shot-kws/blob/master/data/download_prepare_data.py"
click node_speech_data "https://github.com/architparnami/few-shot-kws/blob/master/protonets/data/FewShotSpeechData.py"
click node_episodic_data "https://github.com/architparnami/few-shot-kws/blob/master/protonets/data/base.py"
click node_factory "https://github.com/architparnami/few-shot-kws/blob/master/protonets/models/factory.py"
click node_google_kws "https://github.com/architparnami/few-shot-kws/blob/master/protonets/models/encoder/GoogleKWS.py"
click node_tcresnet "https://github.com/architparnami/few-shot-kws/blob/master/protonets/models/encoder/TCResNet.py"
click node_few_shot "https://github.com/architparnami/few-shot-kws/blob/master/protonets/models/few_shot.py"
click node_model_utils "https://github.com/architparnami/few-shot-kws/blob/master/protonets/models/utils.py"
click node_engine "https://github.com/architparnami/few-shot-kws/blob/master/protonets/engine.py"
click node_train_runner "https://github.com/architparnami/few-shot-kws/blob/master/scripts/train/few_shot/run_train.py"
click node_train_exec "https://github.com/architparnami/few-shot-kws/blob/master/scripts/train/few_shot/train.py"
click node_hpc_submit "https://github.com/architparnami/few-shot-kws/blob/master/scripts/train/few_shot/fewshotspeech/qsub_train.sh"
click node_eval_exec "https://github.com/architparnami/few-shot-kws/blob/master/scripts/predict/few_shot/eval.py"
click node_result_report "https://github.com/architparnami/few-shot-kws/blob/master/scripts/predict/few_shot/eval_results.py"
click node_notebook "https://github.com/architparnami/few-shot-kws/blob/master/results/Analyze%20Results.ipynb"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_prepare,node_speech_data,node_episodic_data toneBlue
class node_factory,node_google_kws,node_tcresnet,node_few_shot,node_model_utils,node_engine toneAmber
class node_train_runner,node_train_exec,node_hpc_submit toneMint
class node_artifacts,node_eval_exec,node_result_report,node_notebook toneRose
class node_setup,node_requirements toneNeutral
```

## Components

### Data pipeline

- [Dataset preparation](https://github.com/architparnami/few-shot-kws/blob/master/data/download_prepare_data.py): data acquisition · Downloads and prepares FS-KWS data on disk. · `data/download_prepare_data.py`
- [Speech dataset variants](https://github.com/architparnami/few-shot-kws/blob/master/protonets/data/FewShotSpeechData.py): dataset layer · Defines keyword, background, silence, and unknown classes. · `protonets/data/FewShotSpeechData.py`
- [Episodic data base](https://github.com/architparnami/few-shot-kws/blob/master/protonets/data/base.py): episode abstraction · Builds support/query episode boundaries. · `protonets/data/base.py`

### Prototypical learning

- [Model factory](https://github.com/architparnami/few-shot-kws/blob/master/protonets/models/factory.py): model construction · Selects configured embedding encoders. · `protonets/models/factory.py`
- [Google KWS encoder](https://github.com/architparnami/few-shot-kws/blob/master/protonets/models/encoder/GoogleKWS.py): CNN backbone · `protonets/models/encoder/GoogleKWS.py`
- [TC-ResNet encoder](https://github.com/architparnami/few-shot-kws/blob/master/protonets/models/encoder/TCResNet.py): CNN backbone · `protonets/models/encoder/TCResNet.py`
- [Prototype classifier](https://github.com/architparnami/few-shot-kws/blob/master/protonets/models/few_shot.py): few-shot model · Embeds episodes, forms prototypes, and classifies queries by distance. · `protonets/models/few_shot.py`
- [Model tensor utilities](https://github.com/architparnami/few-shot-kws/blob/master/protonets/models/utils.py): shared operations · `protonets/models/utils.py`
- [Episodic engine](https://github.com/architparnami/few-shot-kws/blob/master/protonets/engine.py): train/eval orchestration · Separates episodic optimization from speech data and encoder choice. · `protonets/engine.py`

### Training

- [Training launcher](https://github.com/architparnami/few-shot-kws/blob/master/scripts/train/few_shot/run_train.py): CLI runner · Receives ways, shots, experiment type, and experiment ID. · `scripts/train/few_shot/run_train.py`
- [Training execution](https://github.com/architparnami/few-shot-kws/blob/master/scripts/train/few_shot/train.py): training workflow · `scripts/train/few_shot/train.py`
- [HPC submission](https://github.com/architparnami/few-shot-kws/blob/master/scripts/train/few_shot/fewshotspeech/qsub_train.sh): batch wrapper · Optionally submits training to PBS/Grid Engine. · `scripts/train/few_shot/fewshotspeech/qsub_train.sh`

### Evaluation & analysis

- **Experiment artifacts**: filesystem outputs · Saved experiment directories, checkpoints, and CSV summaries.
- [Episodic evaluation](https://github.com/architparnami/few-shot-kws/blob/master/scripts/predict/few_shot/eval.py): prediction workflow · Evaluates saved experiments and checkpoints. · `scripts/predict/few_shot/eval.py`
- [Result aggregation](https://github.com/architparnami/few-shot-kws/blob/master/scripts/predict/few_shot/eval_results.py): reporting workflow · `scripts/predict/few_shot/eval_results.py`
- [Results analysis](https://github.com/architparnami/few-shot-kws/blob/master/results/Analyze%20Results.ipynb): notebook · `results/Analyze Results.ipynb`

### Other components

- [Package installation](https://github.com/architparnami/few-shot-kws/blob/master/setup.py): Python packaging · Installs the local protonets package. · `setup.py`
- [Dependencies](https://github.com/architparnami/few-shot-kws/blob/master/requirements.txt): Python requirements · `requirements.txt`

## Connections

- Package installation → Dependencies: installs with
- Dataset preparation → Speech dataset variants: prepared audio/features
- Speech dataset variants → Episodic data base: specializes
- Model factory → Google KWS encoder: constructs
- Model factory → TC-ResNet encoder: constructs
- Model factory → Prototype classifier: supplies encoder
- Model tensor utilities → Prototype classifier: shared operations
- Episodic data base → Episodic engine: support/query episodes
- Prototype classifier → Episodic engine: episodic model
- Training launcher → Training execution: launches
- HPC submission → Training launcher: submits
- Training execution → Episodic engine: trains through
- Episodic engine → Experiment artifacts: writes checkpoints/results
- Experiment artifacts → Episodic evaluation: saved experiments
- Episodic evaluation → Episodic engine: evaluates through
- Episodic evaluation → Result aggregation: evaluation results
- Result aggregation → Experiment artifacts: writes CSV summaries
- Experiment artifacts → Results analysis: explored in
