lucasb-eyer / pydensecrf

Python wrapper to Philipp Krähenbühl's dense (fully connected) CRFs with gaussian edge potentials.
MIT License
1.93k stars 411 forks source link

Inference runs much slower in python 2.7 than 3.6 #105

Open jdonovanCS opened 3 years ago

jdonovanCS commented 3 years ago

I am currently running the same python code doing inference in two different python versions with very different efficiencies. First I run it in python 2.7, which is the target version of this code. Second, I run it in python 3.6. I use timeit to get the performance time. It takes 10 times as long to compute the inference in python 2.7 as it does in python 3.6. I'm wondering why that might be.

I can provide the code and dependency details if needed, but I'm wondering if it is something that is a known consequence of using pydensecrf inference.

jdonovanCS commented 3 years ago
import timeit
import cv2
import pydensecrf.densecrf as dcrf
import pydensecrf.utils as utils

def densecrf_inference(predictions, image_location, input_image=None):
    img = input_image
    img = cv2.resize(img, dsize=(predictions.shape[2], predictions.shape[1]), interpolation=cv2.INTER_LANCZOS4)

    d = dcrf.DenseCRF2D(predictions.shape[2], predictions.shape[1], 23)
    U = predictions.reshape((23,-1))
    U = U.astype(np.float32)

    d.setUnaryEnergy(-np.log(U))

    # For RGB
    d.addPairwiseBilateral(sxy=(80,80), srgb=(13,13,13), rgbim=img, compat=10)

    # Q = d.inference(5)
    # Time: < .6s in 3.6; > 10s in 2.7
    print('inference: {}'.format(timeit.timeit(lambda: d.inference(5), number=1)))

if __name__ == '__main__':
    with open('predictions.npy', 'rb') as f:
        predictions = np.load(f)
    with open('input_image.npy', 'rb') as f:
        input_image = np.load(f)
    print('densecrf function: {}'.format(timeit.timeit(lambda: densecrf_inference(predictions, None, input_image), number=1)))
jdonovanCS commented 3 years ago
python3 densecrf_efficiency_example.py 
inference: 0.5766793650000182
densecrf function: 0.8799495560015202

python densecrf_efficiency_example.py 
inference: 10.5400309563
densecrf function: 11.7004370689
jdonovanCS commented 3 years ago

Interestingly enough, I don't think that the python versions are the issue. I installed the necessary pip packages and ran the code on another machine (mac OS), and python 2.7 and 3.7 have very similar timings. Whereas this is running on my ubuntu machine and has very different timings. I'm not sure if it has something to do with the setup / install of pydensecrf or some dependency or what. Any ideas are helpful at this point. I've been trying everything I can think of.