Guest User

Untitled

a guest
Jun 22nd, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. import numpy as np
  2. import os
  3. from os import listdir
  4. from scipy import ndimage, misc
  5. from keras.utils import to_categorical
  6. from sklearn.model_selection import train_test_split
  7. from sklearn.utils import shuffle
  8.  
  9. dataset_basepath = 'dataset/'
  10.  
  11. def process_data(path):
  12. try:
  13. X = np.load('data/X.npy')
  14. Y = np.load('data/Y.npy')
  15. print("Data already processed. Loading now.")
  16. except:
  17. print("Need to process data...")
  18. labels = sorted(listdir(path))
  19. X = []
  20. Y = []
  21. for i in labels:
  22. images_path = '{}{}'.format(path ,i)
  23. images = listdir(images_path)
  24. for img in images:
  25. image_path = '{}{}/{}'.format(path, i, img)
  26. image_data = ndimage.imread(image_path, mode="RGB")
  27. image_resized = misc.imresize(image_data, (64, 64))
  28. X.append(image_resized)
  29. Y.append(i)
  30. # Normalize the X data to zero mean
  31. X = np.array(X)/255
  32. # Convert labels to one-hot encoded vector
  33. Y = to_categorical(Y)
  34.  
  35. if not os.path.exists('data/'):
  36. os.makedirs('data/')
  37. np.save('data/X.npy', X)
  38. np.save('data/Y.npy', Y)
  39. return X, Y
  40.  
  41. X, Y = process_data(dataset_basepath)
  42. X, Y = shuffle(X, Y, random_state=0)
  43.  
  44. X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.25, random_state=42)
Add Comment
Please, Sign In to add comment