Hanqer / deep-hough-transform

Jittor and Pytorch code for paper "Deep Hough Transform for Semantic Line Detection" (ECCV 2020, PAMI 2021)
344 stars 71 forks source link

TorchScript #15

Closed RocketFlash closed 3 years ago

RocketFlash commented 3 years ago

Hi! Is it possible to convert trained model to torchscript using torch.jit.trace or torch.jit.script ? I tried to convert trained model, it can be converted using torch.jit.trace but I can't save it. Saving gives an error:

RuntimeError: 
Could not export Python function call 'C_dht_Function'. Remove calls to Python functions before export. Did you forget to add @script or @script_method annotation? If this is a nn.ModuleList, add it to __constants__:
zeakey commented 3 years ago

JIT compiler supports only bullion operators. The DHT module is a C++ extension that cannot be recognized by the JIT compiler.

An ugly workaround here is to only convert the backbone and then fed the features to DHT module.

RocketFlash commented 3 years ago

@zeakey What if I reimplement DHT on PyTorch? How much do you think inference will slow down? If DHT will be on pure PyTorch, so it will be easy to convert the model to Torchscript and run the code on CPU, righ ?

Hanqer commented 3 years ago

@RocketFlash The naive version of DHT that does not parallel at all will slow down about 1000x.

RocketFlash commented 3 years ago

OMG :(

RocketFlash commented 3 years ago

Do you have pure torch implementation code? Could you share it if you have ?

Hanqer commented 3 years ago

@RocketFlash Sorry, the implementation of the python version of DHT is only used in the early time of the project to verify the correctness and speedup. But I can't find it now. It just uses many loop blocks and you can refer to the OpenCV implementation (HT) on a single feature map.

RocketFlash commented 3 years ago

@Hanqer could you explain, why you're adding value from featuremap in cuda code?

float val = feat[imgIndex];
atomicAdd(&(output[outIndex]), val);

I didn't get an idea, why not just adding 1 in accumulator?

Hanqer commented 3 years ago

@RocketFlash DHT accumulates the feature values instead of counting the quantized number. This is different from the original Hough Transform. You can refer to Eq.(4) in the paper.

RocketFlash commented 3 years ago

Got it, thank you ! :)