gpt4 book ai didi

python - Python Tensorflow 中的人脸识别系统

转载 作者:行者123 更新时间:2023-11-30 09:55:04 36 4
gpt4 key购买 nike

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

目录:

amar -> 包含所有目标图像

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

train -> 包含所有训练图像

每个目录下有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()

最佳答案

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

关于python - Python Tensorflow 中的人脸识别系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34570614/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com