See if this works for you:

  
      

      TensorFlow
  

  
    

tf.data: Build TensorFlow input pipelines  |  TensorFlow Core 4



  

  
    
    
  

  


Earlier this summer I implemented a stratified sampler with tf.data that you could refer to as well:

  

      github.com
  

  
    sayakpaul/PAWS-TF/blob/main/utils/labeled_loader.py 2


    from . import multicrop_loader, config
import tensorflow as tf
import numpy as np
import os

GLOBAL_SCALE = [0.75, 1.0]
AUTO = tf.data.AUTOTUNE
(X_TRAIN, Y_TRAIN), (_, _) = tf.keras.datasets.cifar10.load_data()


def onehot_encode(labels, label_smoothing=0.1):
    """
    One-hot encode labels with label smoothing.

    :param labels: (batch_size, )
    return: one-hot encoded labels with optional label smoothing
    """
    labels = tf.one_hot(labels, depth=10)
    # Reference: https://t.ly/CSYO)
    labels *= 1.0 - label_smoothing



  This file has been truncated. show original

  

  
    
    
  

  


The script is a bit involved so please feel free to ask questions as needed.