coremltools icon indicating copy to clipboard operation
coremltools copied to clipboard

PyTorch resize not working with flexible input

Open BIGPPWONG opened this issue 3 years ago • 3 comments

To Reproduce

import torch, torchvision
from torch import nn

class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()

    def forward(self, inp):
        x = torchvision.transforms.Resize((128,128))(inp)
        return x
newmodel = NeuralNetwork()

example_input = torch.rand(1, 3, 400, 300) 
traced_model = torch.jit.trace(newmodel, example_input)
out = traced_model(example_input)

import coremltools as ct

input_shape = ct.Shape(shape=(1, 3, ct.RangeDim(2,15000), ct.RangeDim(2,15000)),default=(1,3,128,128))
# input_shape = ct.Shape(shape=(1, 3, 256, 256))

image_input = ct.ImageType(shape=input_shape,bias=[0,0,0], 
                           scale=1/255,
                           name = "colorImage",
                           color_layout=ct.colorlayout.RGB,
                           )

mlmodel = ct.convert(
    traced_model,
    inputs=[image_input], 
 )

Paste Python code snippet here, complete with any required import statements.

File ~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/load.py:92, in _perform_torch_convert(converter, debug)
     90 def _perform_torch_convert(converter, debug):
     91     try:
---> 92         prog = converter.convert()
     93     except RuntimeError as e:
     94         if debug and "convert function" in str(e):

File ~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/converter.py:269, in TorchConverter.convert(self)
    266 self.convert_const()
    268 # Add the rest of the operations
--> 269 convert_nodes(self.context, self.graph)
    271 graph_outputs = [self.context[name] for name in self.graph.outputs]
    273 # An output can be None when it's a None constant, which happens
    274 # in Fairseq MT.

File ~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/ops.py:95, in convert_nodes(context, graph)
     91 if add_op is None:
     92     raise RuntimeError(
     93         "PyTorch convert function for op '{}' not implemented.".format(node.kind)
     94     )
---> 95 add_op(context, node)
     97 # We've generated all the outputs the graph needs, terminate conversion.
     98 if _all_outputs_present(context, graph):

File ~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/ops.py:2687, in upsample_bilinear2d(context, node)
   2684     else:
   2685         raise ValueError("Failed to infer scale factors from inputs.")
-> 2687 upsample_bilinear = mb.upsample_bilinear(
   2688     x=_input,
   2689     scale_factor_height=scales_h,
   2690     scale_factor_width=scales_w,
   2691     align_corners=align_corners,
   2692     name=node.name,
   2693 )
   2694 context.add(upsample_bilinear)

File ~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/mil/ops/registry.py:172, in SSAOpRegistry.register_op.<locals>.class_wrapper.<locals>.add_op(cls, **kwargs)
    169 else:
    170     op_cls_to_add = op_reg[op_type]
--> 172 return cls._add_op(op_cls_to_add, **kwargs)

File ~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/mil/builder.py:165, in Builder._add_op(cls, op_cls, **kwargs)
    163 # Shallow copy list inputs to ensure op inputs are immutable
    164 kwargs = {k: v if not isinstance(v, (list, tuple)) else v[:] for k, v in kwargs.items() if v is not None}
--> 165 kwargs.update(cls._create_vars(
    166     input_spec=op_cls.input_spec,
    167     op_name=kwargs["name"], before_op=before_op,
    168     candidate_kv=kwargs))
    169 new_op = op_cls(**kwargs)
    171 # Initialize optional input Vars if it wasn't in kwargs

File ~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/mil/builder.py:148, in Builder._create_vars(cls, input_spec, op_name, before_op, candidate_kv)
    145         continue
    147     if isinstance(in_type, (TensorInputType, ListOrTensorInputType)):
--> 148         var = cls._add_const(val, new_var_name, before_op)
    149         update_dict[k] = var
    151 return update_dict

File ~/opt/anaconda3/lib/python3.9/site-packages/coremltools/converters/mil/mil/builder.py:79, in Builder._add_const(cls, val, name, before_op)
     76 @classmethod
     77 def _add_const(cls, val, name, before_op):
     78     if not is_python_value(val):
---> 79         raise ValueError("Cannot add const {}".format(val))
     80     if any_symbolic(val):
     81         msg = (
     82             "Python native vals (list, tuple), np.array that are"
     83             + "operation inputs cannot have symbolic values. Consider feeding"
     84             + "symbolic shape in through placeholder and use mb.shape() "
     85             + "operator. Input {}: {}"
     86         )

ValueError: Cannot add const 128.0001/is908

System environment (please complete the following information):

  • coremltools version: 6.0
  • OS (e.g. MacOS version or Linux type): macOS 13.0
  • Any other relevant version information (e.g. PyTorch or TensorFlow version): PyTorch 1.12

BIGPPWONG avatar Nov 03 '22 08:11 BIGPPWONG

@bigppwong - I'm not able to run your code. traced_model is not defined.

TobyRoseman avatar Nov 03 '22 17:11 TobyRoseman

@bigppwong - I'm not able to run your code. traced_model is not defined.

sorry, I forget to append traced model code, I have updated the code , could you take a look?

BIGPPWONG avatar Nov 04 '22 00:11 BIGPPWONG

updated code

import torch, torchvision
from torch import nn

class NeuralNetwork(nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()

    def forward(self, inp):
        x = torchvision.transforms.Resize((128,128))(inp)
        return x
newmodel = NeuralNetwork()

example_input = torch.rand(1, 3, 400, 300) 
traced_model = torch.jit.trace(newmodel, example_input)
out = traced_model(example_input)

import coremltools as ct

input_shape = ct.Shape(shape=(1, 3, ct.RangeDim(2,15000), ct.RangeDim(2,15000)),default=(1,3,128,128))
# input_shape = ct.Shape(shape=(1, 3, 256, 256))

image_input = ct.ImageType(shape=input_shape,bias=[0,0,0], 
                           scale=1/255,
                           name = "colorImage",
                           color_layout=ct.colorlayout.RGB,
                           )

mlmodel = ct.convert(
    traced_model,
    inputs=[image_input], 
 )

BIGPPWONG avatar Nov 07 '22 06:11 BIGPPWONG