title: Create a function similar to mnist.train.next_batch(128) for custom image data
body: <p>I am trying to implement the function: <code>batch_x, batch_y = mnist.train.next_batch(128)</code> for my dataset of images.</p>
  
  <p>For training, I have 1000 folders (the name of the folder is the same as which class it belongs to "Class1", "Class2", "Class3" and so on).  Each of these folders conatain 500 images belonging to a single class. </p>
  
  <p>So in all, I have 500,000 images which have to be trained. </p>
  
  <p>What is the best way for me to divide them into batches (where images are randomly selected) and feed to my inference model?</p>
  
  <p>I am not using Keras, and using Tensorflow 1.15. </p>
  
  <p>This is my code:</p>
  
  <pre><code>import tensorflow as tf
  tf.enable_eager_execution()
  import numpy as np
  import os
  import pathlib
  tf.__version__
  
  AUTOTUNE = tf.data.experimental.AUTOTUNE
  SHUFFLE_BUFFER_SIZE = 100
  BATCH_SIZE = 128
  IMG_WIDTH = 128
  IMG_HEIGHT = 256
  DATA_DIR = 'D:/PythonWorkspace/train'
  DATA_DIR = pathlib.Path(DATA_DIR) #RD
  
  def get_label(file_path):
    # convert the path to a list of path components
    parts = tf.strings.split(file_path, os.path.sep)
    # The second to last is the class-directory
    return parts[-2] == CLASS_NAMES
  
  def decode_img(img):
    # convert the compressed string to a 3D uint8 tensor
    img = tf.image.decode_jpeg(img, channels=3)
    # Use `convert_image_dtype` to convert to floats in the [0,1] range.
    img = tf.image.convert_image_dtype(img, tf.float32)
    # resize the image to the desired size.
    return tf.image.resize(img, [IMG_WIDTH, IMG_HEIGHT])
  
  def process_path(file_path):
    label = get_label(file_path)
    # load the raw data from the file as a string
    img = tf.io.read_file(file_path)
    img = decode_img(img)
    return img, label
  
  #aataset = tf.data.Dataset.list_files(os.path.join(DATA_DIR,'*/*'))
  dataset = tf.data.Dataset.list_files(str(DATA_DIR/'*/*')) #RD
  for f in dataset.take(5):
    print(f.numpy())
  dataset = dataset.map(process_path, num_parallel_calls=AUTOTUNE)
  dataset = dataset.shuffle(buffer_size=SHUFFLE_BUFFER_SIZE)
  dataset = dataset.repeat()
  dataset = dataset.batch(BATCH_SIZE)
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>'1.15.2'
  
  b'D:\\PythonWorkspace\\train\\1403\\5T04015F015.jpg'
  b'D:\\PythonWorkspace\\train\\0525\\C3T0020F097.jpg'
  b'D:\\PythonWorkspace\\train\\0005\\24T0060F004.jpg'
  b'D:\\PythonWorkspace\\train\\1159\\45T0008F041.jpg'
  b'D:\\PythonWorkspace\\train\\0425\\C5T0021F007.jpg'
  
  ---------------------------------------------------------------------------
  AttributeError                            Traceback (most recent call last)
  &lt;ipython-input-2-b683dfeb641b&gt; in &lt;module&gt;
       32 for f in dataset.take(5):
       33   print(f.numpy())
  ---&gt; 34 dataset = dataset.map(process_path, num_parallel_calls=AUTOTUNE)
       35 dataset = dataset.shuffle(buffer_size=SHUFFLE_BUFFER_SIZE)
       36 dataset = dataset.repeat()
  
  ....
  ....
  ....
  AttributeError: in converted code:
  
      &lt;ipython-input-2-b683dfeb641b&gt;:24 process_path  *
          label = get_label(file_path)
      &lt;ipython-input-2-b683dfeb641b&gt;:11 get_label  *
          parts = tf.strings.split(file_path, os.path.sep)
      d:\venv\lib\site-packages\tensorflow_core\python\ops\ragged\ragged_string_ops.py:642 strings_split_v1
          return ragged_result.to_sparse()
  
      AttributeError: 'Tensor' object has no attribute 'to_sparse'
  </code></pre>