StitchNet: Composing Neural Networks from Pre-Trained Fragments
We propose StitchNet, a novel neural network creation paradigm that stitches together fragments (one or more consecutive network layers) from multiple pre-trained neural networks. StitchNet allows the creation of high-performing neural networks without the large compute and data requirements needed under traditional model creation processes via backpropagation training. We leverage Centered Kernel Alignment (CKA) as a compatibility measure to efficiently guide the selection of these fragments in composing a network for a given task tailored to specific accuracy needs and computing resource constraints. We then show that these fragments can be stitched together to create neural networks with comparable accuracy to traditionally trained networks at a fraction of computing resource and data requirements. Finally, we explore a novel on-the-fly personalized model creation and inference application enabled by this new paradigm.
Installation
pip install stitchnet
Usage
import stitchnet
# load the beans dataset from huggingface
from stitchnet import load_hf_dataset
dataset_train, dataset_val = load_hf_dataset('beans', train_split='validation', val_split='test', label_column='labels', seed=47)
# prepare stitching dataset
import numpy as np
from tqdm import tqdm
stitching_dataset = np.vstack([x['pixel_values'] for x in tqdm(dataset_train.select(range(32)))])
# generate stitchnets 1 sample
score,net = generate(stitching_dataset, threshold=0, totalThreshold=0, maxDepth=10, K=2, sample=True)
# generate multiple stitchnets
generator = generate(stitching_dataset, threshold=0.8, totalThreshold=0.8, maxDepth=10, K=2, sample=False)
for score,net in generator:
print(score,net)
# print macs and params
net.get_macs_params() # {'macs': 4488343528.0, 'params': 25653096}
# save onnx
net.save_onnx('./_data/net') # saving to ./_results/net.onnx
# draw the stitchnet
net.draw_svg('./_data/net') # saving to ./_results/net.svg
# train a classifier
net.fit(dataset_train, label_column="labels")
# use it for prediction
net.predict_files(['./_results/test.jpg']) # [{'score': [0.8, 0.2, 0.0], 'label': 0}]
# evaluate with validation dataset
net.evaluate_dataset(dataset_val, label_column='labels') # {'accuracy': 0.7421875}
CUDA
See https://pytorch.org/get-started/previous-versions/ to install appropriate version. For example
# CUDA 11.6
pip install torch==1.12.1+cu116 torchvision==0.13.1+cu116 torchaudio==0.12.1 --extra-index-url https://download.pytorch.org/whl/cu116
Experiment Notebooks
- Download dogs and cats dataset from https://www.kaggle.com/c/dogs-vs-cats/data and put train data in _data/dogs_cats/raw/train folder
- See 00_prepare_data.ipynb to split the images into cats and dogs folder
- See 01_download_networks.ipynb to download the pretrained networks from Torchvision
- See 02_generate_fragments.ipynb to generate fragments from the pretrained networks
- See 03_stitchnet.ipynb to generate stitchnets
- See 04_render_graph.ipynb to create svg images of the network graphs using netron
- See 05_eval_original_networks.ipynb for evaluating the original pretrained networks
- See 06_finetuning.ipynb to generate the finetuning result
- See 07_ensemble.ipynb to generate the ensemble result
- See 08_number_of_samples_for_stitching.ipynb for experimenting with varying number of samples to use when stitching
- See 09_plot_results.ipynb plot figures of the results for the paper
Installation using conda
Create a new conda env
conda create -n stitchnet python=3.10
Activate stitchnet conda env
conda activate stitchnet
For conda and NVIDIA gpu, please also install for CUDA runtime on onnx
conda install -c conda-forge cudnn
Install poetry
curl -sSL https://install.python-poetry.org | python3 -
Install dependencies using poetry
poetry install
Metadata
Release files for stitchnet 0.2.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| stitchnet-0.2.2.tar.gz | 26.5 kB | Without provenance | Details for stitchnet-0.2.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| stitchnet-0.2.2-py3-none-any.whl | Python 3 | none | any | Details for stitchnet-0.2.2-py3-none-any.whl |
Total release size: 56.7 kB
Release files / stitchnet-0.2.2.tar.gz
| Download URL | stitchnet-0.2.2.tar.gz |
|---|---|
| Size | 26.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
538e8a1896aa8fca8a3e787cef0c09d041753cb229348a598bd47cfbbcec273b
|
|
BLAKE2b-256 checksum How to use checksums |
59818588c0ddab755e8358d892c442e3c569be8ee73122f94484c21cb9f62a0e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.14 CPython/3.9.7 Linux/5.13.0-28-generic
|
Release files / stitchnet-0.2.2-py3-none-any.whl
| Download URL | stitchnet-0.2.2-py3-none-any.whl |
|---|---|
| Size | 30.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
26f5a9b86b3708d59037bd15e8b2507d28a9d4b3507e8fbac19e9e55a0c6fcfc
|
|
BLAKE2b-256 checksum How to use checksums |
646f293e88f2808d570f6f9134d4cb4ec2a24ef7eb2301f0ced342adfcb764f6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.1.14 CPython/3.9.7 Linux/5.13.0-28-generic
|
Security for stitchnet 0.2.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.