longcw / pytorch2caffe

Convert PyTorch model to Caffemodel
541 stars 151 forks source link

AttributeError: tuple object has no attribute 'grad_fn' #27

Closed dzyjjpy closed 5 years ago

dzyjjpy commented 5 years ago

I want to convert pytorch model(hopenet) to caffemodel, However, met the following error: AttributeError: 'tuple' object has no attribute 'grad_fn' model want to convert: pytorch: 0.2.0 torchvision:0.1.8

And hopenet's forward function will return more than one tensor: *return yaw, pitch, roll # all are 166's tensor Might need to feed more than one tensor to output_var, but tuple has no attribute 'grad_fn'.** I try to edit the return value, just return one value, e.g. yaw, it can works, but has errors with size mismatching.

Dose anyone have similar error? Suggesstions needed. Thank you so much.

zhaobaozi commented 5 years ago

have you finish?I meet the same problem

trohit920 commented 5 years ago

@dzyjjpy @zhaobaozi @longcw Any updates on this ?

dzyjjpy commented 5 years ago

output has three branch 3*66, so you should edit the code in pytorch2caffe convert_layer(output_var[0].grad_fn) convert_layer(output_var[1].grad_fn) convert_layer(output_var[2].grad_fn) as tuple object has no attribute 'grad_fn'

trohit920 commented 5 years ago

@dzyjjpy @zhaobaozi I did what you advised but got stuck with this error.

  File "pytorch2caffe.py", line 428, in <module>
    os.path.join(output_dir, 'hopenet-pytorch2caffe.caffemodel'))
  File "pytorch2caffe.py", line 38, in pytorch2caffe
    net_info = pytorch2prototxt(input_var, output_var)
  File "pytorch2caffe.py", line 330, in pytorch2prototxt
    add_layer(output_var[0].grad_fn)
  File "pytorch2caffe.py", line 157, in add_layer
    top_name = add_layer(u[0])
  File "pytorch2caffe.py", line 169, in add_layer
    layer['type'] = layer_dict[parent_type]
KeyError: 'ExpandBackward'

I am a newbie, hope you can understand. Can you share the converted caffe model if possible ?

trohit920 commented 5 years ago

@dzyjjpy I have solved all the previous errors and stuck with this one:

converting AddmmBackward190
((66L,), (66L, 2048L))
(66,)
(66, 8192)
Traceback (most recent call last):
  File "pytorch2caffe.py", line 441, in <module>
    os.path.join(output_dir, 'hopenet-pytorch2caffe.caffemodel'))
  File "pytorch2caffe.py", line 99, in pytorch2caffe
    convert_layer(output_var[0].grad_fn)
  File "pytorch2caffe.py", line 94, in convert_layer
    save_fc2caffe(weights, biases, params[parent_name])
  File "pytorch2caffe.py", line 118, in save_fc2caffe
    fc_param[0].data[...] = weights.numpy()
ValueError: could not broadcast input array from shape (66,2048) into shape (66,8192)

Please if you know how to convert any advice will be helpful.

dzyjjpy commented 5 years ago

shape issue, pay attention to your tensor size. there might be issue caused by pooling layer's calculation mode. floor or ceil. pls check the layer with errors carefully