Guest User

Untitled

a guest
Jan 17th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.99 KB | None | 0 0
  1. import tensorflow as tf
  2. import numpy as np # dealing with arrays
  3. import time as time
  4. from datetime import timedelta
  5. from Gaussian import gauss_decay_step
  6.  
  7. TRAIN_DIR_CATS = 'C:/Users/Alberto/Desktop/Coding/data/PetImages/Cat'
  8. TRAIN_DIR_DOGS = 'C:/Users/Alberto/Desktop/Coding/data/PetImages/Dog'
  9. IMG_SIZE = 50
  10. NUM_CLASSES = 2
  11. LR = tf.get_variable(name = "learning_rate", dtype= tf.float32, shape=[])
  12. BATCH_SIZE = 16
  13. NUM_EPOCHS = 5 #number of times it runs through all the data for each model run
  14. NUM_RUN = 0 #Keep track of how many models we've run
  15. def conv2d(x, W, b, strides=1):
  16. # Conv2D wrapper, with bias and relu activation
  17. x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
  18. x = tf.nn.bias_add(x, b)
  19. return tf.nn.relu(x)
  20.  
  21. def conv2d_batch(x, W, b, strides=1):
  22. # Conv2D wrapper, with bias and relu activation
  23. x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
  24. x = tf.nn.bias_add(x, b)
  25. x = tf.contrib.layers.batch_norm(x, decay=0.99, center=True, scale=True,
  26. updates_collections=None,
  27. is_training=is_training,
  28. reuse=None,
  29. trainable=True
  30. )
  31. return tf.nn.relu(x)
  32.  
  33. def maxpool2d(x, k=2):
  34. # MaxPool2D wrapper
  35. return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],
  36. padding='SAME')
  37.  
  38. #shape must be of shape [height, width, channels, num filters]
  39. def new_weights(shape):
  40. return tf.Variable(tf.truncated_normal(shape, stddev=0.05))
  41.  
  42. def new_biases(length):
  43. return tf.Variable(tf.constant(0.05, shape=[length]))
  44.  
  45. def forward_pass(input, reuse):
  46.  
  47. with tf.variable_scope("convnet", reuse=reuse):
  48.  
  49. for i in range(len(weights)-1):
  50. if i == 0:
  51. convnet = conv2d(input, weights[0], biases[0])
  52. convnet = maxpool2d(convnet)
  53.  
  54. else:
  55. convnet = conv2d(convnet, weights[i], biases[i])
  56. convnet = maxpool2d(convnet)
  57.  
  58. convnet = tf.contrib.layers.flatten(convnet)
  59. convnet = tf.add(tf.matmul(convnet, weights[-1]), biases[-1])
  60. return convnet
  61. def forward_pass_batch(input, reuse):
  62.  
  63. with tf.variable_scope("convnet", reuse=reuse):
  64.  
  65. for i in range(len(weights)-1):
  66. if i == 0:
  67. convnet = conv2d_batch(input, weights[0], biases[0])
  68. convnet = maxpool2d(convnet)
  69.  
  70. else:
  71. convnet = conv2d_batch(convnet, weights[i], biases[i])
  72. convnet = maxpool2d(convnet)
  73.  
  74. convnet = tf.contrib.layers.flatten(convnet)
  75. convnet = tf.add(tf.matmul(convnet, weights[-1]), biases[-1])
  76. return convnet
  77.  
  78. weights = [
  79. new_weights([5,5,1,16]),
  80. new_weights([5,5,16,32]),
  81. new_weights([5,5,32,32]),
  82. new_weights([5,5,32,64]),
  83. new_weights([5,5,64,256]),
  84. new_weights([1024,NUM_CLASSES])
  85. ]
  86. biases = [
  87. new_biases(16),
  88. new_biases(32),
  89. new_biases(32),
  90. new_biases(64),
  91. new_biases(256),
  92. new_biases(NUM_CLASSES)
  93. ]
  94.  
  95. rate_multiplier = tf.get_variable(name = "rate_multiplier", dtype= tf.float32, shape=[len(weights)])
  96. gaussian = tf.placeholder(name = "gaussian", dtype= tf.float32, shape=[])
  97.  
  98. #for debugging. Calculates the average of the absolute value of the gradients at each layer
  99. def gradient_per_layer_list():
  100. returnlist = []
  101. for i in range(len(weights)-1):
  102. gradient_per_layer = tf.reduce_mean(tf.abs(weight_gradient_placeholder))
  103. returnlist.append(gradient_per_layer)
  104. gradient_per_layer = tf.reduce_mean(tf.abs(fc_weight_gradient_placeholder))
  105. returnlist.append(gradient_per_layer)
  106. return returnlist
  107.  
  108. #returns list of parameter update operations for weights and biases [w[0],b[0]...w[i],b[i]]
  109. def update_operations():
  110. update_ops = []
  111. with tf.name_scope('gradient-update'):
  112.  
  113. #convolutional weights and biases
  114. for i in range(len(weights)-1):
  115. update_ops.append(weights[i].assign(tf.subtract(weights[i], tf.multiply(weight_gradient_placeholder, tf.multiply(LR, rate_multiplier[i])))))
  116. update_ops.append(biases[i].assign(tf.subtract(biases[i], tf.multiply(bias_gradient_placeholder, tf.multiply(LR, rate_multiplier[i])))))
  117.  
  118. #fully connected weight and bias
  119. update_ops.append(weights[-1].assign(tf.subtract(weights[-1], tf.multiply(fc_weight_gradient_placeholder, tf.multiply(LR, rate_multiplier[i])))))
  120. update_ops.append(biases[-1].assign(tf.subtract(biases[-1], tf.multiply(bias_gradient_placeholder, tf.multiply(LR, rate_multiplier[i])))))
  121.  
  122. return update_ops
  123.  
  124.  
  125. def multipliers_update():
  126. multipliers = []
  127. for i in range(len(weights)):
  128. multipliers.append(rate_multiplier[i].assign(gaussian))
  129. return multipliers
  130.  
  131.  
  132. #Separate data into train, test, and validation set
  133. data = np.load('train_data.npy')
  134. train_set = data[4000:]
  135. test_set = data[400:4000]
  136. validation_set = data[:400]
  137.  
  138. #Separate training set into X and Y
  139. X_train = np.array([i[0] for i in train_set]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
  140. Y_train = np.array([i[1] for i in train_set])
  141. X_test = np.array([i[0] for i in test_set]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
  142. Y_test = np.array([i[1] for i in test_set])
  143.  
  144. print("Training dataset size", X_train.size)
  145. print("batch size: ", BATCH_SIZE )
  146.  
  147. # Create placeholders for our data
  148. input = tf.placeholder(tf.float32, shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')
  149. y_true = tf.placeholder(tf.int64, shape=[None, NUM_CLASSES], name='y_true')
  150.  
  151. weight_gradient_placeholder = tf.placeholder(tf.float32, shape=[None,None,None,None])
  152. fc_weight_gradient_placeholder = tf.placeholder(tf.float32, shape=[None,None])
  153. bias_gradient_placeholder = tf.placeholder(tf.float32, shape=[None])
  154. is_training = tf.placeholder(tf.bool, name='is_training')
  155.  
  156. #Tensorboard: summary writers
  157. test_writers = []
  158.  
  159. def model(X_train = X_train,Y_train = Y_train, X_test = X_test, Y_test = Y_test, learning_rate = .001,
  160. num_epochs = NUM_EPOCHS, minibatch_size = BATCH_SIZE,
  161. batchnorm = False,
  162. lr_decay_use = False, lr_decay = 1, exp_decay=False, decay_steps = 1,
  163. SD_MULT = None, REDUX = 0):
  164.  
  165. '''
  166.  
  167. :param X_train: Train data
  168. :param Y_train:
  169. :param X_test:
  170. :param Y_test:
  171. :param learning_rate:
  172. :param num_epochs: How many times we run through the complete data set
  173. :param minibatch_size: How big is each mini batch
  174. :param lr_decay_use: set to True to use lr_decay
  175. :param lr_decay: lr_decay * lr after every batch
  176. :param exp_decay: Set to True to use exponential learning rate decay exp decay = LR * lr_decay ** (global_step/ decay_steps)
  177. :param decay_steps: used to
  178. :param SD_MULT:
  179. :param REDUX:
  180. '''
  181.  
  182. global NUM_RUN
  183. epoch_step = 0
  184. global_step = 0
  185. train_size = X_train.shape[0]
  186. #minibatches in every epoch
  187. tot_minibatches = int(train_size / BATCH_SIZE)
  188. decay_steps = decay_steps * tot_minibatches
  189.  
  190. #set
  191. if batchnorm == False:
  192. pred_train = forward_pass(input, reuse = False)
  193. pred_test = forward_pass(input, reuse = True)
  194. else:
  195. with tf.variable_scope(tf.get_variable_scope(), reuse=False):
  196. pred_train = forward_pass_batch(input, reuse=False)
  197.  
  198. pred_test = forward_pass_batch(input, reuse=True)
  199.  
  200. test_pred = tf.equal(tf.argmax(pred_test, 1), tf.argmax(y_true,1))
  201. test_accuracy = tf.reduce_mean(tf.cast(test_pred, tf.float32))
  202. test_summary = tf.summary.scalar('test_accuracy', test_accuracy)
  203.  
  204. # Apply softmax
  205. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits = pred_train, labels = y_true))
  206.  
  207. gradient_per_layer = gradient_per_layer_list()
  208.  
  209. #Calculate gradient of cost function with respect to weights, biases
  210. parameter_gradients = tf.gradients(cost, [*weights, *biases])
  211. update_ops = update_operations()
  212. multipliers = multipliers_update()
  213.  
  214. init = tf.global_variables_initializer()
  215.  
  216. #Learning rate decay operations
  217. #Are we doing exponential decay or standard lr decay
  218. if (exp_decay == True):
  219. lr_decay_op = tf.multiply(LR, lr_decay ** (global_step/ decay_steps))
  220. else:
  221. lr_decay_op = tf.multiply(LR, lr_decay)
  222.  
  223. lr_decay = LR.assign(lr_decay_op)
  224.  
  225. with tf.Session() as sess:
  226. sess.run(init)
  227.  
  228. #Tensorboard: create a file writer for every model run
  229.  
  230. if batchnorm == True:
  231. test_writers.append(tf.summary.FileWriter('C:/Users/Alberto/Desktop/Coding/TensorboardLogs/test/run/batchrun' + str(NUM_RUN)))
  232. test_writers.append(tf.summary.FileWriter('C:/Users/Alberto/Desktop/Coding/TensorboardLogs/test/run'+str(NUM_RUN)))
  233. mergeall_op = tf.summary.merge_all()
  234.  
  235. # reset weights and biases for new run
  236. for i in range(len(weights)):
  237. sess.run(weights[i].assign(tf.truncated_normal(weights[i].shape, stddev=0.05)))
  238. sess.run(biases[i].assign(tf.constant(0.05, shape=biases[i].shape)))
  239. sess.run(rate_multiplier[i].assign(1))
  240. for i in range(len(test_writers)):
  241. test_writers[i].flush
  242.  
  243. #initialzize learning rate and create learning rate update operation
  244. sess.run(LR.assign(learning_rate))
  245.  
  246. start_time = time.time()
  247.  
  248. while epoch_step < NUM_EPOCHS: #Every epoch is a run through ALL of the data
  249.  
  250. # Shuffle the data
  251. p = np.random.permutation(len(X_train))
  252. X_train, Y_train = X_train[p], Y_train[p]
  253.  
  254. #Minibatch Gradient Descent, mini batch size is defined as BATCH_SIZE
  255. for batch_step in range(tot_minibatches):
  256. #Set up feed_dict of mini batches
  257. index_start = batch_step * BATCH_SIZE
  258. index_end = index_start + BATCH_SIZE
  259. x_batch, y_true_batch = X_train[index_start:index_end], Y_train[index_start:index_end]
  260. feed_dict = {input: x_batch,
  261. y_true: y_true_batch}
  262.  
  263. #Here is where we're going to set the learning rate multipliers according to the gaussian decay function
  264. if SD_MULT != None:
  265. multipliers_in = gauss_decay_step(NUM_LAYERS=len(weights), TOT_ITERATIONS=num_epochs*tot_minibatches, step= global_step, SD_MULT=SD_MULT, REDUX = REDUX)
  266.  
  267. for i in range(len(weights)):
  268. sess.run(multipliers[i], {gaussian : multipliers_in[i]})
  269. cur_grad = sess.run(parameter_gradients, {input: x_batch, y_true: y_true_batch, is_training: True})
  270. #Run parameter update for convolution layers
  271. for i in range(len(weights)-1):
  272. sess.run(update_ops[i*2], {weight_gradient_placeholder: cur_grad[i]})
  273. sess.run(update_ops[i*2+1], {bias_gradient_placeholder: cur_grad[len(weights)+ i]})
  274.  
  275. #Now update fully connected layer params
  276. sess.run(update_ops[-2], {fc_weight_gradient_placeholder: cur_grad[len(weights)-1]})
  277. sess.run(update_ops[-1], {bias_gradient_placeholder: cur_grad[-1]})
  278.  
  279. #Apply learning decay
  280. if(lr_decay_use == True):
  281. sess.run(lr_decay)
  282.  
  283. # Tensorboard Summaries
  284. summaries = sess.run(mergeall_op, {input: x_batch, y_true: y_true_batch, is_training: False})
  285.  
  286.  
  287. # Print results every n steps
  288. if global_step % 50 == 0:
  289. print("Step: ", global_step)
  290. end_time = time.time()
  291. time_dif = end_time - start_time
  292. print("Time usage: " + str(timedelta(seconds=int(round(time_dif)))))
  293. start_time = time.time()
  294.  
  295. #Tensorboard: write data to tensorboard
  296. summary = sess.run(mergeall_op, feed_dict={input: X_test,
  297. y_true: Y_test, is_training: False})
  298. test_writers[NUM_RUN].add_summary(summary, global_step)
  299. test_writers[NUM_RUN].flush
  300.  
  301. batch_step += 1
  302. global_step += 1
  303.  
  304. print("Epoch: " + str(epoch_step + 1))
  305. epoch_step += 1
  306. NUM_RUN = NUM_RUN + 1
  307. return
Add Comment
Please, Sign In to add comment