text: from __future__ import print_function
  from keras.preprocessing.image import ImageDataGenerator
  import numpy as np 
  import os
  import glob
  import skimage.io as io
  import skimage.transform as trans
  
  
  def adjustData(img,mask,flag_multi_class,num_class):
      if(flag_multi_class):
          img = img / 255
          mask = mask[:,:,:,0] if(len(mask.shape) == 4) else mask[:,:,0]
          new_mask = np.zeros(mask.shape + (num_class,))
          for i in range(num_class):
              #for one pixel in the image, find the class in mask and convert it into one-hot vector
              #index = np.where(mask == i)
              #index_mask = (index[0],index[1],index[2],np.zeros(len(index[0]),dtype = np.int64) + i) if (len(mask.shape) == 4) else (index[0],index[1],np.zeros(len(index[0]),dtype = np.int64) + i)
              #new_mask[index_mask] = 1
              new_mask[mask == i,i] = 1
          new_mask = np.reshape(new_mask,(new_mask.shape[0],new_mask.shape[1]*new_mask.shape[2],new_mask.shape[3])) if flag_multi_class else np.reshape(new_mask,(new_mask.shape[0]*new_mask.shape[1],new_mask.shape[2]))
          mask = new_mask
      elif(np.max(img) > 1):
          img = img / 255
          mask = mask /255
          mask[mask > 0.5] = 1
          mask[mask <= 0.5] = 0
      return (img,mask)
  
  
  
  def trainGenerator(batch_size,train_path,image_folder,mask_folder,aug_dict,image_color_mode = "grayscale",
                      mask_color_mode = "grayscale",image_save_prefix  = "image",mask_save_prefix  = "mask",
                      flag_multi_class = False,num_class = 2,save_to_dir = None,target_size = (None, None),seed = 1):
      '''
      can generate image and mask at the same time
      use the same seed for image_datagen and mask_datagen to ensure the transformation for image and mask is the same
      if you want to visualize the results of generator, set save_to_dir = "your path"
      '''
      image_datagen = ImageDataGenerator(**aug_dict)
      mask_datagen = ImageDataGenerator(**aug_dict)
      image_generator = image_datagen.flow_from_directory(
          train_path,
          classes = [image_folder],
          class_mode = None,
          color_mode = image_color_mode,
          target_size = target_size,
          batch_size = batch_size,
          save_to_dir = save_to_dir,
          save_prefix  = image_save_prefix,
          seed = seed)
      mask_generator = mask_datagen.flow_from_directory(
          train_path,
          classes = [mask_folder],
          class_mode = None,
          color_mode = mask_color_mode,
          target_size = target_size,
          batch_size = batch_size,
          save_to_dir = save_to_dir,
          save_prefix  = mask_save_prefix,
          seed = seed)
      train_generator = zip(image_generator, mask_generator)
      for (img,mask) in train_generator:
          img,mask = adjustData(img,mask,flag_multi_class,num_class)
          yield (img,mask)
  
  def saveResult(save_path,npyfile,flag_multi_class = False,num_class = 2):
      for i,item in enumerate(npyfile):
          img = labelVisualize(num_class,COLOR_DICT,item) if flag_multi_class else item[:,:,0]
          io.imsave(os.path.join(save_path,"%d_predict.png"%i),img)