Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sat Nov 16 16:24:23 2019
  4.  
  5. @author: Tuguldur
  6. """
  7.  
  8. import pickle
  9.  
  10. def unpickle(file):
  11. fo = open(file, 'rb')
  12. dict = pickle.load(fo, encoding='latin1')
  13. fo.close()
  14. return dict
  15.  
  16. import numpy as np
  17.  
  18. def clean(data):
  19. imgs = data.reshape(data.shape[0], 3, 32, 32)
  20. grayscale_imgs = imgs.mean(1)
  21. cropped_imgs = grayscale_imgs[:, 4:28, 4:28]
  22. img_data = cropped_imgs.reshape(data.shape[0], -1)
  23. img_size = np.shape(img_data)[1]
  24. means = np.mean(img_data, axis=1)
  25. meansT = means.reshape(len(means), 1)
  26. stds = np.std(img_data, axis=1)
  27. stdsT = stds.reshape(len(stds), 1)
  28. adj_stds = np.maximum(stdsT, 1.0 / np.sqrt(img_size))
  29. normalized = (img_data - meansT) / adj_stds
  30. return normalized
  31.  
  32. def read_data(directory):
  33. names = unpickle('{}/batches.meta'.format(directory))#['label_names']
  34. print('names', names)
  35.  
  36. data, labels = [], []
  37. for i in range(1, 6):
  38. filename = '{}/data_batch_{}'.format(directory, i)
  39. batch_data = unpickle(filename)
  40. if i == 1:
  41. print('batch_data=', batch_data)
  42. if len(data) > 0:
  43. data = np.vstack((data, batch_data['data']))
  44. labels = np.hstack((labels, batch_data['labels']))
  45. else:
  46. data = batch_data['data']
  47. labels = batch_data['labels']
  48.  
  49. print(np.shape(data), np.shape(labels))
  50. data = clean(data)
  51. data = data.astype(np.float32)
  52. return names, data, labels
  53. names, data, labels = read_data('E:/Machine learning/CNN/Cifar_data/cifar-10-python/cifar-10-batches-py')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement