Skip to content

Commit a4115b6

Browse files
author
Seth Weidman
committed
Add paths for downloaded files to static quantization tutorial
1 parent 8081dd7 commit a4115b6

File tree

1 file changed

+36
-8
lines changed

1 file changed

+36
-8
lines changed

advanced_source/static_quantization_tutorial.py

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
# - Insert ``QuantStub`` and ``DeQuantStub`` at the beginning and end of the network.
6363
# - Replace ReLU6 with ReLU
6464
#
65-
# Note that this code is taken from
66-
# `here <https://github.com/pytorch/vision/blob/master/torchvision/models/mobilenet.py>`_
65+
# Note: this code is taken from
66+
# `here <https://github.com/pytorch/vision/blob/master/torchvision/models/mobilenet.py>`_.
6767

6868
from torch.quantization import QuantStub, DeQuantStub
6969

@@ -306,7 +306,33 @@ def print_size_of_model(model):
306306
# As our last major setup step, we define our dataloaders for our training and testing set.
307307
# The specific dataset we've created for this tutorial contains just 1000 images, one from
308308
# each class (this dataset, at just over 250 MB, is small enough that it can be downloaded
309-
# relatively easily). These functions mostly come from
309+
# relatively easily). The URL for this custom dataset is:
310+
#
311+
# .. code::
312+
#
313+
# https://s3.amazonaws.com/pytorch-tutorial-assets/imagenet_1k.zip
314+
#
315+
# To download this data locally using Python, then, you could use:
316+
#
317+
# .. code:: python
318+
#
319+
# import requests
320+
#
321+
# url = 'https://s3.amazonaws.com/pytorch-tutorial-assets/imagenet_1k.zip`
322+
# filename = '~/Downloads/imagenet_1k_data.zip'
323+
#
324+
# r = requests.get(url)
325+
#
326+
# with open(filename, 'wb') as f:
327+
# f.write(r.content)
328+
#
329+
#
330+
# For this tutorial to run, we download this data and move it to the right place using
331+
# `these lines <https://github.com/pytorch/tutorials/blob/master/Makefile#L97-L98>`_
332+
# from the `Makefile <https://github.com/pytorch/tutorials/blob/master/Makefile>`_.
333+
#
334+
# With the data downloaded, we show functions below that define dataloaders we'll use to read
335+
# in this data. These functions mostly come from
310336
# `here <https://github.com/pytorch/vision/blob/master/references/detection/train.py>`_.
311337

312338
def prepare_data_loaders(data_path):
@@ -348,7 +374,8 @@ def prepare_data_loaders(data_path):
348374
return data_loader, data_loader_test
349375

350376
######################################################################
351-
# Next, we'll load in the pre-trained MobileNetV2 model
377+
# Next, we'll load in the pre-trained MobileNetV2 model. Similarly to the data about, the file with the pre-trained
378+
# weights is stored at ``https://s3.amazonaws.com/pytorch-tutorial-assets/mobilenet_quantization.pth``:
352379

353380
data_path = 'data/imagenet_1k'
354381
saved_model_dir = 'data/'
@@ -391,7 +418,7 @@ def prepare_data_loaders(data_path):
391418
torch.jit.save(torch.jit.script(float_model), saved_model_dir + scripted_float_model_file)
392419

393420
######################################################################
394-
# You should see 78% accuracy on 300 images, a solid baseline for ImageNet,
421+
# We see 78% accuracy on 300 images, a solid baseline for ImageNet,
395422
# especially considering our model is just 14.0 MB.
396423
#
397424
# This will be our baseline to compare to. Next, let's try different quantization methods
@@ -406,7 +433,8 @@ def prepare_data_loaders(data_path):
406433
# data). These distributions are then used to determine how the specifically the different activations
407434
# should be quantized at inference time (a simple technique would be to simply divide the entire range
408435
# of activations into 256 levels, but we support more sophisticated methods as well). Importantly,
409-
# this additional step allows us to pass quantized values between operations instead of converting these values to floats - and then back to ints - between every operation, resulting in a significant speed-up.
436+
# this additional step allows us to pass quantized values between operations instead of converting these
437+
# values to floats - and then back to ints - between every operation, resulting in a significant speed-up.
410438

411439
num_calibration_batches = 10
412440

@@ -442,7 +470,7 @@ def prepare_data_loaders(data_path):
442470
print('Evaluation accuracy on %d images, %2.2f'%(num_eval_batches * eval_batch_size, top1.avg))
443471

444472
######################################################################
445-
# For this quantized model, we see a significantly lower accuracy of just 62.33% on these same 30
473+
# For this quantized model, we see a significantly lower accuracy of just ~62% on these same 300
446474
# images. Nevertheless, we did reduce the size of our model down to just under 3.6 MB, almost a 4x
447475
# decrease.
448476
#
@@ -470,7 +498,7 @@ def prepare_data_loaders(data_path):
470498

471499
######################################################################
472500
# Changing just this quantization configuration method resulted in an increase
473-
# of the accuracy to 74%! Still, this is 4% worse than the baseline of 78% achieved above.
501+
# of the accuracy to over 76%! Still, this is 1-2% worse than the baseline of 78% achieved above.
474502
# So lets try quantization aware training.
475503
#
476504
# 5. Quantization-aware training

0 commit comments

Comments
 (0)