HW4ML Project Starter Code Template
HW4ML Project Starter Code Template
The below code is for your reference; please feel free to change it partially or
fully.
Please make sure it does not have any bugs or mistakes. Code authors DO NOT claim
the code is bug-free. It is the student's responsibility to ensure its correctness.
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import fashion_mnist, cifar10
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.callbacks import EarlyStopping
import time
import numpy as np
from tqdm import tqdm
def prepare_dataset(dataset_name):
if dataset_name == 'fashion_mnist':
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
num_classes = 10
input_shape = (28, 28, 1)
x_train = x_train.reshape(-1, 28, 28, 1)
x_test = x_test.reshape(-1, 28, 28, 1)
elif dataset_name == 'cifar10':
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
num_classes = 10
input_shape = (32, 32, 3)
else:
raise ValueError(f"Unsupported dataset: {dataset_name}")
with tf.device(device):
model = create_base_model(input_shape, num_classes)
early_stop = EarlyStopping(monitor='val_loss', patience=3,
restore_best_weights=True)
start_time = time.time()
model.fit(x_train, y_train, epochs=max_epoch, batch_size=64,
validation_split=0.2,
callbacks=[early_stop], verbose=1)
train_time = time.time() - start_time
start_time = time.time()
test_loss, test_accuracy = model.evaluate(x_test, y_test, verbose=0)
eval_time = time.time() - start_time
avg_latency = np.mean(latencies)
print(f"Average Latency on {dev_name}: {avg_latency:.2f} ms")
The below code is for your reference; please feel free to change it partially or
fully.
Please make sure it does not have any bugs or mistakes. Code authors DO NOT claim
the code is bug-free. It is the student's responsibility to ensure its correctness.
import tensorflow as tf
from tensorflow.keras import layers, models
from tensorflow.keras.datasets import fashion_mnist, cifar10
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.callbacks import EarlyStopping
import time
import numpy as np
import os
from tqdm import tqdm
def prepare_dataset(dataset_name):
if dataset_name == 'fashion_mnist':
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()
num_classes = 10
input_shape = (28, 28, 1)
x_train = x_train.reshape(-1, 28, 28, 1)
x_test = x_test.reshape(-1, 28, 28, 1)
elif dataset_name == 'cifar10':
(x_train, y_train), (x_test, y_test) = cifar10.load_data()
num_classes = 10
input_shape = (32, 32, 3)
else:
raise ValueError(f"Unsupported dataset: {dataset_name}")
avg_latency = np.mean(latencies)
print(f"Average Latency on {dev_name}: {avg_latency:.2f} ms")
return avg_latency
for _ in range(10):
interpreter.set_tensor(input_details[0]['index'], input_tensor)
interpreter.invoke()
for _ in range(iterations):
start = time.time()
interpreter.set_tensor(input_details[0]['index'], input_tensor)
interpreter.invoke()
latencies.append((time.time() - start) * 1000)
avg_latency = np.mean(latencies)
return avg_latency
tflite_quant_model = converter.convert()
with open(save_path, "wb") as f:
f.write(tflite_quant_model)
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
correct = 0
total = x_test.shape[0]
for i in range(total):
input_data = np.round(x_test[i:i+1] * 255).astype(np.int8) # <- FIXED
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output = interpreter.get_tensor(output_details[0]['index'])
if np.argmax(output) == np.argmax(y_test[i]):
correct += 1
accuracy = correct / total
return accuracy
# -------------------
# Main Code
# -------------------
if tf.config.list_physical_devices('GPU'):
device = '/GPU:0'
dev_name = 'GPU'
elif tf.config.list_physical_devices('MPS'):
device = '/MPS:0'
dev_name = 'Apple MPS'
else:
device = '/CPU:0'
dev_name = 'CPU'
print(f'Using {dev_name}')
with tf.device(device):
model = create_base_model(input_shape, num_classes)
# Quantization
def representative_data_gen():
for input_value in
tf.data.Dataset.from_tensor_slices(x_test).batch(1).take(100):
yield [tf.cast(input_value * 255.0, tf.float32)]
quantize_model_to_int8(model, representative_data_gen,
save_path="model_int8.tflite")
int8_size = get_file_size("model_int8.tflite")
print(f"Quantized Model Size: {int8_size:.2f} KB")
print("\nSummary:")
print(f"{'Metric':<25} {'Before Quantization':<20} {'After Quantization'}")
print(f"{'-'*70}")
print(f"{'Number of Parameters':<25} {num_params:<20} {num_params}")
print(f"{'Test Accuracy (%)':<25} {test_acc_fp32*100:.2f}%{'':<12}
{test_acc_int8*100:.2f}%")
print(f"{'Training Time (s)':<25} {train_time:.2f}{'':<16} {'-'}")
print(f"{'Evaluation Time (s)':<25} {eval_time_fp32:.4f}{'':<14} {'-'}")
print(f"{'Inference Latency (ms)':<25} {orig_latency:.2f}{'':<14}
{int8_latency:.2f}")
print(f"{'Model Size (KB)':<25} {fp32_size:.2f}{'':<14} {int8_size:.2f}")