Comments (3)
Here is a more concise way to reproduce the issue:
import torch
import coremltools as ct
class M(torch.nn.Module):
def forward(self, x):
return torch.tensor_split(x, 3)
x = torch.arange(8)
traced_model = torch.jit.trace(M(), x)
ct.convert(traced_model, inputs=[ct.TensorType(shape=x.shape)])
I think we should be able to use the split MIL ops at least for simple cases.
from coremltools.
Oh, and here's an example of a failing conversion. This is from a script I've written for converting timm models:
import coremltools as ct
import timm
import torch
model_name = "eva02_tiny_patch14_224.mim_in22k"
print(f"Creating model {model_name}")
timm_model = timm.create_model(
model_name,
pretrained=True,
scriptable=False,
exportable=True)
model = torch.nn.Sequential(
timm_model,
torch.nn.Softmax(1)
).eval()
input_size = timm_model.default_cfg.get("input_size")
input_shape = (1,) + input_size
print("Tracing model")
example_input = torch.randn(input_shape)
jit_model = torch.jit.trace(model, example_input)
labels_filename = "imagenet21k_wordnet_lemmas.txt"
with open(labels_filename, "r") as labels_file:
labels = [line.strip() for line in labels_file.readlines()]
classifier_config = ct.ClassifierConfig(labels)
print("Converting model")
# Scale and bias calculations taken from Core ML Tools documentation on
# preprocessing for PyTorch
mean = list(timm_model.default_cfg.get("mean"))
std = list(timm_model.default_cfg.get("std"))
import statistics
mean_std = statistics.mean(std)
scale = 1 / (mean_std * 255)
bias = [-m / s for m, s in zip(mean, std)]
input_type = ct.ImageType(
name="image",
shape=input_shape,
scale=scale,
bias=bias)
coreml_model = ct.convert(
jit_model,
convert_to="mlprogram",
inputs=[input_type],
classifier_config=classifier_config,
skip_model_load=True
)
coreml_model.user_defined_metadata["com.apple.coreml.model.preview.type"] = "imageClassifier"
coreml_model_file_name = f"{model_name}.mlpackage"
print(f"Saving model to {coreml_model_file_name}")
coreml_model.save(coreml_model_file_name)
print("Done!")
I believe a pip install
with the timm, torch and coremltools packages will give you the right environment for running this.
You will also need a labels file, imagenet21k_wordnet_lemmas.txt
, in your working directory. I'm attaching that file.
imagenet21k_wordnet_lemmas.txt
from coremltools.
Related Issues (20)
- tf.random.uniform is optimized out when it shouldn't be HOT 2
- MLUpdateTask not working for HOT 1
- MFCC differences between torchaudio and CoreML HOT 1
- Issue converting OpenELM: Conversion for torch.repeat_interleave with non-zero dim has not been implemented HOT 1
- Is there support to run ANE accelerated loops/while_loop? HOT 1
- Flexible input for MelSpectrogram: 'complex_shape' object has no attribute 'data'
- ValueError: Op "complex_stft_0_promoted" (op_type: cast) Input x="complex_stft_0" expects tensor or scalar of dtype from type domain ['fp16', 'fp32', 'int32', 'bool'] but got tensor[1,1025,250,complex64] HOT 1
- About Image Input and Output. HOT 1
- Adding BitNet Layer Support to CoreML HOT 1
- ct.convert call appears to corrupt torchscript model HOT 1
- ARM64.whl not support on M3 ? HOT 1
- Failed to load _MLModelProxy: No module named 'coremltools.libcoremlpython HOT 3
- Convert Yolov8 to CoreML give different results HOT 3
- x param of type int32 are cast to int16 for gather ops which should overflow, but doesn't HOT 3
- NaN results for PyTorch models on versions > 6.3.0 HOT 3
- Support for MLLMs such as LLaVA? HOT 1
- Dose Coremltools7+ no longer support converting ONNX to Coreml? HOT 3
- need help about both model weight and activation quantization with only a float32 mlmodel HOT 1
- Missing attribute `save` for `Program` type HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coremltools.