private-detector icon indicating copy to clipboard operation
private-detector copied to clipboard

Is it possible to convert the private-detector model to a CoreML model?

Open ChouJay opened this issue 1 year ago • 2 comments

Dear Bumble tech,
I've been attempting to convert the private-detector's saved_model into a CoreML model. However, after the conversion, it seems unable to successfully identify NSFW images. I suspect there might be an issue during the conversion process. Could you guide me on how to correctly convert a saved_model.pb into a CoreML .mlPackage? Thanks a lot! Here's my code:

import coremltools as ct
mlmodel_from_tf = ct.convert(model="/Path/To/private_detector/saved_model",
                           inputs=[ct.ImageType(shape=(1,480,480,3))],
                           source="tensorflow",
                           compute_precision=ct.precision.FLOAT32)

Results of testing the CoreML model:

from PIL import Image
img = Image.open('/Path/To/Desktop/dick3.png')
img = img.resize((480,480)) 
if img.mode != 'RGB':
    img = img.convert('RGB')
out_dict = mlmodel_from_tf.predict({"model_input_images": img})
print(out_dict) 
# {'Identity': array([[0.00386974, 0.9961302 ]], dtype=float32)}
# I believe the first element of the array represents the "confidence level that the content is NSFW." or I'm misunderstanding something?

ChouJay avatar Nov 16 '23 04:11 ChouJay

Hey! - Sorry about the delay in getting round to this

Hmm tbh I've no experience in using CoreML models, so I'm quite limited in what I can do to help 😢

What results are you getting for that image with the normal SavedModel inference? It doesn't seem like you're recreating the preprocessing functions for images with that setup so I'm thinking that may be causing the issue

Steeeephen avatar Nov 28 '23 23:11 Steeeephen

Hi @Steeeephen , The problem was indeed that I hadn't implemented the preprocess function. After implementing the preprocess function in Swift, I was able to get the correct results. Thank you very much for your guidance : )

ChouJay avatar Dec 05 '23 06:12 ChouJay