Forums - SNPE block the UI Thread

2 posts / 0 new
Last post
SNPE block the UI Thread
meow meow
Join Date: 11 Mar 18
Posts: 6
Posted: Tue, 2018-03-20 18:57

 

hi everyone,

android phone with 835 cpu, snpe 1.13.0.0

i build and run the sample, when the time i execute the NeuralNetwork in other thread (not ui thread).

new Thread() {
    void run() {
        nn.execute(inputs)
    }
}.start() ;

it will block the ui thread, , android users will get bad UX.

nn = new SNPE.NeuralNetworkBuilder(application)
        .setPerformanceProfile(NeuralNetwork.PerformanceProfile.DEFAULT)
        .setRuntimeOrder(order) // using gpu
        .setModel( example model )
        .build();

How can i config the SNPE, the Android UI is the high priority, Neural is the lower priority.

 

Tensowflow Mobile:

Running in background will not block the ui, but take 4 seconds,

but we can batch in the background, user will get batter UX.

SNPE 1.13

execute by GPU but will blcok the UI, take 1 seconds, how can i execute in background?

 

  • Up0
  • Down0
gesqdn-forum
Join Date: 4 Nov 18
Posts: 184
Posted: Thu, 2019-05-09 03:04

Hi,

Android blocks the UI rendering if any heavy operation is done in the main thread.
You can try any of the below options to load network using a thread,
1. Use Thread to build neural network, create thread with Handler to infer if the network is successfully built.
2. Use Async Task, Call loadNeuralNetwork() method in doInBackground() and update the UI in onPostExecute() method.

Use below code as the reference to use Thread with Handler.
Start a thread as shown below whenever you want to build the neural network.


new Thread(mMessageSender).start();
private final Runnable mMessageSender = new Runnable() {
    public void run() {
        Message msg = mHandler.obtainMessage();
        Bundle bundle = new Bundle();
        bundle.putString("MSG_KEY", loadNeuralNetwork());
        msg.setData(bundle);
        mHandler.sendMessage(msg);
    }
};
private String loadNeuralNetwork() {
    try {
        
        mNeuralNetwork = new SNPE.NeuralNetworkBuilder(mApplication)
                .setPerformanceProfile(NeuralNetwork.PerformanceProfile.DEFAULT)
                .setRuntimeOrder(mRuntime) // using gpu
                .setModel(mModel.file)
                .build();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return mNeuralNetwork.toString();
}
private final Handler mHandler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        Bundle bundle = msg.getData();
        String string = bundle.getString("MSG_KEY");
       //Update your UI
    }
};

 

  • Up0
  • Down0
or Register

Opinions expressed in the content posted here are the personal opinions of the original authors, and do not necessarily reflect those of Qualcomm Incorporated or its subsidiaries (“Qualcomm”). The content is provided for informational purposes only and is not meant to be an endorsement or representation by Qualcomm or any other party. This site may also provide links or references to non-Qualcomm sites and resources. Qualcomm makes no representations, warranties, or other commitments whatsoever about any non-Qualcomm sites or third-party resources that may be referenced, accessible from, or linked to this site.