Coder Social home page Coder Social logo

Comments (10)

peterjc123 avatar peterjc123 commented on May 9, 2024 1

@Ouskit With 55734c7, you can now use quantize_target_type='uint8', hybrid_quantization_from_float=True, hybrid_per_channel=False to get the desired model.

from tinyneuralnetwork.

peterjc123 avatar peterjc123 commented on May 9, 2024

@Ouskit Below is the code that sets the op version for CONV_2D.
https://github.com/alibaba/TinyNeuralNetwork/blob/main/tinynn/converter/operators/op_version.py#L35-L49
It seems that you are using hybrid quantization, which is actually supported in TF 1.13.2 with op version = 1 as can be seen using the below links.
https://github.com/tensorflow/tensorflow/blob/v1.13.2/tensorflow/lite/kernels/conv.cc#L644
https://github.com/tensorflow/tensorflow/blob/v1.13.2/tensorflow/lite/kernels/register.cc#L170
So you may update the aforementioned logic to adapt the versioning to your usage.

from tinyneuralnetwork.

Ouskit avatar Ouskit commented on May 9, 2024

@peterjc123 Thank your reply, I change [op_version.py#L35-L49](

if op.op.code == ExtendedOperator.CONV_2D:
if (
str(op.inputs[0].dtype) == 'int8'
and str(op.inputs[1].dtype) == 'int8'
and str(op.outputs[0].dtype) == 'int8'
):
op.op.version = 3
elif (
str(op.inputs[0].dtype) == 'float32'
and str(op.inputs[1].dtype) == 'int8'
and str(op.outputs[0].dtype) == 'float32'
):
op.op.version = 2
else:
op.op.version = 1
) all op.version to 1 and inference's result successfully without performing dynamic quantization.

But when I want to do dynamic quantization, pass quantize_target_type='int8', hybrid_quantization_from_float=True, hybrid_per_channel=False to convertr, the tflite inference result is totally wrong in tf 1.13.2. But in tf 1.15.5 result is normal and ok.

Is there anything I don't notice? Thank you.

from tinyneuralnetwork.

peterjc123 avatar peterjc123 commented on May 9, 2024

Please try quantize_target_type='uint8'.

from tinyneuralnetwork.

Ouskit avatar Ouskit commented on May 9, 2024

@peterjc123 Thank your instant reply,
I inspected tf 1.13.2 inference result, it is got all 0.
After changing to quantize_target_type='uint8' will show AttributeError: Hybrid kernels supports int8 only

from tinyneuralnetwork.

Ouskit avatar Ouskit commented on May 9, 2024

@Ouskit I change hybrid_quantization_from_float=False, and infernce result is normal, but the model size is remaining the same. Is it normal? Thank you.

from tinyneuralnetwork.

peterjc123 avatar peterjc123 commented on May 9, 2024

@Ouskit I change hybrid_quantization_from_float=False, and infernce result is normal, but the model size is remaining the same. Is it normal? Thank you.

Yes, it means that you don't use hybrid quantization.

from tinyneuralnetwork.

Ouskit avatar Ouskit commented on May 9, 2024

Please comment out https://github.com/alibaba/TinyNeuralNetwork/blob/main/tinynn/converter/base.py#L96 and change the types in the code in https://github.com/alibaba/TinyNeuralNetwork/blob/main/tinynn/converter/operators/hybrid_quantizer.py#L49 to torch.quint8.

Appreciate your reply,

After I did these changes, the result is from all Nan to very small number, but the model output is not correct
image

Road Semantic segmentation task output, model output, not correct
image

Correct result
image

from tinyneuralnetwork.

peterjc123 avatar peterjc123 commented on May 9, 2024

@Ouskit
https://github.com/alibaba/TinyNeuralNetwork/blob/main/tinynn/converter/operators/hybrid_quantizer.py#L47-L53
Please update line 49 in hybrid_quantizer.py to the following code block.

                new_weight = quantize(name, weight, torch.qint8, torch.per_tensor_symmetric, q_type=np.int8)
                new_weight.dtype = np.dtype('uint8')

from tinyneuralnetwork.

Ouskit avatar Ouskit commented on May 9, 2024

@Ouskit https://github.com/alibaba/TinyNeuralNetwork/blob/main/tinynn/converter/operators/hybrid_quantizer.py#L47-L53 Please update line 49 in hybrid_quantizer.py to the following code block.

                new_weight = quantize(name, weight, torch.qint8, torch.per_tensor_symmetric, q_type=np.int8)
                new_weight.dtype = np.dtype('uint8')

@peterjc123 This works! Now the model output is totally correct. Thank you for your help😊.

from tinyneuralnetwork.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.