tfjs icon indicating copy to clipboard operation
tfjs copied to clipboard

How to draw tensor3D type image data (RGB) to screen by WebGL?

Open chenjin3 opened this issue 8 years ago • 9 comments

I want to draw image data in a tensor3D (It's shape is [height, width, 3]) to screen by WebGL instead downloading the data from GPU. Because outPixels.dataSync() is too slow.

chenjin3 avatar May 28 '18 07:05 chenjin3

How big is your image? I've been doing some timing tests and it looks like downloading pixels isn't that slow, it's usually the entire graph that is slow. One way you can test this is:

var start = performance.now();
var y = model(x);
for (var i = 0; i < 9; i++) y = y.add(model(x));
y.dataSync();
var duration = performance.now() - start;

This only requires one texture download, but you can divide duration by 10 to get (approximately) the run time of your model.

kylemcdonald avatar May 29 '18 18:05 kylemcdonald

@kylemcdonald Thank you for your reply. The width and height of my image are 1800 and 1656. It takes about 4s to download the data from GPU. I want to display this image on a canvas quickly in a image supper resolution application.

chenjin3 avatar May 30 '18 03:05 chenjin3

Is it possible to provide a renderTensorPixels2Canvas(pixels, canvas) method to render the corresponding texture data to specified canvas directly?

chenjin3 avatar May 31 '18 03:05 chenjin3

+1 dataSync() blocks until all GPU operations are done, the timing of that function is not just the download. We've seen that rendering with WebGL isn't fast enough to warrant the complexity. If you don't want to block the UI thread in the meantime, you can call await result.data() and then tf.toPixels(result).

nsthorat avatar Oct 26 '18 15:10 nsthorat

Folks

Need help here. The model execution time is very good but dataSync is taking enormous amount of time. This is making lot of models 'not' real-time. Any thoughts or suggestions. Apparently downloading smaller tensors is much faster e.g. downloading tensor with 2 numbers takes about 5-10ms while 50 numbers tensor takes good 50ms.

Thanks for all the suggestions in advance.

rajeev-tbrew avatar Oct 31 '18 11:10 rajeev-tbrew

Hi @rajeev-tbrew, the profiling time of the dataSync is not indicative of the download time. dataSync blocks the UI thread until all of the work is done and the download is complete. The actual time of a download is pretty cheap, overall. A slow dataSync of a predictions means your model is too large, try reducing the size of the model!

nsthorat avatar Oct 31 '18 14:10 nsthorat

Nikhil

It's possible that I am misunderstanding your suggestions. I have measured how much time model.predict takes and how much time datasync takes. The model predict time is about 12ms while datasync is almost 50ms. I did same analysis with faceapi.js models. BTW, on a machine with nvidia 1060, datasync for the same model is about 10ms. Pls note model.predict time remains same on both machines. Please do let me know if you need any further information. Thanks a lot for all your help.

On Wed, 31 Oct 2018, 8:02 pm Nikhil Thorat <[email protected] wrote:

Hi @rajeev-tbrew https://github.com/rajeev-tbrew, the profiling time of the dataSync is not indicative of the download time. dataSync blocks the UI thread until all of the work is done and the download is complete. The actual time of a download is pretty cheap, overall. A slow dataSync of a predictions means your model is too large, try reducing the size of the model!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/tensorflow/tfjs/issues/351#issuecomment-434709493, or mute the thread https://github.com/notifications/unsubscribe-auth/Ak0dzTON1d1kErT0Rs3QdLx124rSgDloks5uqbRvgaJpZM4UPwfC .

rajeev-tbrew avatar Nov 01 '18 14:11 rajeev-tbrew

If you time a model.predict() the only thing you are actually measuring is the time it takes to schedule the operations on the GPU, not the actual time it takes to run them (calling ops are sync, if we synced at each op call then you would block the UI thread). The synchronization between CPU and GPU happens with a dataSync or a data call -- to time a predict properly (meaning, time all of the operations from start to finish on the GPU) you must introduce a sync between the CPU and GPU.

Hope this helps.

nsthorat avatar Nov 01 '18 14:11 nsthorat

Automatically closing due to lack of recent activity. Please update the issue when new information becomes available, and we will reopen the issue. Thanks!

rthadur avatar Nov 30 '18 22:11 rthadur