Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inference runs much slower in python 2.7 than 3.6 #105

Open
jdonovanCS opened this issue Dec 18, 2020 · 3 comments
Open

Inference runs much slower in python 2.7 than 3.6 #105

jdonovanCS opened this issue Dec 18, 2020 · 3 comments

Comments

@jdonovanCS
Copy link

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
Copy link
Author

jdonovanCS commented Dec 18, 2020

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
Copy link
Author

jdonovanCS commented Dec 18, 2020

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant