basant18 commited on
Commit
896b202
Β·
verified Β·
1 Parent(s): 2e475aa

Upload README.md

Browse files
Files changed (1) hide show
  1. README.md +94 -3
README.md CHANGED
@@ -1,3 +1,94 @@
1
- ---
2
- license: apache-2.0
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Model Quantization Notebook
2
+
3
+ This notebook converts a pre-trained Keras violence detection model into TensorFlow Lite (TFLite) format using three different quantization strategies, making it suitable for deployment on edge/mobile devices.
4
+
5
+ ---
6
+
7
+ ## Overview
8
+
9
+ | Property | Details |
10
+ |---|---|
11
+ | **Framework** | TensorFlow / TFLite |
12
+ | **Base Model** | `modelv2.keras` β€” a Keras video violence detection model |
13
+ | **Input Shape** | `(1, 16, 224, 224, 3)` β€” batch Γ— frames Γ— height Γ— width Γ— channels |
14
+ | **Architecture** | CNN + LSTM (contains dynamic LSTM loops) |
15
+ | **Platform** | Kaggle (GPU hidden to avoid CuDNN conflicts) |
16
+
17
+ ---
18
+
19
+ ## Quantization Methods
20
+
21
+ ### A β€” Dynamic Range Quantization
22
+ - **Output file:** `model_dynamic_quant.tflite`
23
+ - Quantizes weights from float32 to int8 at **conversion time**.
24
+ - Activations are quantized **dynamically** at inference time.
25
+ - Fastest to convert; no calibration data required.
26
+ - Good balance between size reduction and accuracy.
27
+
28
+ ### B β€” Float16 Quantization
29
+ - **Output file:** `model_fp16_quant.tflite`
30
+ - Reduces weight precision from float32 to **float16**.
31
+ - Ideal for GPU-accelerated edge devices that support fp16 natively.
32
+ - Smaller model size with minimal accuracy loss.
33
+
34
+ ### C β€” Full Integer (INT8) Quantization
35
+ - **Output file:** `model_full_int8.tflite`
36
+ - Quantizes **both weights and activations** to int8.
37
+ - Requires a **representative dataset** for calibration (currently uses random dummy data β€” replace with real video samples for best results).
38
+ - Input and output tensors are also forced to int8.
39
+ - Smallest model size; best suited for CPU-only or microcontroller deployment.
40
+
41
+ ---
42
+
43
+ ## Requirements
44
+
45
+ ```
46
+ tensorflow
47
+ numpy
48
+ ```
49
+
50
+ ---
51
+
52
+ ## Usage
53
+
54
+ ### 1. Load the Base Model
55
+ ```python
56
+ import tensorflow as tf
57
+
58
+ tf.config.set_visible_devices([], 'GPU') # Hide GPU to avoid CuDNN issues
59
+ model = tf.keras.models.load_model('path/to/modelv2.keras')
60
+ ```
61
+
62
+ ### 2. Run Quantization
63
+ Open and run the notebook cells in order:
64
+ 1. **Cell 1–2** β€” Load the model
65
+ 2. **Cell 3–4** β€” Dynamic range quantization β†’ `model_dynamic_quant.tflite`
66
+ 3. **Cell 5–6** β€” Float16 quantization β†’ `model_fp16_quant.tflite`
67
+ 4. **Cell 7–8** β€” Full INT8 quantization β†’ `model_full_int8.tflite`
68
+
69
+ ---
70
+
71
+ ## Important Notes
72
+
73
+ - **Representative dataset:** The INT8 quantization cell uses random dummy data for calibration. For production use, replace `dummy_data` in `representative_data_gen()` with real video frames from your training set to get accurate quantization ranges.
74
+
75
+ - **LSTM compatibility flags:** The model contains dynamic LSTM loops. The following flags are set in all conversion paths to prevent conversion failures:
76
+ ```python
77
+ converter.target_spec.supported_ops = [
78
+ tf.lite.OpsSet.TFLITE_BUILTINS,
79
+ tf.lite.OpsSet.SELECT_TF_OPS
80
+ ]
81
+ converter._experimental_lower_tensor_list_ops = False
82
+ ```
83
+
84
+ - **Static input shape:** The INT8 path uses `tf.function` with a `tf.TensorSpec` to lock the input shape to `(1, 16, 224, 224, 3)` before conversion β€” this is required for correct INT8 LSTM quantization.
85
+
86
+ ---
87
+
88
+ ## Output Files
89
+
90
+ | File | Method | Precision |
91
+ |---|---|---|
92
+ | `model_dynamic_quant.tflite` | Dynamic Range | Weights: INT8, Activations: float32 |
93
+ | `model_fp16_quant.tflite` | Float16 | Weights & Activations: float16 |
94
+ | `model_full_int8.tflite` | Full Integer | Weights & Activations: INT8 |