starimeL/PytorchConverter

About upsample_bilinear

Opened this issue · 10 comments

The "upsample_bilinear" in UNet is changed to be "Deconvolution" in ncnn. But Deconvolution is not the meaning of upsample_bilinear. How to cope this issue? Thanks!

Yeah sure, not exactly the same
But since there doesn't exist any functional layer for directly upsampling in both caffe & ncnn, we just unable to do that unless they add a new layer
So this tool converts "upsample_bilinear" to a Deconv filled with bilinear weights, which could achieve an approximation with a little loss on result

Thank you! I have solved it by following,
def UpsampleBilinear(pytorch_layer):
layer = LayerParameter_ncnn()
layer.type = 'Interp'
print pytorch_layer
assert pytorch_layer.scale_factor[0] == pytorch_layer.scale_factor[1]
resize_type = 2
height_scale = pytorch_layer.scale_factor[0]
width_scale = pytorch_layer.scale_factor[1]
output_height = 0
output_width = 0
layer.param.append('%d' % resize_type)
layer.param.append('%f' % height_scale)
layer.param.append('%f' % width_scale)
layer.param.append('%d' % output_height)
layer.param.append('%d' % output_width)
return layer

BTW,while you convert Pytorch to Caffe/ncnn, how do you know which is one layer's top layers?

Oh that's nice, glad to hear. I also have checked out that ncnn got the upsample functionality recently, but didn't try that yet. If you don't mind, please send a pull request and I can merge this into pytorch2ncnn!

As for the tops and bottoms, I recorded the relations of layers in the computational graph. In pytorch this part of information is not stored as layer parameters which is different from caffe

@starimeL Yes, I would like to send a pull request, maybe in 2 weeks. Do you have some Links about Pytorch computational graph, I haven't find it

Really appreciate that!
But they didn't provide any documentations about such details, maybe you can look into their source code

Hi, @jhtao1860 . Have you successfully used the Interp layer in ncnn?

Hi, @starimeL .

When I use the deconvolution layer to approximate the upsample_bilinear layer, I meet the segmentation core error. Do you have any ideas about this? Thanks.

You can directly use upsample_bilinear in pytorch, the converter will automatically generate a deconv layer approximation.