Python Tensorflow中的人脸识别系统

时间:2016-01-02 20:53:00

标签: python machine-learning computer-vision tensorflow conv-neural-network

因此,我决定进一步研究Google的Tensorflow中的MNIST tutorial并尝试创建一个基本的人脸识别系统。

目录:

amar - >包含所有目标图像

测试 - >包含所有带负片的测试图像

火车 - >包含所有训练图像

每个目录中有60个图像文件。我使用目录名称作为图像标签。

此时我能够提取图像强度,一切都已完成,但我收到以下错误:

I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 4
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 4
[[0, 0, 1], [0, 0, 1]]
Traceback (most recent call last):
  File "face.py", line 82, in <module>
    model()
  File "face.py", line 74, in model
    sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/client/session.py", line 357, in run
    np_val = np.array(subfeed_val, dtype=subfeed_t.dtype.as_numpy_dtype)
ValueError: setting an array element with a sequence.

以下是代码:

def preprocessImages(dir):      # Grescaling the images
    from os import listdir
    from os.path import isfile, join
    import Image    
    path = dir
    onlyfiles = [f for f in listdir(path) if isfile(join(path, f))]
    for files in onlyfiles:
        img = Image.open(str(path+files)).convert('L')
        img.save(str(path+files))

def extractImages(path):        # Extracting image pixel intensities in an array
    images = []
    from os import listdir
    from os.path import isfile, join
    import Image
    onlyfiles = [f for f in listdir(path) if isfile(join(path, f))]
    for image in onlyfiles:
        img = Image.open(path+image)
        pixVal = list(img.getdata())
        images.append(pixVal)
    return images

def extractLabels(target):      # Extracting labels or directories accordingly 
    res = []
    path = './'
    from os import listdir
    from os.path import isfile, join
    import Image    
    onlyfiles = [f for f in listdir(path) if not isfile(join(path, f))]
    for i in onlyfiles:
        if i == target:
            res.append(1)
        else:
            res.append(0)
    return res
dirs = ['train','test','amar']  # put some directories here
labels = []
images = []

for di in dirs:
    labels.append(extractLabels(di))        
    images.append(extractImages(('./'+di+'/')))

def batch(no):          # Function to select a batch of elements from both the arrays
    import random
    import numpy as np
    global labels
    global images
    lab = []
    img = []
    for i in range(no):
        lab.append(random.choice(labels))
    for i in range(no):
        img.append(random.choice(images))
    return img,lab

def model():
    import tensorflow as tf
    x = tf.placeholder(tf.float32, [None,409600])       # The images of 240x240 = 409600 pixels
    W = tf.Variable(tf.zeros([409600,3]))           # The weights for each image 
    b = tf.Variable(tf.zeros([3]))              # The labels of the images containing the real numbers
    y = tf.nn.softmax(tf.matmul(x, W) + b)          # The predicted y viz. y = softmax(W*x + b)
    y_ = tf.placeholder(tf.float32, [None, 3])      # The real y that will be checked against the prediction
    cross_entropy = -tf.reduce_sum(y_*tf.log(y))        # The entropy error b/w the y and y_
    train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)    # The method to optimize error at each training step
    init = tf.initialize_all_variables()            # initializing all variables
    sess = tf.Session()
    sess.run(init)

    # Training for our model
    for i in range(1000):                   
      batch_xs, batch_ys = batch(2)
      print batch_ys
      sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

    correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))      # Checking for the prediction
    accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))     # Accuracy of the prediction
    accuracy = sess.run(accuracy, feed_dict={x: extractImages('./test/'), y_: extractLabels('test')})*100   # Normalizing it in terms of percentage
    print "The accuracy of the model was",accuracy," %"
    print type(mnist)

model()

1 个答案:

答案 0 :(得分:1)

batch_xs和batch_ys(以及对feed_dict的一般输入)应该是numpy数组而不是列表。