#coding=utf-8
#import tensorflow as tf
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import CNN_train_data as train_data
import cv2
import random
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import confusion_matrix, classification_report
train_epochs=3000
batch_size = 9
drop_prob = 0.4
learning_rate=0.00001
def weight_init(shape):
weight = tf.truncated_normal(shape,stddev=0.1,dtype=tf.float32)
return tf.Variable(weight)
def bias_init(shape):
bias = tf.random_normal(shape,dtype=tf.float32)
return tf.Variable(bias)
images_input = tf.placeholder(tf.float32,[None,112*92*3],name='input_images')
labels_input = tf.placeholder(tf.float32,[None,2],name='input_labels')
def fch_init(layer1,layer2,const=1):
min = -const * (6.0 / (layer1 + layer2));
max = -min;
weight = tf.random_uniform([layer1, layer2], minval=min, maxval=max, dtype=tf.float32)
return tf.Variable(weight)
def conv2d(images,weight):
return tf.nn.conv2d(images,weight,strides=[1,1,1,1],padding='SAME')
def max_pool2x2(images,tname):
return tf.nn.max_pool(images,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME',name=tname)
x_input = tf.reshape(images_input,[-1,112,92,3])
# 卷积核3*3*3 16个 第一层卷积
w1 = weight_init([3,3,3,16])
b1 = bias_init([16])
# 结果 NHWC N H W C
conv_1 = conv2d(x_input,w1)+b1
relu_1 = tf.nn.relu(conv_1,name='relu_1')
max_pool_1 = max_pool2x2(relu_1,'max_pool_1')
# 卷积核3*3*16 32个 第二层卷积
w2 = weight_init([3,3,16,32])
b2 = bias_init([32])
conv_2 = conv2d(max_pool_1,w2) + b2
relu_2 = tf.nn.relu(conv_2,name='relu_2')
max_pool_2 = max_pool2x2(relu_2,'max_pool_2')
# 卷积核3*3*32 64个 第三层卷积
w3 = weight_init([3,3,32,64])
b3 = bias_init([64])
conv_3 = conv2d(max_pool_2,w3)+b3
relu_3 = tf.nn.relu(conv_3,name='relu_3')
max_pool_3 = max_pool2x2(relu_3,'max_pool_3')
f_input = tf.reshape(max_pool_3,[-1,14*12*64])
#全连接第一层 31*31*32,512
f_w1= fch_init(14*12*64,512)
f_b1 = bias_init([512])
f_r1 = tf.matmul(f_input,f_w1) + f_b1
f_relu_r1 = tf.nn.relu(f_r1)
f_dropout_r1 = tf.nn.dropout(f_relu_r1,drop_prob)
f_w2 = fch_init(512,128)
f_b2 = bias_init([128])
f_r2 = tf.matmul(f_dropout_r1,f_w2) + f_b2
f_relu_r2 = tf.nn.relu(f_r2)
f_dropout_r2 = tf.nn.dropout(f_relu_r2,drop_prob)
#全连接第二层 512,2
f_w3 = fch_init(128,2)
f_b3 = bias_init([2])
f_r3 = tf.matmul(f_dropout_r2,f_w3) + f_b3
f_softmax = tf.nn.softmax(f_r3,name='f_softmax')
#定义交叉熵
cross_entry = tf.reduce_mean(tf.reduce_sum(-labels_input*tf.log(f_softmax)))
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cross_entry)
#计算准确率
arg1 = tf.argmax(labels_input,1)
arg2 = tf.argmax(f_softmax,1)
cos = tf.equal(arg1,arg2)
acc = tf.reduce_mean(tf.cast(cos,dtype=tf.float32))
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
Cost = []
Accuracy=[]
for i in range(train_epochs):
idx=random.randint(0,len(train_data.images)-20)
batch= random.randint(6,18)
train_input = train_data.images[idx:(idx+batch)]
train_labels = train_data.labels[idx:(idx+batch)]
result,acc1,cross_entry_r,cos1,f_softmax1,relu_1_r= sess.run([optimizer,acc,cross_entry,cos,f_softmax,relu_1],feed_dict={images_input:train_input,labels_input:train_labels})
print (acc1)
Cost.append(cross_entry_r)
Accuracy.append(acc1)
# 代价函数曲线
fig1,ax1 = plt.subplots(figsize=(10,7))
plt.plot(Cost)
ax1.set_xlabel('Epochs')
ax1.set_ylabel('Cost')
plt.title('Cross Loss')
plt.grid()
plt.show()
# 准确率曲线
fig7,ax7 = plt.subplots(figsize=(10,7))
plt.plot(Accuracy)
ax7.set_xlabel('Epochs')
ax7.set_ylabel('Accuracy Rate')
plt.title('Train Accuracy Rate')
plt.grid()
plt.show()
#测试
arg2_r = sess.run(arg2,feed_dict={images_input:train_data.test_images,labels_input:train_data.test_labels})
arg1_r = sess.run(arg1,feed_dict={images_input:train_data.test_images,labels_input:train_data.test_labels})
print (classification_report(arg1_r, arg2_r))
#保存模型
saver = tf.train.Saver()
saver.save(sess, './model/my-gender-v1.0')
没有合适的资源?快使用搜索试试~ 我知道了~
基于卷积神经网络算法的人脸识别项目源码+项目说明.zip
共402个文件
bmp:399个
py:3个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 105 浏览量
2024-02-19
10:52:57
上传
评论
收藏 4.24MB ZIP 举报
温馨提示
【资源说明】 1、该资源包括项目的全部源码,下载可以直接使用! 2、本项目适合作为计算机、数学、电子信息等专业的课程设计、期末大作业和毕设项目,作为参考资料学习借鉴。 3、本资源作为“参考资料”如果需要实现其他功能,需要能看懂代码,并且热爱钻研,自行调试。 基于卷积神经网络算法的人脸识别项目源码+项目说明.zip 基于卷积神经网络算法的人脸识别项目源码+项目说明.zip 基于卷积神经网络算法的人脸识别项目源码+项目说明.zip 基于卷积神经网络算法的人脸识别项目源码+项目说明.zip 基于卷积神经网络算法的人脸识别项目源码+项目说明.zip 基于卷积神经网络算法的人脸识别项目源码+项目说明.zip 基于卷积神经网络算法的人脸识别项目源码+项目说明.zip 基于卷积神经网络算法的人脸识别项目源码+项目说明.zip 基于卷积神经网络算法的人脸识别项目源码+项目说明.zip
资源推荐
资源详情
资源评论
收起资源包目录
基于卷积神经网络算法的人脸识别项目源码+项目说明.zip (402个子文件)
face287.bmp 30KB
face220.bmp 30KB
face237.bmp 30KB
face323.bmp 30KB
face244.bmp 30KB
face299.bmp 30KB
face382.bmp 30KB
face347.bmp 30KB
face379.bmp 30KB
face286.bmp 30KB
face295.bmp 30KB
face325.bmp 30KB
face235.bmp 30KB
face297.bmp 30KB
face284.bmp 30KB
face305.bmp 30KB
face203.bmp 30KB
face283.bmp 30KB
face332.bmp 30KB
face257.bmp 30KB
face252.bmp 30KB
face387.bmp 30KB
face292.bmp 30KB
face205.bmp 30KB
face230.bmp 30KB
face276.bmp 30KB
face341.bmp 30KB
face344.bmp 30KB
face338.bmp 30KB
face389.bmp 30KB
face301.bmp 30KB
face238.bmp 30KB
face246.bmp 30KB
face275.bmp 30KB
face256.bmp 30KB
face380.bmp 30KB
face229.bmp 30KB
face348.bmp 30KB
face239.bmp 30KB
face356.bmp 30KB
face342.bmp 30KB
face400.bmp 30KB
face273.bmp 30KB
face248.bmp 30KB
face324.bmp 30KB
face280.bmp 30KB
face326.bmp 30KB
face242.bmp 30KB
face264.bmp 30KB
face370.bmp 30KB
face250.bmp 30KB
face219.bmp 30KB
face251.bmp 30KB
face386.bmp 30KB
face339.bmp 30KB
face353.bmp 30KB
face310.bmp 30KB
face274.bmp 30KB
face214.bmp 30KB
face215.bmp 30KB
face202.bmp 30KB
face221.bmp 30KB
face354.bmp 30KB
face224.bmp 30KB
face364.bmp 30KB
face263.bmp 30KB
face345.bmp 30KB
face243.bmp 30KB
face372.bmp 30KB
face371.bmp 30KB
face352.bmp 30KB
face304.bmp 30KB
face316.bmp 30KB
face359.bmp 30KB
face270.bmp 30KB
face262.bmp 30KB
face358.bmp 30KB
face320.bmp 30KB
face308.bmp 30KB
face328.bmp 30KB
face266.bmp 30KB
face329.bmp 30KB
face291.bmp 30KB
face261.bmp 30KB
face383.bmp 30KB
face349.bmp 30KB
face385.bmp 30KB
face361.bmp 30KB
face298.bmp 30KB
face296.bmp 30KB
face234.bmp 30KB
face346.bmp 30KB
face381.bmp 30KB
face333.bmp 30KB
face312.bmp 30KB
face222.bmp 30KB
face289.bmp 30KB
face391.bmp 30KB
face355.bmp 30KB
face206.bmp 30KB
共 402 条
- 1
- 2
- 3
- 4
- 5
资源评论
土豆片片
- 粉丝: 1840
- 资源: 5690
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功