-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython_edge_ml_quantization.py
More file actions
160 lines (125 loc) · 4.34 KB
/
python_edge_ml_quantization.py
File metadata and controls
160 lines (125 loc) · 4.34 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
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
import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
import time
import os
import matplotlib.pyplot as plt
transform = transforms.Compose([transforms.ToTensor()])
train_dataset = datasets.MNIST(
root="./data", train=True, download=True, transform=transform
)
test_dataset = datasets.MNIST(
root="./data", train=False, download=True, transform=transform
)
train_loader = torch.utils.data.DataLoader(
train_dataset, batch_size=64, shuffle=True
)
test_loader = torch.utils.data.DataLoader(
test_dataset, batch_size=64, shuffle=False
)
class CNNModel(nn.Module):
def __init__(self):
super().__init__()
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.fc1 = nn.Linear(9216, 128)
self.fc2 = nn.Linear(128, 10)
self.relu = nn.ReLU()
self.dropout = nn.Dropout(0.25)
def forward(self, x):
x = self.relu(self.conv1(x))
x = self.relu(self.conv2(x))
x = nn.functional.max_pool2d(x, 2)
x = torch.flatten(x, 1)
x = self.dropout(self.relu(self.fc1(x)))
return self.fc2(x)
def train_model(model, loader, epochs=3):
optimizer = optim.Adam(model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()
loss_history = []
model.train()
for epoch in range(epochs):
epoch_loss = 0
for images, labels in loader:
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
epoch_loss += loss.item()
avg_loss = epoch_loss / len(loader)
loss_history.append(avg_loss)
print(f"Epoch {epoch+1}/{epochs}, Loss: {avg_loss:.4f}")
return loss_history
def evaluate_accuracy(model, loader):
model.eval()
correct, total = 0, 0
with torch.no_grad():
for images, labels in loader:
outputs = model(images)
_, predicted = torch.max(outputs, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
return 100 * correct / total
def evaluate_speed(model, loader):
model.eval()
start = time.time()
with torch.no_grad():
for images, _ in loader:
_ = model(images)
return time.time() - start
model = CNNModel()
loss_history = train_model(model, train_loader, epochs=3)
orig_acc = evaluate_accuracy(model, test_loader)
orig_time = evaluate_speed(model, test_loader)
torch.save(model.state_dict(), "original_model.pth")
orig_size = os.path.getsize("original_model.pth") / 1024
quantized_model = torch.quantization.quantize_dynamic(
model, {nn.Linear}, dtype=torch.qint8
)
quant_acc = evaluate_accuracy(quantized_model, test_loader)
quant_time = evaluate_speed(quantized_model, test_loader)
torch.save(quantized_model.state_dict(), "quantized_model.pth")
quant_size = os.path.getsize("quantized_model.pth") / 1024
print("\n--- RESULTS ---")
print(f"Original Model | Size: {orig_size:.2f} KB | Acc: {orig_acc:.2f}% | Time: {orig_time:.2f}s")
print(f"Quantized Model | Size: {quant_size:.2f} KB | Acc: {quant_acc:.2f}% | Time: {quant_time:.2f}s")
plt.figure()
plt.plot(loss_history, marker='o')
plt.title("Training Loss Curve")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.grid(True)
plt.show()
plt.figure()
plt.bar(["Original", "Quantized"], [orig_size, quant_size])
plt.title("Model Size Comparison (KB)")
plt.ylabel("Size (KB)")
plt.show()
plt.figure()
plt.bar(["Original", "Quantized"], [orig_time, quant_time])
plt.title("Inference Time Comparison (seconds)")
plt.ylabel("Time (s)")
plt.show()
plt.figure(figsize=(14, 4))
# Loss curve
plt.subplot(1, 3, 1)
plt.plot(loss_history, marker='o')
plt.title("Training Loss")
plt.xlabel("Epoch")
plt.ylabel("Loss")
plt.grid(True)
# Model size
plt.subplot(1, 3, 2)
plt.bar(["FP32", "INT8"], [orig_size, quant_size])
plt.title("Model Size (KB)")
plt.ylabel("KB")
# Inference time
plt.subplot(1, 3, 3)
plt.bar(["FP32", "INT8"], [orig_time, quant_time])
plt.title("Inference Time (s)")
plt.ylabel("Seconds")
plt.suptitle("Edge ML Quantization: Accuracy–Size–Latency Trade-offs", fontsize=14)
plt.tight_layout()
plt.show()