Fitting PyTorch models
This repository follows a workshop to set up a Python package, build some neural networks with torch, and publish the models on HuggingFace.
There are two datasets that we simulate:
- Colored shapes (circle, rectangle, triangle, diamond) in a pixellated image
- One-dimensional sinusoids
Alongside these datasets, we fit the following model objectives:
- (Classification) Predict the shape and color in the image
- (Regression) Predict the next time steps of the sine function
Usage
Learn how to train neural networks from scratch.
- Run
answers/simulate-exercise.ipynbto get data. - Fill in the
# TO DOparts inexamples/modeling-exercise-*.ipynb. - Compare to solutions in
answers/modeling-exercise-*.ipynb. - You can explore different parameters on big models with
scripts/modeling.py.- Write a shell script that invokes
scripts/modeling.pyand pass args toslurm.
- Write a shell script that invokes
- Run
scripts/modeling-final.pyfor best model choice (train + val data). - (Optional) Compare to the benchmark here.
The package defined under src/ provides:
- A class
Shapethat instantiates an image with 1 colored shape - A function
simulate_shapes()to make many images for an image classifier - A model class
MyCNNto fit a standard architecture
Caution: you may need GPU resources if your models or data are large.
Requirements
- Python 3.10+
Install
If you want to install the package only from the internet:
pip install zootopia3
If you want to set up an isolated environment and build locally:
python -m venv path-to/your-environment
source path-to/your-environment/bin/activate
pip install -e .
You run the pip command within this repo.
Data
I made a training and validation set with:
- 2000 samples for each combo
mix_x = 20max_x = 100shades = Truemagnitude = 50
I made a testing set with:
- 20 samples for each combo
min_x = 10max_x = 50
Therefore, the test set is a more difficult prediction problem.
You can find the test data here.
Sharing your work
To upload your model to Hugging Face, run these short scripts.
python hf-push.py your-model.pth sdtemple/color-prediction-model
A less elegant solution where you have to manually write the config.json data is:
python scripts/hf-convert.py your-model.pt your-model.safetensors
python scripts/hf-model.py your-model.safetensors sdtemple/color-prediction-model
To upload your data to Hugging Face, run this script.
python scripts/hf-dataset.py images.npy target_color.txt target_shape.txt your-username/colored-shapes
If you successfully uploaded a pretrained model to Hugging Face, you can run it on a CPU with answers/inference-exercise.ipynb.
Test
You can run the test scripts in tests/ with the following:
python -m pytest
Metadata
Release files for zootopia3 1.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 | |
|---|---|---|---|
| zootopia3-1.2.tar.gz | 13.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| zootopia3-1.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 30.2 kB
Release files / zootopia3-1.2.tar.gz
| Download URL | zootopia3-1.2.tar.gz |
|---|---|
| Size | 13.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
53f03a6f90882a85d50a0cf5f68777c9b358e56d308f28ac4bb0b306de010d90
|
|
BLAKE2b-256 checksum How to use checksums |
939028f114d142b64a89e07438e42b1115f4e1c5990f50c1901f9855aa99346b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.10.10
|
Release files / zootopia3-1.2-py3-none-any.whl
| Download URL | zootopia3-1.2-py3-none-any.whl |
|---|---|
| Size | 16.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e59a9629e854540013f4fe4d7a073de5aeb7610d479a70b0cf861af144777a68
|
|
BLAKE2b-256 checksum How to use checksums |
89c7138fdda95adf025e22cd253131544a0520ae344a14a0bca0bf640f7e9d09
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.0.0 CPython/3.10.10
|