Giter Club home page Giter Club logo

Comments (3)

TobyRoseman avatar TobyRoseman commented on June 26, 2024 1

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.

mallman avatar mallman commented on June 26, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.