|
18 | 18 | "## Summary\n", |
19 | 19 | "We will cover the following steps:\n", |
20 | 20 | "\n", |
21 | | - "1. Load a pre-trained MobileNetV3 model\n", |
22 | | - "2. Perform Post-Training Quantization using MCT (default parameter)\n", |
23 | | - "3. General Troubleshooting\n", |
| 21 | + "1. MobileNet V3 Setting\n", |
| 22 | + "2. Dataset Preparation\n", |
| 23 | + "3. Perform Post-Training Quantization using MCT (default parameter)\n", |
| 24 | + "4. General Troubleshooting\n", |
24 | 25 | " - Representative Dataset Size & Diversity\n", |
25 | 26 | " - Bias Correction\n", |
26 | 27 | " - Using More Samples in Mixed Precision Quantization\n", |
27 | 28 | " - Threshold Selection Error Method\n", |
28 | 29 | " - Enabling Hessian-Based Mixed Precision\n", |
29 | 30 | " - GPTQ - Gradient-Based Post-Training Quantization\n", |
30 | | - "4. Conclusion\n", |
| 31 | + "5. Conclusion\n", |
31 | 32 | "\n", |
32 | | - " \n", |
33 | 33 | "## Setup\n", |
34 | 34 | "Install the relevant packages:" |
35 | 35 | ] |
|
99 | 99 | "cell_type": "markdown", |
100 | 100 | "metadata": {}, |
101 | 101 | "source": [ |
102 | | - "## Representative Dataset\n", |
103 | | - "Download ImageNet dataset.\n", |
104 | | - "This step may take several minutes..." |
| 102 | + "## Dataset Preparation\n", |
| 103 | + "### Download ImageNet validation set\n", |
| 104 | + "Download ImageNet dataset (validation split only).\n", |
| 105 | + "\n", |
| 106 | + "This step may take several minutes...\n", |
| 107 | + "\n", |
| 108 | + "**Note:** For demonstration purposes, we use the validation set for the model quantization routines. Usually, a subset of the training dataset is used, but loading it is a heavy procedure that is unnecessary for the sake of this demonstration." |
105 | 109 | ] |
106 | 110 | }, |
107 | 111 | { |
|
113 | 117 | "if not os.path.isdir('imagenet'):\n", |
114 | 118 | " !mkdir imagenet\n", |
115 | 119 | " !wget -P imagenet https://image-net.org/data/ILSVRC/2012/ILSVRC2012_devkit_t12.tar.gz\n", |
116 | | - " !wget -P imagenet https://image-net.org/data/ILSVRC/2012/ILSVRC2012_img_val.tar\n", |
117 | | - "\n", |
118 | | - "dataset_path = './imagenet'\n", |
119 | | - "dataset = ImageNet(root=dataset_path, split='val', transform=weights.transforms())\n", |
| 120 | + " !wget -P imagenet https://image-net.org/data/ILSVRC/2012/ILSVRC2012_img_val.tar" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "markdown", |
| 125 | + "metadata": {}, |
| 126 | + "source": [ |
| 127 | + "Extract ImageNet validation dataset using torchvision \"datasets\" module." |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + "cell_type": "code", |
| 132 | + "execution_count": null, |
| 133 | + "metadata": {}, |
| 134 | + "outputs": [], |
| 135 | + "source": [ |
| 136 | + "dataset = ImageNet(root='./imagenet', split='val', transform=weights.transforms())" |
| 137 | + ] |
| 138 | + }, |
| 139 | + { |
| 140 | + "cell_type": "markdown", |
| 141 | + "metadata": {}, |
| 142 | + "source": [ |
| 143 | + "## Representative Dataset\n", |
| 144 | + "For quantization with MCT, we need to define a representative dataset. This dataset is a generator that returns a list of images:" |
| 145 | + ] |
| 146 | + }, |
| 147 | + { |
| 148 | + "cell_type": "code", |
| 149 | + "execution_count": null, |
| 150 | + "metadata": {}, |
| 151 | + "outputs": [], |
| 152 | + "source": [ |
120 | 153 | "batch_size = 16\n", |
121 | 154 | "n_iter = 10\n", |
122 | 155 | "\n", |
123 | | - "dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)" |
| 156 | + "dataloader = DataLoader(dataset, batch_size=batch_size, shuffle=True)\n", |
| 157 | + "\n", |
| 158 | + "def representative_dataset_gen():\n", |
| 159 | + " dataloader_iter = iter(dataloader)\n", |
| 160 | + " for _ in range(n_iter):\n", |
| 161 | + " yield [next(dataloader_iter)[0]]" |
124 | 162 | ] |
125 | 163 | }, |
126 | 164 | { |
|
151 | 189 | " return dataloader" |
152 | 190 | ] |
153 | 191 | }, |
154 | | - { |
155 | | - "cell_type": "code", |
156 | | - "execution_count": null, |
157 | | - "metadata": {}, |
158 | | - "outputs": [], |
159 | | - "source": [ |
160 | | - "def representative_dataset_gen():\n", |
161 | | - " dataloader_iter = iter(dataloader)\n", |
162 | | - " for _ in range(n_iter):\n", |
163 | | - " yield [next(dataloader_iter)[0]]" |
164 | | - ] |
165 | | - }, |
166 | 192 | { |
167 | 193 | "cell_type": "markdown", |
168 | 194 | "metadata": {}, |
|
547 | 573 | " results.append(val_acc)\n", |
548 | 574 | " torch.cuda.empty_cache()\n", |
549 | 575 | " del quantized_model\n", |
550 | | - " gc.collect()\n", |
551 | | - " " |
| 576 | + " gc.collect() " |
552 | 577 | ] |
553 | 578 | }, |
554 | 579 | { |
|
619 | 644 | "metadata": {}, |
620 | 645 | "source": [ |
621 | 646 | "## Conclusion\n", |
622 | | - "These analyses showed that accuracy improved by 0.31% when the number of images was 80, and by 0.54% when the number of GPTQ epochs was 80, resulting in a reduced quantization accuracy loss.By following these troubleshooting steps, you can improve the accuracy of your quantized model." |
| 647 | + "These analyses showed that accuracy improved by 0.31% when the number of images was 80, and by 0.54% when the number of GPTQ epochs was 80, resulting in a reduced quantization accuracy loss. By following these troubleshooting steps can help improve the accuracy of your quantized model." |
623 | 648 | ] |
624 | 649 | }, |
625 | 650 | { |
|
0 commit comments