![]() | ![]() | ![]() | ![]() |
Tổng quat
Chào mừng bạn đến với ví dụ end-to-end về phân nhóm trọng số , một phần của Bộ công cụ tối ưu hóa mô hình TensorFlow.
Những trang khác
Để được giới thiệu về phân cụm trọng số là gì và để xác định xem bạn có nên sử dụng nó (bao gồm cả những gì được hỗ trợ) hay không, hãy xem trang tổng quan .
Để nhanh chóng tìm thấy các API bạn cần cho trường hợp sử dụng của mình (ngoài việc phân nhóm đầy đủ một mô hình với 16 cụm), hãy xem hướng dẫn toàn diện .
Nội dung
Trong hướng dẫn, bạn sẽ:
- Đào tạo mô hình
tf.keras
cho tập dữ liệu MNIST từ đầu. - Tinh chỉnh mô hình bằng cách áp dụng API phân cụm trọng lượng và xem độ chính xác.
- Tạo mô hình TF và TFLite nhỏ hơn 6 lần từ phân cụm.
- Tạo mô hình TFLite nhỏ hơn 8x từ việc kết hợp phân cụm trọng lượng và lượng tử hóa sau huấn luyện.
- Xem độ chính xác lâu dài từ TF sang TFLite.
Thiết lập
Bạn có thể chạy Máy tính xách tay Jupyter này trong virtualenv hoặc colab cục bộ của mình. Để biết chi tiết về việc thiết lập các phụ thuộc, vui lòng tham khảo hướng dẫn cài đặt .
pip install -q tensorflow-model-optimization
import tensorflow as tf
from tensorflow import keras
import numpy as np
import tempfile
import zipfile
import os
Đào tạo mô hình tf.keras cho MNIST mà không cần phân cụm
# Load MNIST dataset
mnist = keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
# Normalize the input image so that each pixel value is between 0 to 1.
train_images = train_images / 255.0
test_images = test_images / 255.0
# Define the model architecture.
model = keras.Sequential([
keras.layers.InputLayer(input_shape=(28, 28)),
keras.layers.Reshape(target_shape=(28, 28, 1)),
keras.layers.Conv2D(filters=12, kernel_size=(3, 3), activation=tf.nn.relu),
keras.layers.MaxPooling2D(pool_size=(2, 2)),
keras.layers.Flatten(),
keras.layers.Dense(10)
])
# Train the digit classification model
model.compile(optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy'])
model.fit(
train_images,
train_labels,
validation_split=0.1,
epochs=10
)
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz 11493376/11490434 [==============================] - 0s 0us/step Epoch 1/10 1688/1688 [==============================] - 7s 4ms/step - loss: 0.3008 - accuracy: 0.9148 - val_loss: 0.1216 - val_accuracy: 0.9687 Epoch 2/10 1688/1688 [==============================] - 7s 4ms/step - loss: 0.1221 - accuracy: 0.9651 - val_loss: 0.0861 - val_accuracy: 0.9758 Epoch 3/10 1688/1688 [==============================] - 7s 4ms/step - loss: 0.0897 - accuracy: 0.9741 - val_loss: 0.0710 - val_accuracy: 0.9802 Epoch 4/10 1688/1688 [==============================] - 7s 4ms/step - loss: 0.0727 - accuracy: 0.9787 - val_loss: 0.0719 - val_accuracy: 0.9803 Epoch 5/10 1688/1688 [==============================] - 7s 4ms/step - loss: 0.0631 - accuracy: 0.9808 - val_loss: 0.0657 - val_accuracy: 0.9822 Epoch 6/10 1688/1688 [==============================] - 7s 4ms/step - loss: 0.0554 - accuracy: 0.9833 - val_loss: 0.0601 - val_accuracy: 0.9820 Epoch 7/10 1688/1688 [==============================] - 7s 4ms/step - loss: 0.0489 - accuracy: 0.9855 - val_loss: 0.0647 - val_accuracy: 0.9805 Epoch 8/10 1688/1688 [==============================] - 7s 4ms/step - loss: 0.0442 - accuracy: 0.9869 - val_loss: 0.0575 - val_accuracy: 0.9845 Epoch 9/10 1688/1688 [==============================] - 7s 4ms/step - loss: 0.0403 - accuracy: 0.9875 - val_loss: 0.0596 - val_accuracy: 0.9820 Epoch 10/10 1688/1688 [==============================] - 7s 4ms/step - loss: 0.0362 - accuracy: 0.9888 - val_loss: 0.0588 - val_accuracy: 0.9833 <tensorflow.python.keras.callbacks.History at 0x7f0e6f780a58>
Đánh giá mô hình cơ sở và lưu nó để sử dụng sau này
_, baseline_model_accuracy = model.evaluate(
test_images, test_labels, verbose=0)
print('Baseline test accuracy:', baseline_model_accuracy)
_, keras_file = tempfile.mkstemp('.h5')
print('Saving model to: ', keras_file)
tf.keras.models.save_model(model, keras_file, include_optimizer=False)
Baseline test accuracy: 0.9785000085830688 Saving model to: /tmp/tmpjo5b6jen.h5
Tinh chỉnh mô hình được đào tạo trước bằng tính năng phân cụm
Áp dụng API cluster_weights()
cho toàn bộ mô hình được đào tạo trước để chứng minh hiệu quả của nó trong việc giảm kích thước mô hình sau khi áp dụng zip trong khi vẫn giữ độ chính xác tốt. Để biết cách tốt nhất để cân bằng độ chính xác và tốc độ nén cho trường hợp sử dụng của bạn, vui lòng tham khảo ví dụ về mỗi lớp trong hướng dẫn toàn diện .
Xác định mô hình và áp dụng API phân cụm
Trước khi bạn chuyển mô hình tới API phân cụm, hãy đảm bảo rằng nó đã được đào tạo và hiển thị một số độ chính xác có thể chấp nhận được.
import tensorflow_model_optimization as tfmot
cluster_weights = tfmot.clustering.keras.cluster_weights
CentroidInitialization = tfmot.clustering.keras.CentroidInitialization
clustering_params = {
'number_of_clusters': 16,
'cluster_centroids_init': CentroidInitialization.LINEAR
}
# Cluster a whole model
clustered_model = cluster_weights(model, **clustering_params)
# Use smaller learning rate for fine-tuning clustered model
opt = tf.keras.optimizers.Adam(learning_rate=1e-5)
clustered_model.compile(
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
optimizer=opt,
metrics=['accuracy'])
clustered_model.summary()
Model: "sequential" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= cluster_reshape (ClusterWeig (None, 28, 28, 1) 0 _________________________________________________________________ cluster_conv2d (ClusterWeigh (None, 26, 26, 12) 136 _________________________________________________________________ cluster_max_pooling2d (Clust (None, 13, 13, 12) 0 _________________________________________________________________ cluster_flatten (ClusterWeig (None, 2028) 0 _________________________________________________________________ cluster_dense (ClusterWeight (None, 10) 20306 ================================================================= Total params: 20,442 Trainable params: 54 Non-trainable params: 20,388 _________________________________________________________________
Tinh chỉnh mô hình và đánh giá độ chính xác so với đường cơ sở
Tinh chỉnh mô hình bằng cách phân cụm trong 1 kỷ nguyên.
# Fine-tune model
clustered_model.fit(
train_images,
train_labels,
batch_size=500,
epochs=1,
validation_split=0.1)
108/108 [==============================] - 2s 16ms/step - loss: 0.0453 - accuracy: 0.9851 - val_loss: 0.0699 - val_accuracy: 0.9802 <tensorflow.python.keras.callbacks.History at 0x7f0e543ffeb8>
Đối với ví dụ này, độ chính xác của thử nghiệm sau khi phân nhóm bị giảm tối thiểu so với đường cơ sở.
_, clustered_model_accuracy = clustered_model.evaluate(
test_images, test_labels, verbose=0)
print('Baseline test accuracy:', baseline_model_accuracy)
print('Clustered test accuracy:', clustered_model_accuracy)
Baseline test accuracy: 0.9785000085830688 Clustered test accuracy: 0.9746000170707703
Tạo các mô hình nhỏ hơn 6 lần từ phân cụm
Cả strip_clustering
và việc áp dụng một thuật toán nén tiêu chuẩn (ví dụ: qua gzip) đều cần thiết để thấy được lợi ích của việc nén phân cụm.
Đầu tiên, tạo một mô hình có thể nén cho TensorFlow. Ở đây, strip_clustering
loại bỏ tất cả các biến (ví dụ: tf.Variable
để lưu trữ các trung tâm cụm và các chỉ số) mà phân cụm chỉ cần trong quá trình đào tạo, điều này sẽ thêm vào kích thước mô hình trong quá trình suy luận.
final_model = tfmot.clustering.keras.strip_clustering(clustered_model)
_, clustered_keras_file = tempfile.mkstemp('.h5')
print('Saving clustered model to: ', clustered_keras_file)
tf.keras.models.save_model(final_model, clustered_keras_file,
include_optimizer=False)
Saving clustered model to: /tmp/tmpo83fpb0m.h5
Sau đó, tạo các mô hình có thể nén cho TFLite. Bạn có thể chuyển đổi mô hình được phân nhóm sang định dạng có thể chạy được trên chương trình phụ trợ được nhắm mục tiêu của mình. TensorFlow Lite là một ví dụ bạn có thể sử dụng để triển khai cho các thiết bị di động.
clustered_tflite_file = '/tmp/clustered_mnist.tflite'
converter = tf.lite.TFLiteConverter.from_keras_model(final_model)
tflite_clustered_model = converter.convert()
with open(clustered_tflite_file, 'wb') as f:
f.write(tflite_clustered_model)
print('Saved clustered TFLite model to:', clustered_tflite_file)
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.6/site-packages/tensorflow/python/training/tracking/tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version. Instructions for updating: This property should not be used in TensorFlow 2.0, as updates are applied automatically. WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.6/site-packages/tensorflow/python/training/tracking/tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version. Instructions for updating: This property should not be used in TensorFlow 2.0, as updates are applied automatically. INFO:tensorflow:Assets written to: /tmp/tmp4gcxcvlh/assets Saved clustered TFLite model to: /tmp/clustered_mnist.tflite
Xác định một hàm trợ giúp để thực sự nén các mô hình thông qua gzip và đo kích thước đã nén.
def get_gzipped_model_size(file):
# It returns the size of the gzipped model in bytes.
import os
import zipfile
_, zipped_file = tempfile.mkstemp('.zip')
with zipfile.ZipFile(zipped_file, 'w', compression=zipfile.ZIP_DEFLATED) as f:
f.write(file)
return os.path.getsize(zipped_file)
So sánh và thấy rằng các mô hình nhỏ hơn 6 lần so với phân nhóm
print("Size of gzipped baseline Keras model: %.2f bytes" % (get_gzipped_model_size(keras_file)))
print("Size of gzipped clustered Keras model: %.2f bytes" % (get_gzipped_model_size(clustered_keras_file)))
print("Size of gzipped clustered TFlite model: %.2f bytes" % (get_gzipped_model_size(clustered_tflite_file)))
Size of gzipped baseline Keras model: 78047.00 bytes Size of gzipped clustered Keras model: 12524.00 bytes Size of gzipped clustered TFlite model: 12141.00 bytes
Tạo mô hình TFLite nhỏ hơn 8x từ việc kết hợp phân cụm trọng lượng và lượng tử hóa sau đào tạo
Bạn có thể áp dụng lượng tử hóa sau đào tạo cho mô hình nhóm để có thêm lợi ích.
converter = tf.lite.TFLiteConverter.from_keras_model(final_model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
tflite_quant_model = converter.convert()
_, quantized_and_clustered_tflite_file = tempfile.mkstemp('.tflite')
with open(quantized_and_clustered_tflite_file, 'wb') as f:
f.write(tflite_quant_model)
print('Saved quantized and clustered TFLite model to:', quantized_and_clustered_tflite_file)
print("Size of gzipped baseline Keras model: %.2f bytes" % (get_gzipped_model_size(keras_file)))
print("Size of gzipped clustered and quantized TFlite model: %.2f bytes" % (get_gzipped_model_size(quantized_and_clustered_tflite_file)))
INFO:tensorflow:Assets written to: /tmp/tmpt2flzp4s/assets INFO:tensorflow:Assets written to: /tmp/tmpt2flzp4s/assets Saved quantized and clustered TFLite model to: /tmp/tmpgu3loy72.tflite Size of gzipped baseline Keras model: 78047.00 bytes Size of gzipped clustered and quantized TFlite model: 9240.00 bytes
Xem độ chính xác lâu dài từ TF sang TFLite
Xác định chức năng trợ giúp để đánh giá mô hình TFLite trên tập dữ liệu thử nghiệm.
def eval_model(interpreter):
input_index = interpreter.get_input_details()[0]["index"]
output_index = interpreter.get_output_details()[0]["index"]
# Run predictions on every image in the "test" dataset.
prediction_digits = []
for i, test_image in enumerate(test_images):
if i % 1000 == 0:
print('Evaluated on {n} results so far.'.format(n=i))
# Pre-processing: add batch dimension and convert to float32 to match with
# the model's input data format.
test_image = np.expand_dims(test_image, axis=0).astype(np.float32)
interpreter.set_tensor(input_index, test_image)
# Run inference.
interpreter.invoke()
# Post-processing: remove batch dimension and find the digit with highest
# probability.
output = interpreter.tensor(output_index)
digit = np.argmax(output()[0])
prediction_digits.append(digit)
print('\n')
# Compare prediction results with ground truth labels to calculate accuracy.
prediction_digits = np.array(prediction_digits)
accuracy = (prediction_digits == test_labels).mean()
return accuracy
Bạn đánh giá mô hình, đã được nhóm và lượng tử hóa, và sau đó xem độ chính xác từ TensorFlow vẫn tồn tại đối với phần phụ trợ TFLite.
interpreter = tf.lite.Interpreter(model_content=tflite_quant_model)
interpreter.allocate_tensors()
test_accuracy = eval_model(interpreter)
print('Clustered and quantized TFLite test_accuracy:', test_accuracy)
print('Clustered TF test accuracy:', clustered_model_accuracy)
Evaluated on 0 results so far. Evaluated on 1000 results so far. Evaluated on 2000 results so far. Evaluated on 3000 results so far. Evaluated on 4000 results so far. Evaluated on 5000 results so far. Evaluated on 6000 results so far. Evaluated on 7000 results so far. Evaluated on 8000 results so far. Evaluated on 9000 results so far. Clustered and quantized TFLite test_accuracy: 0.9746 Clustered TF test accuracy: 0.9746000170707703
Phần kết luận
Trong hướng dẫn này, bạn đã biết cách tạo các mô hình theo nhóm với API Bộ công cụ tối ưu hóa mô hình TensorFlow. Cụ thể hơn, bạn đã xem qua một ví dụ end-to-end để tạo một mô hình nhỏ hơn 8x cho MNIST với sự khác biệt về độ chính xác tối thiểu. Chúng tôi khuyến khích bạn thử khả năng mới này, khả năng này có thể đặc biệt quan trọng để triển khai trong các môi trường hạn chế về tài nguyên.