title: How to convert all the images into all the sub-folders and convert it into an h5 file?
body: <p>In below code I have read images from the directory as shown in code and the labels and using matplotlib I am able to plot the images, but I am having problem to save all these data in an h5 file.I have 15 classes(i.e. 15 folders) in folder <code>PlantVillage</code>.</p>
  
  <pre class="lang-py prettyprint-override"><code>train_path = '/home/snehil/Desktop/plantdisease/plantvillage/PlantVillage'
  
  train_x = '/home/snehil/Desktop/plantdisease/plantvillage/PlantVillage/Pepper__bell___Bacterial_spot'
  
  train_batches = ImageDataGenerator().flow_from_directory(
      train_path,
      target_size=(224, 224),
      classes=[
          'Pepper__bell___Bacterial_spot',
          'Pepper__bell___healthy', 
          'Potato___Early_blight',
          'Potato___healthy',
          'Potato___Late_blight', 
          'Tomato_Bacterial_spot',
          'Tomato_Early_blight',
          'Tomato_healthy',
          'Tomato_Late_blight',
          'Tomato_Leaf_Mold',
          'Tomato_Septoria_leaf_spot',
          'Tomato_Spider_mites_Two_spotted_spider_mite',
          'Tomato__Target_Spot',
          'Tomato__Tomato_mosaic_virus', 
          'Tomato__Tomato_YellowLeaf__Curl_Virus'
       ],
       batch_size=10
  )
  
  
  # plots images with labels within jupyter notebook
  def plots(ims, figsize=(12, 6), rows=1, interp=False, titles=None):
      if type(ims[0]) is np.ndarray:
          ims = np.array(ims).astype(np.uint8)
          if (ims.shape[-1] != 3):
              ims = ims.transpose((0, 2, 3, 1))
  
      f = plt.figure(figsize=figsize)
  
      cols = len(ims) // rows if len(ims) % 2 == 0 else len(ims) // rows + 1
  
      for i in range(len(ims)):
          sp = f.add_subplot(rows, cols, i+1)
          sp.axis('Off')
          if titles is not None:
              sp.set_title(titles[i], fontsize=16)
          plt.imshow(ims[i], interpolation=None if interp else 'none')
  
  
  imgs, labels = next(train_batches)
  plots(imgs, titles=labels)
  </code></pre>
  
  <p>And I have tried this piece of code, and it's just consuming all my RAM.</p>
  
  <pre><code>import h5py
  h5_train = h5py.File("train_x.h5", 'w')
  h5_train.create_dataset("data_train", data=np.array(train_batches))
  print(h5_train)
  h5_train.close()
  </code></pre>