Open1

適当TFLite

PINTOPINTO
import tensorflow as tf
import os
from pprint import pprint
import numpy as np
np.random.seed(0)
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'

# dummy_input = np.arange(1, 101).reshape(1, 10, 10, 1).astype(np.float32)
# print(dummy_input)

testonly = False


if not testonly:

    # Create a model
    # i = tf.keras.layers.Input(
    #     shape=[
    #         dummy_input.shape[1],
    #         dummy_input.shape[2],
    #         dummy_input.shape[3],
    #     ],
    #     batch_size=dummy_input.shape[0]
    # )
    i = tf.keras.layers.Input(
        shape=[
            1,
        ],
        batch_size=1
    )

    # # Define some parameters
    # patch_size = 3 # height/width of the square image patch size

    # # Generate example input
    # x_original = i #tf.cast(tf.reshape(tf.range(np.prod(i.shape)), i.shape), dtype=tf.float32)

    # x_shape = x_original.shape # Save the original shape
    # x = tf.transpose(x_original, perm=[0, 3, 1, 2]) # Move feature map dimension next to the batch dimension
    # x = tf.expand_dims(x, -1) # Add extra channel at the end

    # # Create an identity kernel
    # kernel = tf.reshape(tf.eye(patch_size**2), [patch_size, patch_size, 1, patch_size**2]) # [filter_height, filter_width, in_channels, out_channels]

    # # Convolve with identity kernel
    # patches_simulation = tf.nn.conv2d(x, kernel, strides=[1, 1, 1, 1], padding='VALID')
    # patches_simulation = tf.transpose(patches_simulation, perm=[0 ,2 ,3, 4, 1]) # Move filter dim to last
    # patches_simulation_shape = patches_simulation.shape
    # patches_simulation = tf.reshape(patches_simulation, [patches_simulation_shape[0], patches_simulation_shape[1], patches_simulation_shape[2], -1]) # Merge last two dims into one

    # # Intended output to compare against
    # patches = tf.image.extract_patches(x_original, sizes=[1, patch_size, patch_size, 1], strides=[1, 1, 1, 1], rates=[1, 1, 1, 1], padding='VALID')

    # print(f'tf.image.extract_patches shape {patches.shape} simulation shape {patches_simulation.shape} same shape: {patches.shape == patches_simulation.shape}')
    # # print(f'Simulation is correct? {tf.reduce_all(tf.math.equal(patches, patches_simulation)).numpy()}')


    a = tf.math.abs(i)

    model = tf.keras.models.Model(inputs=i, outputs=a)
    model.summary()
    output_path = 'saved_model'
    tf.saved_model.save(model, output_path)

    converter = tf.lite.TFLiteConverter.from_keras_model(model)
    converter.target_spec.supported_ops = [
        tf.lite.OpsSet.TFLITE_BUILTINS,
        tf.lite.OpsSet.SELECT_TF_OPS
    ]
    tflite_model = converter.convert()
    open(f"{output_path}/test.tflite", "wb").write(tflite_model)

    def representative_dataset_gen():
        yield [np.asarray([1], dtype=np.float32)]

    converter = tf.lite.TFLiteConverter.from_keras_model(model)
    converter.optimizations = [tf.lite.Optimize.DEFAULT]
    converter.target_spec.supported_ops = [
        tf.lite.OpsSet.TFLITE_BUILTINS_INT8,
        tf.lite.OpsSet.SELECT_TF_OPS
    ]
    converter.inference_input_type = tf.uint8
    converter.inference_output_type = tf.uint8
    converter.representative_dataset = representative_dataset_gen
    tflite_model = converter.convert()
    open(f"{output_path}/test_int8.tflite", "wb").write(tflite_model)

# # Float32
# interpreter = tf.lite.Interpreter('saved_model/test.tflite')
# interpreter.allocate_tensors()
# input_details = interpreter.get_input_details()
# output_details = interpreter.get_output_details()
# interpreter.set_tensor(input_details[0]['index'], dummy_input)
# interpreter.invoke()
# ret = interpreter.get_tensor(output_details[0]['index'])
# print('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ Float32')
# pprint(ret)

# # INT8
# interpreter = tf.lite.Interpreter('saved_model/test_int8.tflite')
# interpreter.allocate_tensors()
# input_details = interpreter.get_input_details()
# output_details = interpreter.get_output_details()
# interpreter.set_tensor(input_details[0]['index'], dummy_input)
# interpreter.invoke()
# ret = interpreter.get_tensor(output_details[0]['index'])
# print('@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ INT8')
# pprint(ret)

"""
docker run -it --rm \
-v `pwd`:/home/user/workdir \
ghcr.io/pinto0309/openvino2tensorflow:latest

$INTEL_OPENVINO_DIR/deployment_tools/model_optimizer/mo_tf.py \
--saved_model_dir saved_model \
--output_dir saved_model/openvino/FP32 \
--data_type FP32

openvino2tensorflow \
--model_path saved_model/openvino/FP32/saved_model.xml \
--output_saved_model \
--output_pb \
--output_integer_quant_typ 'uint8' \
--string_formulas_for_normalization 'data / 255' \
--output_no_quant_float32_tflite \
--output_edgetpu \
--non_verbose

tflite2tensorflow \
--model_path test.tflite \
--flatc_path ../flatc \
--schema_path ../schema.fbs \
--output_pb \
--optimizing_for_openvino_and_myriad \
--rigorous_optimization_for_myriad

../flatc -o . -b ../schema.fbs test.json

rm test.json

tflite2tensorflow \
--model_path test.tflite \
--flatc_path ../flatc \
--schema_path ../schema.fbs \
--output_pb \
--optimizing_for_openvino_and_myriad \
--rigorous_optimization_for_myriad

tflite2tensorflow \
--model_path test.tflite \
--flatc_path ../flatc \
--schema_path ../schema.fbs \
--output_no_quant_float32_tflite \
--output_openvino_and_myriad


openvino2tensorflow \
--model_path saved_model_lite0/openvino/FP32/efficientdet_lite0.xml \
--output_saved_model \
--output_pb \
--output_no_quant_float32_tflite \
--output_myriad \
--non_verbose


tflite2tensorflow \
--model_path model_float32.tflite \
--flatc_path ../flatc \
--schema_path ../schema.fbs \
--output_pb \
--optimizing_for_openvino_and_myriad \
--rigorous_optimization_for_myriad

tflite2tensorflow \
--model_path model_float32.tflite \
--flatc_path ../flatc \
--schema_path ../schema.fbs \
--output_no_quant_float32_tflite \
--output_openvino_and_myriad
"""