apple / coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
https://coremltools.readme.io
BSD 3-Clause "New" or "Revised" License
4.44k stars 643 forks source link

scale and bias question from example notebooks #1087

Closed matthewchung74 closed 3 years ago

matthewchung74 commented 3 years ago

❓Question

I'm looking at these two examples for pytorch and tf, from the coreml website.

https://notebooks.gesis.org/binder/jupyter/user/continuumio-coreml-demo-a8q331jt/notebooks/tensorflow2.ipynb

for this, can you explain where the scale and basis value comes from?

import coremltools as ct

cml_model = ct.convert(tf_model,
                     inputs=[ct.ImageType(color_layout="RGB", scale=1/127.0, bias=[-1, -1, -1], )],
                     classifier_config=ct.ClassifierConfig(class_labels)
                    )

https://notebooks.gesis.org/binder/jupyter/user/continuumio-coreml-demo-a8q331jt/notebooks/pytorch.ipynb

and here can you explain where the scale and bias values comes from? Specifically, why is the scale divided by .226 after being converted from 0 to 1? for the bias numbers, I can see the numerators are the channel means but why divide by .226?

import coremltools as ct

# Trace with random data
example_input = torch.rand(1, *input_shape)
traced_model = torch.jit.trace(torch_model, example_input)
cml_model = ct.convert(traced_model,
                       inputs=[ct.ImageType(color_layout='RGB', scale=1.0/255.0/0.226,
                                            bias=(-0.485/0.226, -0.456/0.226, -0.406/0.226),
                                            shape=example_input.shape)],
                       classifier_config=ct.ClassifierConfig(class_labels)
                      )
TobyRoseman commented 3 years ago

Neither of the links work for me. Please share standalone code here that reproduces the issue.

matthewchung74 commented 3 years ago

Hi Toby, sorry about that. The notebook is from the apple readme https://coremltools.readme.io/docs/what-are-coreml-tools and clicking on the binder link. Then I selected the pytorch notebook. I just tried from the readme page and can load the notebook.

seibert commented 3 years ago

Just to follow up on this. The scale factor comes from the documentation for MobileNet v2 model on PyTorch Hub:

https://pytorch.org/hub/pytorch_vision_mobilenet_v2/

The actual scale factor they recommend [0.229, 0.224, 0.225] for the RGB channels, but coremltools doesn't have a way to apply independent scaling to each color channel.

matthewchung74 commented 3 years ago

oh ic. thank you.