-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquantizer.py
More file actions
55 lines (41 loc) · 1.65 KB
/
quantizer.py
File metadata and controls
55 lines (41 loc) · 1.65 KB
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
import os
import cv2
import tensorflow as tf
import numpy as np
print(os.getcwd())
# Load the trained Keras model
model = tf.keras.models.load_model("./traffic_light_gesture_model_new5pm.keras")
for input_spec in model.inputs:
print(input_spec)
# Path to the dataset
dataset_path = "./GrayscaleProcessedDataset/"
image_size = (32, 32)
# Define a representative dataset generator function
def representative_dataset_gen():
for file in os.listdir(dataset_path):
img = cv2.imread(os.path.join(dataset_path, file))
if img is None:
print(f"Warning: Unable to read image ''. Skipping.")
continue # Skip invalid or unreadable files
img = cv2.resize(img, image_size)
# Normalize and scale the image
img = (img / 255.0 - 0.5) * 256
# Explicitly cast to float32
img = img.astype(np.float32)
# Add batch dimension
yield [np.expand_dims(img, axis=0)]
# Set up the TFLite converter
converter = tf.lite.TFLiteConverter.from_keras_model(model)
# Apply optimizations and specify quantization settings
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8] # Use only integer operations
converter.representative_dataset = representative_dataset_gen
# Ensure input/output tensors are also quantized to int8
converter.inference_input_type = tf.int8
converter.inference_output_type = tf.int8
# Convert the model
quantized_model = converter.convert()
# Save the quantized model
with open("quantized_model5pm.tflite", "wb") as f:
f.write(quantized_model)
# print("Quantized model saved as quantized_model.tflite")