torch.Tensor() sometimes (inconsistently) slow

username: jkoubele
cooked: <p>Hello,<br>
I have a video procesing pipeline that reads frames from video and feeds them to pytorch model. I observed that the call of torch.Tensor() sometimes takes significantly more time than expected.</p>
<p>The pipeline can be simulated by script like this:</p>
<pre><code class="lang-auto">import torch
import numpy as np
import cv2
from time import time
import matplotlib.pyplot as plt

        
video = cv2.VideoCapture("video.mp4")

processing_times = []

for i in range(1000):
    _, frame = video.read() # each frame is numpy array of shape (1080,1920,3)
    frame = np.copy(frame)
   
    t_start = time()
    x = torch.Tensor(frame)
    t_end = time()    
    
    processing_times.append(t_end - t_start)    
    
 
plt.plot(processing_times)
plt.savefig('plot.png')
video.release()
</code></pre>
<p>Note that the time is measured only on the call of torch.Tensor(frame) and does not include reading the frame from video.<br>
The plot from the script looks like this:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/8/5/859dfe0026459f6545c99c7605b63ac6e03fcd0e.png" alt="plot_spikes" data-base62-sha1="j42013ArUyeIbWXW1aSGKagusKG" width="640" height="480"><br>
So the call of torch.Tensor(frame) takes usually about 25ms, but sometimes spikes up to the 90 ms.<br>
Also, the call time seems to be random, when I re-run the script, the graph looks differently (so it’s not the particular frames that causes the long call time).<br>
Sometimes, when re-running the script, the plot is almost withou any spikes (the call time is always between 20-30ms).</p>
<p>My system:<br>
Ubuntu 19.10<br>
Python 3.7<br>
Torch 1.5.1</p>
<p>I run the script from terminal without IDE (to avoid some garbage that may cause the long call time).</p>
<p>So I have no idea what may cause the call of torch.Tensor() to take sometimes much longer time thats usual. I would be grateful for any advices / comments.</p>
<p>edit: I observed that removing the<br>
<code>frame = np.copy(frame)</code><br>
line seems to reduce the problem, the number of spikes is then much lower (although it still may happen that one call takes about 90 ms). The PyTorch tensors share the memory buffer of NumPy ndarrays which may be part of the problem, hovewer I still don’t see how this translates to the sometimes longer torch.Tensor()  call?</p>
<p>-Jakub</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="jkoubele" data-post="1" data-topic="87610">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/j/e56c9b/48.png" class="avatar"> jkoubele:</div>
<blockquote>
<p>The PyTorch tensors share the memory buffer of NumPy ndarrays which may be part of the problem, hovewer I still don’t see how this translates to the sometimes longer torch.Tensor() call?</p>
</blockquote>
</aside>
<p>This shouldn’t be the case in your code snippet, as you are creating a new tensor and thus trigger a copy.<br>
To share the same underlying data, you would have to use <code>torch.from_numpy</code> as seen here:</p>
<pre data-code-wrap="python"><code class="lang-python">frame = np.ones((2, 2))
x = torch.Tensor(frame)

x[0, 0] = 2.
print(x) 
&gt; tensor([[2., 1.],
          [1., 1.]])
print(frame)
&gt; [[1. 1.]
   [1. 1.]]

y = torch.from_numpy(frame)
y[0, 0] = 2.
print(y)
&gt; tensor([[2., 1.],
          [1., 1.]], dtype=torch.float64)
print(frame)
&gt; [[2. 1.]
   [1. 1.]]
</code></pre>