[go: nahoru, domu]

Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DSP Overflow - high pixel values are being clamped when running on DSP #53552

Open
aviaisr opened this issue Dec 27, 2021 · 2 comments
Open

DSP Overflow - high pixel values are being clamped when running on DSP #53552

aviaisr opened this issue Dec 27, 2021 · 2 comments
Assignees
Labels
stat:awaiting tensorflower Status - Awaiting response from tensorflower TF 2.7 Issues related to TF 2.7.0 TFLiteConverter For issues related to TFLite converter type:support Support issues

Comments

@aviaisr
Copy link
aviaisr commented Dec 27, 2021

Hi,
I trained a keras model to extract gray-level segmentation maps.
I converted the model to TFLite and quantized the model.
The quantized model produces similar results on CPU and DSP HWs, if the values of the pixels are not very high (<<1).
If the maps produce pixel predictions with high values that are closer to 1, it seems that the values are being clamped when running on DSP, unlike in CPU which produces reasonable maps.

I repeated the tests on versions 2.2, 2.4, and 2.7 and all demonstrated the same results.

What can I do to change this?
Should I change the dynamic range of the maps?

@aviaisr aviaisr added the TFLiteConverter For issues related to TFLite converter label Dec 27, 2021
@tilakrayal tilakrayal added TF 2.7 Issues related to TF 2.7.0 type:performance Performance Issue labels Dec 28, 2021
@tilakrayal
Copy link
Contributor

@aviaisr ,
In order to expedite the trouble-shooting process, could you please provide the complete code and dataset to reproduce the issue reported here.

@tilakrayal tilakrayal added the stat:awaiting response Status - Awaiting response from author label Dec 28, 2021
@aviaisr
Copy link
Author
aviaisr commented Dec 28, 2021
  1. I saved the kears model + tflite file + quantized tflite file here: https://drive.google.com/drive/folders/1ZIsPCiBPCzDmFcTbVAokjM7CtzLJO8y7?usp=sharing

  2. Conversion code
    _# Quntization script

read an hdf5 keras model and convert to tflite with quantization

import tensorflow as tf
import numpy as np
import cv2
import sys
import os

def rep_data_gen0():
a = []
for i in range(128):
img = 5*np.fromfile('norm_images/image_'+str(i)+".bin", dtype=np.float32)
img = img.reshape(224, 224, 3)
a.append(img)

a = np.array(a)
print(a.shape)
img = tf.data.Dataset.from_tensor_slices(a).batch(1)
for i in img.take(128):
    yield [i]

def convert_and_quantize(model):
converter = tf.lite.TFLiteConverter.from_keras_model(model)
converter.optimizations = [tf.lite.Optimize.DEFAULT]
converter.representative_dataset = rep_data_gen0
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS_INT8]

print("Converting to TfLite using full integer quantization")
quant_model = converter.convert()
return quant_model

def main_func():
print (f'Reading keras model from {sys.argv[1]}')
keras_model = tf.keras.models.load_model(sys.argv[1], compile = False)
input_name = keras_model.input_names[0]
index = keras_model.input_names.index(input_name)
keras_model.inputs[index].set_shape([1, 224, 224, 3])
keras_model.trainable = False
quant_model = convert_and_quantize(keras_model)

print (f'Writing quantized tflite model to {sys.argv[2]}')
f = open(sys.argv[2],'wb')
f.write(quant_model)
f.close()

if name == "main":
print(tf.version)
if len(sys.argv) != 3:
print('Wrong command line arguments')
sys.exit(1)
main_func()
print("Done")_

  1. Example dataset (I train on 5M images, so I just uploaded 400 images as example). My task is a segmentation task and the trained maps are binary maps.
    https://drive.google.com/drive/folders/1Dn0olSmZ9HFGwAIViq1cfl9kfuvC5Qs_?usp=sharing

  2. representative dataset as bins saved in
    https://drive.google.com/drive/folders/1Q0iqM1qHdWFdW_PyA0BEQtzde9dmU7S5?usp=sharing

  3. Main training code details:

_trainDataset = tf.data.Dataset.from_tensor_slices(dataPaths['train']) #same for validation
batchSize = 32

model = Unet('mobilenetv2', input_shape=inputShape, encoder_weights='imagenet', encoder_freeze=False,
decoder_block_type='transpose', decoder_filters=decoderFilters) # Deconvolution

model = utils.set_regularization(model, kernel_regularizer=keras.regularizers.l2(0.001),
bias_regularizer=keras.regularizers.l2(0.001))

lr = keras.callbacks.LearningRateScheduler(scheduler)

loss = losses.binary_focal_dice_loss

checkpoint = keras.callbacks.ModelCheckpoint(weightsPath, monitor='val_loss', verbose=0,
save_best_only=True, save_weights_only=False,
mode='auto', save_freq='epoch')

callbacks_list = [lr, checkpoint, historyLogger]

compile

adam = keras.optimizers.Adam(lr=0.01, beta_1=0.9, beta_2=0.999, epsilon=1e-07, decay=0.0, amsgrad=False)

model.compile(loss=loss, optimizer=adam)

history = model.fit(x=trainDataset,
batch_size=batchSize,
epochs=numEpochs,
verbose=1, # show progress bar
validation_data=valDataset,
initial_epoch=initialEpoch,
steps_per_epoch=stepsPerEpoch,
validation_steps=validationSteps,
callbacks=callbacks_list)_

@tilakrayal tilakrayal added type:support Support issues and removed stat:awaiting response Status - Awaiting response from author type:performance Performance Issue labels Dec 29, 2021
@tilakrayal tilakrayal assigned Saduf2019 and unassigned Saduf2019 and tilakrayal Dec 29, 2021
@Saduf2019 Saduf2019 assigned jvishnuvardhan and unassigned Saduf2019 Jan 3, 2022
@jvishnuvardhan jvishnuvardhan added the stat:awaiting tensorflower Status - Awaiting response from tensorflower label Jan 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stat:awaiting tensorflower Status - Awaiting response from tensorflower TF 2.7 Issues related to TF 2.7.0 TFLiteConverter For issues related to TFLite converter type:support Support issues
Projects
None yet
Development

No branches or pull requests

5 participants