62
62
# - Insert ``QuantStub`` and ``DeQuantStub`` at the beginning and end of the network.
63
63
# - Replace ReLU6 with ReLU
64
64
#
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>`_.
67
67
68
68
from torch .quantization import QuantStub , DeQuantStub
69
69
@@ -304,9 +304,60 @@ def print_size_of_model(model):
304
304
# ----------------------------------
305
305
#
306
306
# 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
308
312
# 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
310
361
# `here <https://github.com/pytorch/vision/blob/master/references/detection/train.py>`_.
311
362
312
363
def prepare_data_loaders (data_path ):
@@ -348,11 +399,12 @@ def prepare_data_loaders(data_path):
348
399
return data_loader , data_loader_test
349
400
350
401
######################################################################
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>`_.
352
404
353
405
data_path = 'data/imagenet_1k'
354
406
saved_model_dir = 'data/'
355
- float_model_file = 'mobilenet_quantization .pth'
407
+ float_model_file = 'mobilenet_pretrained_float .pth'
356
408
scripted_float_model_file = 'mobilenet_quantization_scripted.pth'
357
409
scripted_quantized_model_file = 'mobilenet_quantization_scripted_quantized.pth'
358
410
@@ -391,7 +443,7 @@ def prepare_data_loaders(data_path):
391
443
torch .jit .save (torch .jit .script (float_model ), saved_model_dir + scripted_float_model_file )
392
444
393
445
######################################################################
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,
395
447
# especially considering our model is just 14.0 MB.
396
448
#
397
449
# 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):
406
458
# data). These distributions are then used to determine how the specifically the different activations
407
459
# should be quantized at inference time (a simple technique would be to simply divide the entire range
408
460
# 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.
410
463
411
464
num_calibration_batches = 10
412
465
@@ -442,7 +495,7 @@ def prepare_data_loaders(data_path):
442
495
print ('Evaluation accuracy on %d images, %2.2f' % (num_eval_batches * eval_batch_size , top1 .avg ))
443
496
444
497
######################################################################
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
446
499
# images. Nevertheless, we did reduce the size of our model down to just under 3.6 MB, almost a 4x
447
500
# decrease.
448
501
#
@@ -470,7 +523,7 @@ def prepare_data_loaders(data_path):
470
523
471
524
######################################################################
472
525
# 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.
474
527
# So lets try quantization aware training.
475
528
#
476
529
# 5. Quantization-aware training
0 commit comments