Bachelor thesis - Miroslav Šafář (xsafar23@stud.fit.vutbr.cz)
src/- source code of proposed systemkarolina/- scripts used for running experiments on Karolina supercomputernsga_runs/- folder that contains system runs referred to in the paperlatex/- source code of the paperautomated_quantization_of_neural_networks_paper.pdf- bachelor thesis paperREADME.md- READMEtiny_imagenet100.tar- dataset used for experimentsenvironment_linux_x86.yml- conda environment file for Linuxenvironment_macos_arm64.yml- conda environment file for macOS with Apple Silicon
We provide conda environment file to setup conda environment.
On Apple Silicon computers:
$ conda env create --file environment_macos_arm64.ymlOn Linux:
$ conda env create --file environment_linux_x86.ymlWARNING: Linux environment does not contains libraries for GPU acceleration.
Then you can activate created conda environment by using:
$ conda activate bachelor_thesisThere is another option to setup the project environment using pip. We recommend using Python 3.10.9 and a virtual environment. You can install all required packages using pip:
$ pip install -r requirements_macos_arm64.txtWARNING: You can use this option only with Apple Silicon computer. With linux please use conda environment.
For testing purposes, we provide our tiny-imagenet dataset. To use it you need to extract
tiny_imagenet100.tar into your TensorFlow Datamodels folder (default: ~/tensorflow_datasets).
Switch to the source directory:
$ cd srcTo create and train MobileNet model on tiny-imagenet dataset use:
$ python3 mobilenet_tinyimagenet_train.py --alpha 0.25 --save-as mobilenet_tinyimagenet_025.kerasIf you already have a weights file for a model, you can create it using:
$ python3 mobilenet_tinyimagenet_create.py --alpha 0.25 --weights-path weights_025.hfd5 --destination mobilenet_tinyimagenet_025.kerasSwitch to the source directory:
$ cd srcTo run NSGA-II for per-layer asymmetric quantization with the approximate solution for batch normalization folding use:
$ python3 run_nsga.py --generations 20 --parent-size 16 --offspring-size 16 --logs-dir <nsga_run_log_dir> --approxBy default, it uses pre-trained MobileNet model saved as mobilenet_tinyimagenet_025.keras,
to use different pre-trained model, specify parameter --base-model-path.
Other important parameters:
--per-channeluse per-channel weight quantization for convolutional layers--symmetricuse symmetric quantization for weights--batch-sizebatch size for quantization-aware training--epochsnumber of epochs for partial tuning of models--multigpurun on multiple gpus--helpto print list of all script parameters
Then evaluate the final results with full fine-tuning of the quantized models using:
For evaluation of per-layer asymmetric quantization using more accurate method for batch normalization folding use:
python3 nsga_evaluate.py --run <nsga_run_log_dir>/<run_file>For evaluation of per-layer asymmetric quantization using approximate method for batch normalization folding use:
python3 nsga_evaluate.py --run <nsga_run_log_dir>/<run_file> --approxFor evaluation of per-channel symmetric quantization use:
python3 nsga_evaluate.py --run <nsga_run_log_dir> --approx --per-channel --symmetricOther important parameters:
--batch-sizebatch size for quantization-aware training--epochsnumber of epochs for final fine-tuning--multigpurun on multiple gpus--helpto print a list of all script parameters
To view system results use:
python3 show_layer_configuration.py --run <nsga_run_log_dir> [--per-channel] [--symmetric]This script allows you to choose between best-found configurations and then shows you the bit-width for each layer.