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

nanodet model conerted from pytorch to coreml problem #26

Closed
minushuang opened this issue Oct 31, 2022 · 2 comments
Closed

nanodet model conerted from pytorch to coreml problem #26

minushuang opened this issue Oct 31, 2022 · 2 comments

Comments

@minushuang
Copy link

minushuang commented Oct 31, 2022

Hi, I converted a object detection model from pytorch to coreml with the code

def main(config, model_path, output_path, input_shape=(320, 320)):
    logger = Logger(-1, config.save_dir, False)
    model = build_model(config.model)
    checkpoint = torch.load(model_path, map_location=lambda storage, loc: storage)
    load_model_weight(model, checkpoint, logger)

    dummy_input = torch.autograd.Variable(
        torch.randn(1, 3, input_shape[0], input_shape[1])
    )

    traced_model = torch.jit.trace(model, dummy_input)

    logging.info("convert coreml start.")
    core_model = ct.convert(
        traced_model,
        inputs=[ct.ImageType(shape=dummy_input.shape, name='input', scale=0.017429, bias=(-103.53 * 0.017429, -116.28 * 0.017507, -123.675 * 0.017125))],
        outputs=[ct.TensorType(name="output")],
        debug=True
    )
    core_model.save(output_path)
    logging.info("finish convert coreml.")

the infer code with nms

image = Image.open(img_path).resize((320, 320)).convert('RGB')
model = ct.models.MLModel(mlmodel_path)
preds = model.predict({'input': image})
#post-process nms for preds

and the coreml result seems not correct as below
the pytorch results
image
the coreml results
image

could you please give me some advice about hot to get a correct coreml model, and it is so much better if you can convert the model for me if it is convenient, many thanks.

@minushuang
Copy link
Author

all the model and the code are in https://github.com/minushuang/nanodet-for-coreml now. please let me know if I can provide you with any further assistance for reproduce the problem.

@minushuang
Copy link
Author

sorry, i made a stupid mistake, i forgot to set the model to eval mode before converting, and it's ok now after model.eval()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant