Search code examples
pytorchcoreml

Latent vector variance much larger in CoreML than PyTorch


I have a PyTorch model that I've converted to CoreML. In PyTorch, the inferred latent vector's values range to a limit of around ±1.6, but the converted mlmodel varies as much as ±55.0. What might be causing this huge discrepancy? The conversion is pretty straightforward:

encoder = Encoder_CNN(latent_dim, n_c)
enc_fname = os.path.join(models_dir, encoder.name + '_153.pth.tar')
encoder.load_state_dict(torch.load(enc_fname, map_location={'cuda:0': 'cpu'}))
encoder.cpu()
encoder.eval()

img_in = torch.rand(1, 1, 28, 28).cpu()
traced_encoder_model = torch.jit.trace(encoder, img_in)
traced_encoder_model.save("{}_encoder_mobile_{}_{}.pt".format(model_type, latent_dim, n_c))

coreml_encoder = ct.convert(traced_encoder_model, inputs=[ct.ImageType(name=name, shape=img_in.shape)])
coreml_encoder.save("{}_encoder_{}_{}.mlmodel".format(model_type, latent_dim, n_c))

Solution

  • You probably are using different input normalization for PyTorch and Core ML. Your img_in consists of values between 0 and 1. I don't see the inference code for Core ML, but your input pixels are probably between 0 and 255 there. You can fix this by specifying image preprocessing settings when you convert the PyTorch model to Core ML.