Skip to content

Commit ab127ac

Browse files
Refactor tutorial code
1 parent 899a03a commit ab127ac

File tree

2 files changed

+117
-70
lines changed

2 files changed

+117
-70
lines changed

tutorials/notebooks/mct_features_notebooks/pytorch/example_pytorch_XQuant_Extension_Tool.ipynb

Lines changed: 64 additions & 42 deletions
Large diffs are not rendered by default.

tutorials/notebooks/mct_features_notebooks/pytorch/example_pytorch_XQuant_Extension_Tool_General.ipynb

Lines changed: 53 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,18 @@
1818
"## Summary\n",
1919
"We will cover the following steps:\n",
2020
"\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",
2425
" - Representative Dataset Size & Diversity\n",
2526
" - Bias Correction\n",
2627
" - Using More Samples in Mixed Precision Quantization\n",
2728
" - Threshold Selection Error Method\n",
2829
" - Enabling Hessian-Based Mixed Precision\n",
2930
" - GPTQ - Gradient-Based Post-Training Quantization\n",
30-
"4. Conclusion\n",
31+
"5. Conclusion\n",
3132
"\n",
32-
" \n",
3333
"## Setup\n",
3434
"Install the relevant packages:"
3535
]
@@ -99,9 +99,13 @@
9999
"cell_type": "markdown",
100100
"metadata": {},
101101
"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."
105109
]
106110
},
107111
{
@@ -113,14 +117,48 @@
113117
"if not os.path.isdir('imagenet'):\n",
114118
" !mkdir imagenet\n",
115119
" !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": [
120153
"batch_size = 16\n",
121154
"n_iter = 10\n",
122155
"\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]]"
124162
]
125163
},
126164
{
@@ -151,18 +189,6 @@
151189
" return dataloader"
152190
]
153191
},
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-
},
166192
{
167193
"cell_type": "markdown",
168194
"metadata": {},
@@ -547,8 +573,7 @@
547573
" results.append(val_acc)\n",
548574
" torch.cuda.empty_cache()\n",
549575
" del quantized_model\n",
550-
" gc.collect()\n",
551-
" "
576+
" gc.collect() "
552577
]
553578
},
554579
{
@@ -619,7 +644,7 @@
619644
"metadata": {},
620645
"source": [
621646
"## 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."
623648
]
624649
},
625650
{

0 commit comments

Comments
 (0)