Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. import os
  2. import numpy as np
  3. from PIL import Image
  4.  
  5. def unpickle(file):
  6. import pickle
  7. with open(file, 'rb') as fo:
  8. dict = pickle.load(fo, encoding='bytes')
  9. return dict
  10.  
  11. def rowToFile(row, label, filename):
  12. label = str(label)
  13. if not os.path.exists(label):
  14. os.mkdir(label)
  15. dim = 32
  16. wholeImg = list()
  17. for i in range(dim):
  18. oneRow = list()
  19. for j in range(dim):
  20. oneRGB = list()
  21. for k in range(3): # RGB
  22. pix = row[1024*k + 32*i + j]
  23. oneRGB.append(pix)
  24. oneRow.append(oneRGB)
  25. wholeImg.append(oneRow)
  26. wholeImg = np.array(wholeImg)
  27. img = Image.fromarray(wholeImg)
  28. img.save(os.path.join(label, filename.decode('ascii')))
  29. img.close()
  30.  
  31. trainFiles = ['data_batch_1', 'data_batch_2', 'data_batch_3', 'data_batch_4', 'data_batch_5']
  32. testFiles = ['test_batch']
  33.  
  34. files = testFiles
  35.  
  36. for oneFile in files:
  37. oneBatch = unpickle(oneFile)
  38. labels = oneBatch[b'labels']
  39. data = oneBatch[b'data']
  40. filenames = oneBatch[b'filenames']
  41. for i in range(len(labels)):
  42. rowToFile(data[i], labels[i], filenames[i])
  43. print(oneFile + " is finished")
  44.  
  45. print("Everything is finished")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement