title: np_expand and np.concatenate on large dataset
body: <p>I am performing some data transformation (expand dimension for each image, then concatenate them for training) on a relatively large dataset: 62,000 face images from the wiki dataset. However, using the code below, I have been loading the data for more than 6 hours now and it is still not complete... How could I improve the process of loading images here? The below code is from the book <a href="https://rads.stackoverflow.com/amzn/click/com/1789136679" rel="nofollow noreferrer" rel="nofollow noreferrer">Generative Adversarial Networks Projects</a>. The whole code can be found <a href="https://github.com/PacktPublishing/Generative-Adversarial-Networks-Projects/tree/master/Chapter03" rel="nofollow noreferrer">here</a></p>
  <pre><code>def load_images(data_dir, image_paths, image_shape):
  images = None
  
  for i, image_path in enumerate(image_paths):
      print()
      try:
          # Load image
          loaded_image = image.load_img(os.path.join(data_dir, image_path), target_size=image_shape)
  
          # Convert PIL image to numpy ndarray
          loaded_image = image.img_to_array(loaded_image)
  
          # Add another dimension (Add batch dimension)
          loaded_image = np.expand_dims(loaded_image, axis=0)
  
          # Concatenate all images into one tensor
          if images is None:
              images = loaded_image
          else:
              images = np.concatenate([images, loaded_image], axis=0)
      except Exception as e:
          print(&quot;Error:&quot;, i, e)
  
  return images
  </code></pre>