Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.80 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. import os
  4. from random import shuffle
  5. from tqdm import tqdm
  6. import tensorflow as tf
  7. import matplotlib.pyplot as plt
  8.  
  9. import tflearn
  10. from tflearn.layers.conv import conv_2d, max_pool_2d
  11. from tflearn.layers.core import input_data, dropout, fully_connected
  12. from tflearn.layers.estimator import regression
  13.  
  14.  
  15. TRAIN_DIR = 'train'
  16. TEST_DIR = 'test'
  17. IMG_SIZE = 50
  18. LR = 1e-3
  19.  
  20. MODEL_NAME = 'dogs-vs-cats-convnet'
  21.  
  22.  
  23. def create_label(image_name):
  24. """ Create an one-hot encoded vector from image name """
  25. word_label = image_name.split('.')[0]
  26. if word_label == 'cat':
  27. return np.array([1, 0])
  28. elif word_label == 'dog':
  29. return np.array([0, 1])
  30.  
  31.  
  32. def create_train_data():
  33. training_data = []
  34. for img in tqdm(os.listdir(TRAIN_DIR)):
  35. path = os.path.join(TRAIN_DIR, img)
  36. img_data = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
  37. img_data = cv2.resize(img_data, (IMG_SIZE, IMG_SIZE))
  38. training_data.append([np.array(img_data), create_label(img)])
  39. shuffle(training_data)
  40. np.save('train_data.npy', training_data)
  41. return training_data
  42.  
  43. '''
  44. def create_test_data():
  45. testing_data = []
  46. for img in tqdm(os.listdir(TEST_DIR)):
  47. path = os.path.join(TEST_DIR, img)
  48. img_num = img.split('.')[0]
  49. img_data = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
  50. img_data = cv2.resize(img_data, (IMG_SIZE, IMG_SIZE))
  51. testing_data.append([np.array(img_data), img_num])
  52.  
  53. shuffle(testing_data)
  54. np.save('test_data.npy', testing_data)
  55. return testing_data
  56. '''
  57.  
  58. # If dataset is not created:
  59. train_data = create_train_data()
  60. test_data = create_test_data()
  61.  
  62. # If you have already created the dataset:
  63. # train_data = np.load('train_data.npy')
  64. # test_data = np.load('test_data.npy')
  65.  
  66. train = train_data[:-5000]
  67. test = train_data[-5000:]
  68.  
  69. X_train = np.array([i[0] for i in train]).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
  70. y_train = [i[1] for i in train]
  71.  
  72. X_test = np.array([i[0] for i in test]).reshape(-1, IMG_SIZE, IMG_SIZE, 1)
  73. y_test = [i[1] for i in test]
  74.  
  75. tf.reset_default_graph()
  76.  
  77. convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')
  78.  
  79. convnet = conv_2d(convnet, 32, 5, activation='relu')
  80. convnet = max_pool_2d(convnet, 5)
  81.  
  82. convnet = conv_2d(convnet, 64, 5, activation='relu')
  83. convnet = max_pool_2d(convnet, 5)
  84.  
  85. convnet = fully_connected(convnet, 1024, activation='relu')
  86. convnet = dropout(convnet, 0.8)
  87.  
  88. convnet = fully_connected(convnet, 2, activation='softmax')
  89. convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')
  90.  
  91. model = tflearn.DNN(convnet, tensorboard_dir='log', tensorboard_verbose=0)
  92.  
  93. model.fit({'input': X_train}, {'targets': y_train}, n_epoch=10,
  94. validation_set=({'input': X_test}, {'targets': y_test}),
  95. snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
  96.  
  97. tf.reset_default_graph()
  98.  
  99. convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')
  100.  
  101. convnet = conv_2d(convnet, 32, 5, activation='relu')
  102. convnet = max_pool_2d(convnet, 5)
  103.  
  104. convnet = conv_2d(convnet, 64, 5, activation='relu')
  105. convnet = max_pool_2d(convnet, 5)
  106.  
  107. convnet = conv_2d(convnet, 128, 5, activation='relu')
  108. convnet = max_pool_2d(convnet, 5)
  109.  
  110. convnet = conv_2d(convnet, 64, 5, activation='relu')
  111. convnet = max_pool_2d(convnet, 5)
  112.  
  113. convnet = conv_2d(convnet, 32, 5, activation='relu')
  114. convnet = max_pool_2d(convnet, 5)
  115.  
  116. convnet = fully_connected(convnet, 1024, activation='relu')
  117. convnet = dropout(convnet, 0.8)
  118.  
  119. convnet = fully_connected(convnet, 2, activation='softmax')
  120. convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')
  121.  
  122. model = tflearn.DNN(convnet, tensorboard_dir='log', tensorboard_verbose=0)
  123.  
  124. model.fit({'input': X_train}, {'targets': y_train}, n_epoch=10,
  125. validation_set=({'input': X_test}, {'targets': y_test}),
  126. snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
  127.  
  128. d = test_data[0]
  129. img_data, img_num = d
  130.  
  131. data = img_data.reshape(IMG_SIZE, IMG_SIZE, 1)
  132. prediction = model.predict([data])[0]
  133.  
  134. fig = plt.figure(figsize=(6, 6))
  135. ax = fig.add_subplot(111)
  136. ax.imshow(img_data, cmap="gray")
  137. print(f"cat: {prediction[0]}, dog: {prediction[1]}")
  138.  
  139. fig = plt.figure(figsize=(16, 12))
  140.  
  141. for num, data in enumerate(test_data[:16]):
  142.  
  143. img_num = data[1]
  144. img_data = data[0]
  145.  
  146. y = fig.add_subplot(4, 4, num + 1)
  147. orig = img_data
  148. data = img_data.reshape(IMG_SIZE, IMG_SIZE, 1)
  149. model_out = model.predict([data])[0]
  150.  
  151. if np.argmax(model_out) == 1:
  152. str_label = 'Dog'
  153. else:
  154. str_label = 'Cat'
  155.  
  156. y.imshow(orig, cmap='gray')
  157. plt.title(str_label)
  158. y.axes.get_xaxis().set_visible(False)
  159. y.axes.get_yaxis().set_visible(False)
  160. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement