qa3000 2019-03-21 15:13 采纳率: 0%
浏览 344

tensorflow人脸数据库处理问题

数据集处理有问题。准确率一直是0.5%,loss在数值很大时就保持不变了

import numpy as np
import tensorflow as tf
import random, os, cv2, glob
import time

batch_size = 25
tf.set_random_seed(1)

def loadImageSet(folder=u'feret_face', sampleCount=6):
trainData = [];
testData = [];
yTrain2 = [];
yTest2 = []

for k in range(200):
yTrain1 = np.zeros(200)
yTest1 = np.zeros(200)
folder2 = os.path.join(folder, 'FERET-%d' % (k + 1))
data = []
for d in glob.glob(os.path.join(folder2, '*.tif')):
gray = cv2.imread(d, 0)
equal = cv2.equalizeHist(gray)
gaussian = cv2.GaussianBlur(equal,(1,1),0)
data.append(gaussian)
sample = [0,3,4,5]

trainData.extend([data[i].ravel() for i in range(7) if i in sample])
testData.extend([data[i].ravel() for i in range(7) if i not in sample])
yTrain1[k] = 1
yTest1[k] = 1

yTrain = np.matrix(yTrain1)
yTest = np.matrix(yTest1)
yTrain = np.tile(yTrain1, 4)
yTest = np.tile(yTest1, 3)

yTrain2.extend(yTrain)
yTest2.extend(yTest)
return np.array(trainData), np.array(yTrain2), np.array(testData), np.array(yTest2)

def weight_variable(shape):
inital = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(inital)

def bias_variable(shape):
inital = tf.constant(0.1, shape=shape)
return tf.Variable(inital)

def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding="SAME")

def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")

def main():

loadImageSet()

xTrain_, yTrain, xTest_, yTest = loadImageSet()
yTrain.shape = (800, 200)
yTest.shape = (600, 200)
def return_next_batch(batch_size, pos):
start = pos * batch_size
end = start + batch_size
return xTrain_[start:end], yTrain[start:end]

x = tf.placeholder(tf.float32, [None, 6400*1])
y_ = tf.placeholder(tf.float32, [None, 200])
keep_prob = tf.placeholder(tf.float32)
x_image = tf.reshape(x, [-1, 80, 80,1])

W_conv1 = weight_variable([3, 3,1, 6])
b_conv1 = bias_variable([6])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1) # 40*40*6

W_conv2 = weight_variable([3, 3, 6, 16])
b_conv2 = bias_variable([16])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2) # 20x20x16

W_conv3 = weight_variable(([3, 3, 16, 32]))
b_conv3 = bias_variable([32])
h_conv3 = tf.nn.relu(conv2d(h_pool2, W_conv3) + b_conv3)
h_pool3 = max_pool_2x2(h_conv3)

W_conv4 = weight_variable([3, 3, 32, 64])
b_conv4 = bias_variable([64])
h_conv4 = tf.nn.relu(conv2d(h_pool3, W_conv4) + b_conv4)
h_pool4 = max_pool_2x2(h_conv4) # 5*5*64

W_conv5 = weight_variable([3, 3, 64, 128])
b_conv5 = bias_variable([128])
h_conv5 = tf.nn.relu(conv2d(h_pool4, W_conv5) + b_conv5)
h_pool5 = max_pool_2x2(h_conv5)

h_pool5_flat = tf.reshape(h_pool5, [-1, 3 * 3 * 128])

W_fc1 = weight_variable([3 * 3 * 128,256])
b_fc1 = bias_variable([256])
h_fc1 = tf.nn.relu(tf.matmul(h_pool5_flat, W_fc1) + b_fc1)
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)

W_fc2 = weight_variable([256,200])
b_fc2 = bias_variable([200])
h_fc2 = tf.nn.relu(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

#W_fc3 = weight_variable([84, 40])
#b_fc3 = bias_variable([40])
#h_fc3 = tf.nn.relu(tf.matmul(h_fc2, W_fc3) + b_fc3)

W_fc4 = weight_variable([84, 40])

b_fc4 = bias_variable([40])

prediction = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

cross_entropy = -tf.reduce_sum(y_ * tf.log(prediction+(1e-10)))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

def compute_accuracy(v_xs, v_ys):
y_pre = sess.run(prediction, feed_dict={x: v_xs, keep_prob: 1})
correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
result = sess.run(accuracy, feed_dict={x: v_xs, y_: v_ys, keep_prob: 1})
return result

gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.8)
sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options))
sess.run(tf.global_variables_initializer())
for i in range(5000):
for j in range(32):
batch_x, batch_y = return_next_batch(batch_size, j)
batch_x = batch_x / 255

sess.run(train_step, feed_dict={x: np.matrix(batch_x), y_: np.matrix(batch_y), keep_prob: 0.5})
if i % 50 == 0:
correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(y_, 1))
cross_entropy = -tf.reduce_sum(y_ * tf.log(prediction))

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

print("acc:%.3f%%" % ((sess.run(accuracy, feed_dict={x: np.matrix(xTest_ / 255), y_: np.matrix(yTest),
keep_prob: 1})) * 100))
print('train entropy:%.3f'%((sess.run(cross_entropy,feed_dict={x: np.matrix(batch_x), y_: np.matrix(batch_y), keep_prob: 0.5}))))

print('test entropy:%.3f'%((sess.run(cross_entropy,feed_dict={x:np.matrix(xTest_/255),y_:np.matrix(yTest),keep_prob:1}))))

if name == '__main__':
main()

  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-10-25 19:26
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?