-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f4900f0
commit fc11b8d
Showing
11 changed files
with
1,062 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,65 @@ | ||
# First-Impression | ||
# First-Impression | ||
|
||
This is the solution to the poblem "First Impressions V2" given in CVPR'17, ECCV '16 & ICPR '16 and this piece of code is the implementation of the [paper](https://cs.nju.edu.cn/wujx/paper/eccvw16_APA.pdf) which is the winner of ECCV 2016 | ||
|
||
This problem is a challenge on “first impressions”, in which participants will develop solutions for recognizing personality traits of users in short video sequences. They have made available a large newly collected data set sponsored by Microsoft of at least 10,000 15-second videos collected from YouTube, annotated with personality traits by AMT workers. | ||
|
||
The traits to be recognized will correspond to the “big five” personality traits used in psychology and well known of hiring managers using standardized personality profiling: | ||
* Extroversion | ||
* Agreeableness | ||
* Conscientiousness | ||
* Neuroticism | ||
* Openness to experience. | ||
|
||
As is known, the first impression made is highly important in many contexts, such as human resourcing or job interviews. This work could become very relevant to training young people to present themselves better by changing their behavior in simple ways. The participants who obtain the best results in the challenge will be invited to submit a paper to the workshop. | ||
|
||
|
||
## Getting Started | ||
|
||
These instructions will get you a copy of the project up and running on your local machine for development and testing purposes. | ||
|
||
### Prerequisites | ||
|
||
* [Python2](https://www.python.org/download/releases/2.7.2/) - Python version 2.7.2 | ||
* [Numpy](http://www.numpy.org/) - Multidimensioanl Mathematical Computing | ||
* [Tensorflow](https://www.tensorflow.org/) - Deep Learning python module | ||
* [Pandas](https://pandas.pydata.org/) - | ||
* [Cha-Learn Dataset](http://chalearnlap.cvc.uab.es/dataset/24/description/) - Dataset for this problem | ||
* [Pretrained VGG16 Model](https://www.cs.toronto.edu/~frossard/vgg16/vgg16_weights.npz) Pretrained Vgg16 mode | ||
|
||
### Installing | ||
|
||
* Clone the repository | ||
|
||
``` | ||
git clone https://github.com/zishansami102/CNN-from-Scratch | ||
``` | ||
|
||
* Downlad the training dataset and copy that to this folder and extract it into a new /data directory with all 75 zip files as it is | ||
|
||
* [Download](https://www.cs.toronto.edu/~frossard/vgg16/vgg16_weights.npz) Pretrained Vgg16 model and move it to the root directory | ||
|
||
|
||
* Run the vidToimg.py file to scrape the images from the videos and save it to a new ImageData directory | ||
|
||
``` | ||
python VidToimg.py | ||
``` | ||
|
||
|
||
* If succesfully completed then run the savetfreco.py file to form a data pipeline by saving the all the images into train500.tfrecords file to load it later during training | ||
|
||
``` | ||
python savetfreco.py | ||
``` | ||
|
||
* Start the training by running the following command | ||
|
||
``` | ||
python train.py | ||
## Acknowledgments | ||
* [paper](https://cs.nju.edu.cn/wujx/paper/eccvw16_APA.pdf) - Implemented paper | ||
* [TfRecord Data Pipeline](http://machinelearninguru.com/deep_learning/data_preparation/tfrecord/tfrecord.html#read) - Used to make data pipeline |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,251 @@ | ||
######################################################################################## | ||
# Davi Frossard, 2016 # | ||
# VGG16 implementation in TensorFlow # | ||
# Details: # | ||
# http://www.cs.toronto.edu/~frossard/post/vgg16/ # | ||
# # | ||
# Model from https://gist.github.com/ksimonyan/211839e770f7b538e2d8#file-readme-md # | ||
# Weights from Caffe converted using https://github.com/ethereon/caffe-tensorflow # | ||
######################################################################################## | ||
|
||
import tensorflow as tf | ||
import numpy as np | ||
import warnings | ||
|
||
|
||
warnings.filterwarnings("ignore") | ||
|
||
|
||
|
||
|
||
class DAN: | ||
def __init__(self, imgs, weights=None, sess=None): | ||
self.imgs = imgs | ||
self.convlayers() | ||
self.dan_part() | ||
if weights is not None and sess is not None: | ||
self.load_weights(weights, sess) | ||
self.output = tf.nn.sigmoid(self.reg_head) | ||
|
||
|
||
|
||
def convlayers(self): | ||
self.parameters = [] | ||
|
||
# zero-mean input | ||
with tf.name_scope('preprocess') as scope: | ||
mean = tf.constant([123.68, 116.779, 103.939], dtype=tf.float32, shape=[1, 1, 1, 3], name='img_mean') | ||
images = self.imgs-mean | ||
|
||
# conv1_1 | ||
with tf.name_scope('conv1_1') as scope: | ||
kernel = tf.Variable(tf.truncated_normal([3, 3, 3, 64], dtype=tf.float32, | ||
stddev=1e-1), name='weights') | ||
conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME') | ||
biases = tf.Variable(tf.constant(0.0, shape=[64], dtype=tf.float32), | ||
trainable=True, name='biases') | ||
out = tf.nn.bias_add(conv, biases) | ||
self.conv1_1 = tf.nn.relu(out, name=scope) | ||
self.parameters += [kernel, biases] | ||
|
||
# conv1_2 | ||
with tf.name_scope('conv1_2') as scope: | ||
kernel = tf.Variable(tf.truncated_normal([3, 3, 64, 64], dtype=tf.float32, | ||
stddev=1e-1), name='weights') | ||
conv = tf.nn.conv2d(self.conv1_1, kernel, [1, 1, 1, 1], padding='SAME') | ||
biases = tf.Variable(tf.constant(0.0, shape=[64], dtype=tf.float32), | ||
trainable=True, name='biases') | ||
out = tf.nn.bias_add(conv, biases) | ||
self.conv1_2 = tf.nn.relu(out, name=scope) | ||
self.parameters += [kernel, biases] | ||
|
||
# pool1 | ||
self.pool1 = tf.nn.max_pool(self.conv1_2, | ||
ksize=[1, 2, 2, 1], | ||
strides=[1, 2, 2, 1], | ||
padding='SAME', | ||
name='pool1') | ||
|
||
# conv2_1 | ||
with tf.name_scope('conv2_1') as scope: | ||
kernel = tf.Variable(tf.truncated_normal([3, 3, 64, 128], dtype=tf.float32, | ||
stddev=1e-1), name='weights') | ||
conv = tf.nn.conv2d(self.pool1, kernel, [1, 1, 1, 1], padding='SAME') | ||
biases = tf.Variable(tf.constant(0.0, shape=[128], dtype=tf.float32), | ||
trainable=True, name='biases') | ||
out = tf.nn.bias_add(conv, biases) | ||
self.conv2_1 = tf.nn.relu(out, name=scope) | ||
self.parameters += [kernel, biases] | ||
|
||
# conv2_2 | ||
with tf.name_scope('conv2_2') as scope: | ||
kernel = tf.Variable(tf.truncated_normal([3, 3, 128, 128], dtype=tf.float32, | ||
stddev=1e-1), name='weights') | ||
conv = tf.nn.conv2d(self.conv2_1, kernel, [1, 1, 1, 1], padding='SAME') | ||
biases = tf.Variable(tf.constant(0.0, shape=[128], dtype=tf.float32), | ||
trainable=True, name='biases') | ||
out = tf.nn.bias_add(conv, biases) | ||
self.conv2_2 = tf.nn.relu(out, name=scope) | ||
self.parameters += [kernel, biases] | ||
|
||
# pool2 | ||
self.pool2 = tf.nn.max_pool(self.conv2_2, | ||
ksize=[1, 2, 2, 1], | ||
strides=[1, 2, 2, 1], | ||
padding='SAME', | ||
name='pool2') | ||
|
||
# conv3_1 | ||
with tf.name_scope('conv3_1') as scope: | ||
kernel = tf.Variable(tf.truncated_normal([3, 3, 128, 256], dtype=tf.float32, | ||
stddev=1e-1), name='weights') | ||
conv = tf.nn.conv2d(self.pool2, kernel, [1, 1, 1, 1], padding='SAME') | ||
biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32), | ||
trainable=True, name='biases') | ||
out = tf.nn.bias_add(conv, biases) | ||
self.conv3_1 = tf.nn.relu(out, name=scope) | ||
self.parameters += [kernel, biases] | ||
|
||
# conv3_2 | ||
with tf.name_scope('conv3_2') as scope: | ||
kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256], dtype=tf.float32, | ||
stddev=1e-1), name='weights') | ||
conv = tf.nn.conv2d(self.conv3_1, kernel, [1, 1, 1, 1], padding='SAME') | ||
biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32), | ||
trainable=True, name='biases') | ||
out = tf.nn.bias_add(conv, biases) | ||
self.conv3_2 = tf.nn.relu(out, name=scope) | ||
self.parameters += [kernel, biases] | ||
|
||
# conv3_3 | ||
with tf.name_scope('conv3_3') as scope: | ||
kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 256], dtype=tf.float32, | ||
stddev=1e-1), name='weights') | ||
conv = tf.nn.conv2d(self.conv3_2, kernel, [1, 1, 1, 1], padding='SAME') | ||
biases = tf.Variable(tf.constant(0.0, shape=[256], dtype=tf.float32), | ||
trainable=True, name='biases') | ||
out = tf.nn.bias_add(conv, biases) | ||
self.conv3_3 = tf.nn.relu(out, name=scope) | ||
self.parameters += [kernel, biases] | ||
|
||
# pool3 | ||
self.pool3 = tf.nn.max_pool(self.conv3_3, | ||
ksize=[1, 2, 2, 1], | ||
strides=[1, 2, 2, 1], | ||
padding='SAME', | ||
name='pool3') | ||
|
||
# conv4_1 | ||
with tf.name_scope('conv4_1') as scope: | ||
kernel = tf.Variable(tf.truncated_normal([3, 3, 256, 512], dtype=tf.float32, | ||
stddev=1e-1), name='weights') | ||
conv = tf.nn.conv2d(self.pool3, kernel, [1, 1, 1, 1], padding='SAME') | ||
biases = tf.Variable(tf.constant(0.0, shape=[512], dtype=tf.float32), | ||
trainable=True, name='biases') | ||
out = tf.nn.bias_add(conv, biases) | ||
self.conv4_1 = tf.nn.relu(out, name=scope) | ||
self.parameters += [kernel, biases] | ||
|
||
# conv4_2 | ||
with tf.name_scope('conv4_2') as scope: | ||
kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512], dtype=tf.float32, | ||
stddev=1e-1), name='weights') | ||
conv = tf.nn.conv2d(self.conv4_1, kernel, [1, 1, 1, 1], padding='SAME') | ||
biases = tf.Variable(tf.constant(0.0, shape=[512], dtype=tf.float32), | ||
trainable=True, name='biases') | ||
out = tf.nn.bias_add(conv, biases) | ||
self.conv4_2 = tf.nn.relu(out, name=scope) | ||
self.parameters += [kernel, biases] | ||
|
||
# conv4_3 | ||
with tf.name_scope('conv4_3') as scope: | ||
kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512], dtype=tf.float32, | ||
stddev=1e-1), name='weights') | ||
conv = tf.nn.conv2d(self.conv4_2, kernel, [1, 1, 1, 1], padding='SAME') | ||
biases = tf.Variable(tf.constant(0.0, shape=[512], dtype=tf.float32), | ||
trainable=True, name='biases') | ||
out = tf.nn.bias_add(conv, biases) | ||
self.conv4_3 = tf.nn.relu(out, name=scope) | ||
self.parameters += [kernel, biases] | ||
|
||
# pool4 | ||
self.pool4 = tf.nn.max_pool(self.conv4_3, | ||
ksize=[1, 2, 2, 1], | ||
strides=[1, 2, 2, 1], | ||
padding='SAME', | ||
name='pool4') | ||
|
||
# conv5_1 | ||
with tf.name_scope('conv5_1') as scope: | ||
kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512], dtype=tf.float32, | ||
stddev=1e-1), name='weights') | ||
conv = tf.nn.conv2d(self.pool4, kernel, [1, 1, 1, 1], padding='SAME') | ||
biases = tf.Variable(tf.constant(0.0, shape=[512], dtype=tf.float32), | ||
trainable=True, name='biases') | ||
out = tf.nn.bias_add(conv, biases) | ||
self.conv5_1 = tf.nn.relu(out, name=scope) | ||
self.parameters += [kernel, biases] | ||
|
||
# conv5_2 | ||
with tf.name_scope('conv5_2') as scope: | ||
kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512], dtype=tf.float32, | ||
stddev=1e-1), name='weights') | ||
conv = tf.nn.conv2d(self.conv5_1, kernel, [1, 1, 1, 1], padding='SAME') | ||
biases = tf.Variable(tf.constant(0.0, shape=[512], dtype=tf.float32), | ||
trainable=True, name='biases') | ||
out = tf.nn.bias_add(conv, biases) | ||
self.conv5_2 = tf.nn.relu(out, name=scope) | ||
self.parameters += [kernel, biases] | ||
|
||
# conv5_3 | ||
with tf.name_scope('conv5_3') as scope: | ||
kernel = tf.Variable(tf.truncated_normal([3, 3, 512, 512], dtype=tf.float32, | ||
stddev=1e-1), name='weights') | ||
conv = tf.nn.conv2d(self.conv5_2, kernel, [1, 1, 1, 1], padding='SAME') | ||
biases = tf.Variable(tf.constant(0.0, shape=[512], dtype=tf.float32), | ||
trainable=True, name='biases') | ||
out = tf.nn.bias_add(conv, biases) | ||
self.conv5_3 = tf.nn.relu(out, name=scope) | ||
self.parameters += [kernel, biases] | ||
|
||
# MaxPool5 | ||
self.maxpool5 = tf.nn.max_pool(self.conv5_3, | ||
ksize=[1, 2, 2, 1], | ||
strides=[1, 2, 2, 1], | ||
padding='SAME', | ||
name='maxpool5') | ||
|
||
# AvgPool5 | ||
self.avgpool5 = tf.nn.avg_pool(self.conv5_3, | ||
ksize=[1, 2, 2, 1], | ||
strides=[1, 2, 2, 1], | ||
padding='SAME', | ||
name='avgpool5') | ||
|
||
def dan_part(self): | ||
|
||
# fc1 | ||
with tf.name_scope('reg_head') as scope: | ||
shape = 2*int(np.prod(self.maxpool5.get_shape()[1:])) | ||
# shape = int(np.prod(self.pool5.get_shape()[1:])) | ||
fc1w = tf.Variable(tf.truncated_normal([shape, 5], | ||
dtype=tf.float32, | ||
stddev=1e-1), name='weights') | ||
fc1b = tf.Variable(tf.constant(1.0, shape=[5], dtype=tf.float32), | ||
trainable=True, name='biases') | ||
|
||
maxpool5_flat = tf.nn.l2_normalize(tf.reshape(self.maxpool5, [-1, shape/2]), 1) | ||
avgpool5_flat = tf.nn.l2_normalize(tf.reshape(self.avgpool5, [-1, shape/2]), 1) | ||
|
||
self.concat = tf.concat([maxpool5_flat, avgpool5_flat], 1) | ||
self.reg_head = tf.nn.bias_add(tf.matmul(self.concat, fc1w), fc1b) | ||
self.parameters += [fc1w, fc1b] | ||
|
||
|
||
def load_weights(self, weight_file, sess): | ||
weights = np.load(weight_file) | ||
keys = sorted(weights.keys()) | ||
for i, k in enumerate(keys): | ||
if i==len(self.parameters)-2: | ||
break | ||
sess.run(self.parameters[i].assign(weights[k])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import tensorflow as tf | ||
|
||
def printTime(remtime): | ||
hrs = int(remtime)/3600 | ||
mins = int((remtime/60-hrs*60)) | ||
secs = int(remtime-mins*60-hrs*3600) | ||
print(str(hrs)+"Hrs "+str(mins)+"Mins "+str(secs)+"Secs remaining..") | ||
|
||
def dataBatch(data_path, BATCH_SIZE, N_EPOCHS=1): | ||
reader = tf.TFRecordReader() | ||
filename_queue = tf.train.string_input_producer([data_path], num_epochs=N_EPOCHS) | ||
_, serialized_example = reader.read(filename_queue) | ||
# Decode the record read by the reader | ||
feature = {'train/image': tf.FixedLenFeature([], tf.string), 'train/label': tf.FixedLenFeature([], tf.string)} | ||
features = tf.parse_single_example(serialized_example, features=feature) | ||
# Convert the image data from string back to the numbers | ||
image = tf.decode_raw(features['train/image'], tf.float32) | ||
label = tf.decode_raw(features['train/label'], tf.float32) | ||
# Reshape image data into the original shape | ||
image = tf.reshape(image, [224, 224, 3]) | ||
label = tf.reshape(label, [5]) | ||
|
||
images, labels = tf.train.shuffle_batch([image, label], batch_size=BATCH_SIZE, capacity=100, min_after_dequeue=BATCH_SIZE, allow_smaller_final_batch=True) | ||
return images, labels | ||
|
||
|
||
|
||
# VID_BATCH = 100 | ||
|
||
|
||
# df = pd.read_csv('small_train_sample.csv') | ||
# NUM_VID = len(df) | ||
# images=np.empty((NUM_VID*100, 224, 224, 3)) | ||
# traits=np.zeros((NUM_VID*100, 5)) | ||
|
||
# for i in range(NUM_VID): | ||
# filelist=glob.glob('ImageData/trainingData/'+(df['VideoName'].iloc[i]).split('.mp4')[0]+'/*.jpg') | ||
# print(str(i)+" "+ str(len(filelist))+" "+df['VideoName'].iloc[i]) | ||
# images[i*100:(i+1)*100] = np.array([np.array(Image.open(fname).resize((224,224), Image.ANTIALIAS)) for fname in filelist]) | ||
# traits[i*100:(i+1)*100]=np.array(df.drop(['VideoName'], 1, inplace=False).iloc[i]) | ||
|
||
|
||
# ind = [i for i in range(NUM_IMAGES)] | ||
|
||
# print(images.shape) | ||
# print(traits.shape) |
Binary file not shown.
Oops, something went wrong.