Additionally, in case needed as well, here is my conversion script:
import torch
import coremltools as ct
import numpy as np
import logging
from ball_tracker_model import BallTrackerNet
def convert_to_coreml(model_path):
logging.basicConfig(level=logging.DEBUG)
model = BallTrackerNet()
model.load_state_dict(torch.load(model_path, map_location='cpu'))
model.eval()
example_input = torch.rand(1, 9, 360, 640)
# Trace the model to verify shapes
traced_model = torch.jit.trace(model, example_input)
model_coreml = ct.convert(
traced_model,
inputs=[
ct.TensorType(
name="input_frames",
shape=(1, 9, 360, 640),
dtype=np.float32,
)
],
convert_to="mlprogram",
minimum_deployment_target=ct.target.iOS15,
)
model_coreml.save("BallTracker.mlpackage")
return model_coreml
# Run conversion
try:
model = convert_to_coreml("balltrackerbest.pt")
print("Conversion successful!")
except Exception as e:
print(f"Conversion error: {str(e)}")
Thanks again!
Topic:
Machine Learning & AI
SubTopic:
Core ML
Tags: