anilsathyan7 / Portrait-Segmentation

Real-time portrait segmentation for mobile devices
MIT License
638 stars 133 forks source link

PortraitNet Quant Inference #24

Closed shivSD closed 3 years ago

shivSD commented 3 years ago

@anilsathyan7 Thanks for such a good collection of network repo.

I was trying to do inference on Quantized Portrait_video and getting a wrong results.

model file : models->portrait_video->portrait_video_quant.tflite when checked the input and output details

_input: [{'name': 'input.1', 'index': 1, 'shape': array([ 1, 224, 224, 4], dtype=int32), 'shape_signature': array([ 1, 224, 224, 4], dtype=int32), 'dtype': <class 'numpy.int8'>, 'quantization': (0.0183159988373518, -13), 'quantization_parameters': {'scales': array([0.018316], dtype=float32), 'zero_points': array([-13], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]

output: [{'name': 'Identity', 'index': 0, 'shape': array([ 1, 50176], dtype=int32), 'shape_signature': array([ 1, 50176], dtype=int32), 'dtype': <class 'numpy.int8'>, 'quantization': (0.00390625, -128), 'quantization_parameters': {'scales': array([0.00390625], dtype=float32), 'zero_points': array([-128], dtype=int32), 'quantized_dimension': 0}, 'sparsityparameters': {}}]

It needs INT8 images. So i converted the image (after resize and normalize) to INT8. changes made to portrait_video.py

    ###Add prior as fourth channel
    image=np.dstack([image,prior])
    prepimg = image[np.newaxis, :, :, :]
    print('prepimg type :', prepimg.dtype)
    prepimg = np.array(prepimg, dtype=np.int8)
    print('after prepimg type :', prepimg.dtype)
    print(prepimg.shape)

when i input this image to the network i'm getting some random noise (garbage output).

Here is my question do we need to normalize the image using quantization parameters before we feed it to the network. Highly appreciate if you can suggest on preprocessing required on input image.

anilsathyan7 commented 3 years ago

Yes, we need to normalize the images before inference. Please check the following notebook on quantization: https://github.com/anilsathyan7/Portrait-Segmentation/blob/5d9f0cc54b4bb14467499fe085b4012e8c52d3c1/quantization_edgetpu.ipynb

shivSD commented 3 years ago

Yes, we need to normalize the images before inference. Please check the following notebook on quantization: https://github.com/anilsathyan7/Portrait-Segmentation/blob/5d9f0cc54b4bb14467499fe085b4012e8c52d3c1/quantization_edgetpu.ipynb