anilsathyan7 / Portrait-Segmentation

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

Slimnet pre-trained model questions #43

Closed ari-ruokamo closed 3 years ago

ari-ruokamo commented 3 years ago

Hi! And thanks for your amazing source of DL information. I'm exploring the repository and the pre-trained 'slim_reshape.tflite' model.

In my python script I'm feeding input of 512x512x3 RGB-frame but I can't seem to get any sensible result out from the model. I'm suspecting that my input tensor data array is ill-formatted, or I'm interpreting the output tensor wrong (alpha mask?).

Thanks for any tips!

anilsathyan7 commented 3 years ago

The exported model had some issue, it seems(orginal hdf5 is correct). Now it has been fixed and you can try the updated versions of the model from the same folder. A slightly different version of the model(output shape) is available at: https://github.com/anilsathyan7/Portrait-Segmentation/blob/master/mediapipe_slimnet/portrait_segmentation.tflite

ari-ruokamo commented 3 years ago

Edited:

Thanks for your response! After staring at my code long enough I realised I was using cv2.resize vs. I should've done reshaping with numpy. :-)


while True:

    # Read webcam frame 
    success, frame = cap.read()
    image = center_crop_resize(frame, side=512)
    image = np.float32(image)/255

    expimg = image[np.newaxis, :, :, :]

    # Invoke interpreter for inference
    interpreter.set_tensor(input_details[0]['index'], np.array(expimg, dtype=np.float32))
    interpreter.invoke()
    output = interpreter.get_tensor(output_details[0]['index'])

    # Reshape byte buffer & display the mask
    output = np.uint8(output*255.0)
    output = output.reshape(512,512,1)
    cv2.imshow('Portrait Mask', output)
ari-ruokamo commented 3 years ago

Issue author error, model works.