profile
viewpoint

Ask questionsAttributeError: module 'tensorflow._api.v1.lite' has no attribute 'Optimize' version 1.13

following the Post-training quantization guide,

converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
AttributeError: module 'tensorflow._api.v1.lite' has no attribute 'Optimize'

i want to convert the mnist model, full code here

from __future__ import absolute_import, division, print_function
import os
import tensorflow as tf
from tensorflow import keras

tf.__version__

(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()

train_labels = train_labels[:1000]
test_labels = test_labels[:1000]

train_images = train_images[:1000].reshape(-1, 28 * 28) / 255.0
test_images = test_images[:1000].reshape(-1, 28 * 28) / 255.0

# Returns a short sequential model
def create_model():
  model = tf.keras.models.Sequential([
    keras.layers.Dense(512, activation=tf.keras.activations.relu, input_shape=(784,)),
    keras.layers.Dropout(0.2),
    keras.layers.Dense(10, activation=tf.keras.activations.softmax)
  ])
  
  model.compile(optimizer=tf.keras.optimizers.Adam(),
                loss=tf.keras.losses.sparse_categorical_crossentropy,
                metrics=['accuracy'])
  
  return model


# Create a basic model instance
model = create_model()
model.fit(train_images, train_labels, epochs=5)
model.summary()
model.save('my_mnist_v2.h5')

converter = tf.lite.TFLiteConverter.from_keras_model_file("my_mnist_v2.h5")
converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_SIZE]
tflite_quant_model = converter.convert()

tf version 1.13

tensorflow/tensorflow

Answer questions 2h4dl

Thank you @sunejas, I refered to the code you post. Just add one line and make it.

    converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_path)
   
    # added code to convert model to a quantized tflite format.
    converter.post_training_quantize = True

    tflite_model = converter.convert()
    open("converted_model.tflite", "wb").write(tflite_model)
    print("convert model to tflite format done.")
useful!

Related questions

Error occurred when finalizing GeneratorDataset iterator hot 4
The flag 'log_dir' is defined twice. hot 3
AttributeError: module 'tensorflow' has no attribute 'set_random_seed' hot 3
Incorrect Error TypeError: padded_batch() missing 1 required positional argument: 'padded_shapes' hot 3
tensorflow2.0 detected 'xla_gpu' , but 'gpu' expected hot 2
Using tensorflow gpu 2.1 with Cuda 10.2 hot 2
Restoring Keras model fails inside a distribution strategy scope hot 2
tf.strings.split bug hot 2
Cannot run tensorflow GPU 2.0 with cuda 9.2 hot 2
module 'tensorflow_core._api.v2.nn' has no attribute 'rnn_cell' hot 2
Lossy conversion from float32 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning. hot 2
compiling c++ code with TFLite library gives error with cstring hot 2
Tensorflow 2.0 error when used via pyinstaller build - ImportError: cannot import name 'pywrap_tensorflow' from 'tensorflow_core.python' hot 2
ModuleNotFoundError: No module named 'tensorflow.contrib' hot 2
TF2.0 AutoGraph issue hot 2
Github User Rank List