Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: 'list' object has no attribute 'val'. When set flexible input shapes #2037

Open
ThuyyTran opened this issue Oct 31, 2023 · 5 comments
Labels
bug Unexpected behaviour that should be corrected (type) PyTorch (traced) triaged Reviewed and examined, release as been assigned if applicable (status)

Comments

@ThuyyTran
Copy link

🐞Describing the bug

Traceback (most recent call last):
  File "/media/anlab/data-2tb/ANLAB_THUY/ImageSearcher/ConvertSolar2Coreml.py", line 119, in <module>
    mlprogram = ct.convert(
  File "/home/anlab/anaconda3/envs/convertmodel1/lib/python3.9/site-packages/coremltools/converters/_converters_entry.py", line 551, in convert
    mlmodel = mil_convert(
  File "/home/anlab/anaconda3/envs/convertmodel1/lib/python3.9/site-packages/coremltools/converters/mil/converter.py", line 188, in mil_convert
    return _mil_convert(model, convert_from, convert_to, ConverterRegistry, MLModel, compute_units, **kwargs)
  File "/home/anlab/anaconda3/envs/convertmodel1/lib/python3.9/site-packages/coremltools/converters/mil/converter.py", line 212, in _mil_convert
    proto, mil_program = mil_convert_to_proto(
  File "/home/anlab/anaconda3/envs/convertmodel1/lib/python3.9/site-packages/coremltools/converters/mil/converter.py", line 286, in mil_convert_to_proto
    prog = frontend_converter(model, **kwargs)
  File "/home/anlab/anaconda3/envs/convertmodel1/lib/python3.9/site-packages/coremltools/converters/mil/converter.py", line 108, in __call__
    return load(*args, **kwargs)
  File "/home/anlab/anaconda3/envs/convertmodel1/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 75, in load
    return _perform_torch_convert(converter, debug)
  File "/home/anlab/anaconda3/envs/convertmodel1/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/load.py", line 114, in _perform_torch_convert
    prog = converter.convert()
  File "/home/anlab/anaconda3/envs/convertmodel1/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/converter.py", line 484, in convert
    convert_nodes(self.context, self.graph)
  File "/home/anlab/anaconda3/envs/convertmodel1/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 93, in convert_nodes
    add_op(context, node)
  File "/home/anlab/anaconda3/envs/convertmodel1/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 3923, in avg_pool2d
    _avg_pool(context, node, inputs)
  File "/home/anlab/anaconda3/envs/convertmodel1/lib/python3.9/site-packages/coremltools/converters/mil/frontend/torch/ops.py", line 3876, in _avg_pool
    strides = mb.const(val=kernel_sizes.val, name=strides.name)
AttributeError: 'list' object has no attribute 'val'

To Reproduce

Init model pytorch:

class Network(nn.Module):
    def __init__(self, model):
        super().__init__()
        self.model = model.cpu()
        self.mean = torch.tensor([0.485, 0.456, 0.406]).view(3, 1, 1)
        self.std = torch.tensor([0.229, 0.224, 0.225]).view(3, 1, 1)
    def forward(self,x):
        out1 = self.model(x)   
        reshaped_tensor1 = out1.view(1, 2048)
        return reshaped_tensor1
state = torch.load(os.path.join(get_data_root(), 'networks/model_best.pth.tar'),map_location=torch.device('cpu'))
net_params = {}
net_params['architecture'] = state['meta']['architecture']
net_params['pooling'] = state['meta']['pooling'] 
net_params['local_whitening'] = state['meta'].get('local_whitening', False)
net_params['regional'] = state['meta'].get('regional', False)
net_params['whitening'] = state['meta'].get('whitening', True)
net_params['mean'] = state['meta']['mean']
net_params['std'] = state['meta']['std']
net_params['pretrained'] = False
net = load_network('model_best.pth.tar')
net.load_state_dict(state['state_dict'])
net.eval()
test_model = Network(net)

Convert pytorch to coreml

scale = 1/(0.226*255.0)
bias = [- 0.485/(0.229) , - 0.456/(0.224), - 0.406/(0.225)]
input_shape = ct.Shape(shape=(1, 3, ct.RangeDim(lower_bound=100, upper_bound=800),
                              ct.RangeDim(lower_bound=100, upper_bound=800)))
dummy_input = torch.rand(1,3,300,300)
input_tensor = ct.ImageType(name="my_input", shape=input_shape,scale=scale, bias=bias)
traced_model = torch.jit.trace(test_model.eval(), dummy_input)
traced_model.eval()

mlprogram = ct.convert(
    traced_model,
    minimum_deployment_target=ct.target.iOS13,
    inputs=[input_tensor],
    outputs=[ct.TensorType(name="embeddings")],
    convert_to="neuralnetwork",
    compute_units=ct.ComputeUnit.CPU_ONLY,
)
saved_model = 'ModelConvert/TestModel/Solar300_image_CPU_FlexibleInput.mlmodel'
outputmodel.save(saved_model)

System environment

  • coremltools version: 7.0
  • OS (e.g. MacOS version or Linux type): Linux
  • Any other relevant version information (e.g. PyTorch or TensorFlow version): Torch 1.9.1
@ThuyyTran ThuyyTran added the bug Unexpected behaviour that should be corrected (type) label Oct 31, 2023
@jakesabathia2 jakesabathia2 added PyTorch (traced) triaged Reviewed and examined, release as been assigned if applicable (status) labels Nov 2, 2023
@xorange
Copy link

xorange commented Nov 9, 2023

Could you try #2050 to see if it fixes this problem ?
Or could you provide a standalone minimum example for reproduce ?

@ThuyyTran
Copy link
Author

Could you try #2050 to see if it fixes this problem ? Or could you provide a standalone minimum example for reproduce ?

I tried to change the code in file coremltools/converters/mil/frontend/torch/ops.py similar to #2050. But when I convert the model, it always pauses at a certain threshold. I tried waiting but it didn't seem to work.
you can reproduce using my colab code here: https://colab.research.google.com/drive/1jqT_K3ValOyfc_o_2od44eiz4UiE8xAk?usp=sharing

@xorange
Copy link

xorange commented Nov 10, 2023

Could you try #2050 to see if it fixes this problem ? Or could you provide a standalone minimum example for reproduce ?

I tried to change the code in file coremltools/converters/mil/frontend/torch/ops.py similar to #2050. But when I convert the model, it always pauses at a certain threshold. I tried waiting but it didn't seem to work. you can reproduce using my colab code here: https://colab.research.google.com/drive/1jqT_K3ValOyfc_o_2od44eiz4UiE8xAk?usp=sharing

OK. I also notice the pause and has a plan yet to be implemented. I'll try your code after the optimization.

@ThuyyTran
Copy link
Author

@xorange Thanks. Looking forward to hearing from you.

@xorange
Copy link

xorange commented Nov 14, 2023

My PR focused on "padding" and failed to fix this problem. I've encountered the same situation on another net structure with a similar root cause.

I'll try to generalize my fix for these.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected behaviour that should be corrected (type) PyTorch (traced) triaged Reviewed and examined, release as been assigned if applicable (status)
Projects
None yet
Development

No branches or pull requests

3 participants