title: Best way to load frames of video with tensorflow
body: <p>I was working on a video classification problem. the data is given as frames per video so I created a generator that takes an array of videos with dim = (batch_size, #frames, IMG_SIZE, IMG_SIZE, 3) with <code>keras.utils.Sequence</code></p>
  <p>I was working on Kaggle notebooks and the code was working fine then I used the same code in a python script to run it on my machine and I found that the code was not using the GPU</p>
  <p>I did some research and some people suggested that to use <code>tf.data</code> instead of <code>keras.utils.Sequence</code> but I didn't find a way to do that</p>
  <pre><code>from skimage.io import imread
  from skimage.transform import resize
  import math
  
  class imgs_gen(tf.keras.utils.Sequence):
  
      def __init__(self, x_set, y_set, batch_size, IMG_SIZE):
          self.x, self.y = x_set, y_set
          self.batch_size = batch_size
          self.img_size = IMG_SIZE
  
      def __len__(self):        
          return int(len(self.x) / self.batch_size)
  
  
      def __getitem__(self, idx):
          
          vedios_batch = self.x[idx * self.batch_size : (idx + 1) * self.batch_size]
          batch_y = [int(x[3:]) - 1 for x in  self.y[idx * self.batch_size : (idx + 1) * self.batch_size]]
          
         
          X = np.array([[resize(imread(file_name), (self.img_size, self.img_size)) for file_name in imgs_batch] for imgs_batch in vedios_batch])
          y = tf.keras.utils.to_categorical(batch_y , num_classes=16)
          
         
          return X, y
  
  </code></pre>