Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import matplotlib.pyplot as plt
- try:
- from scipy import misc
- except ImportError:
- !pip install scipy
- from scipy import misc
- import tensorflow as tf
- import numpy as np
- import imageio
- import matplotlib.pyplot as plt
- try:
- from scipy import misc
- except ImportError:
- !pip install scipy
- from scipy import misc
- training_size = 265
- col_row = 400
- img_size = 400*400
- training_data = np.empty(shape=(training_size, 400*400))
- import glob
- i = 0
- for filename in glob.glob('C:/Mah/TensorPalms/Train/*.jpg'):
- image = imageio.imread(filename)
- print(image.shape)
- training_data[i] = image.reshape(-1)
- i+=1
- #label
- a= [0,0,0,0,0,
- 1,1,1,1,1,
- 2,2,2,2,2,
- 3,3,3,3,3,
- 4,4,4,4,4,
- 5,5,5,5,5,
- 6,6,6,6,6,
- 7,7,7,7,7,
- 8,8,8,8,8,
- 9,9,9,9,9,
- 10,10,10,10,10,
- 11,11,11,11,11,
- 12,12,12,12,12,
- 13,13,13,13,13,
- 14,14,14,14,14,
- 15,15,15,15,15,
- 16,16,16,16,16,
- 17,17,17,17,17,
- 18,18,18,18,18,
- 19,19,19,19,19,
- 20,20,20,20,20,
- 21,21,21,21,21,
- 22,22,22,22,22,
- 23,23,23,23,23,
- 24,24,24,24,24,
- 25,25,25,25,25,
- 26,26,26,26,26,
- 27,27,27,27,27,
- 28,28,28,28,28,
- 29,29,29,29,29,
- 30,30,30,30,30,
- 31,31,31,31,31,
- 32,32,32,32,32,
- 33,33,33,33,33,
- 34,34,34,34,34,
- 35,35,35,35,35,
- 36,36,36,36,36,
- 37,37,37,37,37,
- 38,38,38,38,38,
- 39,39,39,39,39,
- 40,40,40,40,40,
- 41,41,41,41,41,
- 42,42,42,42,42,
- 43,43,43,43,43,
- 44,44,44,44,44,
- 45,45,45,45,45,
- 46,46,46,46,46,
- 47,47,47,47,47,
- 48,48,48,48,48,
- 49,49,49,49,49,
- 50,50,50,50,50,
- 51,51,51,51,51,
- 52,52,52,52,52,]
- b = tf.one_hot(a,53)
- sess = tf.Session()
- sess.run(b)
- print(b.shape)
- from sklearn.preprocessing import OneHotEncoder
- training_label = OneHotEncoder(sparse=False).fit_transform(np.asarray(a).reshape(-1, 1))
- print(training_label)
- #test
- import tensorflow as tf
- import numpy as np
- import matplotlib.pyplot as plt
- try:
- from scipy import misc
- except ImportError:
- !pip install scipy
- from scipy import misc
- test_size = 159
- img_size = 400*400
- test_images = np.empty(shape=(test_size,400*400))
- import glob
- i = 0
- for filename in glob.glob('C:/Mah/TensorPalms/Test/*.jpg'):
- image = imageio.imread(filename)
- print(image.shape)
- test_images[i] = image.reshape(-1)
- i+=1
- c= [0,0,0,
- 1,1,1,
- 2,2,2,
- 3,3,3,
- 4,4,4,
- 5,5,5,
- 6,6,6,
- 7,7,7,
- 8,8,8,
- 9,9,9,
- 10,10,10,
- 11,11,11,
- 12,12,12,
- 13,13,13,
- 14,14,14,
- 15,15,15,
- 16,16,16,
- 17,17,17,
- 18,18,18,
- 19,19,19,
- 20,20,20,
- 21,21,21,
- 22,22,22,
- 23,23,23,
- 24,24,24,
- 25,25,25,
- 26,26,26,
- 27,27,27,
- 28,28,28,
- 29,29,29,
- 30,30,30,
- 31,31,31,
- 32,32,32,
- 33,33,33,
- 34,34,34,
- 35,35,35,
- 36,36,36,
- 37,37,37,
- 38,38,38,
- 39,39,39,
- 40,40,40,
- 41,41,41,
- 42,42,42,
- 43,43,43,
- 44,44,44,
- 45,45,45,
- 46,46,46,
- 47,47,47,
- 48,48,48,
- 49,49,49,
- 50,50,50,
- 51,51,51,
- 52,52,52]
- test_labels = tf.one_hot(c,53)
- sess = tf.Session()
- sess.run(test_labels)
- from sklearn.preprocessing import OneHotEncoder
- test_label = OneHotEncoder(sparse=False).fit_transform(np.asarray(c).reshape(-1, 1))
- print(test_label)
- import tensorflow as tf
- sess = tf.InteractiveSession()
- x = tf.placeholder(tf.float32, [None, img_size])
- y_ = tf.placeholder(tf.float32, [None, 53])
- def weight_variable(shape):
- initial = tf.truncated_normal(shape, stddev=0.1)
- return tf.Variable(initial)
- def bias_variable(shape):
- initial = tf.constant(0.1, shape=shape)
- return tf.Variable(initial)
- def conv2d(x, W):
- return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
- def max_pool_2x2(x):
- return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
- strides=[1, 2, 2, 1], padding='SAME')
- W_conv1 = weight_variable([5, 5, 3, 32])
- b_conv1 = bias_variable([32])
- x_image = tf.reshape(x, [-1, 400, 400, 3])
- h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
- h_pool1 = max_pool_2x2(h_conv1)
- #Second Convolutional Layer
- W_conv2 = weight_variable([5, 5, 32, 64])
- b_conv2 = bias_variable([64])
- h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
- h_pool2 = max_pool_2x2(h_conv2)
- #Densely Connected Layer
- W_fc1 = weight_variable([7 * 7 * 64, 1024])
- b_fc1 = bias_variable([1024])
- h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
- h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
- #Dropout
- keep_prob = tf.placeholder(tf.float32)
- h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
- #Readout Layer
- W_fc2 = weight_variable([1024, 10])
- b_fc2 = bias_variable([10])
- y_conv = tf.matmul(h_fc1_drop, W_fc2) + b_fc2
- import numpy as np
- def next_batch(num, data, labels):
- '''
- Return a total of `num` random samples and labels.
- '''
- idx = np.arange(0 , len(data))
- np.random.shuffle(idx)
- idx = idx[:num]
- data_shuffle = [data[ i] for i in idx]
- labels_shuffle = [labels[ i] for i in idx]
- return np.asarray(data_shuffle), np.asarray(labels_shuffle)
- cross_entropy = tf.reduce_mean(
- tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y_conv))
- train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
- correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
- accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
- with tf.Session() as sess:
- sess.run(tf.global_variables_initializer())
- for i in range(20000):
- batch_xs, batch_ys = next_batch(50,training_data,training_label)
- array_batch_xs = np.reshape(batch_xs, [-1, img_size ])
- if i % 100 == 0:
- train_accuracy = accuracy.eval(feed_dict={
- x: array_batch_xs, y_: batch_ys, keep_prob: 1.0})
- print('step %d, training accuracy %g' % (i, train_accuracy))
- train_step.run(feed_dict={x: array_batch_xs, y_: batch_ys, keep_prob: 0.5})
- print('test accuracy %g' % accuracy.eval(feed_dict={
- x: test_images, y_: test_label, keep_prob: 1.0}))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement