ahmzakif commited on
Commit
6b2cb9a
·
verified ·
1 Parent(s): 725a92f

Upload 7 files

Browse files
.gitattributes CHANGED
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ model/cnn_model.keras filter=lfs diff=lfs merge=lfs -text
37
+ model/mobnet_model.keras filter=lfs diff=lfs merge=lfs -text
01_Preprocessing-Data.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
02_Train-Model.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
03_Quantization_model.ipynb ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 1,
6
+ "metadata": {},
7
+ "outputs": [
8
+ {
9
+ "name": "stdout",
10
+ "output_type": "stream",
11
+ "text": [
12
+ "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",
13
+ "\n"
14
+ ]
15
+ }
16
+ ],
17
+ "source": [
18
+ "import tensorflow as tf\n",
19
+ "import pathlib\n",
20
+ "import os"
21
+ ]
22
+ },
23
+ {
24
+ "attachments": {},
25
+ "cell_type": "markdown",
26
+ "metadata": {},
27
+ "source": [
28
+ "# Quantize Model"
29
+ ]
30
+ },
31
+ {
32
+ "cell_type": "code",
33
+ "execution_count": 2,
34
+ "metadata": {},
35
+ "outputs": [],
36
+ "source": [
37
+ "def quantize_model(model_path, output_path):\n",
38
+ " \"\"\"\n",
39
+ " Load Keras model and convert it to TFLite with quantization\n",
40
+ " \n",
41
+ " Args:\n",
42
+ " model_path: Path to the .keras model file\n",
43
+ " output_path: Path to save the quantized TFLite model\n",
44
+ " \"\"\"\n",
45
+ " # Step 1: Load the Keras model\n",
46
+ " print(\"Loading model...\")\n",
47
+ " model = tf.keras.models.load_model(model_path)\n",
48
+ " \n",
49
+ " # Step 2: Convert the model to TFLite format\n",
50
+ " print(\"Converting to TFLite...\")\n",
51
+ " converter = tf.lite.TFLiteConverter.from_keras_model(model)\n",
52
+ " \n",
53
+ " # Step 3: Enable quantization\n",
54
+ " # Using dynamic range quantization (post-training quantization)\n",
55
+ " converter.optimizations = [tf.lite.Optimize.DEFAULT]\n",
56
+ " \n",
57
+ " # Additional options for quantization\n",
58
+ " converter.target_spec.supported_types = [tf.float16]\n",
59
+ " \n",
60
+ " # Step 4: Convert the model\n",
61
+ " print(\"Applying quantization...\")\n",
62
+ " tflite_model = converter.convert()\n",
63
+ " \n",
64
+ " # Step 5: Save the quantized model\n",
65
+ " print(f\"Saving quantized model to {output_path}\")\n",
66
+ " with open(output_path, 'wb') as f:\n",
67
+ " f.write(tflite_model)\n",
68
+ " \n",
69
+ " # Calculate and print model size reduction\n",
70
+ " original_size = os.path.getsize(model_path) / (1024 * 1024) # Size in MB\n",
71
+ " quantized_size = os.path.getsize(output_path) / (1024 * 1024) # Size in MB\n",
72
+ " \n",
73
+ " print(f\"\\nModel size comparison:\")\n",
74
+ " print(f\"Original model size: {original_size:.2f} MB\")\n",
75
+ " print(f\"Quantized model size: {quantized_size:.2f} MB\")\n",
76
+ " print(f\"Size reduction: {((original_size - quantized_size) / original_size * 100):.2f}%\")"
77
+ ]
78
+ },
79
+ {
80
+ "cell_type": "code",
81
+ "execution_count": 3,
82
+ "metadata": {},
83
+ "outputs": [],
84
+ "source": [
85
+ "# Define paths\n",
86
+ "model_path = \"model/mobnet_model.keras\"\n",
87
+ "output_path = \"model/mobnet_model_quantized.tflite\""
88
+ ]
89
+ },
90
+ {
91
+ "cell_type": "code",
92
+ "execution_count": 4,
93
+ "metadata": {},
94
+ "outputs": [
95
+ {
96
+ "name": "stdout",
97
+ "output_type": "stream",
98
+ "text": [
99
+ "Loading model...\n",
100
+ "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",
101
+ "\n",
102
+ "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",
103
+ "\n",
104
+ "Converting to TFLite...\n",
105
+ "Applying quantization...\n",
106
+ "INFO:tensorflow:Assets written to: C:\\Users\\ASUS\\AppData\\Local\\Temp\\tmpq8y46ide\\assets\n"
107
+ ]
108
+ },
109
+ {
110
+ "name": "stderr",
111
+ "output_type": "stream",
112
+ "text": [
113
+ "INFO:tensorflow:Assets written to: C:\\Users\\ASUS\\AppData\\Local\\Temp\\tmpq8y46ide\\assets\n"
114
+ ]
115
+ },
116
+ {
117
+ "name": "stdout",
118
+ "output_type": "stream",
119
+ "text": [
120
+ "Saving quantized model to model/mobnet_model_quantized.tflite\n",
121
+ "\n",
122
+ "Model size comparison:\n",
123
+ "Original model size: 23.44 MB\n",
124
+ "Quantized model size: 4.27 MB\n",
125
+ "Size reduction: 81.79%\n"
126
+ ]
127
+ }
128
+ ],
129
+ "source": [
130
+ "# Create output directory if it doesn't exist\n",
131
+ "pathlib.Path(output_path).parent.mkdir(parents=True, exist_ok=True)\n",
132
+ "\n",
133
+ "# Run quantization\n",
134
+ "quantize_model(model_path, output_path)"
135
+ ]
136
+ }
137
+ ],
138
+ "metadata": {
139
+ "kernelspec": {
140
+ "display_name": "Python 3",
141
+ "language": "python",
142
+ "name": "python3"
143
+ },
144
+ "language_info": {
145
+ "codemirror_mode": {
146
+ "name": "ipython",
147
+ "version": 3
148
+ },
149
+ "file_extension": ".py",
150
+ "mimetype": "text/x-python",
151
+ "name": "python",
152
+ "nbconvert_exporter": "python",
153
+ "pygments_lexer": "ipython3",
154
+ "version": "3.9.0"
155
+ },
156
+ "orig_nbformat": 4
157
+ },
158
+ "nbformat": 4,
159
+ "nbformat_minor": 2
160
+ }
model/cnn_model.keras ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:af231b5835045547eaf2d79fe9f539a32fa2d8e2072ae50bb006c7935f91be4d
3
+ size 8488520
model/mobnet_model.keras ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:09aa4a182f3d6a868b18ec980b794044ffd2ba7b5c1755d157891857376d2215
3
+ size 24577292
model/mobnet_model_quantized.tflite ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:411f792d9283c27975e30d264717f1c900e073087429ba631571af6e2c561369
3
+ size 4474992
requirements.txt ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ tqdm==4.66.5
2
+ imutils==0.5.4
3
+ numpy==1.26.4
4
+ pandas==2.0.3
5
+ pillow==10.4.0
6
+ matplotlib==3.7.3
7
+ seaborn==0.11.0
8
+ albumentations==1.4.1
9
+ opencv-python==4.10.0.84
10
+ tensorflow==2.15.1
11
+ keras==2.15.1
12
+ scikit-learn==1.2.2
13
+ wandb==0.19.1