code: def test_convert_torch_vision_mobilenet_v2(tmpdir):
      import torch
      import torchvision
  
      """
          In this example, we'll instantiate a PyTorch classification model and convert
          it to Core ML.
          """
  
      """
          Here we instantiate our model. In a real use case this would be your trained
          model.
          """
      model = torchvision.models.mobilenet_v2()
  
      """
          The next thing we need to do is generate TorchScript for the model. The easiest
          way to do this is by tracing it.
          """
  
      """
          It's important that a model be in evaluation mode (not training mode) when it's
          traced. This makes sure things like dropout are disabled.
          """
      model.eval()
  
      """
          Tracing takes an example input and traces its flow through the model. Here we
          are creating an example image input.
  
          The rank and shape of the tensor will depend on your model use case. If your
          model expects a fixed size input, use that size here. If it can accept a
          variety of input sizes, it's generally best to keep the example input small to
          shorten how long it takes to run a forward pass of your model. In all cases,
          the rank of the tensor must be fixed.
          """
      example_input = torch.rand(1, 3, 256, 256)
  
      """
          Now we actually trace the model. This will produce the TorchScript that the
          CoreML converter needs.
          """
      traced_model = torch.jit.trace(model, example_input)
  
      """
          Now with a TorchScript representation of the model, we can call the CoreML
          converter. The converter also needs a description of the input to the model,
          where we can give it a convenient name.
          """
      mlmodel = ct.convert(
          traced_model,
          inputs=[
              ct.TensorType(name="input", shape=example_input.shape)
          ],
      )
  
      """
          Now with a conversion complete, we can save the MLModel and run inference.
          """
      save_path = os.path.join(str(tmpdir), "mobilenet_v2.mlmodel")
      mlmodel.save(save_path)
  
      """
          Running predict() is only supported on macOS.
          """
      if ct.utils._is_macos():
          results = mlmodel.predict({"input": example_input.numpy()})
          expected = model(example_input)
          np.testing.assert_allclose(
              list(results.values())[0],
              expected.detach().numpy(),
              rtol=1e-2,
          )
  
imports: from os import getcwd, chdir
  from shutil import rmtree
  from os.path import exists
  from tempfile import mkdtemp
  import pytest
  import numpy
  import coremltools
  import os
  from coremltools._deps import _HAS_TF_1, _HAS_TF_2, _HAS_TORCH, MSG_TF1_NOT_FOUND, MSG_TF2_NOT_FOUND, MSG_TORCH_NOT_FOUND
label_desc: Use image node_id 1 for calling the TestPyTorchConverterExamples obj's underlying member method code with example usage: obj.test_convert_torch_vision_mobilenet_v2(tmpdir) without return types