This project presents an implementation of One-Shot Learning with Bayesian Program Learning (BPL), a machine learning approach that enables models to recognize patterns or concepts from just one example. In traditional machine learning, models typically require extensive training data to learn and generalize. However, BPL allows models to learn from a single example by constructing probabilistic programs that represent the relationships between variables and uncertain states.
In this project, we apply One-Shot Learning with BPL to handwritten character classification. By leveraging the Bayesian network representation of these relationships, our system learns to recognize new characters based on just one example. This approach enables novel applications in handwriting recognition and machine learning-based writing tools.
- Data Preparation: Handwritten character images are organized into labeled runs.
- Program Learning: For each example, a probabilistic program representing the character's structure is learned.
- Classification: New characters are classified by comparing them to previously learned programs using the Modified Hausdorff Distance (MHD).
- One-shot learning: Classify new characters using only a single example.
- Probabilistic Program Induction: Uses BPL to learn and classify handwritten characters.
- Flexible data preparation with a customizable folder structure. The system requires preparing datasets using a specific folder structure for easy processing
- Python 3.8 or higher
requirements.txt(install via pip)
pip install -r requirements.txtThe project expects a specific directory structure for input data:
📂 Project Root/
├── 📁 all_runs/ # Directory containing *all* your data runs
│ ├── run01/ # Individual data run 1
│ ├── run02/ # Individual data run 2
│ └── ... # Each run has its own label file (`class_labels.txt`)
│
├── 📄 README.md # This file
└── 📘 main.py # Main implementation script
- Store your handwritten character images in the
all_runs/directory, organized into subfolders (run01,run02, etc.). Note: Add more character images to create more runs. - Ensure each run subfolder (e.g.,
run01/) contains a file namedclass_labels.txt. - The
class_labels.txtformat should be a list of pairs, each line representing one training/test example:image_test_path image_train_path.
Here is a concrete example of what the class_labels.txt file should look like for one run:
run01/images/image_test_01.png run01/images/image_train_01.png
run01/images/image_test_02.png run01/images/image_train_02.png
run01/images/image_test_03.png run01/images/image_train_03.png
...
To run the classifier, use:
python main.py [number_of_runs]- The script reads data from
all_runs/and performs classification. - It displays error rates for each run, concluding with the average across all runs.
- Input: Organized data in
all_runs/withclass_labels.txtfiles. - Output:
- Error rates for each run
- Final average error rate across all runs
Example Output:
Running one-shot handwritten character classifier
[INFO] Run 01: Error rate X.X%
[INFO] Run 02: Error rate X.X%
...
[INFO] Run 20: Error rate Y.Y%
[RESULT] Average error rate across *n* independent experiments (where n is the number of runs): Z.Z%This project builds on the Bayesian Program Learning (BPL) framework from academic research.
Lake, B. M., Salakhutdinov, R., and Tenenbaum, J. B. (2015).
Human-level concept learning through probabilistic program induction.
Science, 350(6266), 1332-1338.
This is an adaptation based on the MATLAB code from Brenden Lake's BPL repository.
This project is licensed under the MIT License.
For details, please refer to the
LICENSEfile included in this project's root directory.