%matplotlib inline from graphviz import Digraph from IPython.core.display import display, SVG def tf_to_dot(graph): dot = Digraph() for n in graph.as_graph_def().node: name = n.name.split('/')[0] dot.node(name, label=name) for src in n.input: src = src.split('/')[0] if src != name: dot.edge(src, name) display(SVG(dot._repr_svg_())) return dot import numpy as np import scipy.io import matplotlib.pyplot as plt import tensorflow as tf mat = scipy.io.loadmat('./FaceNonFace.mat') faces = np.rollaxis(mat["face"].astype(np.uint8),-1,0) non_faces = np.rollaxis(mat["nonFace"].astype(np.uint8),-1,0) rand_idx = np.arange(0,faces.shape[0]) np.random.shuffle(rand_idx) train_test_split = 0.8 face_split = np.int(train_test_split*faces.shape[0]) train_faces = faces[rand_idx[:face_split]] test_faces = faces[rand_idx[face_split:]] non_face_split = np.int(train_test_split*non_faces.shape[0]) train_non_faces = non_faces[rand_idx[:non_face_split]] test_non_faces = non_faces[rand_idx[non_face_split:]] train_data = np.vstack([train_faces,train_non_faces]) train_labels = np.array([0]*len(train_faces)+[1]*len(train_non_faces)).astype(np.float32).reshape(-1,1) test_data = np.vstack([test_faces,test_non_faces]) test_labels = np.array([0]*len(test_faces)+[1]*len(test_non_faces)).astype(np.float32).reshape(-1,1) train_data = train_data.reshape(train_data.shape[0],-1).astype(np.float32) test_data = test_data.reshape(test_data.shape[0],-1).astype(np.float32) # plt.imshow((train_data[1000].reshape(60,60,3)*255).astype(np.float32)) # plt.show() # tf.reset_default_graph() def build_graph(): g = tf.Graph() with g.as_default(): train_x = tf.placeholder(shape=[None,train_data.shape[1]],name="train_data",dtype=np.float32) train_y = tf.placeholder(shape=[None,1],name="train_label",dtype=np.float32) train_x1 = tf.div(train_x,255.0)-0.5 learning_rate = tf.constant(0.05) weights = tf.Variable(tf.random_normal([train_data.shape[1],1],stddev=1e-3),name="weights") bias = tf.Variable(0.1,dtype=np.float32) h = tf.matmul(train_x1,weights)+bias z = tf.sigmoid(h)+1e-6 loss = -tf.reduce_mean(train_y*tf.log(z) + (1-train_y)*tf.log(1-z)) dw,db = tf.gradients(loss,[weights,bias]) weights_update = tf.assign_add(weights,-learning_rate*dw,name='weight_update') bias_update = tf.assign_add(bias,-learning_rate*db,name='bias_update') with tf.control_dependencies([weights_update,bias_update]): train_op = tf.no_op() tf_to_dot(g) return (g,loss,train_op,train_x,train_y,z) (g,loss,train_op,train_x,train_y,z) = build_graph() with g.as_default(): with tf.Session() as sess: sess.run(tf.global_variables_initializer()) for i in xrange(2000): l,_ = sess.run([loss,train_op],feed_dict={train_x:train_data,train_y:train_labels}) if i%100 == 0: print 'Training loss after %d iterations: %f'%(i,l) y_ = sess.run(z,feed_dict = {train_x:test_data}) y_ = y_ > 0.5 accuracy = np.sum((y_ == (test_labels > 0)),0)[0]/(y_.shape[0]*1.0) print 'Accuracy of the model is ',accuracy