Open zengchang233 opened 5 years ago
you could first include
from torch.nn.modules.utils import _list_with_default
and then add
def _adaptive_avg_pool2d(raw, input, output_size):
_output_size = _list_with_default(output_size, input.size())
x = raw(input, _output_size)
_pool('ave', raw, input, x, input.shape[2], input.shape[2], 0, False)
return x
and
F.adaptive_avg_pool2d=Rp(F.adaptive_avg_pool2d, _adaptive_avg_pool2d)
in pytorch_to_caffe.py.
@LilySys code solves the error in the pooling layer. However, it looks like current model available through pip, uses 'reshape' layer instead of 'view'. (The latest torchvision model uses 'flatten' layer, though.)
You should add a handler for reshape layer like the following:
def _reshape(input, *args):
x=raw_reshape(input, *args)
if not NET_INITTED:
return x layer_name=log.add_layer(name='reshape')
top_blobs=log.add_blobs([x],name='reshape_blob')
layer=caffe_net.Layer_param(name=layer_name,type='Reshape', bottom=[log.blobs(input)],top=top_blobs)
dims=list(args)
dims[0]=0 # the first dim should be batch_size
layer.param.reshape_param.shape.CopyFrom(caffe_net.pb.BlobShape(dim=dims))
log.cnet.add_layer(layer)
return x
and
try:
raw_view=Variable.view
Variable.view=_view
raw_reshape=Variable.reshape
Variable.reshape=_reshape
and
except:
# for new version 0.4.0 and later version
for t in [torch.Tensor]:
raw_view = t.view
t.view = _view
raw_reshape = t.reshape
t.reshape = _reshape
@Jungho-Jo ,hi ,I try your method, but it can not work. when I convert the flatten layer it can not come into the _reshape(), How should I do?
@Jungho-Jo ,hi ,I try your method, but it can not work. when I convert the flatten layer it can not come into the _reshape(), How should I do?
Hi ,have you solved the problem?I meet the same problem.
met the same problem
Thank you for your nice work! I encoutered some problems when converting resnet18 from pytorch to caffe. I just modified the
example/resnet_pytorch_2_caffe.py
to convert pretrained model as followingBut I got the KeyError prompt like following.
I guess maybe there are some bugs when convert
torch.Tensor.view
method to caffe.May you check it later? Thanks a lot.