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

ModuleNotFoundError: No module named 'tensorflow.contrib' hot 8
Error occurred when finalizing GeneratorDataset iterator hot 6
ModuleNotFoundError: No module named 'tensorflow.contrib'
When importing TensorFlow, error loading Hadoop
tensorflow-gpu CUPTI errors hot 4
[TF 2.0] tf.keras.optimizers.Adam hot 4
Lossy conversion from float32 to uint8. Range [0, 1]. Convert image to uint8 prior to saving to suppress this warning. hot 4
TF2.0 AutoGraph issue hot 4
Tf.Keras metrics issue hot 4
module 'tensorflow' has no attribute 'ConfigProto' hot 4
TF 2.0 'Tensor' object has no attribute 'numpy' while using .numpy() although eager execution enabled by default hot 4
ModuleNotFoundError: No module named 'tensorflow.examples.tutorials' hot 4
AttributeError: module 'tensorflow.python.framework.op_def_registry' has no attribute 'register_op_list' hot 4
tf.keras.layers.Conv1DTranspose ? hot 4
tensorflow2.0 detected 'xla_gpu' , but 'gpu' expected hot 3
Github User Rank List