In [1]:
%matplotlib inline
import matplotlib.pyplot as plt
import tensorflow as tf
import numpy as np
from sklearn.metrics import confusion_matrix
import time
from datetime import timedelta
import math
from tqdm import tqdm
from sklearn.utils import shuffle 
import cv2
import os
import glob
import operator

Get data


In [24]:
# convert video to imgs
vidcap = cv2.VideoCapture('/Users/xueguoliang/Desktop/data/VideoRecording-2017-06-20-21-53-44.mp4')
vidcap.set(cv2.CAP_PROP_POS_MSEC,5000)
success,image = vidcap.read()
count = 0
success = True
while success:
    success,image = vidcap.read()
    #rotation
    num_rows, num_cols = image.shape[:2]
    rotation_matrix = cv2.getRotationMatrix2D((num_cols/2, num_rows/2), -90, 1)
    img_rotation = cv2.warpAffine(image, rotation_matrix, (num_cols, num_rows))
    #crop the image to get the facial region
    img = img_rotation[50:400,150:350]
    cv2.imwrite("converted/frame{}.jpg".format(count), img)     # save frame as JPEG file
    count += 1


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-24-d1e9de24f6ad> in <module>()
      8     success,image = vidcap.read()
      9     #rotation
---> 10     num_rows, num_cols = image.shape[:2]
     11     rotation_matrix = cv2.getRotationMatrix2D((num_cols/2, num_rows/2), -90, 1)
     12     img_rotation = cv2.warpAffine(image, rotation_matrix, (num_cols, num_rows))

AttributeError: 'NoneType' object has no attribute 'shape'

In [43]:
# convert imgs to array
dirname = "/Users/xueguoliang/myGithub/affective computing/converted"

# Load every image file in the provided directory
filenames = [os.path.join(dirname, fname)
             for fname in os.listdir(dirname) if fname.split(".")[1] == 'jpg']

# Make sure we have exactly 100 image files!
filenames_100 = filenames[:100]
assert(len(filenames_100) == 100)
# Read every filename as an RGB image
imgs = [plt.imread(fname,)[..., :3] for fname in filenames_100]
# Then resize the square image to 48 x 48 pixels
imgs = [cv2.resize(img_i, (48, 48)) for img_i in imgs]
# Finally make our list of 3-D images a 4-D array with the first dimension the number of images:
imgs = np.array(imgs).astype(np.float32)
print (imgs)


[[[[ 247.  227.  205.]
   [ 244.  226.  204.]
   [ 243.  225.  205.]
   ..., 
   [ 211.  188.  172.]
   [ 216.  192.  165.]
   [ 214.  189.  168.]]

  [[ 249.  231.  209.]
   [ 246.  229.  209.]
   [ 245.  228.  208.]
   ..., 
   [ 212.  187.  165.]
   [ 212.  187.  165.]
   [ 213.  189.  164.]]

  [[ 250.  233.  216.]
   [ 249.  232.  213.]
   [ 248.  231.  213.]
   ..., 
   [ 213.  188.  166.]
   [ 214.  188.  164.]
   [ 213.  189.  163.]]

  ..., 
  [[  23.   18.   24.]
   [  26.   21.   27.]
   [  21.   16.   22.]
   ..., 
   [  14.   10.   16.]
   [  14.    9.   14.]
   [  14.    9.   13.]]

  [[  18.   13.   19.]
   [  20.   15.   22.]
   [  17.   12.   19.]
   ..., 
   [  15.   10.   16.]
   [  14.    9.   15.]
   [  13.    8.   12.]]

  [[  18.   13.   19.]
   [  19.   12.   19.]
   [  18.   11.   18.]
   ..., 
   [  14.    9.   15.]
   [  10.    8.   11.]
   [  12.    7.   11.]]]


 [[[ 247.  227.  205.]
   [ 244.  226.  204.]
   [ 243.  225.  205.]
   ..., 
   [ 213.  189.  166.]
   [ 217.  190.  170.]
   [ 213.  187.  170.]]

  [[ 249.  231.  209.]
   [ 246.  229.  209.]
   [ 245.  228.  208.]
   ..., 
   [ 212.  188.  164.]
   [ 214.  186.  162.]
   [ 212.  186.  160.]]

  [[ 250.  233.  216.]
   [ 249.  232.  213.]
   [ 247.  230.  213.]
   ..., 
   [ 213.  188.  159.]
   [ 214.  187.  164.]
   [ 214.  188.  163.]]

  ..., 
  [[  24.   19.   24.]
   [  22.   17.   23.]
   [  17.   12.   15.]
   ..., 
   [  15.   10.   16.]
   [  14.   10.   12.]
   [  14.    9.   13.]]

  [[  21.   16.   23.]
   [  17.   12.   19.]
   [  17.   13.   14.]
   ..., 
   [  15.   10.   16.]
   [  14.    9.   15.]
   [  13.    8.   12.]]

  [[  18.   13.   20.]
   [  18.   11.   18.]
   [  18.   12.   16.]
   ..., 
   [  11.    9.   11.]
   [  10.    8.   11.]
   [  12.    7.   11.]]]


 [[[ 254.  248.  231.]
   [ 255.  245.  225.]
   [ 254.  241.  224.]
   ..., 
   [ 212.  190.  166.]
   [ 212.  187.  165.]
   [ 212.  187.  165.]]

  [[ 254.  252.  236.]
   [ 255.  250.  230.]
   [ 255.  247.  228.]
   ..., 
   [ 210.  188.  164.]
   [ 212.  186.  161.]
   [ 211.  186.  157.]]

  [[ 254.  253.  243.]
   [ 255.  253.  238.]
   [ 255.  250.  233.]
   ..., 
   [ 212.  188.  163.]
   [ 212.  187.  168.]
   [ 212.  186.  163.]]

  ..., 
  [[  21.   14.   25.]
   [  19.   12.   23.]
   [  19.   12.   19.]
   ..., 
   [  13.   11.   14.]
   [  15.   10.   14.]
   [  14.    8.   18.]]

  [[  20.   13.   20.]
   [  18.   12.   16.]
   [  20.   14.   18.]
   ..., 
   [  15.    8.   14.]
   [  14.    9.   13.]
   [  13.    8.   14.]]

  [[  17.   11.   20.]
   [  18.   13.   21.]
   [  18.   13.   17.]
   ..., 
   [  13.    8.   12.]
   [  15.   10.   14.]
   [  14.    9.   12.]]]


 ..., 
 [[[ 191.  166.  147.]
   [ 190.  164.  150.]
   [ 188.  161.  151.]
   ..., 
   [ 176.  149.  130.]
   [ 177.  151.  127.]
   [ 177.  151.  127.]]

  [[ 194.  168.  151.]
   [ 192.  165.  153.]
   [ 191.  166.  145.]
   ..., 
   [ 177.  152.  130.]
   [ 178.  151.  135.]
   [ 179.  152.  134.]]

  [[ 198.  171.  150.]
   [ 196.  170.  153.]
   [ 195.  170.  148.]
   ..., 
   [ 181.  153.  138.]
   [ 179.  152.  136.]
   [ 179.  154.  131.]]

  ..., 
  [[ 198.  178.  160.]
   [ 197.  174.  157.]
   [ 199.  174.  152.]
   ..., 
   [ 177.  149.  137.]
   [ 180.  148.  127.]
   [ 181.  150.  123.]]

  [[ 200.  176.  152.]
   [ 197.  175.  151.]
   [ 197.  176.  149.]
   ..., 
   [ 178.  150.  117.]
   [ 176.  151.  121.]
   [ 178.  150.  120.]]

  [[ 202.  180.  156.]
   [ 201.  180.  152.]
   [ 198.  176.  153.]
   ..., 
   [ 186.  152.  121.]
   [ 183.  151.  122.]
   [ 181.  151.  126.]]]


 [[[ 190.  167.  144.]
   [ 189.  165.  144.]
   [ 190.  161.  145.]
   ..., 
   [ 177.  148.  131.]
   [ 177.  150.  132.]
   [ 177.  150.  131.]]

  [[ 192.  170.  149.]
   [ 191.  167.  154.]
   [ 191.  162.  146.]
   ..., 
   [ 176.  155.  128.]
   [ 178.  151.  132.]
   [ 179.  152.  133.]]

  [[ 197.  170.  151.]
   [ 196.  170.  152.]
   [ 195.  170.  146.]
   ..., 
   [ 180.  152.  138.]
   [ 180.  153.  135.]
   [ 179.  155.  130.]]

  ..., 
  [[ 199.  177.  158.]
   [ 197.  174.  157.]
   [ 199.  175.  155.]
   ..., 
   [ 175.  149.  124.]
   [ 178.  149.  126.]
   [ 181.  151.  122.]]

  [[ 198.  176.  152.]
   [ 197.  175.  151.]
   [ 199.  175.  147.]
   ..., 
   [ 176.  149.  122.]
   [ 177.  150.  120.]
   [ 180.  150.  120.]]

  [[ 202.  182.  157.]
   [ 201.  180.  152.]
   [ 201.  176.  154.]
   ..., 
   [ 185.  152.  114.]
   [ 183.  150.  124.]
   [ 183.  150.  128.]]]


 [[[ 189.  167.  144.]
   [ 188.  166.  142.]
   [ 190.  162.  141.]
   ..., 
   [ 176.  149.  129.]
   [ 177.  150.  129.]
   [ 177.  150.  130.]]

  [[ 194.  168.  150.]
   [ 192.  167.  149.]
   [ 192.  163.  149.]
   ..., 
   [ 177.  153.  127.]
   [ 178.  151.  134.]
   [ 179.  152.  132.]]

  [[ 197.  171.  153.]
   [ 196.  170.  154.]
   [ 195.  168.  155.]
   ..., 
   [ 181.  154.  131.]
   [ 179.  152.  132.]
   [ 180.  155.  128.]]

  ..., 
  [[ 201.  176.  156.]
   [ 198.  176.  153.]
   [ 197.  175.  156.]
   ..., 
   [ 175.  149.  123.]
   [ 175.  150.  127.]
   [ 177.  152.  124.]]

  [[ 200.  175.  153.]
   [ 198.  176.  151.]
   [ 198.  177.  146.]
   ..., 
   [ 178.  151.  122.]
   [ 177.  150.  121.]
   [ 179.  151.  124.]]

  [[ 203.  181.  158.]
   [ 201.  180.  152.]
   [ 199.  178.  151.]
   ..., 
   [ 185.  152.  117.]
   [ 184.  152.  121.]
   [ 182.  150.  123.]]]]

Model building


In [2]:
def getData(balance_ones=True):
    # images are 48x48 = 2304 size vectors
    # N = 35887
    Y_train = []
    X_train = []
    
    Y_test = []
    X_test = []
    
    first = True
   
    #init length of one-hot vector
    n_labels = 7
    
    for line in open('fer2013/fer2013.csv'):
        if first:
            first = False
        else:
            row = line.split(',')
            if row[2].strip()=="Training":
                Y_train.append(int(row[0]))
                X_train.append([int(p) for p in row[1].split()])
            elif row[2].strip()=="PublicTest":
                Y_test.append(int(row[0]))
                X_test.append([int(p) for p in row[1].split()])
                            
    X_train, Y_train = np.array(X_train) / 255.0, np.array(Y_train)
    X_test, Y_test = np.array(X_test) / 255.0, np.array(Y_test)
        
    return X_train, Y_train, X_test, Y_test

In [3]:
X_train, Y_train, X_test, Y_test = getData()

In [10]:
cv2.imshow((X_train[0]*255).reshape(48,48), cmap='hot')


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-7548435121b4> in <module>()
----> 1 cv2.imshow((X_train[0]*255).reshape(48,48), cmap='hot')

TypeError: Required argument 'mat' (pos 2) not found

In [4]:
N = len(X_train)
N_test = len(X_test)
print (N, N_test)


28709 3589

Input Parameters


In [5]:
# Facial images are 48 pixels in each dimension.
img_size = 48

# Images are stored in one-dimensional arrays of this length.
img_size_flat = img_size * img_size

# Tuple with height and width of images used to reshape arrays.
img_shape = (img_size, img_size)

# Number of colour channels for the images: 1 channel for gray-scale.
num_channels = 1

# Number of classes
num_classes = 7  # 0=Angry, 1=Disgust, 2=Fear, 3=Happy, 4=Sad, 5=Surprise, 6=Neutral

configuration of neural network


In [6]:
# Convolutional Layer 1.
filter_size1 = 5          # Convolution filters are 5 x 5 pixels.
num_filters1 = 16         # There are 16 of these filters.

# Convolutional Layer 2.
filter_size2 = 5          # Convolution filters are 5 x 5 pixels.
num_filters2 = 36         # There are 36 of these filters.

# Fully-connected layer.
fc_size = 128             # Number of neurons in fully-connected layer.

Plot image


In [7]:
def plot_images(images, cls_true, cls_pred=None):
    
    liter = ["Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral"]
    
    # Create figure with 3x3 sub-plots.
    fig, axes = plt.subplots(3, 3)
    fig.subplots_adjust(hspace=0.3, wspace=0.3)

    for i, ax in enumerate(axes.flat):
        # Plot image.
        ax.imshow(images[i].reshape(img_shape) , cmap='hot')

        # Show true and predicted classes.
        if cls_pred is None: 
            xlabel = "True: {0}".format(liter[cls_true[i]])
        else:
            xlabel = "True: {0}, Pred: {1}".format(cls_true[i], cls_pred[i])

        # Show the classes as the label on the x-axis.
        ax.set_xlabel(xlabel)
        
        # Remove ticks from the plot.
        ax.set_xticks([])
        ax.set_yticks([])
    
    # Ensure the plot is shown correctly with multiple plots
    # in a single Notebook cell.
    plt.show()

In [13]:
# Get the first images from the test-set.
faces = X[0:9]

# Get the true classes for those images.
cls_true = Y[0:9]

# Plot the images and labels using our helper-function above.
plot_images(images=faces, cls_true=cls_true)


Build of CNN


In [8]:
def new_weights(shape):
    return tf.Variable(tf.truncated_normal(shape, stddev=0.05))

def new_biases(length):
    return tf.Variable(tf.constant(0.05, shape=[length]))

In [9]:
def new_conv_layer(input,              # The previous layer.
                   num_input_channels, # Num. channels in prev. layer.
                   filter_size,        # Width and height of each filter.
                   num_filters,        # Number of filters.
                   use_pooling=True):  # Use 2x2 max-pooling.

    # Shape of the filter-weights for the convolution.
    # This format is determined by the TensorFlow API.
    shape = [filter_size, filter_size, num_input_channels, num_filters]

    # Create new weights aka. filters with the given shape.
    weights = new_weights(shape=shape)

    # Create new biases, one for each filter.
    biases = new_biases(length=num_filters)

    # Create the TensorFlow operation for convolution.
    # Note the strides are set to 1 in all dimensions.
    # The first and last stride must always be 1,
    # because the first is for the image-number and
    # the last is for the input-channel.
    # But e.g. strides=[1, 2, 2, 1] would mean that the filter
    # is moved 2 pixels across the x- and y-axis of the image.
    # The padding is set to 'SAME' which means the input image
    # is padded with zeroes so the size of the output is the same.
    layer = tf.nn.conv2d(input=input,
                         filter=weights,
                         strides=[1, 1, 1, 1],
                         padding='SAME')

    # Add the biases to the results of the convolution.
    # A bias-value is added to each filter-channel.
    layer += biases

    # Use pooling to down-sample the image resolution?
    if use_pooling:
        # This is 2x2 max-pooling, which means that we
        # consider 2x2 windows and select the largest value
        # in each window. Then we move 2 pixels to the next window.
        layer = tf.nn.max_pool(value=layer,
                               ksize=[1, 2, 2, 1],
                               strides=[1, 2, 2, 1],
                               padding='SAME')

    # Rectified Linear Unit (ReLU).
    # It calculates max(x, 0) for each input pixel x.
    # This adds some non-linearity to the formula and allows us
    # to learn more complicated functions.
    layer = tf.nn.relu(layer)

    # Note that ReLU is normally executed before the pooling,
    # but since relu(max_pool(x)) == max_pool(relu(x)) we can
    # save 75% of the relu-operations by max-pooling first.

    # We return both the resulting layer and the filter-weights
    # because we will plot the weights later.
    return layer, weights

In [10]:
def flatten_layer(layer):
    # Get the shape of the input layer.
    layer_shape = layer.get_shape()

    # The shape of the input layer is assumed to be:
    # layer_shape == [num_images, img_height, img_width, num_channels]

    # The number of features is: img_height * img_width * num_channels
    # We can use a function from TensorFlow to calculate this.
    num_features = layer_shape[1:4].num_elements()
    
    # Reshape the layer to [num_images, num_features].
    # Note that we just set the size of the second dimension
    # to num_features and the size of the first dimension to -1
    # which means the size in that dimension is calculated
    # so the total size of the tensor is unchanged from the reshaping.
    layer_flat = tf.reshape(layer, [-1, num_features])

    # The shape of the flattened layer is now:
    # [num_images, img_height * img_width * num_channels]

    # Return both the flattened layer and the number of features.
    return layer_flat, num_features

In [11]:
def new_fc_layer(input,          # The previous layer.
                 num_inputs,     # Num. inputs from prev. layer.
                 num_outputs,    # Num. outputs.
                 use_relu=True): # Use Rectified Linear Unit (ReLU)?

    # Create new weights and biases.
    weights = new_weights(shape=[num_inputs, num_outputs])
    biases = new_biases(length=num_outputs)

    # Calculate the layer as the matrix multiplication of
    # the input and weights, and then add the bias-values.
    layer = tf.matmul(input, weights) + biases

    # Use ReLU?
    if use_relu:
        layer = tf.nn.relu(layer)

    return layer

Placeholder


In [12]:
x = tf.placeholder(tf.float32, shape=[None, img_size_flat], name='x')
x_image = tf.reshape(x, [-1, img_size, img_size, num_channels])
y_true = tf.placeholder(tf.float32, shape=[None, 7], name='y_true')
y_true_cls = tf.argmax(y_true, dimension=1)

Convolutional Layers


In [13]:
layer_conv1, weights_conv1 = new_conv_layer(input=x_image,
                                            num_input_channels=num_channels,
                                            filter_size=filter_size1,
                                            num_filters=num_filters1,
                                            use_pooling=True)
  
layer_conv1


Out[13]:
<tf.Tensor 'Relu:0' shape=(?, 24, 24, 16) dtype=float32>

In [14]:
layer_conv2, weights_conv2 = new_conv_layer(input=layer_conv1,
                                            num_input_channels=num_filters1,
                                            filter_size=filter_size2,
                                            num_filters=num_filters2,
                                            use_pooling=True)
layer_conv2


Out[14]:
<tf.Tensor 'Relu_1:0' shape=(?, 12, 12, 36) dtype=float32>

Flatten Layer


In [15]:
layer_flat, num_features = flatten_layer(layer_conv2)
layer_flat


Out[15]:
<tf.Tensor 'Reshape_1:0' shape=(?, 5184) dtype=float32>

In [22]:
num_features


Out[22]:
5184

Fully Connected Layers


In [16]:
layer_fc1 = new_fc_layer(input=layer_flat,
                         num_inputs=num_features,
                         num_outputs=fc_size,
                         use_relu=True)
layer_fc1


Out[16]:
<tf.Tensor 'Relu_2:0' shape=(?, 128) dtype=float32>

In [17]:
layer_fc2 = new_fc_layer(input=layer_fc1,
                         num_inputs=fc_size,
                         num_outputs=num_classes,
                         use_relu=False)
layer_fc2


Out[17]:
<tf.Tensor 'add_3:0' shape=(?, 7) dtype=float32>

Predicted Class


In [18]:
y_pred = tf.nn.softmax(layer_fc2)
y_pred_cls = tf.argmax(y_pred, dimension=1)

Cost Function to be Optimezed


In [19]:
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(logits=layer_fc2,
                                                        labels=y_true)
cost = tf.reduce_mean(cross_entropy)

optimizer = tf.train.AdamOptimizer(learning_rate=1e-4).minimize(cost)

correct_prediction = tf.equal(y_pred_cls, y_true_cls)

accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

Execution


In [20]:
session = tf.Session()
session.run(tf.global_variables_initializer())

In [21]:
train_batch_size = 64
n_batches = N // train_batch_size

# Counter for total number of iterations performed so far.
total_iterations = 0

def optimize(num_iterations, X, Y):
    # Ensure we update the global variable rather than a local copy.
    global total_iterations

    # Start-time used for printing time-usage below.
    start_time = time.time()
    
    for i in range(total_iterations,
                   total_iterations + num_iterations):

        # Get a batch of training examples.
        # x_batch now holds a batch of images and
        # y_true_batch are the true labels for those images.
        
        X, Y = shuffle(X, Y)
        for j in tqdm(range(n_batches)):
            x_batch = X[j*train_batch_size:(j*train_batch_size+train_batch_size)]
            ys = Y[j*train_batch_size:(j*train_batch_size+train_batch_size)] 

            #convert to one-hot
            y_true_batch = []
            for y in ys:
                tmp = [0.,0.,0.,0.,0.,0.,0.]
                tmp[y] = 1
                y_true_batch.append(tmp)
                
            y_true_batch = np.array(y_true_batch)
            #print (y_true_batch)
            # Put the batch into a dict with the proper names
            # for placeholder variables in the TensorFlow graph.
            feed_dict_train = {x: x_batch,
                               y_true: y_true_batch}

            # Run the optimizer using this batch of training data.
            # TensorFlow assigns the variables in feed_dict_train
            # to the placeholder variables and then runs the optimizer.
            session.run(optimizer, feed_dict=feed_dict_train)

            # Print status every 100 iterations.
        if i % 100 == 0:
            # Calculate the accuracy on the training-set.
            acc = session.run(accuracy, feed_dict=feed_dict_train)

            # Message for printing.
            msg = "Optimization Iteration: {0:>6}, Training Accuracy: {1:>6.1%}"

            # Print it.
            print(msg.format(i + 1, acc))

    # Update the total number of iterations performed.
    total_iterations += num_iterations

    # Ending time.
    end_time = time.time()

    # Difference between start and end-times.
    time_dif = end_time - start_time

    # Print the time-usage.
    print("Time usage: " + str(timedelta(seconds=int(round(time_dif)))))

In [23]:
optimize(95, X_train, Y_train)


100%|██████████| 448/448 [02:19<00:00,  2.90it/s]     | 1/448 [00:00<05:33,  1.34it/s]
100%|██████████| 448/448 [02:18<00:00,  3.53it/s]
100%|██████████| 448/448 [02:27<00:00,  3.46it/s]
100%|██████████| 448/448 [02:19<00:00,  3.33it/s]
100%|██████████| 448/448 [02:23<00:00,  3.22it/s]
100%|██████████| 448/448 [02:19<00:00,  2.72it/s]
100%|██████████| 448/448 [02:21<00:00,  3.01it/s]
100%|██████████| 448/448 [02:11<00:00,  3.60it/s]
100%|██████████| 448/448 [02:04<00:00,  3.66it/s]
100%|██████████| 448/448 [02:04<00:00,  3.54it/s]
100%|██████████| 448/448 [02:04<00:00,  3.65it/s]
100%|██████████| 448/448 [02:07<00:00,  3.62it/s]
100%|██████████| 448/448 [41:07<00:00,  3.43it/s]    
100%|██████████| 448/448 [1:25:24<00:00,  3.18it/s]    
100%|██████████| 448/448 [02:13<00:00,  3.35it/s]
100%|██████████| 448/448 [02:10<00:00,  3.42it/s]
100%|██████████| 448/448 [02:15<00:00,  3.51it/s]
100%|██████████| 448/448 [02:11<00:00,  3.38it/s]
100%|██████████| 448/448 [02:15<00:00,  3.55it/s]
100%|██████████| 448/448 [55:06<00:00,  3.35it/s]    
100%|██████████| 448/448 [02:06<00:00,  3.59it/s]
100%|██████████| 448/448 [02:04<00:00,  3.63it/s]
100%|██████████| 448/448 [02:11<00:00,  2.72it/s]
100%|██████████| 448/448 [02:27<00:00,  3.59it/s]
100%|██████████| 448/448 [02:15<00:00,  3.11it/s]
100%|██████████| 448/448 [02:27<00:00,  3.29it/s]
100%|██████████| 448/448 [02:22<00:00,  3.02it/s]
100%|██████████| 448/448 [02:30<00:00,  3.35it/s]
100%|██████████| 448/448 [02:22<00:00,  3.25it/s]
100%|██████████| 448/448 [02:33<00:00,  3.29it/s]
100%|██████████| 448/448 [02:13<00:00,  3.48it/s]
100%|██████████| 448/448 [02:17<00:00,  3.23it/s]
100%|██████████| 448/448 [02:19<00:00,  3.26it/s]
100%|██████████| 448/448 [02:19<00:00,  3.31it/s]
100%|██████████| 448/448 [02:23<00:00,  3.20it/s]
100%|██████████| 448/448 [02:18<00:00,  3.22it/s]
100%|██████████| 448/448 [02:18<00:00,  3.34it/s]
100%|██████████| 448/448 [02:18<00:00,  2.89it/s]
100%|██████████| 448/448 [02:26<00:00,  2.41it/s]
100%|██████████| 448/448 [02:28<00:00,  3.37it/s]
100%|██████████| 448/448 [02:31<00:00,  2.89it/s]
100%|██████████| 448/448 [02:28<00:00,  3.52it/s]
100%|██████████| 448/448 [02:16<00:00,  3.70it/s]
100%|██████████| 448/448 [02:18<00:00,  3.70it/s]
100%|██████████| 448/448 [02:01<00:00,  3.68it/s]
100%|██████████| 448/448 [02:17<00:00,  2.96it/s]
100%|██████████| 448/448 [02:30<00:00,  3.14it/s]
100%|██████████| 448/448 [02:33<00:00,  2.48it/s]
100%|██████████| 448/448 [02:44<00:00,  1.82it/s]
100%|██████████| 448/448 [03:11<00:00,  2.88it/s]
100%|██████████| 448/448 [02:39<00:00,  2.97it/s]
100%|██████████| 448/448 [02:10<00:00,  3.54it/s]
100%|██████████| 448/448 [02:15<00:00,  3.03it/s]
100%|██████████| 448/448 [02:35<00:00,  3.06it/s]
100%|██████████| 448/448 [02:30<00:00,  3.00it/s]
100%|██████████| 448/448 [02:15<00:00,  3.50it/s]
100%|██████████| 448/448 [02:15<00:00,  3.49it/s]
100%|██████████| 448/448 [02:08<00:00,  3.50it/s]
100%|██████████| 448/448 [02:07<00:00,  3.49it/s]
100%|██████████| 448/448 [02:08<00:00,  3.53it/s]
100%|██████████| 448/448 [02:07<00:00,  3.54it/s]
100%|██████████| 448/448 [06:17<00:00,  3.42it/s]  
100%|██████████| 448/448 [02:10<00:00,  3.51it/s]
100%|██████████| 448/448 [02:09<00:00,  3.12it/s]
100%|██████████| 448/448 [02:13<00:00,  3.57it/s]
100%|██████████| 448/448 [02:12<00:00,  3.66it/s]
100%|██████████| 448/448 [02:07<00:00,  3.60it/s]
100%|██████████| 448/448 [02:11<00:00,  3.35it/s]
100%|██████████| 448/448 [02:07<00:00,  3.60it/s]
100%|██████████| 448/448 [02:06<00:00,  3.58it/s]
100%|██████████| 448/448 [02:18<00:00,  3.34it/s]
100%|██████████| 448/448 [02:18<00:00,  3.06it/s]
100%|██████████| 448/448 [02:18<00:00,  3.55it/s]
100%|██████████| 448/448 [02:06<00:00,  3.59it/s]
100%|██████████| 448/448 [02:14<00:00,  3.55it/s]
100%|██████████| 448/448 [02:11<00:00,  3.51it/s]
100%|██████████| 448/448 [02:05<00:00,  3.47it/s]
100%|██████████| 448/448 [02:13<00:00,  3.46it/s]
100%|██████████| 448/448 [02:08<00:00,  3.47it/s]
100%|██████████| 448/448 [02:09<00:00,  3.52it/s]
100%|██████████| 448/448 [02:08<00:00,  3.51it/s]
100%|██████████| 448/448 [02:09<00:00,  3.47it/s]
100%|██████████| 448/448 [02:41<00:00,  3.21it/s]
100%|██████████| 448/448 [02:20<00:00,  3.58it/s]
100%|██████████| 448/448 [02:16<00:00,  2.78it/s]
100%|██████████| 448/448 [02:16<00:00,  3.59it/s]
100%|██████████| 448/448 [02:19<00:00,  2.96it/s]
100%|██████████| 448/448 [02:09<00:00,  3.62it/s]
100%|██████████| 448/448 [02:06<00:00,  3.40it/s]
100%|██████████| 448/448 [02:04<00:00,  3.59it/s]
100%|██████████| 448/448 [02:09<00:00,  3.69it/s]
100%|██████████| 448/448 [02:08<00:00,  3.64it/s]
100%|██████████| 448/448 [02:03<00:00,  3.62it/s]
100%|██████████| 448/448 [02:03<00:00,  3.65it/s]
100%|██████████| 448/448 [02:03<00:00,  3.70it/s]
Time usage: 6:37:16


In [31]:
# convert imgs to array
dirname = "/Users/xueguoliang/myGithub/affective computing/converted"

# Load every image file in the provided directory
filenames = [os.path.join(dirname, fname)
             for fname in os.listdir(dirname) if fname.split(".")[1] == 'jpg']

# Make sure we have exactly 100 image files!
filenames_100 = filenames[:100]
assert(len(filenames_100) == 100)
# Read every filename as an RGB image
imgs = [plt.imread(fname)[..., :3] for fname in filenames_100]
# Then resize the square image to 48 x 48 pixels
imgs = [cv2.resize(img_i, (48, 48)) for img_i in imgs]
# Finally make our list of 3-D images a 4-D array with the first dimension the number of images:
imgs = np.array(imgs).astype(np.float32)

In [34]:
print (imgs[0][0][0])


[ 247.  227.  205.]

In [ ]:
feed_dict = {x: [your_image]}
classification = tf.run(y_pred_cls, feed_dict)
print classification

In [ ]:
# Split the test-set into smaller batches of this size.
test_batch_size = 256

def print_test_accuracy(show_example_errors=False,
                        show_confusion_matrix=False):

    # Number of images in the test-set.
    num_test = len(X_test)

    # Allocate an array for the predicted classes which
    # will be calculated in batches and filled into this array.
    cls_pred = np.zeros(shape=num_test, dtype=np.int)

    # Now calculate the predicted classes for the batches.
    # We will just iterate through all the batches.
    # There might be a more clever and Pythonic way of doing this.

    # The starting index for the next batch is denoted i.
    i = 0

    while i < num_test:
        # The ending index for the next batch is denoted j.
        j = min(i + test_batch_size, num_test)

        # Get the images from the test-set between index i and j.
        images = X_test[i:j, :]

        # Get the associated labels.
        labels = data.test.labels[i:j, :]

        # Create a feed-dict with these images and labels.
        feed_dict = {x: images,
                     y_true: labels}

        # Calculate the predicted class using TensorFlow.
        cls_pred[i:j] = session.run(y_pred_cls, feed_dict=feed_dict)

        # Set the start-index for the next batch to the
        # end-index of the current batch.
        i = j

    # Convenience variable for the true class-numbers of the test-set.
    cls_true = data.test.cls

    # Create a boolean array whether each image is correctly classified.
    correct = (cls_true == cls_pred)

    # Calculate the number of correctly classified images.
    # When summing a boolean array, False means 0 and True means 1.
    correct_sum = correct.sum()

    # Classification accuracy is the number of correctly classified
    # images divided by the total number of images in the test-set.
    acc = float(correct_sum) / num_test

    # Print the accuracy.
    msg = "Accuracy on Test-Set: {0:.1%} ({1} / {2})"
    print(msg.format(acc, correct_sum, num_test))

In [28]:



Tensor("ArgMax_1:0", shape=(?,), dtype=int64)

In [38]:
print_test_accuracy()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-38-f148a7c94917> in <module>()
----> 1 print_test_accuracy()

NameError: name 'print_test_accuracy' is not defined