Answer To: CS 615 - Deep Learning Assignment 3 - Auto-Encoders and Multi-Layer Perceptrons Winter 2021...
Pulkit answered on Feb 26 2021
Solution/helper.pyimport cv2
import numpy as np
import os
from skimage import io
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import LabelEncoder
from keras.utils import np_utils
OPENCV_HAAR_CSC_PATH = "C:\\Users\\Pulkit\\AppData\\Local\\Continuum\\Anaconda3\\pkgs\\opencv-3.3.0-py36_200\\Library\\etc\\haarcascades\\haarcascade_frontalface_default.xml"
def create_data_set(x_crop= 150, y_crop=150, train_size=.8):
""" Load the Yale Faces data set, extract the faces on the images and generate labels for each image.
Returns: Train and validation samples with their labels. The training samples are flattened arrays
of size 22500 (150 * 150) , the labels are one-hot-encoded values for each category
"""
images_path = [ os.path.join("yalefaces", item) for item in os.listdir("yalefaces") ]
image_data = []
image_labels = []
for i,im_path in enumerate(images_path):
im = io.imread(im_path,as_grey=True)
# if( i== 10) or (i==40) or (i==50):
# io.imshow(im)
# io.show()
image_data.append(np.array(im, dtype='uint8'))
label = int(os.path.split(im_path)[1].split(".")[0].replace("subject", "")) -1
image_labels.append(label)
faceDetectClassifier = cv2.CascadeClassifier(OPENCV_HAAR_CSC_PATH)
cropped_faces = []
for im in image_data:
facePoints = faceDetectClassifier.detectMultiScale(im)
x,y = facePoints[0][:2]
cropped = im[y: y + y_crop, x: x + x_crop]
cropped_faces.append(cropped/255)
X_ = np.array(cropped_faces).astype('float32')
enc = LabelEncoder()
y_ = enc.fit_transform(np.array(image_labels))
y_ = np_utils.to_categorical(y_)
X_train, X_test, y_train, y_test = train_test_split(X_, y_, train_size=train_size, random_state = 22)
return (X_train).reshape((X_train.shape[0],X_train.shape[1]*X_train.shape[2])), (X_test).reshape((X_test.shape[0],X_test.shape[1]*X_test.shape[2])), y_train, y_test
Solution/network.pyimport tensorflow as tf
from datetime import datetime
import os
import numpy as np
class FaceDetector(object):
def __init__(self, dropout=.2, epochs=3, batch_size=5, learning_rate = .00001):
self.layer = Layers()
self.learning_rate = learning_rate
self.dropout = dropout
self.epochs = epochs
self.batch_size = batch_size
def neural_net(self,x):
"""
Multi-layer perceptron.
Returns a multi-layer-perceptron to use with tensorflow
Positional arguments:
x -- tensorflow place holder for input data
"""
# Hidden fully connected layer with 512 neurons
layer_1 = tf.layers.dense(x, 512)
# Hidden fully connected layer with 512 neurons
layer_2 = tf.layers.dense(layer_1, 512)
# Output fully connected layer with a neuron for each class
out_layer = tf.layers.dense(layer_2, self.num_classes)
return out_layer
def build_model(self, input_size, output_size):
"""
Build a tensorflow model for multi-label classification
Positional arguments:
input_size -- dimension of the input samples
output_size -- dimension of the labels
"""
input_x = tf.placeholder(tf.float32, [None, input_size], name="input_x")
input_y = tf.placeholder(tf.int32, [None, output_size], name="input_y")
y_pred= self.neural_net(input_x)
with tf.name_scope('cross_entropy'):
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=input_y,logits=y_pred))
optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate)
train = optimizer.minimize(cross_entropy)
return train, cross_entropy, input_x, input_y, y_pred
def fit(self, X_train, y_train, X_valid=None, y_valid=None):
"""
Fit a tensorflow model
Positional arguments:
X_train -- numpy ndarray of training input
y_train -- one-hot-encoded training labels
Keyword arguments:
X_valid -- numpy ndarray of validation input
y_valid -- one-hot-encoded validation labels
"""
self.num_classes = y_train.shape[1]
print(y_train.shape)
train, cross_entropy, input_x, input_y , y_pred= self.build_model(X_train.shape[1], self.num_classes )
init = tf.global_variables_initializer()
steps = X_train.shape[0]
with tf.Session() as sess:
saver = tf.train.Saver()
sess.run(init)
total_parameters = 0
for variable in tf.trainable_variables():
# shape is an array of tf.Dimension
print("")
shape = variable.get_shape()
print(shape)
print(len(shape))
variable_parameters = 1
for dim in shape:
print(dim)
variable_parameters *= dim.value
print(variable_parameters)
total_parameters += variable_parameters
print("")
print("total_parameters",total_parameters)
while True:
for i in range(0, steps, self.batch_size):
x_batch_train = X_train[i: i+self.batch_size]
y_batch_train = y_train[i: i+self.batch_size]
tr, ce, pr =sess.run([train,cross_entropy, y_pred],feed_dict={input_x:x_batch_train, input_y:y_batch_train})
print("{} iterations: {} loss: {} ".format(str(datetime.now()),i, ce))
if X_valid is not None:
print("\n\nEvaluation...")
tr, ce =sess.run([train,cross_entropy],feed_dict={input_x:X_valid, input_y:y_valid})
print("{} iterations: {} loss: {} ".format(str(datetime.now()),i, ce))
print("\n")
self.epochs -= 1
if self.epochs ==0:
if not os.path.exists(os.path.join(os.getcwd(), 'saved_model')):
os.makedirs(os.path.join(os.getcwd(), 'saved_model'))
saver.save(sess, os.path.join(os.getcwd(), 'saved_model','my_test_model'))
break
def predict(self, X_valid, y_valid):
"""
Returns a dictionnary containing the model's predictino as well as the ground truth,
encoded as...