Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. from __future__ import absolute_import
  2. from __future__ import division
  3. from __future__ import print_function
  4.  
  5. import tarfile
  6. import os
  7. import pickle as pkl
  8. import numpy as np
  9. import skimage
  10. import skimage.io
  11. import skimage.transform
  12. from tensorflow.examples.tutorials.mnist import input_data
  13. import cv2
  14. import PIL
  15.  
  16. mnist = input_data.read_data_sets('MNIST_data')
  17.  
  18.  
  19.  
  20. BST_PATH = 'BSR_bsds500.tgz'
  21.  
  22. rand = np.random.RandomState(42)
  23.  
  24. f = tarfile.open(BST_PATH)
  25. train_files = []
  26. for name in f.getnames():
  27. if name.startswith('BSR/BSDS500/data/images/train/'):
  28. train_files.append(name)
  29.  
  30. print('Loading BSR training images')
  31. background_data = []
  32. for name in train_files:
  33. try:
  34. fp = f.extractfile(name)
  35. bg_img = skimage.io.imread(fp)
  36. background_data.append(bg_img)
  37. except:
  38. continue
  39.  
  40.  
  41. def compose_image(digit, background):
  42. """Difference-blend a digit and a random patch from a background image."""
  43. w, h, _ = background.shape
  44. dw, dh, _ = digit.shape
  45. x = np.random.randint(0, w - dw)
  46. y = np.random.randint(0, h - dh)
  47.  
  48. bg = background[x:x+dw, y:y+dh]
  49. return np.abs(bg - digit).astype(np.uint8)
  50.  
  51.  
  52. def mnist_to_img(x):
  53. """Binarize MNIST digit and convert to RGB."""
  54. x = (x > 0).astype(np.float32)
  55. d = x.reshape([28, 28, 1]) * 255
  56. return np.concatenate([d, d, d], 2)
  57.  
  58.  
  59. def create_mnistm(X,labels,train=True):
  60. """
  61. Give an array of MNIST digits, blend random background patches to
  62. build the MNIST-M dataset as described in
  63. http://jmlr.org/papers/volume17/15-239/15-239.pdf
  64. """
  65. X_ = np.zeros([X.shape[0], 28, 28, 3], np.uint8)
  66.  
  67. for i in range(X.shape[0]):
  68.  
  69. if i % 1000 == 0:
  70. print('Processing example', i)
  71.  
  72. bg_img = rand.choice(background_data)
  73.  
  74. d = mnist_to_img(X[i])
  75. d = compose_image(d, bg_img)
  76.  
  77. X_[i] = d
  78. # print(X_[i])
  79. # print(labels[i])
  80. if train:
  81. if(not os.path.exists("mnistm_png/train/"+str(labels[i]))):
  82. os.mkdir("mnistm_png/train/"+str(labels[i]))
  83.  
  84. fullpath = "mnistm_png/train/"+str(labels[i])+"/"+str(1000+i)+".png"
  85. cv2.imwrite(fullpath,X_[i])
  86. print("saved",fullpath)
  87. else:
  88. if(not os.path.exists("mnistm_png/val/"+str(labels[i]))):
  89. os.mkdir("mnistm_png/val/"+str(labels[i]))
  90. rgb = cv2.cvtColor(X_[i], cv2.COLOR_BGR2RGB)
  91. fullpath = "mnistm_png/val/"+str(labels[i])+"/"+str(1000+i)+".png"
  92. cv2.imwrite(fullpath,rgb)
  93. print("saved",fullpath)
  94.  
  95.  
  96. return X_
  97.  
  98.  
  99. print('Building train set...')
  100. train = create_mnistm(mnist.train.images,mnist.train.labels)
  101. print('Building test set...')
  102. test = create_mnistm(mnist.test.images,mnist.test.labels,train=False)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement