Search code examples
javascalaakkadeeplearning4j

Deeplearning4j Sharing Computational Graph between Threads in Scala


I'm trying to do image classification. I'm using Scala, the Akka actor system, and deeplearning4j. The thing is that I have to detect always on the same spots or crop on the image. I was thinking of creating a new actor for each crop of the image, on each frame. The thing is that, from what I understand, instantiating a new model for each actor creation is not viable, but having an instance of the model, and passing to each actor isn't either. Should I have a pool of instances? I'm a bit stuck with this problem, since it is the first time I'm trying deeplearning4j. Previously, I would use a python REST api, but I think that this solution should perform better.

Thank you in advance.


Solution

    1. There is no need to instantiate a new actor for each crop of the image. Simply keep a pool of actors that ask a master node to give them more images to classify as soon as they are done with the previous image. I also suggest to check whether it actually buys you anything compared to the ordinary parallel collections (sth. like images.par.map(model.classify) could already do the job; it would take care of a thread pool all by itself).
    2. There is no need to instantiate a new model for each actor. The classification does not mutate the model, so you can simply share the same model between all actors. That's like 8 bytes overhead per actor for a reference to the model object, therefore negligible.