File size: 5,151 Bytes
6b2cb9a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING:tensorflow:From c:\\Users\\ASUS\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\keras\\src\\losses.py:2976: The name tf.losses.sparse_softmax_cross_entropy is deprecated. Please use tf.compat.v1.losses.sparse_softmax_cross_entropy instead.\n",
"\n"
]
}
],
"source": [
"import tensorflow as tf\n",
"import pathlib\n",
"import os"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"# Quantize Model"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def quantize_model(model_path, output_path):\n",
" \"\"\"\n",
" Load Keras model and convert it to TFLite with quantization\n",
" \n",
" Args:\n",
" model_path: Path to the .keras model file\n",
" output_path: Path to save the quantized TFLite model\n",
" \"\"\"\n",
" # Step 1: Load the Keras model\n",
" print(\"Loading model...\")\n",
" model = tf.keras.models.load_model(model_path)\n",
" \n",
" # Step 2: Convert the model to TFLite format\n",
" print(\"Converting to TFLite...\")\n",
" converter = tf.lite.TFLiteConverter.from_keras_model(model)\n",
" \n",
" # Step 3: Enable quantization\n",
" # Using dynamic range quantization (post-training quantization)\n",
" converter.optimizations = [tf.lite.Optimize.DEFAULT]\n",
" \n",
" # Additional options for quantization\n",
" converter.target_spec.supported_types = [tf.float16]\n",
" \n",
" # Step 4: Convert the model\n",
" print(\"Applying quantization...\")\n",
" tflite_model = converter.convert()\n",
" \n",
" # Step 5: Save the quantized model\n",
" print(f\"Saving quantized model to {output_path}\")\n",
" with open(output_path, 'wb') as f:\n",
" f.write(tflite_model)\n",
" \n",
" # Calculate and print model size reduction\n",
" original_size = os.path.getsize(model_path) / (1024 * 1024) # Size in MB\n",
" quantized_size = os.path.getsize(output_path) / (1024 * 1024) # Size in MB\n",
" \n",
" print(f\"\\nModel size comparison:\")\n",
" print(f\"Original model size: {original_size:.2f} MB\")\n",
" print(f\"Quantized model size: {quantized_size:.2f} MB\")\n",
" print(f\"Size reduction: {((original_size - quantized_size) / original_size * 100):.2f}%\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"# Define paths\n",
"model_path = \"model/mobnet_model.keras\"\n",
"output_path = \"model/mobnet_model_quantized.tflite\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Loading model...\n",
"WARNING:tensorflow:From c:\\Users\\ASUS\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\keras\\src\\backend.py:1398: The name tf.executing_eagerly_outside_functions is deprecated. Please use tf.compat.v1.executing_eagerly_outside_functions instead.\n",
"\n",
"WARNING:tensorflow:From c:\\Users\\ASUS\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages\\keras\\src\\layers\\normalization\\batch_normalization.py:979: The name tf.nn.fused_batch_norm is deprecated. Please use tf.compat.v1.nn.fused_batch_norm instead.\n",
"\n",
"Converting to TFLite...\n",
"Applying quantization...\n",
"INFO:tensorflow:Assets written to: C:\\Users\\ASUS\\AppData\\Local\\Temp\\tmpq8y46ide\\assets\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"INFO:tensorflow:Assets written to: C:\\Users\\ASUS\\AppData\\Local\\Temp\\tmpq8y46ide\\assets\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Saving quantized model to model/mobnet_model_quantized.tflite\n",
"\n",
"Model size comparison:\n",
"Original model size: 23.44 MB\n",
"Quantized model size: 4.27 MB\n",
"Size reduction: 81.79%\n"
]
}
],
"source": [
"# Create output directory if it doesn't exist\n",
"pathlib.Path(output_path).parent.mkdir(parents=True, exist_ok=True)\n",
"\n",
"# Run quantization\n",
"quantize_model(model_path, output_path)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.0"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}
|