title: tensforflow 2 model training make no progress for keras.utils Sequence data generator
body: <p>I would like to train a neural network model (built by tf2.8 and keras) on EC2 instance (cpu only, e.g. m4) from jupyter.</p>
  <p>In order to improve training runtime, I want to use &quot;multiprocessing&quot; for keras.model.fit().</p>
  <p>My code:</p>
  <pre><code>import tensorflow as tf
  from tensorflow import keras
  from tensorflow.keras.utils import Sequence 
  
  class DataGenerator(Sequence):
      def __init__(self, train_file_paths, batch_size, total_data_size, epoch,
               num_parallel_calls=tf.data.AUTOTUNE):
  
          def process_samples(input_ds):
              features = tf.io.parse_single_example(input_ds, feaure_names)
              labels = tf.io.parse_single_example(input_ds, label_name)['label']
              return (features, labels)
      
          train_dataset = tf.data.TFRecordDataset(train_file_paths, compression_type='GZIP')
      
          train_dataset = train_dataset.map(lambda x: process_samples(x), num_parallel_calls=num_parallel_calls)
      
          train_dataset = train_dataset.shuffle(300)
          train_dataset = train_dataset.repeat(epoch) #2588
         
          train_dataset = train_dataset.batch(batch_size, drop_remainder=True)
          self.train_dataset = train_dataset.prefetch(batch_size)
  
          self.total_data_size = total_data_size
          self.batch_size = batch_size
            
      def __len__(self):
          return np.math.ceil(self.total_data_size/self.batch_size)
  
      def __getitem__(self, idx):
          features = next(iter(self.train_dataset))
          return features
  
  
  my_train_data_gen = DataGenerator(train_file_paths, batch_size=4, total_data_size=10000, epoch=10) 
  my_test_data_gen = DataGenerator(test_file_paths, batch_size=4, total_data_size=500, epoch=10) 
  
  model.fit(my_train_data_gen,
        epochs=10, 
        steps_per_epoch = np.math.floor(train_data_size/4),
        validation_data=my_test_data_gen,
        validation_steps=np.math.ceil(test_data_size/4),
        workers=8,
        use_multiprocessing=True)
  </code></pre>
  <p>But, the code is stuck in the beginning of the first epoch and does not move forward. No errors popped !</p>
  <pre><code>DataGenerator, train_dataset type is &lt;class 'tuple'&gt; size is 2
  
   Epoch 1/2
  </code></pre>
  <p>No any progress, all cpu cores on the EC2 instance are idle.
  But, the jupyter notebook is still running.
  I have checked <a href="https://www.tensorflow.org/api_docs/python/tf/keras/utils/Sequence" rel="nofollow noreferrer">https://www.tensorflow.org/api_docs/python/tf/keras/utils/Sequence</a>,
  In order to speed up training process, what could be better choice for keras.utils.Sequence or tf.data ?</p>
  <p>Could anybody help me with this?</p>