# Convert RT-DETR model to coreml

**URL:** </static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fconvert-rt-detr-model-to-coreml%2F137516%3E%5C
**Category:** 🤗Transformers\
**Created:** [January 27, 2025, 10:09am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fconvert-rt-detr-model-to-coreml%2F137516 "2025-01-27T10:09:31Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ProfessorHT](https://avatars.discourse-cdn.com/v4/letter/p/cdc98d/32.png) [@ProfessorHT](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FProfessorHT)%5C
**Post date:** [January 27, 2025, 10:09am UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fconvert-rt-detr-model-to-coreml%2F137516%2F1 "2025-01-27T10:09:31Z")

</div>

Hello,

I am currently working on an iOS application that requires object detection functionality, and I’m interested in using the RT-DETR model for this purpose. I would like to ask if anyone has experience or knows of a way to transfer the RT-DETR model into the CoreML format to be used on iOS devices.

Thank you in advance!

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJohn6666)%5C
**Post date:** [January 27, 2025, 4:12pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fconvert-rt-detr-model-to-coreml%2F137516%2F2 "2025-01-27T16:12:48Z")

</div>

There may be some problems, but it looks like it will work.

> <https://stackoverflow.com/questions/75749584/how-can-i-convert-a-yolov8s-model-to-coreml-model-using-a-custom-dataset>

> <https://github.com/ultralytics/ultralytics/issues/8918>
>
> \### Search before asking
> 
> \- \[X\] I have searched the YOLOv8 \[issues\](https://gith…ub.com/ultralytics/ultralytics/issues) and found no similar bug report.
> 
> 
> \### YOLOv8 Component
> 
> Export
> 
> \### Bug
> 
> from ultralytics import RTDETR
> 
> model = RTDETR('RTDETR.pt')
> 
> model.export(format="coreml")
> 
> when exporting a custom trained RT-DETR model i get the following error:
> 
> Ultralytics YOLOv8.1.19 🚀 Python-3.10.13 torch-1.12.0+cu102 CPU (AMD EPYC 7282 16-Core Processor)
> rt-detr-l summary: 498 layers, 31985795 parameters, 0 gradients, 103.4 GFLOPs
> 
> PyTorch: starting from 'RTDETR.pt' with input shape (1, 3, 640, 640) BCHW and output shape(s) (1, 300, 5) (63.1 MB)
> 
> CoreML: starting export with coremltools 6.2...
> Converting PyTorch Frontend ==\> MIL Ops: 18%|▏| 417/2352 \[00:00\<00:00, 4165.75 Saving value type of int64 into a builtin type of int32, might lose precision!
> Saving value type of int64 into a builtin type of int32, might lose precision!
> Saving value type of float64 into a builtin type of fp32, might lose precision!
> Converting PyTorch Frontend ==\> MIL Ops: 25%|▏| 586/2352 \[00:00\<00:00, 3093.35 
> CoreML: export failure ❌ 21.4s: In op, of type linear, named out\_w, the named input \`weight\` must have the same data type as the named input \`x\`. However, weight has dtype fp32 whereas x has dtype int32.
> Traceback (most recent call last):
> File "/opt/conda/bin/yolo", line 8, in \<module\>
> sys.exit(entrypoint())
> File "/usr/src/ultralytics/ultralytics/cfg/\_\_init\_\_.py", line 568, in entrypoint
> getattr(model, mode)(\*\*overrides) # default args from model
> File "/usr/src/ultralytics/ultralytics/engine/model.py", line 577, in export
> return Exporter(overrides=args, \_callbacks=self.callbacks)(model=self.model)
> File "/opt/conda/lib/python3.10/site-packages/torch/autograd/grad\_mode.py", line 27, in decorate\_context
> return func(\*args, \*\*kwargs)
> File "/usr/src/ultralytics/ultralytics/engine/exporter.py", line 281, in \_\_call\_\_
> f\[4\], \_ = self.export\_coreml()
> File "/usr/src/ultralytics/ultralytics/engine/exporter.py", line 136, in outer\_func
> raise e
> File "/usr/src/ultralytics/ultralytics/engine/exporter.py", line 131, in outer\_func
> f, model = inner\_func(\*args, \*\*kwargs)
> File "/usr/src/ultralytics/ultralytics/engine/exporter.py", line 594, in export\_coreml
> ct\_model = ct.convert(
> File "/opt/conda/lib/python3.10/site-packages/coremltools/converters/\_converters\_entry.py", line 444, in convert
> mlmodel = mil\_convert(
> File "/opt/conda/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 187, in mil\_convert
> return \_mil\_convert(model, convert\_from, convert\_to, ConverterRegistry, MLModel, compute\_units, \*\*kwargs)
> File "/opt/conda/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 211, in \_mil\_convert
> proto, mil\_program = mil\_convert\_to\_proto(
> File "/opt/conda/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 281, in mil\_convert\_to\_proto
> prog = frontend\_converter(model, \*\*kwargs)
> File "/opt/conda/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 109, in \_\_call\_\_
> return load(\*args, \*\*kwargs)
> File "/opt/conda/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 57, in load
> return \_perform\_torch\_convert(converter, debug)
> File "/opt/conda/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 96, in \_perform\_torch\_convert
> prog = converter.convert()
> File "/opt/conda/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 281, in convert
> convert\_nodes(self.context, self.graph)
> File "/opt/conda/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 89, in convert\_nodes
> add\_op(context, node)
> File "/opt/conda/lib/python3.10/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 749, in matmul
> res = mb.linear(x=inputs\[0\], weight=\_np.transpose(inputs\[1\].val), name=node.name)
> File "/opt/conda/lib/python3.10/site-packages/coremltools/converters/mil/mil/ops/registry.py", line 176, in add\_op
> return cls.\_add\_op(op\_cls\_to\_add, \*\*kwargs)
> File "/opt/conda/lib/python3.10/site-packages/coremltools/converters/mil/mil/builder.py", line 166, in \_add\_op
> new\_op = op\_cls(\*\*kwargs)
> File "/opt/conda/lib/python3.10/site-packages/coremltools/converters/mil/mil/operation.py", line 187, in \_\_init\_\_
> self.\_validate\_and\_set\_inputs(input\_kv)
> File "/opt/conda/lib/python3.10/site-packages/coremltools/converters/mil/mil/operation.py", line 503, in \_validate\_and\_set\_inputs
> self.input\_spec.validate\_inputs(self.name, self.op\_type, input\_kvs)
> File "/opt/conda/lib/python3.10/site-packages/coremltools/converters/mil/mil/input\_type.py", line 137, in validate\_inputs
> raise ValueError(msg)
> ValueError: In op, of type linear, named out\_w, the named input \`weight\` must have the same data type as the named input \`x\`. However, weight has dtype fp32 whereas x has dtype int32.
> 
> This error is consistent when using either the coreml or mlmodel format and across different pytorch versions.
> 
> \### Environment
> 
> Ultralytics YOLOv8.1.19 🚀 Python-3.10.13 torch-1.12.0+cu102 CPU (AMD EPYC 7282 16-Core Processor)
> Setup complete ✅ (32 CPUs, 251.6 GB RAM, 304.2/313.0 GB disk)
> 
> OS Linux-5.4.0-170-generic-x86\_64-with-glibc2.35
> Environment Docker
> Python 3.10.13
> Install git
> RAM 251.57 GB
> CPU AMD EPYC 7282 16-Core Processor
> CUDA None
> 
> matplotlib ✅ 3.8.3\>=3.3.0
> opencv-python ✅ 4.9.0.80\>=4.6.0
> pillow ✅ 10.0.1\>=7.1.2
> pyyaml ✅ 6.0.1\>=5.3.1
> requests ✅ 2.31.0\>=2.23.0
> scipy ✅ 1.12.0\>=1.4.1
> torch ✅ 1.12.0\>=1.8.0
> torchvision ✅ 0.13.0\>=0.9.0
> tqdm ✅ 4.65.0\>=4.64.0
> psutil ✅ 5.9.0
> py-cpuinfo ✅ 9.0.0
> thop ✅ 0.1.1-2209072238\>=0.1.1
> pandas ✅ 2.2.1\>=1.1.4
> seaborn ✅ 0.13.2\>=0.11.0
> 
> error was also present when using a fresh ultralytics install in Google Colab
> 
> \### Minimal Reproducible Example
> 
> from ultralytics import RTDETR
> 
> model = RTDETR('RTDETR.pt')
> model.export(format="coreml")
> 
> \### Additional
> 
> \_No response\_
> 
> \### Are you willing to submit a PR?
> 
> \- \[x\] Yes I'd like to help by submitting a PR!

---

<div class="post-metadata">

**Author:** ![ProfessorHT](https://avatars.discourse-cdn.com/v4/letter/p/cdc98d/32.png) [@ProfessorHT](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FProfessorHT)%5C
**Post date:** [January 27, 2025, 4:21pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fconvert-rt-detr-model-to-coreml%2F137516%2F3 "2025-01-27T16:21:53Z")

</div>

Thank you for the quick reply. I am using the RT DETR from huggingface and not the ultralytics

---

<div class="post-metadata">

**Author:** ![John6666](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/john6666/32/27664_2.png) [@John6666](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Fu%2FJohn6666)%5C
**Post date:** [January 27, 2025, 5:51pm UTC](/static-proxy?url=https%3A%2F%2Fdiscuss.huggingface.co%2Ft%2Fconvert-rt-detr-model-to-coreml%2F137516%2F4 "2025-01-27T17:51:00Z")

</div>

It doesn’t say how to convert it, but I think it can be converted. I think this is probably a real example. There also seem to be several CoreML communities.

> **[TheCluster/YOLOv11-CoreML · Hugging Face](https://huggingface.co/TheCluster/YOLOv11-CoreML)**
>
> We’re on a journey to advance and democratize artificial intelligence through open source and open science.

> **[coreml-community (Core ML Models)](https://huggingface.co/coreml-community)**
>
> Org profile for Core ML Models on Hugging Face, the AI community building the future.
