conv_net
conv_net
May 6, 2019
print(tf.__version__)
1.10.1
1 Get Mnist data and split into train validation and test
In [ ]: def get_mnist():
data=np.float64(np.load('/Users/amit/Box Sync/tex/courses/LSDA/data_sets/mnist/MNIST
labels=np.float32(np.load('/Users/amit/Box Sync/tex/courses/LSDA/data_sets/mnist/MNI
print(data.shape)
data=np.float32(data)/255.
train_dat=data[0:50000]
train_labels=one_hot(np.int32(labels[0:50000]))
val_dat=data[50000:60000]
val_labels=one_hot(np.int32(labels[50000:60000]))
test_dat=data[60000:70000]
test_labels=one_hot(np.int32(labels[60000:70000]))
1
2 Get CIFAR10 data and split into train, validation and test
In [3]: def get_cifar():
tr=np.float32(np.load('../../mnist/CIFAR_10.npy'))
tr_lb=np.int32(np.load('/project/cmsc25025/mnist/CIFAR_labels.npy'))
tr=tr.reshape((-1,np.prod(np.array(tr.shape)[1:4])))
train_data=tr[0:45000]/255.
train_labels=one_hot(tr_lb[0:45000])
val_data=tr[45000:]/255.
val_labels=one_hot(tr_lb[45000:])
test_data=np.float32(np.load('/project/cmsc25025/mnist/CIFAR_10_test.npy'))
test_data=test_data.reshape((-1,np.prod(np.array(test_data.shape)[1:4])))
test_data=test_data/255.
test_labels=one_hot(np.int32(np.load('/project/cmsc25025/mnist/CIFAR_labels_test.npy
return (train_data, train_labels), (val_data, val_labels), (test_data, test_labels)
# Get number of input features from input and add to shape of new layer
shape=filter_size+[input.get_shape().as_list()[-1],num_features]
W = tf.get_variable('W',shape=shape) # Default initialization is Glorot (the one exp
b = tf.get_variable('b',shape=[num_features],initializer=tf.zeros_initializer)
conv = tf.nn.conv2d(input, W, strides=[1, 1, 1, 1], padding='SAME')
relu = tf.nn.relu(conv + b)
return(relu)
2
b_fc = tf.get_variable('b',shape=[num_features],initializer=tf.zeros_initializer)
fc = tf.matmul(input_flattened, W_fc) + b_fc
return(fc)
6 The network
In [5]: #tf.reset_default_graph()
def create_network():
pool_ksize=[1,2,2,1]
pool_strides=[1,2,2,1]
# The network:
with tf.variable_scope("conv1"):
# Variables created here will be named "conv1/weights", "conv1/biases".
relu1 = conv_relu_layer(x_image, filter_size=[5, 5],num_features=32)
pool1 = tf.nn.max_pool(relu1, ksize=pool_ksize, strides=pool_strides, paddin
with tf.variable_scope("conv2"):
# Variables created here will be named "conv1/weights", "conv1/biases".
relu2 = conv_relu_layer(pool1, filter_size=[5, 5],num_features=64)
pool2 = tf.nn.max_pool(relu2, ksize=pool_ksize, strides=pool_strides, paddin
with tf.variable_scope('dropout2'):
drop2=tf.nn.dropout(pool2,keep_prob)
with tf.variable_scope("fc1"):
fc1 = fully_connected_layer(drop2, num_features=256)
fc1r=tf.nn.relu(fc1)
with tf.variable_scope("fc2"):
fc2 = fully_connected_layer(fc1r, num_features=10)
# Names (OUT,LOSS, ACC) below added to make it easier to use this tensor when restor
fc2 = tf.identity(fc2, name="OUT")
# The loss computation
with tf.variable_scope('cross_entropy_loss'):
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_
# Accuracy computation
with tf.variable_scope('helpers'):
correct_prediction = tf.equal(tf.argmax(fc2, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32),name="ACC")
# We return the final functions (they contain all the information about the graph of
return cross_entropy, accuracy
3
n=data[0].shape[0]
if (num is not None):
n=np.minimum(n,num)
ii=np.array(np.arange(0,n,1))
8 Run the training. Save the model and test at the end
In [ ]: import time
batch_size=500
step_size=.001
num_epochs=4
num_train=10000
minimizer="Adam"
data_set="mnist"
model_name="model"
keep_prob=.5
dim=28
nchannels=1
if (data_set=="cifar"):
dim=32
nchannels=3
train,val,test=get_data(data_set=data_set)
4
tf.reset_default_graph()
x = tf.placeholder(tf.float32, shape=[None, dim*dim*nchannels],name="x")
x_image = tf.reshape(x, [-1, dim, dim, nchannels])
# Dimensions of x_image: [Batch size, Column size, Row size, Number of incoming channels
# The number of incoming channels, for example, will be 3 if the image is color: RGB (re
# We will slide filter over this 2d picture with conv2d function.
y_ = tf.placeholder(tf.float32, shape=[None,10],name="y")
# Allows you to control the time step during the iterations
lr_ = tf.placeholder(tf.float32, shape=[],name="learning_rate")
keep_prob_=tf.placeholder(tf.float32, shape=[],name="keep_prob")
# Run epochs
for i in range(num_epochs): # number of epochs
lo,ac=run_epoch(sess,OPS,train,batch_size,type="train",num=num_train)
print('Epoch',i,'Train loss, accuracy',lo,ac)
vlo,vac = run_epoch(sess,OPS,val,batch_size,type="test")
print('Epoch',i,'Validation loss, accuracy',vlo,vac)
# Test set accuracy
telo,teac = run_epoch(sess,OPS,test,batch_size,type="test")
print('test accuracy',telo,teac)
# Save model
tf.add_to_collection("optimizer", train_step)
saver = tf.train.Saver()
save_path = saver.save(sess, "tmp/"+model_name)
print("Model saved in path: %s" % save_path)
5
9 Reload the model that was saved and continue training
In [ ]: # Reloading an existing model.
tf.reset_default_graph()
batch_size=500
step_size=.001
num_epochs=4
num_train=10000
data_set="mnist"
model_name="model"
Train=True
dim=28
nchannels=1
if (data_set=="cifar"):
dim=32
nchannels=3
ii=np.arange(0,num_train,1)
for i in range(num_epochs): # Run epochs
lo,ac=run_epoch(sess,OPS,train,batch_size,type="train",num=num_train)
print('Epoch',i,'Train loss, accuracy',lo,ac)
vlo,vac = run_epoch(sess,OPS,val,batch_size,type="test")
print('EPoch',i,'Validation loss, accuracy',vlo,vac)
6
# Test set accuracy
telo,teac = run_epoch(sess,OPS,test,batch_size,type="test")
print('test accuracy',telo,teac)
tf.add_to_collection("optimizer", train_step)
saver = tf.train.Saver()
save_path = saver.save(sess, "tmp/"+model_name)
print("Model saved in path: %s" % save_path)