VeriSilicon / TIM-VX

VeriSilicon Tensor Interface Module
Other
221 stars 84 forks source link

Strange error on step of model validation #183

Closed kventinel closed 2 years ago

kventinel commented 3 years ago

I have model, for example:

model = tf.keras.Sequential()
model.add(tf.keras.layers.Conv1D(256, 8, padding='same'))
model.add(tf.keras.layers.ReLU())
model.add(tf.keras.layers.Conv1D(256, 8, padding='same'))
model.add(tf.keras.layers.ReLU())
model.add(tf.keras.layers.Conv1D(256, 8, padding='same'))
model.add(tf.keras.layers.ReLU())
model.add(tf.keras.layers.Conv1D(256, 8, padding='same'))
model.add(tf.keras.layers.ReLU())
model.add(tf.keras.layers.Conv1D(256, 8, padding='same'))
model.add(tf.keras.layers.ReLU())
model.add(tf.keras.layers.Conv1D(256, 8, padding='same'))
model.add(tf.keras.layers.ReLU())
model.add(tf.keras.layers.Conv1D(64, 1))
model.add(tf.keras.layers.ReLU())
model.add(tf.keras.layers.Flatten())
model.add(tf.keras.layers.Dense(512))
model.add(tf.keras.layers.ReLU())
model.add(tf.keras.layers.Dense(512))
model.add(tf.keras.layers.ReLU())
model.add(tf.keras.layers.Dense(1))

input_shape = (1, 256, 40)
x = tf.random.normal(input_shape)
y = model(x)

But on device with A311D it's return errors, like that: input_scale[0.000001000000] * weight_scale[0.000381198333] != bias_scale[0.000001000000]

Without dense layer on the end it's work good.

Can you help me solve this error please?

Code for quantization:

def representative_dataset():
    for _ in range(100):
        data = np.random.rand(1, 256, 40)
        yield [data.astype(np.float32)]

COOL_TFLITE_PATH = 'new_model/cool_tn.tflite'
converter = tf.lite.TFLiteConverter.from_saved_model(NEW_MODEL_PATH)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = representative_dataset
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
converter.experimental_new_converter = True
converter._experimental_disable_per_channel = True
converter.target_spec.supported_types = [tf.int8]
converter.inference_input_type = tf.int8 
converter.inference_output_type = tf.int8 
quantized_tflite_model = converter.convert()
with open(COOL_TFLITE_PATH, 'wb') as fout:
    fout.write(quantized_tflite_model)
sunshinemyson commented 2 years ago

input_scale[0.000001000000] * weight_scale[0.000381198333] != bias_scale[0.000001000000] we have this check because in the original paper from google about quantization, scale of bias have such restriction.

My suggestion is you can check tflite quantization tool if there is some bug fix about this.

kventinel commented 2 years ago

<[My suggestion is you can check tflite quantization tool if there is some bug fix about this.]>

But this is default tensorflow converter. May be you now some parameters of tf converter, that's can pass this checks?

sunshinemyson commented 2 years ago

@kventinel ,

Sorry, I don't know the answer, You can create an issue in TensorFlow, that will be better because you have totally wrong scale for bias.