Bantuan melindungi Great Barrier Reef dengan TensorFlow pada Kaggle Bergabung Tantangan

Pemangkasan dalam contoh Keras

Lihat di TensorFlow.org Jalankan di Google Colab Lihat sumber di GitHub Unduh buku catatan

Gambaran

Selamat Datang contoh end-to-end untuk berbasis besarnya pemangkasan berat.

halaman lain

Untuk pengenalan apa pemangkasan adalah dan untuk menentukan apakah Anda harus menggunakannya (termasuk apa yang didukung), lihat halaman ikhtisar .

Untuk cepat menemukan API yang Anda butuhkan untuk kasus penggunaan (di luar sepenuhnya pemangkasan model dengan 80% sparsity), lihat panduan yang komprehensif .

Ringkasan

Dalam tutorial ini, Anda akan:

  1. Melatih tf.keras model untuk MNIST dari awal.
  2. Sempurnakan model dengan menerapkan API pemangkasan dan lihat akurasinya.
  3. Buat model TF dan TFLite 3x lebih kecil dari pemangkasan.
  4. Buat model TFLite 10x lebih kecil dari penggabungan kuantisasi pemangkasan dan pasca-pelatihan.
  5. Lihat kegigihan akurasi dari TF ke TFLite.

Mempersiapkan

 pip install -q tensorflow-model-optimization
import tempfile
import os

import tensorflow as tf
import numpy as np

from tensorflow import keras

%load_ext tensorboard

Latih model untuk MNIST tanpa pemangkasan

# 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 and 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='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,
  epochs=4,
  validation_split=0.1,
)
Downloading data from https://storage.googleapis.com/tensorflow/tf-keras-datasets/mnist.npz
11493376/11490434 [==============================] - 0s 0us/step
Epoch 1/4
1688/1688 [==============================] - 10s 6ms/step - loss: 0.2785 - accuracy: 0.9220 - val_loss: 0.1031 - val_accuracy: 0.9740
Epoch 2/4
1688/1688 [==============================] - 9s 5ms/step - loss: 0.1063 - accuracy: 0.9691 - val_loss: 0.0782 - val_accuracy: 0.9790
Epoch 3/4
1688/1688 [==============================] - 9s 5ms/step - loss: 0.0815 - accuracy: 0.9765 - val_loss: 0.0788 - val_accuracy: 0.9775
Epoch 4/4
1688/1688 [==============================] - 9s 5ms/step - loss: 0.0689 - accuracy: 0.9797 - val_loss: 0.0633 - val_accuracy: 0.9840
<tensorflow.python.keras.callbacks.History at 0x7f146fbd8bd0>

Evaluasi akurasi tes dasar dan simpan model untuk penggunaan nanti.

_, baseline_model_accuracy = model.evaluate(
    test_images, test_labels, verbose=0)

print('Baseline test accuracy:', baseline_model_accuracy)

_, keras_file = tempfile.mkstemp('.h5')
tf.keras.models.save_model(model, keras_file, include_optimizer=False)
print('Saved baseline model to:', keras_file)
Baseline test accuracy: 0.9775999784469604
Saved baseline model to: /tmp/tmpjj6swf59.h5

Sempurnakan model pra-terlatih dengan pemangkasan

Tentukan modelnya

Anda akan menerapkan pemangkasan ke seluruh model dan melihatnya di ringkasan model.

Dalam contoh ini, Anda memulai model dengan 50% sparsity (50% bobot nol) dan diakhiri dengan 80% sparsity.

Dalam panduan yang komprehensif , Anda dapat melihat bagaimana untuk memangkas beberapa lapisan untuk perbaikan akurasi model.

import tensorflow_model_optimization as tfmot

prune_low_magnitude = tfmot.sparsity.keras.prune_low_magnitude

# Compute end step to finish pruning after 2 epochs.
batch_size = 128
epochs = 2
validation_split = 0.1 # 10% of training set will be used for validation set. 

num_images = train_images.shape[0] * (1 - validation_split)
end_step = np.ceil(num_images / batch_size).astype(np.int32) * epochs

# Define model for pruning.
pruning_params = {
      'pruning_schedule': tfmot.sparsity.keras.PolynomialDecay(initial_sparsity=0.50,
                                                               final_sparsity=0.80,
                                                               begin_step=0,
                                                               end_step=end_step)
}

model_for_pruning = prune_low_magnitude(model, **pruning_params)

# `prune_low_magnitude` requires a recompile.
model_for_pruning.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

model_for_pruning.summary()
/tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/keras/engine/base_layer.py:2191: UserWarning: `layer.add_variable` is deprecated and will be removed in a future version. Please use `layer.add_weight` method instead.
  warnings.warn('`layer.add_variable` is deprecated and '
Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
prune_low_magnitude_reshape  (None, 28, 28, 1)         1         
_________________________________________________________________
prune_low_magnitude_conv2d ( (None, 26, 26, 12)        230       
_________________________________________________________________
prune_low_magnitude_max_pool (None, 13, 13, 12)        1         
_________________________________________________________________
prune_low_magnitude_flatten  (None, 2028)              1         
_________________________________________________________________
prune_low_magnitude_dense (P (None, 10)                40572     
=================================================================
Total params: 40,805
Trainable params: 20,410
Non-trainable params: 20,395
_________________________________________________________________

Latih dan evaluasi model terhadap baseline

Sempurnakan dengan pemangkasan untuk dua zaman.

tfmot.sparsity.keras.UpdatePruningStep diperlukan selama pelatihan, dan tfmot.sparsity.keras.PruningSummaries menyediakan log untuk melacak kemajuan dan debugging.

logdir = tempfile.mkdtemp()

callbacks = [
  tfmot.sparsity.keras.UpdatePruningStep(),
  tfmot.sparsity.keras.PruningSummaries(log_dir=logdir),
]

model_for_pruning.fit(train_images, train_labels,
                  batch_size=batch_size, epochs=epochs, validation_split=validation_split,
                  callbacks=callbacks)
Epoch 1/2
WARNING:tensorflow:From /tmpfs/src/tf_docs_env/lib/python3.7/site-packages/tensorflow/python/ops/array_ops.py:5049: calling gather (from tensorflow.python.ops.array_ops) with validate_indices is deprecated and will be removed in a future version.
Instructions for updating:
The `validate_indices` argument has no effect. Indices are always validated on CPU and never validated on GPU.
  3/422 [..............................] - ETA: 12s - loss: 0.0628 - accuracy: 0.9896  WARNING:tensorflow:Callback method `on_train_batch_end` is slow compared to the batch time (batch time: 0.0075s vs `on_train_batch_end` time: 0.0076s). Check your callbacks.
422/422 [==============================] - 5s 9ms/step - loss: 0.0797 - accuracy: 0.9771 - val_loss: 0.0828 - val_accuracy: 0.9790
Epoch 2/2
422/422 [==============================] - 3s 8ms/step - loss: 0.0971 - accuracy: 0.9741 - val_loss: 0.0839 - val_accuracy: 0.9775
<tensorflow.python.keras.callbacks.History at 0x7f12e4502910>

Untuk contoh ini, ada kerugian minimal dalam akurasi pengujian setelah pemangkasan, dibandingkan dengan baseline.

_, model_for_pruning_accuracy = model_for_pruning.evaluate(
   test_images, test_labels, verbose=0)

print('Baseline test accuracy:', baseline_model_accuracy) 
print('Pruned test accuracy:', model_for_pruning_accuracy)
Baseline test accuracy: 0.9775999784469604
Pruned test accuracy: 0.972100019454956

Log menunjukkan perkembangan sparity pada basis per-lapisan.

%tensorboard --logdir={logdir}

Untuk pengguna non-CoLab, Anda dapat melihat hasil sebelumnya menjalankan blok kode ini di TensorBoard.dev .

Buat model 3x lebih kecil dari pemangkasan

Kedua tfmot.sparsity.keras.strip_pruning dan menerapkan algoritma kompresi standar (misalnya melalui gzip) yang diperlukan untuk melihat manfaat kompresi pemangkasan.

  • strip_pruning diperlukan karena menghilangkan setiap tf.Variable bahwa pemangkasan hanya membutuhkan selama pelatihan, yang tidak akan menambah ukuran model yang selama inferensi
  • Menerapkan algoritma kompresi standar diperlukan karena matriks bobot serial memiliki ukuran yang sama seperti sebelum pemangkasan. Namun, pemangkasan membuat sebagian besar bobot menjadi nol, yang menambahkan redundansi yang dapat digunakan algoritme untuk mengompresi model lebih lanjut.

Pertama, buat model kompresibel untuk TensorFlow.

model_for_export = tfmot.sparsity.keras.strip_pruning(model_for_pruning)

_, pruned_keras_file = tempfile.mkstemp('.h5')
tf.keras.models.save_model(model_for_export, pruned_keras_file, include_optimizer=False)
print('Saved pruned Keras model to:', pruned_keras_file)
WARNING:tensorflow:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.
Saved pruned Keras model to: /tmp/tmp22u333hk.h5

Kemudian, buat model kompresibel untuk TFLite.

converter = tf.lite.TFLiteConverter.from_keras_model(model_for_export)
pruned_tflite_model = converter.convert()

_, pruned_tflite_file = tempfile.mkstemp('.tflite')

with open(pruned_tflite_file, 'wb') as f:
  f.write(pruned_tflite_model)

print('Saved pruned TFLite model to:', pruned_tflite_file)
WARNING:tensorflow:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.
INFO:tensorflow:Assets written to: /tmp/tmp51falze0/assets
Saved pruned TFLite model to: /tmp/tmpehx5la2i.tflite

Tentukan fungsi pembantu untuk benar-benar mengompresi model melalui gzip dan mengukur ukuran zip.

def get_gzipped_model_size(file):
  # Returns size of 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)

Bandingkan dan lihat bahwa modelnya 3x lebih kecil dari pemangkasan.

print("Size of gzipped baseline Keras model: %.2f bytes" % (get_gzipped_model_size(keras_file)))
print("Size of gzipped pruned Keras model: %.2f bytes" % (get_gzipped_model_size(pruned_keras_file)))
print("Size of gzipped pruned TFlite model: %.2f bytes" % (get_gzipped_model_size(pruned_tflite_file)))
Size of gzipped baseline Keras model: 78211.00 bytes
Size of gzipped pruned Keras model: 25797.00 bytes
Size of gzipped pruned TFlite model: 24995.00 bytes

Buat model 10x lebih kecil dari menggabungkan pemangkasan dan kuantisasi

Anda dapat menerapkan kuantisasi pasca-pelatihan ke model yang dipangkas untuk manfaat tambahan.

converter = tf.lite.TFLiteConverter.from_keras_model(model_for_export)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
quantized_and_pruned_tflite_model = converter.convert()

_, quantized_and_pruned_tflite_file = tempfile.mkstemp('.tflite')

with open(quantized_and_pruned_tflite_file, 'wb') as f:
  f.write(quantized_and_pruned_tflite_model)

print('Saved quantized and pruned TFLite model to:', quantized_and_pruned_tflite_file)

print("Size of gzipped baseline Keras model: %.2f bytes" % (get_gzipped_model_size(keras_file)))
print("Size of gzipped pruned and quantized TFlite model: %.2f bytes" % (get_gzipped_model_size(quantized_and_pruned_tflite_file)))
WARNING:tensorflow:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.
WARNING:tensorflow:Compiled the loaded model, but the compiled metrics have yet to be built. `model.compile_metrics` will be empty until you train or evaluate the model.
INFO:tensorflow:Assets written to: /tmp/tmp6tzu3z87/assets
INFO:tensorflow:Assets written to: /tmp/tmp6tzu3z87/assets
Saved quantized and pruned TFLite model to: /tmp/tmp0mvlkin1.tflite
Size of gzipped baseline Keras model: 78211.00 bytes
Size of gzipped pruned and quantized TFlite model: 8031.00 bytes

Lihat ketekunan akurasi dari TF ke TFLite

Tentukan fungsi pembantu untuk mengevaluasi model TF Lite pada kumpulan data uji.

import numpy as np

def evaluate_model(interpreter):
  input_index = interpreter.get_input_details()[0]["index"]
  output_index = interpreter.get_output_details()[0]["index"]

  # Run predictions on ever y 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

Anda mengevaluasi model yang dipangkas dan dikuantisasi dan melihat bahwa akurasi dari TensorFlow tetap ada hingga backend TFLite.

interpreter = tf.lite.Interpreter(model_content=quantized_and_pruned_tflite_model)
interpreter.allocate_tensors()

test_accuracy = evaluate_model(interpreter)

print('Pruned and quantized TFLite test_accuracy:', test_accuracy)
print('Pruned TF test accuracy:', model_for_pruning_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.


Pruned and quantized TFLite test_accuracy: 0.9722
Pruned TF test accuracy: 0.972100019454956

Kesimpulan

Dalam tutorial ini, Anda melihat cara membuat model sparse dengan TensorFlow Model Optimization Toolkit API untuk TensorFlow dan TFLite. Anda kemudian menggabungkan pemangkasan dengan kuantisasi pasca-pelatihan untuk manfaat tambahan.

Anda membuat model 10x lebih kecil untuk MNIST, dengan perbedaan akurasi minimal.

Kami mendorong Anda untuk mencoba kemampuan baru ini, yang dapat menjadi sangat penting untuk penerapan di lingkungan dengan sumber daya terbatas.