marcoancona / DeepExplain

A unified framework of perturbation and gradient-based attribution methods for Deep Neural Networks interpretability. DeepExplain also includes support for Shapley Values sampling. (ICLR 2018)
https://arxiv.org/abs/1711.06104
MIT License
720 stars 133 forks source link

If I run de.explain multiple times, it becomes slower and slower. #34

Closed linchundan88 closed 5 years ago

linchundan88 commented 5 years ago

Thank for your work.

If I run de.explain multiple times, it becomes slower and slower.

This problem has not been solved in v0.2.

linchundan88 commented 5 years ago

I developed a RPC service to generate saliency maps using your great library. I load my models and create DeepExplain session before hand, for every request from RPC client, RPC service invode de.explain. So I found it becomes slower and slower.

marcoancona commented 5 years ago

So, if I understood correctly, you call multiple times explain() with a different xs but the same Tensors T and X?

linchundan88 commented 5 years ago
    with DeepExplain(session=K.get_session()) as de:  # <-- init DeepExplain context
        input_tensor = model.layers[0].input

        fModel = Model(inputs=input_tensor, outputs=model.layers[-1].output)
        target_tensor = fModel(input_tensor)

        xs = img_preprocess
        ys = keras.utils.to_categorical([pred], NUM_CLASSES)

        for i in range(TEST_NUM):
            time1 = time.time()  #2 second faster than before   DeepExplain(session=K.get_session()) as de:
            attributions = de.explain('deeplift', target_tensor * ys, input_tensor, xs, batch_size=1)
            # attributions = de.explain('deeplift', target_tensor, input_tensor, xs, ys=ys)
            # attributions = de.explain('elrp', target_tensor * ys, input_tensor, xs, batch_size=1)
            # attributions = de.explain('shapley_sampling', target_tensor, input_tensor, xs, ys=ys, samples=100)
            time2 = time.time()
            print(time2-time1)