apple / apple/coremltools

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

Open
#2,037 5 comments 0 reactions 0 assignees View on GitHub
bug PyTorch (traced) triaged
Dominant language
Python
Stars
5.4k
Forks
850
Avg merge
4d 5h
Merged PRs (30d)
10

Description

## 🐞Describing the bug
```
Traceback (most recent call last):
File "/media/anlab/data-2tb/ANLAB_THUY/ImageSearcher/ConvertSolar2Coreml.py", line 119, in
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
- I try convert model SOLAR- (https://github.com/tonyngjichun/SOLAR/tree/master)
### 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

Contributor guide

Open the contributing guide

Research direction

Start with the flexible-shape conversion in the PyTorch frontend, especially mil/frontend/torch/ops.py around _avg_pool and avg_pool2d, using the reported traceback as the entry point. Reproduce the SOLAR conversion with coremltools 7.0, Torch 1.9.1, and RangeDim inputs, then verify conversion succeeds without the AttributeError and that the generated model handles the requested shape range.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, pytorch
Domain
machine-learning
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.