Advertisement
Guest User

Untitled

a guest
Dec 10th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. import numpy as np
  2. import tensorflow as tf
  3. import random
  4. import linecache
  5. import cv2
  6.  
  7.  
  8. path = "D:\\Audio_Dataset\\Animals\\annotations\\annotations\\trainval.txt"
  9. imagepath = "D:\\Audio_Dataset\\Animals\\annotations\\images"
  10. def get_batch(path) :
  11. file_object = open(path, 'r')
  12. num_lines = sum(1 for line in open(path))
  13. list = [random.randint(0,num_lines) for x in range(20)]
  14. x_batch = []
  15. y_labels = []
  16. for x in list:
  17. photoname = linecache.getline(path, x)
  18. photoname = photoname.split(" ")
  19. imagename = photoname[0]
  20. imagearray = read_image(imagepath+"\\"+imagename+".jpg", 28)
  21. y_labels.append(photoname[1])
  22. x_batch.append(imagearray.flatten())
  23.  
  24.  
  25. file_object.close()
  26.  
  27. return x_batch, np.array(y_labels)
  28.  
  29.  
  30. def read_image(image_path, size):
  31. image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
  32. image = cv2.resize(image,(size,size))
  33. #data = tf.convert_to_tensor(image)
  34. data = np.array(image)
  35. return data
  36.  
  37. def init_weights(shape) :
  38. init_random_dist = tf.truncated_normal(shape, stddev=0.1)
  39. return tf.Variable(init_random_dist)
  40.  
  41. def init_bias(shape) :
  42. init_bias_val = tf.constant(0.1,shape=shape)
  43. return tf.Variable(init_bias_val)
  44.  
  45. def conv2d(x,W) :
  46. return tf.nn.conv2d(x,W,strides=[1,1,1,1], padding='SAME')
  47.  
  48. def max_pool_2by2(x) :
  49. return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
  50.  
  51. def conv_layer(input_x,shape) :
  52. W = init_weights(shape)
  53. b = init_bias([shape[3]])
  54. return tf.nn.relu(conv2d(input_x, W)+b)
  55.  
  56. def full_layer(input_layer, size) :
  57. input_size = int(input_layer.get_shape()[1])
  58. W = init_weights([input_size, size])
  59. b = init_bias([size])
  60. return tf.matmul(input_layer,W) + b
  61.  
  62.  
  63.  
  64. ## Placeholders
  65.  
  66. x = tf.placeholder(tf.float32, shape=[None, 784])
  67. y_true = tf.placeholder(tf.float32,shape=[None, 37])
  68.  
  69. x_image = tf.reshape(x, [-1,28,28,1])
  70.  
  71. convo_1 = conv_layer(x_image, shape=[5, 5, 1, 32])
  72. convo_1_pooling = max_pool_2by2(convo_1)
  73.  
  74. convo_2 = conv_layer(convo_1_pooling,shape=[5, 5, 32, 64])
  75. convo_2_pooling = max_pool_2by2(convo_2)
  76.  
  77. convo_2_flat = tf.reshape(convo_2_pooling, [-1, 7*7*64])
  78. full_layer_one = tf.nn.relu(full_layer(convo_2_flat, 1024))
  79.  
  80. hold_prob = tf.placeholder(tf.float32)
  81. full_one_dropout = tf.nn.dropout(full_layer_one,keep_prob=hold_prob)
  82.  
  83. y_pred = full_layer(full_one_dropout, 2)
  84.  
  85. cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_true, logits=y_pred))
  86. optimizer = tf.train.AdamOptimizer(learning_rate=0.001)
  87. train = optimizer.minimize(cross_entropy)
  88.  
  89. init = tf.global_variables_initializer()
  90.  
  91. steps = 500
  92. with tf.Session() as sess:
  93. sess.run(init)
  94. batch_size = 20
  95. for i in range(steps) :
  96. batch_x , batch_y = get_batch(path)
  97. y2 = ['1','2']
  98. batch_size = 20
  99. sess.run(train, feed_dict={x: batch_x, y_true: batch_y, hold_prob: 0.5})
  100.  
  101. if i%100==0 :
  102. print("ON STEP: {}".format(i))
  103. print("Accuracy:")
  104. matchs = tf.euqal(tf.argmax(y_pred,1), tf.argmax(y_true,1))
  105. acc = tf.reduce_mean(tf.cast(matchs,tf.float32))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement