Skip to content

Instantly share code, notes, and snippets.

@jurinco
Created June 28, 2017 13:29
Show Gist options
  • Save jurinco/ace04b84d4157d4b41f6d3cac4d9d770 to your computer and use it in GitHub Desktop.
Save jurinco/ace04b84d4157d4b41f6d3cac4d9d770 to your computer and use it in GitHub Desktop.
TensorFlow Quickstart Samples
import tensorflow as tf
import numpy as np
import cv2
import matplotlib.pyplot as plt
import time
# Training with 50k cifar images and testing with their set.
# be sure to use cifar data in python format.
def unpickle(file):
import cPickle
fo = open(file, 'rb')
dict = cPickle.load(fo)
fo.close()
return dict
def np_format( d ):
data = np.array( d['data'] )
label = np.array( d['labels'] )
label_r = np.zeros( (label.shape[0], label.max()+1 ), dtype='float32' )
for count, i in enumerate(label):
label_r[count,i] = 1
data_r = data.reshape( [-1, 3,32,32 ] ).transpose(0,2,3,1)
return data_r, label_r
meta = unpickle( 'CIFAR10_data/batches.meta')
label_names = meta['label_names']
#
# Load Training Data
data_r = []; label_r = []
for i in range(1,6):
fname = 'CIFAR10_data/data_batch_'+str(i)
print 'Reading cifar file : ', fname
d = unpickle( fname )
data_tmp, label_tmp = np_format( d )
print data_tmp.shape
data_r.append(data_tmp)
label_r.append(label_tmp)
data_r = np.concatenate(data_r, axis=0)
label_r = np.concatenate(label_r, axis=0)
#
# Load Testing Data
d = unpickle( 'CIFAR10_data/test_batch' )
data_test, label_test = np_format( d )
# for i in range(10):
# im_rgb = data_r[i,:,:,:]
# cv2.imshow( 'win', cv2.cvtColor( im_rgb, cv2.COLOR_RGB2BGR ) )
# print label[i], label_names[ label[i]]
# cv2.waitKey(0)
#
# graph
batch_size = 100
with tf.device( '/gpu:1'):
X = tf.placeholder( 'float32', [batch_size, 32, 32, 3] )
Y = tf.placeholder( 'float32', [batch_size,10] )
#conv1
with tf.variable_scope('conv1') as scope:
kernel = tf.Variable( np.random.randn(5,5,3,64)*.01, dtype='float32' )
biases = tf.Variable( np.full(64, 0.1), dtype='float32' )
conv = tf.nn.conv2d( X, kernel, strides=[1,1,1,1], padding='SAME' )
bias = tf.nn.bias_add( conv, biases )
conv1 = tf.nn.relu( bias, name=scope.name )
pool1 = tf.nn.max_pool( conv1, ksize=[1,3,3,1], strides=[1,2,2,1], padding='SAME', name='pool1')
#norm1 = tf.nn.lrn( pool1, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm1' )
norm1 = pool1
with tf.variable_scope('conv2') as scope:
kernel = tf.Variable( np.random.randn(5,5,64,64)*.01, dtype='float32' )
biases = tf.Variable( np.full(64, 0.1), dtype='float32' )
conv = tf.nn.conv2d( norm1, kernel, strides=[1,1,1,1], padding='SAME' )
bias = tf.nn.bias_add( conv, biases )
conv2 = tf.nn.relu( bias, name=scope.name )
#norm2 = tf.nn.lrn( conv2, 4, bias=1.0, alpha=0.001 / 9.0, beta=0.75, name='norm2' )
norm2 = conv2
pool2 = tf.nn.max_pool( norm2, ksize=[1,3,3,1], strides=[1,2,2,1], padding='SAME', name='pool2')
with tf.variable_scope('fc1') as scope:
reshaped = tf.reshape( pool2, [batch_size, -1] )
dim = 8*8*64
weights = tf.Variable( np.random.randn(dim,384)*.01, dtype='float32' )
biases = tf.Variable( np.full(384, 0.1), dtype='float32' )
local3 = tf.nn.relu( tf.matmul(reshaped,weights)+biases, name=scope.name)
with tf.variable_scope('fc2') as scope:
weights = tf.Variable( np.random.randn(384,192)*.01, dtype='float32' )
biases = tf.Variable( np.full(192, 0.1), dtype='float32' )
local4 = tf.nn.relu( tf.matmul(local3,weights)+biases, name=scope.name)
with tf.variable_scope( 'softmax') as scope:
weights = tf.Variable( np.random.randn(192,10)*.01, dtype='float32' )
biases = tf.Variable( np.full(10, 0.1), dtype='float32' )
softmax_lin = tf.add( tf.matmul(local4,weights), biases, name=scope.name)
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(softmax_lin, Y) )
#
# Make Gradient Solver
global_step = tf.Variable( 0, trainable=False, dtype='float32', name="global_step")
lr = tf.train.exponential_decay(1e-3, global_step, 500, 0.96, staircase=True)
train_op = tf.train.MomentumOptimizer(lr, 0.9).minimize( cost, global_step=global_step )
predict_op = tf.argmax( softmax_lin, 1 )
#
# Session
config = tf.ConfigProto( log_device_placement=True )
sess = tf.InteractiveSession( config=config )
sess.run( tf.initialize_all_variables() )
#
# Iterations
cost_ary = []
for i in range(100):
start_time = time.time()
start = (i*batch_size)% (data_r.shape[0]-batch_size)
end = start+batch_size
_, tcost = sess.run( [train_op,cost], feed_dict={X: data_r[start:end, :,:,:], Y:label_r[start:end] } )
print 'Itr: %d, Cost: %-6.6f, time: %2.4f' %(i, tcost, time.time() - start_time)
#if i%100 == 0:
# print np.mean( sess.run( predict_op, feed_dict={X: data_r[0:1000,:,:,:], Y:teY[0:1000,:] } ) == np.argmax(teY[0:1000,:], axis=1) )
cost_ary.append( tcost )
test_batch_scores = []
for i in range(0,10000, batch_size ):
t = np.mean( sess.run( predict_op, feed_dict={X: data_test[i:i+batch_size,:,:,:], Y:label_test[i:i+batch_size,:] } ) == np.argmax(label_test[i:i+batch_size,:], axis=1) )
test_batch_scores.append(t)
print 'Average Acc on Test set : ', np.mean( test_batch_scores )
# Convolutional Nets with tf using MNIST data. Accuracy ~ 0.98
# This script is structurally similar to `fc_net.py`, just the model is different.
# One common error that comes up is with cudnn configuration. If you installed from
# precompiled binary and your cudnn versions do not match this wont work. I used tf
# compiled from source and this work.
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
mnist = input_data.read_data_sets( 'MNIST_data', one_hot=True )
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels
trI = trX.reshape( [-1, 28,28, 1] )
teI = teX.reshape( [-1, 28,28, 1] )
#
# graph
with tf.device( '/gpu:1'):
# placeholders
X = tf.placeholder( 'float32', [None, 28, 28, 1] )
Y = tf.placeholder( 'float', [None,10] )
# weights
w1 = tf.Variable( np.random.randn(5,5,1,32)*.01, dtype='float32' )
b1 = tf.Variable( np.full(32, 0.1), dtype='float32' )
w2 = tf.Variable( np.random.randn(5,5,32,64)*.01, dtype='float32' )
b2 = tf.Variable( np.full(64, 0.1), dtype='float32' )
w_fc1 = tf.Variable( np.random.randn(3136,1024)*.01, dtype='float32' )
b_fc1 = tf.Variable( np.full(1024, 0.1), dtype='float32' )
w_fc2 = tf.Variable( np.random.randn(1024,10)*.01, dtype='float32' )
b_fc2 = tf.Variable( np.full(10, 0.1), dtype='float32' )
# def layers - conv
conv1 = tf.nn.conv2d( X, w1, strides=[1,1,1,1], padding='SAME')
h_conv1 = tf.nn.relu( conv1 + b1 )
p_conv1 = tf.nn.max_pool(h_conv1, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
conv2 = tf.nn.conv2d( p_conv1, w2, strides=[1,1,1,1], padding='SAME')
h_conv2 = tf.nn.relu( conv2 + b2 )
p_conv2 = tf.nn.max_pool(h_conv2, ksize=[1,2,2,1], strides=[1,2,2,1], padding='SAME')
# def layers - fc
flat = tf.reshape( p_conv2, [-1, 7*7*64] )
fc1 = tf.nn.relu( tf.matmul( flat, w_fc1) + b_fc1 )
fc2 = tf.nn.relu( tf.matmul( fc1, w_fc2) + b_fc2 )
#
# Optimizer
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(fc2, Y) )
train_op = tf.train.MomentumOptimizer(0.01, 0.9).minimize( cost )
predict_op = tf.argmax( fc2, 1 )
#
# Session
config = tf.ConfigProto( device_count = {'cpu' : 0 }, log_device_placement=True )
sess = tf.InteractiveSession( config=config )
sess.run( tf.initialize_all_variables() )
# Iterations
cost_ary = []
batch_size = 100
for i in range(10000):
start = (i*batch_size)% (trI.shape[0]-batch_size)
end = start+batch_size
_, tcost = sess.run( [train_op,cost], feed_dict={X: trI[start:end, :,:,:], Y:trY[start:end, :] } )
print i, tcost
cost_ary.append( tcost )
# final prediction
print np.mean( sess.run( predict_op, feed_dict={X: teI[0:1000,:,:,:], Y:teY[0:1000,:] } ) == np.argmax(teY[0:1000,:], axis=1) )
plt.plot( np.log(cost_ary) )
plt.show()
# A simple fully connected net with tf using MNIST data (accuracy ~ 0.91)
#https://github.com/nlintz/TensorFlow-Tutorials/blob/master/03_net.py
import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
import matplotlib.pyplot as plt
mnist = input_data.read_data_sets( 'MNIST_data', one_hot=True )
trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels
#with tf.device('/cpu:0'):
#
# graph
X = tf.placeholder( 'float', [None,784])
Y = tf.placeholder( 'float', [None,10] )
w = tf.Variable( np.random.randn(784, 625)*.01, dtype='float' )
w1 = tf.Variable( np.random.randn(625, 100)*.01, dtype='float' )
w2 = tf.Variable( np.random.randn(100, 10)*.01, dtype='float' )
#b = tf.Variable( np.random.randn(100,10)*.01 )
# # model
# h = tf.nn.sigmoid(tf.matmul(X, w))
# pr_y = tf.matmul(h, w1)
# #pr_y = tf.nn.sigmoid( X*w ) * w1
# model-2
h = tf.nn.relu(tf.matmul(X, w))
h2 = tf.nn.relu(tf.matmul(h, w1))
pr_y = tf.matmul(h2, w2)
# define cost and stepper
cost = tf.reduce_mean( tf.nn.softmax_cross_entropy_with_logits(pr_y, Y) )
#train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize( cost )
train_op = tf.train.MomentumOptimizer(0.01, 0.9).minimize( cost )
predict_op = tf.argmax( pr_y, 1 )
# Iterations
config = tf.ConfigProto( log_device_placement=True )
sess = tf.InteractiveSession(config=config)
sess.run( tf.initialize_all_variables() )
cost_ary = []
for i in range(10000):
start = i% (trX.shape[0]-100)
end = start+100
_, tcost = sess.run( [train_op,cost], feed_dict={X: trX[start:end, :], Y:trY[start:end, :] } )
print i, tcost
cost_ary.append( tcost )
# final prediction
print np.mean( sess.run( predict_op, feed_dict={X: teX, Y:teY } ) == np.argmax(teY, axis=1) )
plt.plot( np.log(cost_ary) )
plt.show()
# Scalar 1-param regression with tensorflow
#https://github.com/nlintz/TensorFlow-Tutorials/blob/master/01_linear_regression.py
import tensorflow as tf
import numpy as np
trX = np.linspace(-1,1,101)
trY = 2*trX + np.random.randn( int(trX.shape[0]) )*.33
# computation graph
X = tf.placeholder('float32')
Y = tf.placeholder('float32')
w = tf.Variable(0.0, name='weights')
y_cap = w * X
cost = tf.reduce_mean( tf.square(Y - y_cap) )
# optimizer
train_op = tf.train.GradientDescentOptimizer(0.25).minimize(cost)
#
sess = tf.InteractiveSession()
sess.run( tf.initialize_all_variables() )
for i in range(200):
sess.run( train_op, feed_dict={X: trX, Y: trY })
print i, sess.run(w)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment