From 56b0f6def5d827c9e71624da37a0428607019db7 Mon Sep 17 00:00:00 2001 From: yardeny-sony Date: Sun, 18 Aug 2024 20:13:38 +0300 Subject: [PATCH 1/5] torchvision tutorial notebook --- .../notebooks/imx500_notebooks/README.md | 38 +- ...orchvision_classification_for_imx500.ipynb | 1570 +++++++++++++++++ 2 files changed, 1607 insertions(+), 1 deletion(-) create mode 100644 tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb diff --git a/tutorials/notebooks/imx500_notebooks/README.md b/tutorials/notebooks/imx500_notebooks/README.md index 05f6ba704..f5f448faa 100644 --- a/tutorials/notebooks/imx500_notebooks/README.md +++ b/tutorials/notebooks/imx500_notebooks/README.md @@ -23,7 +23,7 @@ deployment performance. - Classification + Classification MobilenetV2 Keras Keras Applications @@ -68,6 +68,42 @@ deployment performance. 74.026 73.72 + + mnasnet1_0 + PyTorch + torchvision + + ImageNet + 73.47 + 73.16 + + + mobilenet_v2 + PyTorch + torchvision + + ImageNet + 72.01 + 71.25 + + + regnet_y_400mf + PyTorch + torchvision + + ImageNet + 74.03 + 73.69 + + + shufflenet_v2_x1_5 + PyTorch + torchvision + + ImageNet + 69.34 + 69.04 + Object Detection diff --git a/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb b/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb new file mode 100644 index 000000000..5c5cefe1a --- /dev/null +++ b/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb @@ -0,0 +1,1570 @@ +{ + "cells": [ + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "# PyTorch Model from torchvision - Quantization for IMX500\n", + "\n", + "[Run this tutorial in Google Colab](https://colab.research.google.com/github/sony/model_optimization/blob/main/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_model_for_imx500.ipynb)\n", + "\n", + "## Overview\n", + "\n", + "In this tutorial, we will illustrate a basic and quick process of preparing a pre-trained model for deployment using MCT. \n", + "We will use an existing pre-trained model from [torchvision](https://pytorch.org/vision/stable/models.html). The user can choose any torchvision model from this list. " + ], + "id": "ca9e0ba1f92d22fc" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "## Setup\n", + "### Install the relevant packages" + ], + "id": "7e737ed9d9b11d2a" + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": [ + "!pip install -q torch\n", + "!pip install -q torchvision\n", + "!pip install -q onnx" + ], + "id": "5250b07975f15b5f" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": "Install MCT (if it’s not already installed). Additionally, in order to use all the necessary utility functions for this tutorial, we also copy [MCT tutorials folder](https://github.com/sony/model_optimization/tree/main/tutorials) and add it to the system path.", + "id": "b1a05efedd4dbc77" + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": [ + "import sys\n", + "import importlib\n", + "\n", + "if not importlib.util.find_spec('model_compression_toolkit'):\n", + " # !pip install model_compression_toolkit #TODO: delete nightly\n", + " !pip install mct-nightly\n", + "!git clone https://github.com/sony/model_optimization.git temp_mct && mv temp_mct/tutorials . && \\rm -rf temp_mct\n", + "sys.path.insert(0,\"tutorials\")" + ], + "id": "391a9ed0d178002e" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "### Download ImageNet validation set\n", + "Download ImageNet dataset with only the validation split.\n", + "\n", + "Note that 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.\n", + "\n", + "This step may take several minutes..." + ], + "id": "5923827fab97d1de" + }, + { + "metadata": {}, + "cell_type": "code", + "outputs": [], + "execution_count": null, + "source": [ + "import os\n", + "if not os.path.isdir('imagenet'):\n", + " print(\"downloading\")\n", + " !mkdir imagenet\n", + " !wget https://image-net.org/data/ILSVRC/2012/ILSVRC2012_devkit_t12.tar.gz\n", + " !mv ILSVRC2012_devkit_t12.tar.gz imagenet/\n", + " !wget https://image-net.org/data/ILSVRC/2012/ILSVRC2012_img_val.tar\n", + " !mv ILSVRC2012_img_val.tar imagenet/" + ], + "id": "ec301c30cd83d535" + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "## Model Quantization\n", + "\n", + "### Download a Pre-Trained Model" + ], + "id": "7059e58ac6efff74" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-08-18T16:32:41.449942Z", + "start_time": "2024-08-18T16:32:39.757189Z" + } + }, + "cell_type": "code", + "source": [ + "# from torchvision.models import mobilenet_v2, MobileNet_V2_Weights\n", + "# from torchvision.models import regnet_x_400mf, RegNet_X_400MF_Weights\n", + "# from torchvision.models import mnasnet1_0, MNASNet1_0_Weights\n", + "# from torchvision.models import regnet_y_400mf, RegNet_Y_400MF_Weights\n", + "# from torchvision.models import shufflenet_v2_x1_0, ShuffleNet_V2_X1_0_Weights\n", + "# from torchvision.models import squeezenet1_1, SqueezeNet1_1_Weights\n", + "from torchvision.models import efficientnet_b0, EfficientNet_B0_Weights\n", + "\n", + "model = efficientnet_b0(weights=EfficientNet_B0_Weights)\n", + "model.eval()" + ], + "id": "ea84e114c819dde0", + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "/Vols/vol_design/tools/swat/users/yardeny/projects/model_optimization/mct_venv/lib/python3.10/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=EfficientNet_B0_Weights.IMAGENET1K_V1`. You can also use `weights=EfficientNet_B0_Weights.DEFAULT` to get the most up-to-date weights.\n", + " warnings.warn(msg)\n", + "Downloading: \"https://download.pytorch.org/models/efficientnet_b0_rwightman-7f5810bc.pth\" to /home/yardeny/.cache/torch/hub/checkpoints/efficientnet_b0_rwightman-7f5810bc.pth\n", + "100%|██████████| 20.5M/20.5M [00:00<00:00, 28.8MB/s]\n" + ] + }, + { + "data": { + "text/plain": [ + "EfficientNet(\n", + " (features): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n", + " (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): Sequential(\n", + " (0): MBConv(\n", + " (block): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=32, bias=False)\n", + " (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): SqueezeExcitation(\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (fc1): Conv2d(32, 8, kernel_size=(1, 1), stride=(1, 1))\n", + " (fc2): Conv2d(8, 32, kernel_size=(1, 1), stride=(1, 1))\n", + " (activation): SiLU(inplace=True)\n", + " (scale_activation): Sigmoid()\n", + " )\n", + " (2): Conv2dNormActivation(\n", + " (0): Conv2d(32, 16, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (stochastic_depth): StochasticDepth(p=0.0, mode=row)\n", + " )\n", + " )\n", + " (2): Sequential(\n", + " (0): MBConv(\n", + " (block): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(16, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): Conv2dNormActivation(\n", + " (0): Conv2d(96, 96, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=96, bias=False)\n", + " (1): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (2): SqueezeExcitation(\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (fc1): Conv2d(96, 4, kernel_size=(1, 1), stride=(1, 1))\n", + " (fc2): Conv2d(4, 96, kernel_size=(1, 1), stride=(1, 1))\n", + " (activation): SiLU(inplace=True)\n", + " (scale_activation): Sigmoid()\n", + " )\n", + " (3): Conv2dNormActivation(\n", + " (0): Conv2d(96, 24, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (stochastic_depth): StochasticDepth(p=0.0125, mode=row)\n", + " )\n", + " (1): MBConv(\n", + " (block): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(24, 144, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): Conv2dNormActivation(\n", + " (0): Conv2d(144, 144, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=144, bias=False)\n", + " (1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (2): SqueezeExcitation(\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (fc1): Conv2d(144, 6, kernel_size=(1, 1), stride=(1, 1))\n", + " (fc2): Conv2d(6, 144, kernel_size=(1, 1), stride=(1, 1))\n", + " (activation): SiLU(inplace=True)\n", + " (scale_activation): Sigmoid()\n", + " )\n", + " (3): Conv2dNormActivation(\n", + " (0): Conv2d(144, 24, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (stochastic_depth): StochasticDepth(p=0.025, mode=row)\n", + " )\n", + " )\n", + " (3): Sequential(\n", + " (0): MBConv(\n", + " (block): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(24, 144, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): Conv2dNormActivation(\n", + " (0): Conv2d(144, 144, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2), groups=144, bias=False)\n", + " (1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (2): SqueezeExcitation(\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (fc1): Conv2d(144, 6, kernel_size=(1, 1), stride=(1, 1))\n", + " (fc2): Conv2d(6, 144, kernel_size=(1, 1), stride=(1, 1))\n", + " (activation): SiLU(inplace=True)\n", + " (scale_activation): Sigmoid()\n", + " )\n", + " (3): Conv2dNormActivation(\n", + " (0): Conv2d(144, 40, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(40, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (stochastic_depth): StochasticDepth(p=0.037500000000000006, mode=row)\n", + " )\n", + " (1): MBConv(\n", + " (block): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(40, 240, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(240, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): Conv2dNormActivation(\n", + " (0): Conv2d(240, 240, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=240, bias=False)\n", + " (1): BatchNorm2d(240, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (2): SqueezeExcitation(\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (fc1): Conv2d(240, 10, kernel_size=(1, 1), stride=(1, 1))\n", + " (fc2): Conv2d(10, 240, kernel_size=(1, 1), stride=(1, 1))\n", + " (activation): SiLU(inplace=True)\n", + " (scale_activation): Sigmoid()\n", + " )\n", + " (3): Conv2dNormActivation(\n", + " (0): Conv2d(240, 40, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(40, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (stochastic_depth): StochasticDepth(p=0.05, mode=row)\n", + " )\n", + " )\n", + " (4): Sequential(\n", + " (0): MBConv(\n", + " (block): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(40, 240, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(240, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): Conv2dNormActivation(\n", + " (0): Conv2d(240, 240, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=240, bias=False)\n", + " (1): BatchNorm2d(240, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (2): SqueezeExcitation(\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (fc1): Conv2d(240, 10, kernel_size=(1, 1), stride=(1, 1))\n", + " (fc2): Conv2d(10, 240, kernel_size=(1, 1), stride=(1, 1))\n", + " (activation): SiLU(inplace=True)\n", + " (scale_activation): Sigmoid()\n", + " )\n", + " (3): Conv2dNormActivation(\n", + " (0): Conv2d(240, 80, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(80, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (stochastic_depth): StochasticDepth(p=0.0625, mode=row)\n", + " )\n", + " (1): MBConv(\n", + " (block): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(80, 480, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): Conv2dNormActivation(\n", + " (0): Conv2d(480, 480, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=480, bias=False)\n", + " (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (2): SqueezeExcitation(\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (fc1): Conv2d(480, 20, kernel_size=(1, 1), stride=(1, 1))\n", + " (fc2): Conv2d(20, 480, kernel_size=(1, 1), stride=(1, 1))\n", + " (activation): SiLU(inplace=True)\n", + " (scale_activation): Sigmoid()\n", + " )\n", + " (3): Conv2dNormActivation(\n", + " (0): Conv2d(480, 80, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(80, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (stochastic_depth): StochasticDepth(p=0.07500000000000001, mode=row)\n", + " )\n", + " (2): MBConv(\n", + " (block): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(80, 480, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): Conv2dNormActivation(\n", + " (0): Conv2d(480, 480, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=480, bias=False)\n", + " (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (2): SqueezeExcitation(\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (fc1): Conv2d(480, 20, kernel_size=(1, 1), stride=(1, 1))\n", + " (fc2): Conv2d(20, 480, kernel_size=(1, 1), stride=(1, 1))\n", + " (activation): SiLU(inplace=True)\n", + " (scale_activation): Sigmoid()\n", + " )\n", + " (3): Conv2dNormActivation(\n", + " (0): Conv2d(480, 80, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(80, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (stochastic_depth): StochasticDepth(p=0.08750000000000001, mode=row)\n", + " )\n", + " )\n", + " (5): Sequential(\n", + " (0): MBConv(\n", + " (block): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(80, 480, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): Conv2dNormActivation(\n", + " (0): Conv2d(480, 480, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=480, bias=False)\n", + " (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (2): SqueezeExcitation(\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (fc1): Conv2d(480, 20, kernel_size=(1, 1), stride=(1, 1))\n", + " (fc2): Conv2d(20, 480, kernel_size=(1, 1), stride=(1, 1))\n", + " (activation): SiLU(inplace=True)\n", + " (scale_activation): Sigmoid()\n", + " )\n", + " (3): Conv2dNormActivation(\n", + " (0): Conv2d(480, 112, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(112, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (stochastic_depth): StochasticDepth(p=0.1, mode=row)\n", + " )\n", + " (1): MBConv(\n", + " (block): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(112, 672, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): Conv2dNormActivation(\n", + " (0): Conv2d(672, 672, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=672, bias=False)\n", + " (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (2): SqueezeExcitation(\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (fc1): Conv2d(672, 28, kernel_size=(1, 1), stride=(1, 1))\n", + " (fc2): Conv2d(28, 672, kernel_size=(1, 1), stride=(1, 1))\n", + " (activation): SiLU(inplace=True)\n", + " (scale_activation): Sigmoid()\n", + " )\n", + " (3): Conv2dNormActivation(\n", + " (0): Conv2d(672, 112, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(112, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (stochastic_depth): StochasticDepth(p=0.1125, mode=row)\n", + " )\n", + " (2): MBConv(\n", + " (block): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(112, 672, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): Conv2dNormActivation(\n", + " (0): Conv2d(672, 672, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=672, bias=False)\n", + " (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (2): SqueezeExcitation(\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (fc1): Conv2d(672, 28, kernel_size=(1, 1), stride=(1, 1))\n", + " (fc2): Conv2d(28, 672, kernel_size=(1, 1), stride=(1, 1))\n", + " (activation): SiLU(inplace=True)\n", + " (scale_activation): Sigmoid()\n", + " )\n", + " (3): Conv2dNormActivation(\n", + " (0): Conv2d(672, 112, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(112, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (stochastic_depth): StochasticDepth(p=0.125, mode=row)\n", + " )\n", + " )\n", + " (6): Sequential(\n", + " (0): MBConv(\n", + " (block): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(112, 672, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): Conv2dNormActivation(\n", + " (0): Conv2d(672, 672, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2), groups=672, bias=False)\n", + " (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (2): SqueezeExcitation(\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (fc1): Conv2d(672, 28, kernel_size=(1, 1), stride=(1, 1))\n", + " (fc2): Conv2d(28, 672, kernel_size=(1, 1), stride=(1, 1))\n", + " (activation): SiLU(inplace=True)\n", + " (scale_activation): Sigmoid()\n", + " )\n", + " (3): Conv2dNormActivation(\n", + " (0): Conv2d(672, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (stochastic_depth): StochasticDepth(p=0.1375, mode=row)\n", + " )\n", + " (1): MBConv(\n", + " (block): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(192, 1152, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): Conv2dNormActivation(\n", + " (0): Conv2d(1152, 1152, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=1152, bias=False)\n", + " (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (2): SqueezeExcitation(\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (fc1): Conv2d(1152, 48, kernel_size=(1, 1), stride=(1, 1))\n", + " (fc2): Conv2d(48, 1152, kernel_size=(1, 1), stride=(1, 1))\n", + " (activation): SiLU(inplace=True)\n", + " (scale_activation): Sigmoid()\n", + " )\n", + " (3): Conv2dNormActivation(\n", + " (0): Conv2d(1152, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (stochastic_depth): StochasticDepth(p=0.15000000000000002, mode=row)\n", + " )\n", + " (2): MBConv(\n", + " (block): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(192, 1152, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): Conv2dNormActivation(\n", + " (0): Conv2d(1152, 1152, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=1152, bias=False)\n", + " (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (2): SqueezeExcitation(\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (fc1): Conv2d(1152, 48, kernel_size=(1, 1), stride=(1, 1))\n", + " (fc2): Conv2d(48, 1152, kernel_size=(1, 1), stride=(1, 1))\n", + " (activation): SiLU(inplace=True)\n", + " (scale_activation): Sigmoid()\n", + " )\n", + " (3): Conv2dNormActivation(\n", + " (0): Conv2d(1152, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (stochastic_depth): StochasticDepth(p=0.1625, mode=row)\n", + " )\n", + " (3): MBConv(\n", + " (block): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(192, 1152, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): Conv2dNormActivation(\n", + " (0): Conv2d(1152, 1152, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=1152, bias=False)\n", + " (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (2): SqueezeExcitation(\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (fc1): Conv2d(1152, 48, kernel_size=(1, 1), stride=(1, 1))\n", + " (fc2): Conv2d(48, 1152, kernel_size=(1, 1), stride=(1, 1))\n", + " (activation): SiLU(inplace=True)\n", + " (scale_activation): Sigmoid()\n", + " )\n", + " (3): Conv2dNormActivation(\n", + " (0): Conv2d(1152, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (stochastic_depth): StochasticDepth(p=0.17500000000000002, mode=row)\n", + " )\n", + " )\n", + " (7): Sequential(\n", + " (0): MBConv(\n", + " (block): Sequential(\n", + " (0): Conv2dNormActivation(\n", + " (0): Conv2d(192, 1152, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (1): Conv2dNormActivation(\n", + " (0): Conv2d(1152, 1152, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=1152, bias=False)\n", + " (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " (2): SqueezeExcitation(\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (fc1): Conv2d(1152, 48, kernel_size=(1, 1), stride=(1, 1))\n", + " (fc2): Conv2d(48, 1152, kernel_size=(1, 1), stride=(1, 1))\n", + " (activation): SiLU(inplace=True)\n", + " (scale_activation): Sigmoid()\n", + " )\n", + " (3): Conv2dNormActivation(\n", + " (0): Conv2d(1152, 320, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(320, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " )\n", + " )\n", + " (stochastic_depth): StochasticDepth(p=0.1875, mode=row)\n", + " )\n", + " )\n", + " (8): Conv2dNormActivation(\n", + " (0): Conv2d(320, 1280, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", + " (1): BatchNorm2d(1280, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", + " (2): SiLU(inplace=True)\n", + " )\n", + " )\n", + " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", + " (classifier): Sequential(\n", + " (0): Dropout(p=0.2, inplace=True)\n", + " (1): Linear(in_features=1280, out_features=1000, bias=True)\n", + " )\n", + ")" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "execution_count": 43 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "### Post training quantization using Model Compression Toolkit \n", + "\n", + "Now, we're all set to use MCT's post-training quantization. To begin, we'll define a representative dataset and proceed with the model quantization. Please note that, for demonstration purposes, we'll use the evaluation dataset as our representative dataset. We'll calibrate the model using 80 representative images, divided into 20 iterations of 'batch_size' images each. " + ], + "id": "7d334e3dd747ba68" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-08-18T16:33:32.948099Z", + "start_time": "2024-08-18T16:32:48.044728Z" + } + }, + "cell_type": "code", + "source": [ + "import model_compression_toolkit as mct\n", + "from model_compression_toolkit.core.pytorch.pytorch_device_config import get_working_device\n", + "from typing import Iterator, Tuple, List\n", + "import torchvision\n", + "from torch.utils.data import DataLoader\n", + "from torchvision import transforms, datasets\n", + "\n", + "\n", + "BATCH_SIZE = 4\n", + "n_iters = 20\n", + "device = get_working_device()\n", + "\n", + "# Define transformations for the validation set\n", + "val_transform = transforms.Compose([\n", + " transforms.Resize(256),\n", + " transforms.CenterCrop(224),\n", + " transforms.ToTensor(),\n", + " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])\n", + "\n", + "# Extract ImageNet validation dataset using torchvision \"datasets\" module\n", + "val_dataset = torchvision.datasets.ImageNet(root='./imagenet', split='val', transform=val_transform)\n", + "val_loader = DataLoader(val_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4)\n", + "\n", + "# Define representative dataset generator\n", + "def get_representative_dataset(n_iter: int, dataset_loader: Iterator[Tuple]):\n", + " \"\"\"\n", + " This function creates a representative dataset generator. The generator yields numpy\n", + " arrays of batches of shape: [Batch, H, W ,C].\n", + " Args:\n", + " n_iter: number of iterations for MCT to calibrate on\n", + " Returns:\n", + " A representative dataset generator\n", + " \"\"\" \n", + " def representative_dataset() -> Iterator[List]:\n", + " ds_iter = iter(dataset_loader)\n", + " for _ in range(n_iter):\n", + " yield [next(ds_iter)[0]]\n", + "\n", + " return representative_dataset\n", + "\n", + "# Get representative dataset generator\n", + "representative_dataset_gen = get_representative_dataset(n_iter=n_iters, dataset_loader=val_loader)\n", + "\n", + "# Perform post training quantization with the default configuration\n", + "quant_model, _ = mct.ptq.pytorch_post_training_quantization(model, representative_dataset_gen)\n", + "print('Quantized model is ready')" + ], + "id": "c231499204b5fe58", + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Statistics Collection: 20it [00:15, 1.30it/s]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "Running quantization parameters search. This process might take some time, depending on the model size and the selected quantization methods.\n", + "\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Calculating quantization parameters: 100%|██████████| 201/201 [00:26<00:00, 7.52it/s]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Weights_memory: 5236192.0, Activation_memory: 2408448.0, Total_memory: 7644640.0, BOPS: 7804365562445824\n", + "\n", + "Please run your accuracy evaluation on the exported quantized model to verify it's accuracy.\n", + "Checkout the FAQ and Troubleshooting pages for resolving common issues and improving the quantized model accuracy:\n", + "FAQ: https://github.com/sony/model_optimization/tree/main/FAQ.md\n", + "Quantization Troubleshooting: https://github.com/sony/model_optimization/tree/main/quantization_troubleshooting.md\n", + "Quantized model is ready\n" + ] + } + ], + "execution_count": 44 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "### Model Export\n", + "\n", + "Now, we can export the quantized model, ready for deployment, into a `.onnx` format file. Please ensure that the `save_model_path` has been set correctly. " + ], + "id": "2659cdc8ec1a3008" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-08-18T16:34:07.006173Z", + "start_time": "2024-08-18T16:34:04.885684Z" + } + }, + "cell_type": "code", + "source": [ + "mct.exporter.pytorch_export_model(model=quant_model,\n", + " save_model_path='./torchvision_qmodel.onnx',\n", + " repr_dataset=representative_dataset_gen,\n", + " onnx_opset_version=17)" + ], + "id": "de8dd4dbc0c8d393", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Exporting onnx model with MCTQ quantizers: ./torchvision_qmodel.onnx\n" + ] + } + ], + "execution_count": 47 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "## Evaluation on ImageNet dataset\n", + "\n", + "### Floating point model evaluation\n", + "Please ensure that the dataset path has been set correctly before running this code cell." + ], + "id": "d98c5b8acb2e7397" + }, + { + "metadata": { + "ExecuteTime": { + "end_time": "2024-08-18T16:36:30.262328Z", + "start_time": "2024-08-18T16:34:11.606371Z" + } + }, + "cell_type": "code", + "source": [ + "from tutorials.resources.utils.pytorch_tutorial_tools import classification_eval\n", + "print(f\"Model name: {model.__class__.__name__}\")\n", + "# Evaluate the model on ImageNet\n", + "eval_results = classification_eval(model, val_loader)\n", + "print(f\"Model name: {model.__class__.__name__}\")\n", + "# Print float model Accuracy results\n", + "print(\"Float model Accuracy: {:.4f}\".format(round(100 * eval_results[0], 2)))" + ], + "id": "6cd077108ef55889", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Model name: EfficientNet\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 2%|▏ | 259/12500 [00:03<02:23, 85.51it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 1000, Accuracy: 90.3 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 4%|▍ | 517/12500 [00:06<02:10, 91.56it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 2000, Accuracy: 85.7 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 6%|▌ | 759/12500 [00:08<02:09, 91.00it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 3000, Accuracy: 83.7 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 8%|▊ | 1014/12500 [00:11<02:11, 87.39it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 4000, Accuracy: 81.47 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 10%|█ | 1268/12500 [00:14<01:57, 95.52it/s] " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 5000, Accuracy: 83.42 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 12%|█▏ | 1519/12500 [00:17<01:57, 93.70it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 6000, Accuracy: 83.65 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 14%|█▍ | 1759/12500 [00:19<02:05, 85.79it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 7000, Accuracy: 84.31 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 16%|█▌ | 2008/12500 [00:22<01:50, 95.21it/s] " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 8000, Accuracy: 84.65 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 18%|█▊ | 2267/12500 [00:25<01:47, 95.46it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 9000, Accuracy: 83.86 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 20%|██ | 2512/12500 [00:28<01:50, 90.19it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 10000, Accuracy: 83.42 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 22%|██▏ | 2758/12500 [00:30<01:44, 92.90it/s] " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 11000, Accuracy: 83.56 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 24%|██▍ | 3016/12500 [00:33<01:33, 101.15it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 12000, Accuracy: 83.35 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 26%|██▌ | 3261/12500 [00:36<01:46, 86.67it/s] " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 13000, Accuracy: 83.22 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 28%|██▊ | 3514/12500 [00:38<01:45, 85.20it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 14000, Accuracy: 83.11 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 30%|███ | 3765/12500 [00:41<01:44, 83.32it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 15000, Accuracy: 83.01 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 32%|███▏ | 4010/12500 [00:44<01:27, 96.84it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 16000, Accuracy: 82.88 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 34%|███▍ | 4266/12500 [00:47<01:32, 89.26it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 17000, Accuracy: 83.34 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 36%|███▌ | 4511/12500 [00:50<01:29, 89.45it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 18000, Accuracy: 83.16 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 38%|███▊ | 4764/12500 [00:52<01:21, 94.59it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 19000, Accuracy: 83.26 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 40%|████ | 5012/12500 [00:55<01:18, 95.63it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 20000, Accuracy: 83.08 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 42%|████▏ | 5265/12500 [00:58<01:21, 88.90it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 21000, Accuracy: 82.52 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 44%|████▍ | 5513/12500 [01:01<01:14, 93.34it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 22000, Accuracy: 82.12 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 46%|████▌ | 5766/12500 [01:03<01:12, 92.72it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 23000, Accuracy: 81.82 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 48%|████▊ | 6014/12500 [01:06<01:19, 81.58it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 24000, Accuracy: 81.38 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 50%|█████ | 6258/12500 [01:09<01:13, 84.65it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 25000, Accuracy: 80.77 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 52%|█████▏ | 6512/12500 [01:12<01:12, 82.64it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 26000, Accuracy: 80.49 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 54%|█████▍ | 6758/12500 [01:15<01:07, 85.59it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 27000, Accuracy: 80.2 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 56%|█████▌ | 7012/12500 [01:18<00:59, 92.22it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 28000, Accuracy: 80.08 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 58%|█████▊ | 7262/12500 [01:20<00:56, 92.30it/s] " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 29000, Accuracy: 80.18 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 60%|██████ | 7513/12500 [01:23<00:52, 95.25it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 30000, Accuracy: 79.96 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 62%|██████▏ | 7759/12500 [01:26<00:50, 94.21it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 31000, Accuracy: 79.91 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 64%|██████▍ | 8012/12500 [01:29<00:53, 83.89it/s] " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 32000, Accuracy: 79.4 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 66%|██████▌ | 8260/12500 [01:31<00:44, 96.04it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 33000, Accuracy: 79.21 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 68%|██████▊ | 8511/12500 [01:34<00:43, 90.82it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 34000, Accuracy: 79.08 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 70%|███████ | 8759/12500 [01:37<00:43, 85.54it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 35000, Accuracy: 78.97 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 72%|███████▏ | 9019/12500 [01:40<00:36, 94.31it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 36000, Accuracy: 78.93 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 74%|███████▍ | 9261/12500 [01:42<00:37, 86.96it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 37000, Accuracy: 78.88 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 76%|███████▌ | 9509/12500 [01:45<00:31, 94.80it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 38000, Accuracy: 78.68 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 78%|███████▊ | 9762/12500 [01:48<00:30, 88.44it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 39000, Accuracy: 78.58 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 80%|████████ | 10020/12500 [01:51<00:25, 95.97it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 40000, Accuracy: 78.43 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 82%|████████▏ | 10268/12500 [01:54<00:24, 89.86it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 41000, Accuracy: 78.31 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 84%|████████▍ | 10512/12500 [01:56<00:21, 90.91it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 42000, Accuracy: 78.08 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 86%|████████▌ | 10767/12500 [01:59<00:17, 97.33it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 43000, Accuracy: 77.94 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 88%|████████▊ | 11014/12500 [02:02<00:16, 91.97it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 44000, Accuracy: 77.87 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 90%|█████████ | 11258/12500 [02:04<00:13, 95.50it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 45000, Accuracy: 77.74 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 92%|█████████▏| 11509/12500 [02:07<00:10, 92.71it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 46000, Accuracy: 77.62 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 94%|█████████▍| 11759/12500 [02:10<00:08, 91.53it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 47000, Accuracy: 77.64 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 96%|█████████▌| 12015/12500 [02:13<00:05, 94.17it/s] " + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 48000, Accuracy: 77.77 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 98%|█████████▊| 12266/12500 [02:15<00:02, 82.37it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 49000, Accuracy: 77.52 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 100%|██████████| 12500/12500 [02:18<00:00, 90.18it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 50000, Accuracy: 77.67 %\n", + "Model name: EfficientNet\n", + "Float model Accuracy: 77.6700\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "execution_count": 48 + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "### Quantized model evaluation\n", + "We can evaluate the performance of the quantized model. There is a slight decrease in performance that can be further mitigated by either expanding the representative dataset or employing MCT's advanced quantization methods, such as GPTQ (Gradient-Based/Enhanced Post Training Quantization)." + ], + "id": "eb86aae997a5210a" + }, + { + "metadata": { + "jupyter": { + "is_executing": true + }, + "ExecuteTime": { + "start_time": "2024-08-18T16:47:11.755896Z" + } + }, + "cell_type": "code", + "source": [ + "print(f\"Model name: {model.__class__.__name__}\")\n", + "# Evaluate the quantized model on ImageNet\n", + "eval_results = classification_eval(quant_model, val_loader)\n", + "# Print quantized model Accuracy results\n", + "print(\"Quantized model Accuracy: {:.4f}\".format(round(100 * eval_results[0], 2)))" + ], + "id": "b881138870761d89", + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Model name: EfficientNet\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 2%|▏ | 211/12500 [00:08<07:35, 26.98it/s]" + ] + } + ], + "execution_count": null + }, + { + "metadata": {}, + "cell_type": "markdown", + "source": [ + "\\\n", + "Copyright 2024 Sony Semiconductor Israel, Inc. All rights reserved.\n", + "\n", + "Licensed under the Apache License, Version 2.0 (the \"License\");\n", + "you may not use this file except in compliance with the License.\n", + "You may obtain a copy of the License at\n", + "\n", + " http://www.apache.org/licenses/LICENSE-2.0\n", + "\n", + "Unless required by applicable law or agreed to in writing, software\n", + "distributed under the License is distributed on an \"AS IS\" BASIS,\n", + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", + "See the License for the specific language governing permissions and\n", + "limitations under the License" + ], + "id": "a4d344795b87475a" + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 2 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython2", + "version": "2.7.6" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} From 2d528f6b3be31ee2cd58c6b41948cafcd8b05a49 Mon Sep 17 00:00:00 2001 From: yardeny-sony Date: Sun, 18 Aug 2024 20:27:41 +0300 Subject: [PATCH 2/5] code cleanup --- ...orchvision_classification_for_imx500.ipynb | 731 +++++++++++++++++- 1 file changed, 710 insertions(+), 21 deletions(-) diff --git a/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb b/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb index 5c5cefe1a..68b6285a7 100644 --- a/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb +++ b/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb @@ -52,8 +52,7 @@ "import importlib\n", "\n", "if not importlib.util.find_spec('model_compression_toolkit'):\n", - " # !pip install model_compression_toolkit #TODO: delete nightly\n", - " !pip install mct-nightly\n", + " !pip install model_compression_toolkit\n", "!git clone https://github.com/sony/model_optimization.git temp_mct && mv temp_mct/tutorials . && \\rm -rf temp_mct\n", "sys.path.insert(0,\"tutorials\")" ], @@ -80,7 +79,6 @@ "source": [ "import os\n", "if not os.path.isdir('imagenet'):\n", - " print(\"downloading\")\n", " !mkdir imagenet\n", " !wget https://image-net.org/data/ILSVRC/2012/ILSVRC2012_devkit_t12.tar.gz\n", " !mv ILSVRC2012_devkit_t12.tar.gz imagenet/\n", @@ -108,15 +106,9 @@ }, "cell_type": "code", "source": [ - "# from torchvision.models import mobilenet_v2, MobileNet_V2_Weights\n", - "# from torchvision.models import regnet_x_400mf, RegNet_X_400MF_Weights\n", - "# from torchvision.models import mnasnet1_0, MNASNet1_0_Weights\n", - "# from torchvision.models import regnet_y_400mf, RegNet_Y_400MF_Weights\n", - "# from torchvision.models import shufflenet_v2_x1_0, ShuffleNet_V2_X1_0_Weights\n", - "# from torchvision.models import squeezenet1_1, SqueezeNet1_1_Weights\n", - "from torchvision.models import efficientnet_b0, EfficientNet_B0_Weights\n", + "from torchvision.models import mobilenet_v2, MobileNet_V2_Weights\n", "\n", - "model = efficientnet_b0(weights=EfficientNet_B0_Weights)\n", + "model = mobilenet_v2(weights=MobileNet_V2_Weights)\n", "model.eval()" ], "id": "ea84e114c819dde0", @@ -609,7 +601,6 @@ "import model_compression_toolkit as mct\n", "from model_compression_toolkit.core.pytorch.pytorch_device_config import get_working_device\n", "from typing import Iterator, Tuple, List\n", - "import torchvision\n", "from torch.utils.data import DataLoader\n", "from torchvision import transforms, datasets\n", "\n", @@ -626,7 +617,7 @@ " transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])\n", "\n", "# Extract ImageNet validation dataset using torchvision \"datasets\" module\n", - "val_dataset = torchvision.datasets.ImageNet(root='./imagenet', split='val', transform=val_transform)\n", + "val_dataset = datasets.ImageNet(root='./imagenet', split='val', transform=val_transform)\n", "val_loader = DataLoader(val_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=4)\n", "\n", "# Define representative dataset generator\n", @@ -751,10 +742,9 @@ "cell_type": "code", "source": [ "from tutorials.resources.utils.pytorch_tutorial_tools import classification_eval\n", - "print(f\"Model name: {model.__class__.__name__}\")\n", "# Evaluate the model on ImageNet\n", "eval_results = classification_eval(model, val_loader)\n", - "print(f\"Model name: {model.__class__.__name__}\")\n", + "\n", "# Print float model Accuracy results\n", "print(\"Float model Accuracy: {:.4f}\".format(round(100 * eval_results[0], 2)))" ], @@ -1490,18 +1480,16 @@ }, { "metadata": { - "jupyter": { - "is_executing": true - }, "ExecuteTime": { + "end_time": "2024-08-18T16:55:09.690679Z", "start_time": "2024-08-18T16:47:11.755896Z" } }, "cell_type": "code", "source": [ - "print(f\"Model name: {model.__class__.__name__}\")\n", "# Evaluate the quantized model on ImageNet\n", "eval_results = classification_eval(quant_model, val_loader)\n", + "\n", "# Print quantized model Accuracy results\n", "print(\"Quantized model Accuracy: {:.4f}\".format(round(100 * eval_results[0], 2)))" ], @@ -1518,11 +1506,712 @@ "name": "stderr", "output_type": "stream", "text": [ - "Classification evaluation: 2%|▏ | 211/12500 [00:08<07:35, 26.98it/s]" + "Classification evaluation: 2%|▏ | 253/12500 [00:10<08:54, 22.92it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 1000, Accuracy: 87.3 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 4%|▍ | 502/12500 [00:20<08:46, 22.78it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 2000, Accuracy: 83.2 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 6%|▌ | 754/12500 [00:30<07:33, 25.90it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 3000, Accuracy: 80.03 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 8%|▊ | 1003/12500 [00:40<07:18, 26.21it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 4000, Accuracy: 77.65 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 10%|▉ | 1249/12500 [00:50<06:48, 27.54it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 5000, Accuracy: 80.04 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 12%|█▏ | 1504/12500 [01:00<06:39, 27.50it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 6000, Accuracy: 80.0 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 14%|█▍ | 1753/12500 [01:10<06:52, 26.05it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 7000, Accuracy: 80.7 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 16%|█▌ | 2005/12500 [01:20<06:24, 27.32it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 8000, Accuracy: 81.12 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 18%|█▊ | 2255/12500 [01:29<06:55, 24.63it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 9000, Accuracy: 80.29 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 20%|██ | 2504/12500 [01:39<06:27, 25.79it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 10000, Accuracy: 79.56 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 22%|██▏ | 2753/12500 [01:48<06:49, 23.78it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 11000, Accuracy: 79.59 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 24%|██▍ | 3005/12500 [01:58<06:05, 25.97it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 12000, Accuracy: 79.19 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 26%|██▌ | 3254/12500 [02:08<06:31, 23.60it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 13000, Accuracy: 79.12 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 28%|██▊ | 3503/12500 [02:18<06:01, 24.89it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 14000, Accuracy: 78.8 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 30%|███ | 3755/12500 [02:28<05:33, 26.20it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 15000, Accuracy: 78.75 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 32%|███▏ | 4004/12500 [02:37<05:09, 27.42it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 16000, Accuracy: 78.73 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 34%|███▍ | 4253/12500 [02:47<05:27, 25.20it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 17000, Accuracy: 79.29 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 36%|███▌ | 4505/12500 [02:57<05:22, 24.76it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 18000, Accuracy: 79.16 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 38%|███▊ | 4754/12500 [03:06<04:53, 26.41it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 19000, Accuracy: 79.35 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 40%|████ | 5003/12500 [03:16<04:34, 27.34it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 20000, Accuracy: 79.21 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 42%|████▏ | 5255/12500 [03:26<04:21, 27.71it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 21000, Accuracy: 78.66 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 44%|████▍ | 5504/12500 [03:35<04:41, 24.88it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 22000, Accuracy: 78.25 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 46%|████▌ | 5753/12500 [03:45<04:10, 26.98it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 23000, Accuracy: 77.94 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 48%|████▊ | 6004/12500 [03:55<04:01, 26.84it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 24000, Accuracy: 77.56 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 50%|█████ | 6253/12500 [04:04<03:44, 27.85it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 25000, Accuracy: 76.9 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 52%|█████▏ | 6505/12500 [04:14<03:56, 25.30it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 26000, Accuracy: 76.54 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 54%|█████▍ | 6754/12500 [04:23<03:51, 24.78it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 27000, Accuracy: 76.27 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 56%|█████▌ | 7003/12500 [04:33<03:43, 24.54it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 28000, Accuracy: 76.13 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 58%|█████▊ | 7252/12500 [04:43<03:14, 27.04it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 29000, Accuracy: 76.32 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 60%|██████ | 7504/12500 [04:53<03:09, 26.34it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 30000, Accuracy: 76.13 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 62%|██████▏ | 7753/12500 [05:02<03:14, 24.41it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 31000, Accuracy: 76.09 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 64%|██████▍ | 8003/12500 [05:12<02:34, 29.08it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 32000, Accuracy: 75.57 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 66%|██████▌ | 8255/12500 [05:21<02:28, 28.54it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 33000, Accuracy: 75.42 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 68%|██████▊ | 8503/12500 [05:30<02:19, 28.61it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 34000, Accuracy: 75.22 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 70%|███████ | 8753/12500 [05:39<02:05, 29.80it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 35000, Accuracy: 75.11 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 72%|███████▏ | 9004/12500 [05:48<01:58, 29.43it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 36000, Accuracy: 75.03 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 74%|███████▍ | 9254/12500 [05:57<01:56, 27.84it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 37000, Accuracy: 74.9 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 76%|███████▌ | 9505/12500 [06:06<01:49, 27.45it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 38000, Accuracy: 74.68 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 78%|███████▊ | 9754/12500 [06:15<01:47, 25.58it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 39000, Accuracy: 74.56 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 80%|████████ | 10004/12500 [06:25<01:28, 28.31it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 40000, Accuracy: 74.41 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 82%|████████▏ | 10253/12500 [06:34<01:18, 28.47it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 41000, Accuracy: 74.32 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 84%|████████▍ | 10503/12500 [06:43<01:12, 27.64it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 42000, Accuracy: 74.1 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 86%|████████▌ | 10754/12500 [06:53<01:13, 23.81it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 43000, Accuracy: 73.94 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 88%|████████▊ | 11005/12500 [07:02<00:57, 25.99it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 44000, Accuracy: 73.9 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 90%|█████████ | 11255/12500 [07:12<00:46, 26.69it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 45000, Accuracy: 73.76 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 92%|█████████▏| 11505/12500 [07:21<00:35, 28.21it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 46000, Accuracy: 73.67 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 94%|█████████▍| 11755/12500 [07:30<00:27, 27.12it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 47000, Accuracy: 73.65 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 96%|█████████▌| 12003/12500 [07:39<00:18, 26.94it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 48000, Accuracy: 73.71 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 98%|█████████▊| 12253/12500 [07:48<00:08, 27.94it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 49000, Accuracy: 73.47 %\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "Classification evaluation: 100%|██████████| 12500/12500 [07:57<00:00, 26.15it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Num of images: 50000, Accuracy: 73.64 %\n", + "Quantized model Accuracy: 73.6400\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\n" ] } ], - "execution_count": null + "execution_count": 50 }, { "metadata": {}, From ddf9f434806d42a24d0de7a747d297075376bd87 Mon Sep 17 00:00:00 2001 From: yardeny-sony Date: Mon, 19 Aug 2024 14:37:06 +0300 Subject: [PATCH 3/5] updates according to PR --- .../notebooks/imx500_notebooks/README.md | 2 +- ...orchvision_classification_for_imx500.ipynb | 2025 +---------------- 2 files changed, 32 insertions(+), 1995 deletions(-) diff --git a/tutorials/notebooks/imx500_notebooks/README.md b/tutorials/notebooks/imx500_notebooks/README.md index f5f448faa..b18f354ea 100644 --- a/tutorials/notebooks/imx500_notebooks/README.md +++ b/tutorials/notebooks/imx500_notebooks/README.md @@ -98,7 +98,7 @@ deployment performance. shufflenet_v2_x1_5 PyTorch - torchvision + torchvision ImageNet 69.34 diff --git a/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb b/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb index 68b6285a7..4789cbdbb 100644 --- a/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb +++ b/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb @@ -27,14 +27,14 @@ { "metadata": {}, "cell_type": "code", - "outputs": [], - "execution_count": null, "source": [ "!pip install -q torch\n", "!pip install -q torchvision\n", "!pip install -q onnx" ], - "id": "5250b07975f15b5f" + "id": "5250b07975f15b5f", + "outputs": [], + "execution_count": null }, { "metadata": {}, @@ -45,8 +45,6 @@ { "metadata": {}, "cell_type": "code", - "outputs": [], - "execution_count": null, "source": [ "import sys\n", "import importlib\n", @@ -56,7 +54,9 @@ "!git clone https://github.com/sony/model_optimization.git temp_mct && mv temp_mct/tutorials . && \\rm -rf temp_mct\n", "sys.path.insert(0,\"tutorials\")" ], - "id": "391a9ed0d178002e" + "id": "391a9ed0d178002e", + "outputs": [], + "execution_count": null }, { "metadata": {}, @@ -74,8 +74,6 @@ { "metadata": {}, "cell_type": "code", - "outputs": [], - "execution_count": null, "source": [ "import os\n", "if not os.path.isdir('imagenet'):\n", @@ -85,7 +83,9 @@ " !wget https://image-net.org/data/ILSVRC/2012/ILSVRC2012_img_val.tar\n", " !mv ILSVRC2012_img_val.tar imagenet/" ], - "id": "ec301c30cd83d535" + "id": "ec301c30cd83d535", + "outputs": [], + "execution_count": null }, { "metadata": {}, @@ -98,486 +98,18 @@ "id": "7059e58ac6efff74" }, { - "metadata": { - "ExecuteTime": { - "end_time": "2024-08-18T16:32:41.449942Z", - "start_time": "2024-08-18T16:32:39.757189Z" - } - }, + "metadata": {}, "cell_type": "code", "source": [ - "from torchvision.models import mobilenet_v2, MobileNet_V2_Weights\n", + "# from torchvision.models import mobilenet_v2, MobileNet_V2_Weights\n", + "from torchvision.models import efficientnet_b0, EfficientNet_B0_Weights\n", "\n", - "model = mobilenet_v2(weights=MobileNet_V2_Weights)\n", + "model = efficientnet_b0(weights=EfficientNet_B0_Weights)\n", "model.eval()" ], "id": "ea84e114c819dde0", - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "/Vols/vol_design/tools/swat/users/yardeny/projects/model_optimization/mct_venv/lib/python3.10/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=EfficientNet_B0_Weights.IMAGENET1K_V1`. You can also use `weights=EfficientNet_B0_Weights.DEFAULT` to get the most up-to-date weights.\n", - " warnings.warn(msg)\n", - "Downloading: \"https://download.pytorch.org/models/efficientnet_b0_rwightman-7f5810bc.pth\" to /home/yardeny/.cache/torch/hub/checkpoints/efficientnet_b0_rwightman-7f5810bc.pth\n", - "100%|██████████| 20.5M/20.5M [00:00<00:00, 28.8MB/s]\n" - ] - }, - { - "data": { - "text/plain": [ - "EfficientNet(\n", - " (features): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(3, 32, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), bias=False)\n", - " (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): Sequential(\n", - " (0): MBConv(\n", - " (block): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=32, bias=False)\n", - " (1): BatchNorm2d(32, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): SqueezeExcitation(\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (fc1): Conv2d(32, 8, kernel_size=(1, 1), stride=(1, 1))\n", - " (fc2): Conv2d(8, 32, kernel_size=(1, 1), stride=(1, 1))\n", - " (activation): SiLU(inplace=True)\n", - " (scale_activation): Sigmoid()\n", - " )\n", - " (2): Conv2dNormActivation(\n", - " (0): Conv2d(32, 16, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(16, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " )\n", - " )\n", - " (stochastic_depth): StochasticDepth(p=0.0, mode=row)\n", - " )\n", - " )\n", - " (2): Sequential(\n", - " (0): MBConv(\n", - " (block): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(16, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): Conv2dNormActivation(\n", - " (0): Conv2d(96, 96, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=96, bias=False)\n", - " (1): BatchNorm2d(96, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (2): SqueezeExcitation(\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (fc1): Conv2d(96, 4, kernel_size=(1, 1), stride=(1, 1))\n", - " (fc2): Conv2d(4, 96, kernel_size=(1, 1), stride=(1, 1))\n", - " (activation): SiLU(inplace=True)\n", - " (scale_activation): Sigmoid()\n", - " )\n", - " (3): Conv2dNormActivation(\n", - " (0): Conv2d(96, 24, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " )\n", - " )\n", - " (stochastic_depth): StochasticDepth(p=0.0125, mode=row)\n", - " )\n", - " (1): MBConv(\n", - " (block): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(24, 144, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): Conv2dNormActivation(\n", - " (0): Conv2d(144, 144, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=144, bias=False)\n", - " (1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (2): SqueezeExcitation(\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (fc1): Conv2d(144, 6, kernel_size=(1, 1), stride=(1, 1))\n", - " (fc2): Conv2d(6, 144, kernel_size=(1, 1), stride=(1, 1))\n", - " (activation): SiLU(inplace=True)\n", - " (scale_activation): Sigmoid()\n", - " )\n", - " (3): Conv2dNormActivation(\n", - " (0): Conv2d(144, 24, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(24, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " )\n", - " )\n", - " (stochastic_depth): StochasticDepth(p=0.025, mode=row)\n", - " )\n", - " )\n", - " (3): Sequential(\n", - " (0): MBConv(\n", - " (block): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(24, 144, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): Conv2dNormActivation(\n", - " (0): Conv2d(144, 144, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2), groups=144, bias=False)\n", - " (1): BatchNorm2d(144, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (2): SqueezeExcitation(\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (fc1): Conv2d(144, 6, kernel_size=(1, 1), stride=(1, 1))\n", - " (fc2): Conv2d(6, 144, kernel_size=(1, 1), stride=(1, 1))\n", - " (activation): SiLU(inplace=True)\n", - " (scale_activation): Sigmoid()\n", - " )\n", - " (3): Conv2dNormActivation(\n", - " (0): Conv2d(144, 40, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(40, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " )\n", - " )\n", - " (stochastic_depth): StochasticDepth(p=0.037500000000000006, mode=row)\n", - " )\n", - " (1): MBConv(\n", - " (block): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(40, 240, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(240, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): Conv2dNormActivation(\n", - " (0): Conv2d(240, 240, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=240, bias=False)\n", - " (1): BatchNorm2d(240, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (2): SqueezeExcitation(\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (fc1): Conv2d(240, 10, kernel_size=(1, 1), stride=(1, 1))\n", - " (fc2): Conv2d(10, 240, kernel_size=(1, 1), stride=(1, 1))\n", - " (activation): SiLU(inplace=True)\n", - " (scale_activation): Sigmoid()\n", - " )\n", - " (3): Conv2dNormActivation(\n", - " (0): Conv2d(240, 40, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(40, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " )\n", - " )\n", - " (stochastic_depth): StochasticDepth(p=0.05, mode=row)\n", - " )\n", - " )\n", - " (4): Sequential(\n", - " (0): MBConv(\n", - " (block): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(40, 240, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(240, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): Conv2dNormActivation(\n", - " (0): Conv2d(240, 240, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1), groups=240, bias=False)\n", - " (1): BatchNorm2d(240, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (2): SqueezeExcitation(\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (fc1): Conv2d(240, 10, kernel_size=(1, 1), stride=(1, 1))\n", - " (fc2): Conv2d(10, 240, kernel_size=(1, 1), stride=(1, 1))\n", - " (activation): SiLU(inplace=True)\n", - " (scale_activation): Sigmoid()\n", - " )\n", - " (3): Conv2dNormActivation(\n", - " (0): Conv2d(240, 80, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(80, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " )\n", - " )\n", - " (stochastic_depth): StochasticDepth(p=0.0625, mode=row)\n", - " )\n", - " (1): MBConv(\n", - " (block): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(80, 480, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): Conv2dNormActivation(\n", - " (0): Conv2d(480, 480, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=480, bias=False)\n", - " (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (2): SqueezeExcitation(\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (fc1): Conv2d(480, 20, kernel_size=(1, 1), stride=(1, 1))\n", - " (fc2): Conv2d(20, 480, kernel_size=(1, 1), stride=(1, 1))\n", - " (activation): SiLU(inplace=True)\n", - " (scale_activation): Sigmoid()\n", - " )\n", - " (3): Conv2dNormActivation(\n", - " (0): Conv2d(480, 80, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(80, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " )\n", - " )\n", - " (stochastic_depth): StochasticDepth(p=0.07500000000000001, mode=row)\n", - " )\n", - " (2): MBConv(\n", - " (block): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(80, 480, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): Conv2dNormActivation(\n", - " (0): Conv2d(480, 480, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=480, bias=False)\n", - " (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (2): SqueezeExcitation(\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (fc1): Conv2d(480, 20, kernel_size=(1, 1), stride=(1, 1))\n", - " (fc2): Conv2d(20, 480, kernel_size=(1, 1), stride=(1, 1))\n", - " (activation): SiLU(inplace=True)\n", - " (scale_activation): Sigmoid()\n", - " )\n", - " (3): Conv2dNormActivation(\n", - " (0): Conv2d(480, 80, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(80, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " )\n", - " )\n", - " (stochastic_depth): StochasticDepth(p=0.08750000000000001, mode=row)\n", - " )\n", - " )\n", - " (5): Sequential(\n", - " (0): MBConv(\n", - " (block): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(80, 480, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): Conv2dNormActivation(\n", - " (0): Conv2d(480, 480, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=480, bias=False)\n", - " (1): BatchNorm2d(480, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (2): SqueezeExcitation(\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (fc1): Conv2d(480, 20, kernel_size=(1, 1), stride=(1, 1))\n", - " (fc2): Conv2d(20, 480, kernel_size=(1, 1), stride=(1, 1))\n", - " (activation): SiLU(inplace=True)\n", - " (scale_activation): Sigmoid()\n", - " )\n", - " (3): Conv2dNormActivation(\n", - " (0): Conv2d(480, 112, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(112, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " )\n", - " )\n", - " (stochastic_depth): StochasticDepth(p=0.1, mode=row)\n", - " )\n", - " (1): MBConv(\n", - " (block): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(112, 672, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): Conv2dNormActivation(\n", - " (0): Conv2d(672, 672, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=672, bias=False)\n", - " (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (2): SqueezeExcitation(\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (fc1): Conv2d(672, 28, kernel_size=(1, 1), stride=(1, 1))\n", - " (fc2): Conv2d(28, 672, kernel_size=(1, 1), stride=(1, 1))\n", - " (activation): SiLU(inplace=True)\n", - " (scale_activation): Sigmoid()\n", - " )\n", - " (3): Conv2dNormActivation(\n", - " (0): Conv2d(672, 112, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(112, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " )\n", - " )\n", - " (stochastic_depth): StochasticDepth(p=0.1125, mode=row)\n", - " )\n", - " (2): MBConv(\n", - " (block): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(112, 672, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): Conv2dNormActivation(\n", - " (0): Conv2d(672, 672, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=672, bias=False)\n", - " (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (2): SqueezeExcitation(\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (fc1): Conv2d(672, 28, kernel_size=(1, 1), stride=(1, 1))\n", - " (fc2): Conv2d(28, 672, kernel_size=(1, 1), stride=(1, 1))\n", - " (activation): SiLU(inplace=True)\n", - " (scale_activation): Sigmoid()\n", - " )\n", - " (3): Conv2dNormActivation(\n", - " (0): Conv2d(672, 112, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(112, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " )\n", - " )\n", - " (stochastic_depth): StochasticDepth(p=0.125, mode=row)\n", - " )\n", - " )\n", - " (6): Sequential(\n", - " (0): MBConv(\n", - " (block): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(112, 672, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): Conv2dNormActivation(\n", - " (0): Conv2d(672, 672, kernel_size=(5, 5), stride=(2, 2), padding=(2, 2), groups=672, bias=False)\n", - " (1): BatchNorm2d(672, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (2): SqueezeExcitation(\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (fc1): Conv2d(672, 28, kernel_size=(1, 1), stride=(1, 1))\n", - " (fc2): Conv2d(28, 672, kernel_size=(1, 1), stride=(1, 1))\n", - " (activation): SiLU(inplace=True)\n", - " (scale_activation): Sigmoid()\n", - " )\n", - " (3): Conv2dNormActivation(\n", - " (0): Conv2d(672, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " )\n", - " )\n", - " (stochastic_depth): StochasticDepth(p=0.1375, mode=row)\n", - " )\n", - " (1): MBConv(\n", - " (block): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(192, 1152, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): Conv2dNormActivation(\n", - " (0): Conv2d(1152, 1152, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=1152, bias=False)\n", - " (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (2): SqueezeExcitation(\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (fc1): Conv2d(1152, 48, kernel_size=(1, 1), stride=(1, 1))\n", - " (fc2): Conv2d(48, 1152, kernel_size=(1, 1), stride=(1, 1))\n", - " (activation): SiLU(inplace=True)\n", - " (scale_activation): Sigmoid()\n", - " )\n", - " (3): Conv2dNormActivation(\n", - " (0): Conv2d(1152, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " )\n", - " )\n", - " (stochastic_depth): StochasticDepth(p=0.15000000000000002, mode=row)\n", - " )\n", - " (2): MBConv(\n", - " (block): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(192, 1152, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): Conv2dNormActivation(\n", - " (0): Conv2d(1152, 1152, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=1152, bias=False)\n", - " (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (2): SqueezeExcitation(\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (fc1): Conv2d(1152, 48, kernel_size=(1, 1), stride=(1, 1))\n", - " (fc2): Conv2d(48, 1152, kernel_size=(1, 1), stride=(1, 1))\n", - " (activation): SiLU(inplace=True)\n", - " (scale_activation): Sigmoid()\n", - " )\n", - " (3): Conv2dNormActivation(\n", - " (0): Conv2d(1152, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " )\n", - " )\n", - " (stochastic_depth): StochasticDepth(p=0.1625, mode=row)\n", - " )\n", - " (3): MBConv(\n", - " (block): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(192, 1152, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): Conv2dNormActivation(\n", - " (0): Conv2d(1152, 1152, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), groups=1152, bias=False)\n", - " (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (2): SqueezeExcitation(\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (fc1): Conv2d(1152, 48, kernel_size=(1, 1), stride=(1, 1))\n", - " (fc2): Conv2d(48, 1152, kernel_size=(1, 1), stride=(1, 1))\n", - " (activation): SiLU(inplace=True)\n", - " (scale_activation): Sigmoid()\n", - " )\n", - " (3): Conv2dNormActivation(\n", - " (0): Conv2d(1152, 192, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(192, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " )\n", - " )\n", - " (stochastic_depth): StochasticDepth(p=0.17500000000000002, mode=row)\n", - " )\n", - " )\n", - " (7): Sequential(\n", - " (0): MBConv(\n", - " (block): Sequential(\n", - " (0): Conv2dNormActivation(\n", - " (0): Conv2d(192, 1152, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (1): Conv2dNormActivation(\n", - " (0): Conv2d(1152, 1152, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=1152, bias=False)\n", - " (1): BatchNorm2d(1152, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " (2): SqueezeExcitation(\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (fc1): Conv2d(1152, 48, kernel_size=(1, 1), stride=(1, 1))\n", - " (fc2): Conv2d(48, 1152, kernel_size=(1, 1), stride=(1, 1))\n", - " (activation): SiLU(inplace=True)\n", - " (scale_activation): Sigmoid()\n", - " )\n", - " (3): Conv2dNormActivation(\n", - " (0): Conv2d(1152, 320, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(320, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " )\n", - " )\n", - " (stochastic_depth): StochasticDepth(p=0.1875, mode=row)\n", - " )\n", - " )\n", - " (8): Conv2dNormActivation(\n", - " (0): Conv2d(320, 1280, kernel_size=(1, 1), stride=(1, 1), bias=False)\n", - " (1): BatchNorm2d(1280, eps=1e-05, momentum=0.1, affine=True, track_running_stats=True)\n", - " (2): SiLU(inplace=True)\n", - " )\n", - " )\n", - " (avgpool): AdaptiveAvgPool2d(output_size=1)\n", - " (classifier): Sequential(\n", - " (0): Dropout(p=0.2, inplace=True)\n", - " (1): Linear(in_features=1280, out_features=1000, bias=True)\n", - " )\n", - ")" - ] - }, - "execution_count": 43, - "metadata": {}, - "output_type": "execute_result" - } - ], - "execution_count": 43 + "outputs": [], + "execution_count": null }, { "metadata": {}, @@ -590,12 +122,7 @@ "id": "7d334e3dd747ba68" }, { - "metadata": { - "ExecuteTime": { - "end_time": "2024-08-18T16:33:32.948099Z", - "start_time": "2024-08-18T16:32:48.044728Z" - } - }, + "metadata": {}, "cell_type": "code", "source": [ "import model_compression_toolkit as mct\n", @@ -604,6 +131,9 @@ "from torch.utils.data import DataLoader\n", "from torchvision import transforms, datasets\n", "\n", + "q_config = mct.core.QuantizationConfig(z_threshold=18)\n", + "ptq_config = mct.core.CoreConfig(quantization_config=q_config)\n", + "\n", "\n", "BATCH_SIZE = 4\n", "n_iters = 20\n", @@ -641,49 +171,12 @@ "representative_dataset_gen = get_representative_dataset(n_iter=n_iters, dataset_loader=val_loader)\n", "\n", "# Perform post training quantization with the default configuration\n", - "quant_model, _ = mct.ptq.pytorch_post_training_quantization(model, representative_dataset_gen)\n", + "quant_model, _ = mct.ptq.pytorch_post_training_quantization(model, representative_data_gen=representative_dataset_gen, core_config=ptq_config)\n", "print('Quantized model is ready')" ], "id": "c231499204b5fe58", - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Statistics Collection: 20it [00:15, 1.30it/s]\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "Running quantization parameters search. This process might take some time, depending on the model size and the selected quantization methods.\n", - "\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Calculating quantization parameters: 100%|██████████| 201/201 [00:26<00:00, 7.52it/s]\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Weights_memory: 5236192.0, Activation_memory: 2408448.0, Total_memory: 7644640.0, BOPS: 7804365562445824\n", - "\n", - "Please run your accuracy evaluation on the exported quantized model to verify it's accuracy.\n", - "Checkout the FAQ and Troubleshooting pages for resolving common issues and improving the quantized model accuracy:\n", - "FAQ: https://github.com/sony/model_optimization/tree/main/FAQ.md\n", - "Quantization Troubleshooting: https://github.com/sony/model_optimization/tree/main/quantization_troubleshooting.md\n", - "Quantized model is ready\n" - ] - } - ], - "execution_count": 44 + "outputs": [], + "execution_count": null }, { "metadata": {}, @@ -696,12 +189,7 @@ "id": "2659cdc8ec1a3008" }, { - "metadata": { - "ExecuteTime": { - "end_time": "2024-08-18T16:34:07.006173Z", - "start_time": "2024-08-18T16:34:04.885684Z" - } - }, + "metadata": {}, "cell_type": "code", "source": [ "mct.exporter.pytorch_export_model(model=quant_model,\n", @@ -710,16 +198,8 @@ " onnx_opset_version=17)" ], "id": "de8dd4dbc0c8d393", - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Exporting onnx model with MCTQ quantizers: ./torchvision_qmodel.onnx\n" - ] - } - ], - "execution_count": 47 + "outputs": [], + "execution_count": null }, { "metadata": {}, @@ -733,12 +213,7 @@ "id": "d98c5b8acb2e7397" }, { - "metadata": { - "ExecuteTime": { - "end_time": "2024-08-18T16:36:30.262328Z", - "start_time": "2024-08-18T16:34:11.606371Z" - } - }, + "metadata": {}, "cell_type": "code", "source": [ "from tutorials.resources.utils.pytorch_tutorial_tools import classification_eval\n", @@ -749,725 +224,8 @@ "print(\"Float model Accuracy: {:.4f}\".format(round(100 * eval_results[0], 2)))" ], "id": "6cd077108ef55889", - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Model name: EfficientNet\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 2%|▏ | 259/12500 [00:03<02:23, 85.51it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 1000, Accuracy: 90.3 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 4%|▍ | 517/12500 [00:06<02:10, 91.56it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 2000, Accuracy: 85.7 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 6%|▌ | 759/12500 [00:08<02:09, 91.00it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 3000, Accuracy: 83.7 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 8%|▊ | 1014/12500 [00:11<02:11, 87.39it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 4000, Accuracy: 81.47 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 10%|█ | 1268/12500 [00:14<01:57, 95.52it/s] " - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 5000, Accuracy: 83.42 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 12%|█▏ | 1519/12500 [00:17<01:57, 93.70it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 6000, Accuracy: 83.65 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 14%|█▍ | 1759/12500 [00:19<02:05, 85.79it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 7000, Accuracy: 84.31 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 16%|█▌ | 2008/12500 [00:22<01:50, 95.21it/s] " - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 8000, Accuracy: 84.65 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 18%|█▊ | 2267/12500 [00:25<01:47, 95.46it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 9000, Accuracy: 83.86 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 20%|██ | 2512/12500 [00:28<01:50, 90.19it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 10000, Accuracy: 83.42 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 22%|██▏ | 2758/12500 [00:30<01:44, 92.90it/s] " - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 11000, Accuracy: 83.56 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 24%|██▍ | 3016/12500 [00:33<01:33, 101.15it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 12000, Accuracy: 83.35 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 26%|██▌ | 3261/12500 [00:36<01:46, 86.67it/s] " - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 13000, Accuracy: 83.22 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 28%|██▊ | 3514/12500 [00:38<01:45, 85.20it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 14000, Accuracy: 83.11 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 30%|███ | 3765/12500 [00:41<01:44, 83.32it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 15000, Accuracy: 83.01 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 32%|███▏ | 4010/12500 [00:44<01:27, 96.84it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 16000, Accuracy: 82.88 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 34%|███▍ | 4266/12500 [00:47<01:32, 89.26it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 17000, Accuracy: 83.34 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 36%|███▌ | 4511/12500 [00:50<01:29, 89.45it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 18000, Accuracy: 83.16 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 38%|███▊ | 4764/12500 [00:52<01:21, 94.59it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 19000, Accuracy: 83.26 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 40%|████ | 5012/12500 [00:55<01:18, 95.63it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 20000, Accuracy: 83.08 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 42%|████▏ | 5265/12500 [00:58<01:21, 88.90it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 21000, Accuracy: 82.52 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 44%|████▍ | 5513/12500 [01:01<01:14, 93.34it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 22000, Accuracy: 82.12 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 46%|████▌ | 5766/12500 [01:03<01:12, 92.72it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 23000, Accuracy: 81.82 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 48%|████▊ | 6014/12500 [01:06<01:19, 81.58it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 24000, Accuracy: 81.38 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 50%|█████ | 6258/12500 [01:09<01:13, 84.65it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 25000, Accuracy: 80.77 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 52%|█████▏ | 6512/12500 [01:12<01:12, 82.64it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 26000, Accuracy: 80.49 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 54%|█████▍ | 6758/12500 [01:15<01:07, 85.59it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 27000, Accuracy: 80.2 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 56%|█████▌ | 7012/12500 [01:18<00:59, 92.22it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 28000, Accuracy: 80.08 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 58%|█████▊ | 7262/12500 [01:20<00:56, 92.30it/s] " - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 29000, Accuracy: 80.18 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 60%|██████ | 7513/12500 [01:23<00:52, 95.25it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 30000, Accuracy: 79.96 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 62%|██████▏ | 7759/12500 [01:26<00:50, 94.21it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 31000, Accuracy: 79.91 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 64%|██████▍ | 8012/12500 [01:29<00:53, 83.89it/s] " - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 32000, Accuracy: 79.4 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 66%|██████▌ | 8260/12500 [01:31<00:44, 96.04it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 33000, Accuracy: 79.21 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 68%|██████▊ | 8511/12500 [01:34<00:43, 90.82it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 34000, Accuracy: 79.08 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 70%|███████ | 8759/12500 [01:37<00:43, 85.54it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 35000, Accuracy: 78.97 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 72%|███████▏ | 9019/12500 [01:40<00:36, 94.31it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 36000, Accuracy: 78.93 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 74%|███████▍ | 9261/12500 [01:42<00:37, 86.96it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 37000, Accuracy: 78.88 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 76%|███████▌ | 9509/12500 [01:45<00:31, 94.80it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 38000, Accuracy: 78.68 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 78%|███████▊ | 9762/12500 [01:48<00:30, 88.44it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 39000, Accuracy: 78.58 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 80%|████████ | 10020/12500 [01:51<00:25, 95.97it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 40000, Accuracy: 78.43 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 82%|████████▏ | 10268/12500 [01:54<00:24, 89.86it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 41000, Accuracy: 78.31 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 84%|████████▍ | 10512/12500 [01:56<00:21, 90.91it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 42000, Accuracy: 78.08 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 86%|████████▌ | 10767/12500 [01:59<00:17, 97.33it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 43000, Accuracy: 77.94 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 88%|████████▊ | 11014/12500 [02:02<00:16, 91.97it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 44000, Accuracy: 77.87 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 90%|█████████ | 11258/12500 [02:04<00:13, 95.50it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 45000, Accuracy: 77.74 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 92%|█████████▏| 11509/12500 [02:07<00:10, 92.71it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 46000, Accuracy: 77.62 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 94%|█████████▍| 11759/12500 [02:10<00:08, 91.53it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 47000, Accuracy: 77.64 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 96%|█████████▌| 12015/12500 [02:13<00:05, 94.17it/s] " - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 48000, Accuracy: 77.77 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 98%|█████████▊| 12266/12500 [02:15<00:02, 82.37it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 49000, Accuracy: 77.52 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 100%|██████████| 12500/12500 [02:18<00:00, 90.18it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 50000, Accuracy: 77.67 %\n", - "Model name: EfficientNet\n", - "Float model Accuracy: 77.6700\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], - "execution_count": 48 + "outputs": [], + "execution_count": null }, { "metadata": {}, @@ -1479,12 +237,7 @@ "id": "eb86aae997a5210a" }, { - "metadata": { - "ExecuteTime": { - "end_time": "2024-08-18T16:55:09.690679Z", - "start_time": "2024-08-18T16:47:11.755896Z" - } - }, + "metadata": {}, "cell_type": "code", "source": [ "# Evaluate the quantized model on ImageNet\n", @@ -1494,724 +247,8 @@ "print(\"Quantized model Accuracy: {:.4f}\".format(round(100 * eval_results[0], 2)))" ], "id": "b881138870761d89", - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Model name: EfficientNet\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 2%|▏ | 253/12500 [00:10<08:54, 22.92it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 1000, Accuracy: 87.3 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 4%|▍ | 502/12500 [00:20<08:46, 22.78it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 2000, Accuracy: 83.2 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 6%|▌ | 754/12500 [00:30<07:33, 25.90it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 3000, Accuracy: 80.03 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 8%|▊ | 1003/12500 [00:40<07:18, 26.21it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 4000, Accuracy: 77.65 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 10%|▉ | 1249/12500 [00:50<06:48, 27.54it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 5000, Accuracy: 80.04 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 12%|█▏ | 1504/12500 [01:00<06:39, 27.50it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 6000, Accuracy: 80.0 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 14%|█▍ | 1753/12500 [01:10<06:52, 26.05it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 7000, Accuracy: 80.7 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 16%|█▌ | 2005/12500 [01:20<06:24, 27.32it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 8000, Accuracy: 81.12 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 18%|█▊ | 2255/12500 [01:29<06:55, 24.63it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 9000, Accuracy: 80.29 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 20%|██ | 2504/12500 [01:39<06:27, 25.79it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 10000, Accuracy: 79.56 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 22%|██▏ | 2753/12500 [01:48<06:49, 23.78it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 11000, Accuracy: 79.59 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 24%|██▍ | 3005/12500 [01:58<06:05, 25.97it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 12000, Accuracy: 79.19 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 26%|██▌ | 3254/12500 [02:08<06:31, 23.60it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 13000, Accuracy: 79.12 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 28%|██▊ | 3503/12500 [02:18<06:01, 24.89it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 14000, Accuracy: 78.8 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 30%|███ | 3755/12500 [02:28<05:33, 26.20it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 15000, Accuracy: 78.75 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 32%|███▏ | 4004/12500 [02:37<05:09, 27.42it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 16000, Accuracy: 78.73 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 34%|███▍ | 4253/12500 [02:47<05:27, 25.20it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 17000, Accuracy: 79.29 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 36%|███▌ | 4505/12500 [02:57<05:22, 24.76it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 18000, Accuracy: 79.16 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 38%|███▊ | 4754/12500 [03:06<04:53, 26.41it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 19000, Accuracy: 79.35 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 40%|████ | 5003/12500 [03:16<04:34, 27.34it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 20000, Accuracy: 79.21 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 42%|████▏ | 5255/12500 [03:26<04:21, 27.71it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 21000, Accuracy: 78.66 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 44%|████▍ | 5504/12500 [03:35<04:41, 24.88it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 22000, Accuracy: 78.25 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 46%|████▌ | 5753/12500 [03:45<04:10, 26.98it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 23000, Accuracy: 77.94 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 48%|████▊ | 6004/12500 [03:55<04:01, 26.84it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 24000, Accuracy: 77.56 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 50%|█████ | 6253/12500 [04:04<03:44, 27.85it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 25000, Accuracy: 76.9 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 52%|█████▏ | 6505/12500 [04:14<03:56, 25.30it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 26000, Accuracy: 76.54 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 54%|█████▍ | 6754/12500 [04:23<03:51, 24.78it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 27000, Accuracy: 76.27 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 56%|█████▌ | 7003/12500 [04:33<03:43, 24.54it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 28000, Accuracy: 76.13 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 58%|█████▊ | 7252/12500 [04:43<03:14, 27.04it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 29000, Accuracy: 76.32 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 60%|██████ | 7504/12500 [04:53<03:09, 26.34it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 30000, Accuracy: 76.13 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 62%|██████▏ | 7753/12500 [05:02<03:14, 24.41it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 31000, Accuracy: 76.09 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 64%|██████▍ | 8003/12500 [05:12<02:34, 29.08it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 32000, Accuracy: 75.57 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 66%|██████▌ | 8255/12500 [05:21<02:28, 28.54it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 33000, Accuracy: 75.42 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 68%|██████▊ | 8503/12500 [05:30<02:19, 28.61it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 34000, Accuracy: 75.22 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 70%|███████ | 8753/12500 [05:39<02:05, 29.80it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 35000, Accuracy: 75.11 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 72%|███████▏ | 9004/12500 [05:48<01:58, 29.43it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 36000, Accuracy: 75.03 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 74%|███████▍ | 9254/12500 [05:57<01:56, 27.84it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 37000, Accuracy: 74.9 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 76%|███████▌ | 9505/12500 [06:06<01:49, 27.45it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 38000, Accuracy: 74.68 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 78%|███████▊ | 9754/12500 [06:15<01:47, 25.58it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 39000, Accuracy: 74.56 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 80%|████████ | 10004/12500 [06:25<01:28, 28.31it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 40000, Accuracy: 74.41 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 82%|████████▏ | 10253/12500 [06:34<01:18, 28.47it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 41000, Accuracy: 74.32 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 84%|████████▍ | 10503/12500 [06:43<01:12, 27.64it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 42000, Accuracy: 74.1 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 86%|████████▌ | 10754/12500 [06:53<01:13, 23.81it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 43000, Accuracy: 73.94 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 88%|████████▊ | 11005/12500 [07:02<00:57, 25.99it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 44000, Accuracy: 73.9 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 90%|█████████ | 11255/12500 [07:12<00:46, 26.69it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 45000, Accuracy: 73.76 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 92%|█████████▏| 11505/12500 [07:21<00:35, 28.21it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 46000, Accuracy: 73.67 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 94%|█████████▍| 11755/12500 [07:30<00:27, 27.12it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 47000, Accuracy: 73.65 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 96%|█████████▌| 12003/12500 [07:39<00:18, 26.94it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 48000, Accuracy: 73.71 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 98%|█████████▊| 12253/12500 [07:48<00:08, 27.94it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 49000, Accuracy: 73.47 %\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Classification evaluation: 100%|██████████| 12500/12500 [07:57<00:00, 26.15it/s]" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Num of images: 50000, Accuracy: 73.64 %\n", - "Quantized model Accuracy: 73.6400\n" - ] - }, - { - "name": "stderr", - "output_type": "stream", - "text": [ - "\n" - ] - } - ], - "execution_count": 50 + "outputs": [], + "execution_count": null }, { "metadata": {}, From 1838fbdda07f7dab622f49eb3f139a9488e2d14e Mon Sep 17 00:00:00 2001 From: yardeny-sony Date: Mon, 19 Aug 2024 14:38:14 +0300 Subject: [PATCH 4/5] remove ptq_config --- .../pytorch_torchvision_classification_for_imx500.ipynb | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb b/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb index 4789cbdbb..a4386e172 100644 --- a/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb +++ b/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb @@ -131,9 +131,6 @@ "from torch.utils.data import DataLoader\n", "from torchvision import transforms, datasets\n", "\n", - "q_config = mct.core.QuantizationConfig(z_threshold=18)\n", - "ptq_config = mct.core.CoreConfig(quantization_config=q_config)\n", - "\n", "\n", "BATCH_SIZE = 4\n", "n_iters = 20\n", @@ -171,7 +168,7 @@ "representative_dataset_gen = get_representative_dataset(n_iter=n_iters, dataset_loader=val_loader)\n", "\n", "# Perform post training quantization with the default configuration\n", - "quant_model, _ = mct.ptq.pytorch_post_training_quantization(model, representative_data_gen=representative_dataset_gen, core_config=ptq_config)\n", + "quant_model, _ = mct.ptq.pytorch_post_training_quantization(model, representative_data_gen=representative_dataset_gen)\n", "print('Quantized model is ready')" ], "id": "c231499204b5fe58", From 9ab7734fee72dcfe894af6ae80839e2ce70469ea Mon Sep 17 00:00:00 2001 From: yardeny-sony Date: Mon, 19 Aug 2024 15:28:43 +0300 Subject: [PATCH 5/5] fix notebook link --- ... => pytorch_torchvision_classification_model_for_imx500.ipynb} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tutorials/notebooks/imx500_notebooks/pytorch/{pytorch_torchvision_classification_for_imx500.ipynb => pytorch_torchvision_classification_model_for_imx500.ipynb} (100%) diff --git a/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb b/tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_model_for_imx500.ipynb similarity index 100% rename from tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_for_imx500.ipynb rename to tutorials/notebooks/imx500_notebooks/pytorch/pytorch_torchvision_classification_model_for_imx500.ipynb