{"id":13443249,"url":"https://github.com/mit-han-lab/haq","last_synced_at":"2025-05-13T18:38:14.887Z","repository":{"id":45559501,"uuid":"191860494","full_name":"mit-han-lab/haq","owner":"mit-han-lab","description":"[CVPR 2019, Oral] HAQ: Hardware-Aware Automated Quantization with Mixed Precision","archived":false,"fork":false,"pushed_at":"2021-02-26T02:37:26.000Z","size":66,"stargazers_count":380,"open_issues_count":17,"forks_count":85,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-03-20T16:39:54.297Z","etag":null,"topics":["automl","efficient-model","mixed-precision","quantization"],"latest_commit_sha":null,"homepage":"https://hanlab.mit.edu/projects/haq/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mit-han-lab.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-06-14T02:06:06.000Z","updated_at":"2025-03-14T05:47:46.000Z","dependencies_parsed_at":"2022-07-14T20:30:40.011Z","dependency_job_id":null,"html_url":"https://github.com/mit-han-lab/haq","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mit-han-lab%2Fhaq","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mit-han-lab%2Fhaq/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mit-han-lab%2Fhaq/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mit-han-lab%2Fhaq/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mit-han-lab","download_url":"https://codeload.github.com/mit-han-lab/haq/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254004749,"owners_count":21998118,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["automl","efficient-model","mixed-precision","quantization"],"created_at":"2024-07-31T03:01:58.091Z","updated_at":"2025-05-13T18:38:14.857Z","avatar_url":"https://github.com/mit-han-lab.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"\n# HAQ: Hardware-Aware Automated Quantization with Mixed Precision\n\n## Introduction\n\nThis repo contains PyTorch implementation for paper [HAQ: Hardware-Aware Automated Quantization with Mixed Precision](http://openaccess.thecvf.com/content_CVPR_2019/papers/Wang_HAQ_Hardware-Aware_Automated_Quantization_With_Mixed_Precision_CVPR_2019_paper.pdf) (CVPR2019, oral)\n\n![overview](https://hanlab.mit.edu/projects/haq/images/overview.png)\n\n```\n@inproceedings{haq,\nauthor = {Wang, Kuan and Liu, Zhijian and Lin, Yujun and Lin, Ji and Han, Song},\ntitle = {HAQ: Hardware-Aware Automated Quantization With Mixed Precision},\nbooktitle = {IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},\nyear = {2019}\n}\n```\n\nOther papers related to automated model design:\n- AMC: AutoML for Model Compression and Acceleration on Mobile Devices ([ECCV 2018](https://arxiv.org/abs/1802.03494))\n\n- ProxylessNAS: Direct Neural Architecture Search on Target Task and Hardware ([ICLR 2019](https://arxiv.org/abs/1812.00332))\n\n## Dependencies\nWe evaluate this code with Pytorch 1.1 (cuda10) and torchvision 0.3.0, you can install pytorch with conda:\n```\n# install pytorch\nconda install -y pytorch torchvision cudatoolkit=10.0 -c pytorch\n```\nAnd you can use the following command to set up the environment:\n```\n# install packages and download the pretrained model\nbash run/setup.sh\n```\n(If the server is down, you can download the pretrained model from google drive: [mobilenetv2-150.pth.tar](https://drive.google.com/open?id=1fZ1gNSzSZTQfJ0dL-bNYULNvZJxp_Y53))\n\nCurrent code base is tested under following environment:\n1. Python         3.7.3\n2. PyTorch        1.1\n3. torchvision    0.3.0\n4. numpy          1.14\n5. matplotlib     3.0.1\n6. scikit-learn   0.21.0\n7. easydict       1.8\n8. progress       1.4\n9. tensorboardX   1.7\n\n## Dataset\nIf you already have the ImageNet dataset for pytorch, you could create a link to data folder and use it:\n```\n# prepare dataset, change the path to your own\nln -s /path/to/imagenet/ data/\n```\nIf you do not have the ImageNet yet, you can download the ImageNet dataset and move validation images to labeled subfolders. To do this, you can use the following script: \n[https://raw.githubusercontent.com/soumith/imagenetloader.torch/master/valprep.sh](https://raw.githubusercontent.com/soumith/imagenetloader.torch/master/valprep.sh)\n\nWe use a subset of ImageNet in the linear quantizaiton search phase to save the training time, to create the link of the subset, you can use the following tool:\n ```\n# prepare imagenet100 dataset\npython lib/utils/make_data.py\n```\n\n\n## Reinforcement learning search\n- You can run the bash file as following to search the K-Means quantization strategy, which only quantizes the weights with K-Means to compress model size of specific model.\n```\n# K-Means quantization, for model size\nbash run/run_kmeans_quantize_search.sh\n```\n- You can run the bash file as following to search the linear quantization strategy, which linearly quantizes both the weights and activations to reduce latency/energy of specific model.\n```\n# Linear quantization, for latency/energy\nbash run/run_linear_quantize_search.sh\n```\n- Usage details\n```\npython rl_quantize.py --help\n```\n\n## Finetune Policy\n- After searching, you can get the quantization strategy list, and you can replace the strategy list in **finetune.py** to finetune and evaluate the performance on ImageNet dataset.\n- We set the default K-Means quantization strategy searched under preserve ratio = 0.1 like:\n```\n# preserve ratio 10%\nstrategy = [6, 6, 5, 5, 5, 5, 4, 5, 5, 4, 5, 5, 5, 5, 5, 5, 3, 5, 4, 3, 5, 4, 3, 4, 4, 4, 2, 5, 4, 3, 3, 5, 3, 2, 5, 3, 2, 4, 3, 2, 5, 3, 2, 5, 3, 4, 2, 5, 2, 3, 4, 2, 3, 4]\n```\nYou can follow the following bash file to finetune the K-Means quantized model to get a better performance:\n```\nbash run/run_kmeans_quantize_finetune.sh\n```\n- We set the default linear quantization strategy searched under preserve ratio = 0.6 like:\n```\n# preserve ratio 60%\nstrategy = [[8, -1], [7, 7], [5, 6], [4, 6], [5, 6], [5, 7], [5, 6], [7, 4], [4, 6], [4, 6], [7, 7], [5, 6], [4, 6], [7, 3], [5, 7], [4, 7], [7, 3], [5, 7], [4, 7], [7, 7], [4, 7], [4, 7], [6, 4], [6, 7], [4, 7], [7, 4], [6, 7], [5, 7], [7, 4], [6, 7], [5, 7], [7, 4], [6, 7], [6, 7], [6, 4], [5, 7], [6, 7], [6, 4], [5, 7], [6, 7], [7, 7], [4, 7], [7, 7], [7, 7], [4, 7], [7, 7], [7, 7], [4, 7], [7, 7], [7, 7], [4, 7], [7, 7], [8, 8]]\n```\nYou can follow the following bash file to finetune the linear quantized model to get a better performance:\n```\nbash run/run_linear_quantize_finetune.sh\n```\n- Usage details\n```\npython finetune.py --help\n```\n## Evaluate\nYou can download the pretrained quantized model like this:\n```\n# download checkpoint\nmkdir -p checkpoints/resnet50/\nmkdir -p checkpoints/mobilenetv2/\ncd checkpoints/resnet50/\nwget https://hanlab.mit.edu/files/haq/resnet50_0.1_75.48.pth.tar\ncd ../mobilenetv2/\nwget https://hanlab.mit.edu/files/haq/qmobilenetv2_0.6_71.23.pth.tar\ncd ../..\n```\n(If the server is down, you can download the pretrained model from google drive: [qmobilenetv2_0.6_71.23.pth.tar](https://drive.google.com/open?id=1oW1Jq17LIwcOckOzZPWDlKEhGWkZ3F_r)) \n\nYou can evaluate the K-Means quantized model like this:\n```\n# evaluate K-Means quantization\nbash run/run_kmeans_quantize_eval.sh\n```\n| Models                   | preserve ratio | Top1 Acc (%) | Top5 Acc (%) |\n| ------------------------ | -------------- | ------------ | ------------ |\n| resnet50 (original)      |       1.0      |     76.15    |    92.87     |\n| resnet50 (10x compress)  |       0.1      |     75.48    |    92.42     |\n\nYou can evaluate the linear quantized model like this:\n```\n# evaluate linear quantization\nbash run/run_linear_quantize_eval.sh\n```\n\n| Models                    | preserve ratio | Top1 Acc (%) | Top5 Acc (%) |\n| ------------------------  | -------------- | ------------ | ------------ |\n| mobilenetv2 (original)    |       1.0      |     72.05    |    90.49     |\n| mobilenetv2 (0.6x latency)|       0.6      |     71.23    |    90.00     |\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmit-han-lab%2Fhaq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmit-han-lab%2Fhaq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmit-han-lab%2Fhaq/lists"}