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
6868from torch .quantization import QuantStub , DeQuantStub
6969
@@ -304,9 +304,60 @@ def print_size_of_model(model):
304304# ----------------------------------
305305#
306306# As our last major setup step, we define our dataloaders for our training and testing set.
307- # The specific dataset we've created for this tutorial contains just 1000 images, one from
307+ #
308+ # ImageNet Data
309+ # ^^^^^^^^^^^^^
310+ #
311+ # The specific dataset we've created for this tutorial contains just 1000 images from the ImageNet data, one from
308312# 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
313+ # relatively easily). The URL for this custom dataset is:
314+ #
315+ # .. code::
316+ #
317+ # https://s3.amazonaws.com/pytorch-tutorial-assets/imagenet_1k.zip
318+ #
319+ # To download this data locally using Python, you could use:
320+ #
321+ # .. code:: python
322+ #
323+ # import requests
324+ #
325+ # url = 'https://s3.amazonaws.com/pytorch-tutorial-assets/imagenet_1k.zip`
326+ # filename = '~/Downloads/imagenet_1k_data.zip'
327+ #
328+ # r = requests.get(url)
329+ #
330+ # with open(filename, 'wb') as f:
331+ # f.write(r.content)
332+ #
333+ # For this tutorial to run, we download this data and move it to the right place using
334+ # `these lines <https://github.com/pytorch/tutorials/blob/master/Makefile#L97-L98>`_
335+ # from the `Makefile <https://github.com/pytorch/tutorials/blob/master/Makefile>`_.
336+ #
337+ # To run the code in this tutorial using the entire ImageNet dataset, on the other hand, you could download
338+ # the data using ``torchvision`` following
339+ # `here <https://pytorch.org/docs/stable/torchvision/datasets.html#imagenet>`_. For example,
340+ # to download the training set and apply some standard transformations to it, you could use:
341+ #
342+ # .. code:: python
343+ #
344+ # import torchvision
345+ # import torchvision.transforms as transforms
346+ #
347+ # imagenet_dataset = torchvision.datasets.ImageNet(
348+ # '~/.data/imagenet',
349+ # split='train',
350+ # download=True,
351+ # transforms.Compose([
352+ # transforms.RandomResizedCrop(224),
353+ # transforms.RandomHorizontalFlip(),
354+ # transforms.ToTensor(),
355+ # transforms.Normalize(mean=[0.485, 0.456, 0.406],
356+ # std=[0.229, 0.224, 0.225]),
357+ # ])
358+ #
359+ # With the data downloaded, we show functions below that define dataloaders we'll use to read
360+ # in this data. These functions mostly come from
310361# `here <https://github.com/pytorch/vision/blob/master/references/detection/train.py>`_.
311362
312363def prepare_data_loaders (data_path ):
@@ -348,11 +399,12 @@ def prepare_data_loaders(data_path):
348399 return data_loader , data_loader_test
349400
350401######################################################################
351- # Next, we'll load in the pre-trained MobileNetV2 model
402+ # Next, we'll load in the pre-trained MobileNetV2 model. We provide the URL to download the data from in ``torchvision``
403+ # `here <https://github.com/pytorch/vision/blob/master/torchvision/models/mobilenet.py#L9>`_.
352404
353405data_path = 'data/imagenet_1k'
354406saved_model_dir = 'data/'
355- float_model_file = 'mobilenet_quantization .pth'
407+ float_model_file = 'mobilenet_pretrained_float .pth'
356408scripted_float_model_file = 'mobilenet_quantization_scripted.pth'
357409scripted_quantized_model_file = 'mobilenet_quantization_scripted_quantized.pth'
358410
@@ -391,7 +443,7 @@ def prepare_data_loaders(data_path):
391443torch .jit .save (torch .jit .script (float_model ), saved_model_dir + scripted_float_model_file )
392444
393445######################################################################
394- # You should see 78% accuracy on 300 images, a solid baseline for ImageNet,
446+ # We see 78% accuracy on 300 images, a solid baseline for ImageNet,
395447# especially considering our model is just 14.0 MB.
396448#
397449# This will be our baseline to compare to. Next, let's try different quantization methods
@@ -406,7 +458,8 @@ def prepare_data_loaders(data_path):
406458# data). These distributions are then used to determine how the specifically the different activations
407459# should be quantized at inference time (a simple technique would be to simply divide the entire range
408460# 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.
461+ # this additional step allows us to pass quantized values between operations instead of converting these
462+ # values to floats - and then back to ints - between every operation, resulting in a significant speed-up.
410463
411464num_calibration_batches = 10
412465
@@ -442,7 +495,7 @@ def prepare_data_loaders(data_path):
442495print ('Evaluation accuracy on %d images, %2.2f' % (num_eval_batches * eval_batch_size , top1 .avg ))
443496
444497######################################################################
445- # For this quantized model, we see a significantly lower accuracy of just 62.33 % on these same 30
498+ # For this quantized model, we see a significantly lower accuracy of just ~62 % on these same 300
446499# images. Nevertheless, we did reduce the size of our model down to just under 3.6 MB, almost a 4x
447500# decrease.
448501#
@@ -470,7 +523,7 @@ def prepare_data_loaders(data_path):
470523
471524######################################################################
472525# 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.
526+ # of the accuracy to over 76 %! Still, this is 1-2 % worse than the baseline of 78% achieved above.
474527# So lets try quantization aware training.
475528#
476529# 5. Quantization-aware training
0 commit comments