04

1、数据来源于yale大学
2、准备训练标签,训练数据
3、搭载神经网络
4、开始检测

一、图片读取

# 1 数据yale 2 准备train label-》train 
# 3 cnn 4 检测
import tensorflow as tf
import numpy as np
import scipy.io as sio

f = open('Yale_64x64.mat','rb')
mdict = sio.loadmat(f)    #加载数据,读取是一个字典
# fea gnd
train_data = mdict['fea']    #读取数据的键
train_label = mdict['gnd']  #读取数据的值

train_data = np.random.permutation(train_data)  #对原有数据进行无序排列
train_label = np.random.permutation(train_label)
test_data = train_data[0:64]   #随机抽取一部分数据作为训练数据
test_label = train_label[0:64]
np.random.seed(100)    #重新生产随机数种子
test_data = np.random.permutation(test_data)  #进行无序排列
np.random.seed(100)
test_label = np.random.permutation(test_label)

二、图片处理


#读取的数据是64*64,rgb:0-255  需要对其归一化处理,变成灰度图,1表示灰色通道
# train [0-9] [10*N] [15*N]  [0 0 1 0 0 0 0 0 0 0] -> 2
train_data = train_data.reshape(train_data.shape[0],64,64,1).astype(np.float32)/255
train_labels_new = np.zeros((165,15))# 165 image 15  (yale数据库共165张图,分别描述15人)
for i in range(0,165):  #与用[0 0 1 0 0 0 0 0 0 0] 来代表数字2类似
    j = int(train_label[i,0])-1 # 之前是1-15进行标志,现转为 0-14,所以-1 
    train_labels_new[i,j] = 1   #识别到谁,就将谁设置谁的下标为1

#下面完成测试数据的处理
test_data_input = test_data.reshape(test_data.shape[0],64,64,1).astype(np.float32)/255
test_labels_input = np.zeros((64,15))# 64 image 15  前面抽取了64张图片作为测试图片,所以写64
for i in range(0,64):  #遍历图片,识别后把相应的下标置1
    j = int(test_label[i,0])-1 # 1-15 0-14 
    test_labels_input[i,j] = 1

三、构建神经网络

# cnn acc  tf.nn tf.layer
data_input = tf.placeholder(tf.float32,[None,64,64,1])
label_input = tf.placeholder(tf.float32,[None,15])
#卷积(输入数据,滤波核设置为32,卷积核大小,滑动步长1,'SAME'边缘停留,激活函数relu)
layer1 = tf.layers.conv2d(inputs=data_input,filters=32,kernel_size=2,strides=1,padding='SAME',activation=tf.nn.relu)
#池化,数据减维,留下最大值; 最大值池化(输入数据,数据行列均减少1半,滑动步长)
layer1_pool = tf.layers.max_pooling2d(layer1,pool_size=2,strides=2)

#输出层,激励层(激励函数relu)
layer2 = tf.reshape(layer1_pool,[-1,32*32*32])  
layer2_relu = tf.layers.dense(layer2,1024,tf.nn.relu)
output = tf.layers.dense(layer2_relu,15)  #输出,15维,因为一共15人

#定义损失函数,采用交叉熵和梯度下降法(下降步长0.01)并最小化损失函数
loss = tf.losses.softmax_cross_entropy(onehot_labels=label_input,logits=output)
train = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
#检测概率 axis=1:表示维度为1
accuracy = tf.metrics.accuracy(labels=tf.argmax(label_input,axis=1),predictions=tf.argmax(output,axis=1))[1]

四、完整代码

# 1 数据yale 2 准备train label-》train 
# 3 cnn 4 检测
import tensorflow as tf
import numpy as np
import scipy.io as sio

f = open('Yale_64x64.mat','rb')
mdict = sio.loadmat(f)    #加载数据,读取是一个字典
# fea gnd
train_data = mdict['fea']    #读取数据的键
train_label = mdict['gnd']  #读取数据的值

train_data = np.random.permutation(train_data)  #对原有数据进行无序排列
train_label = np.random.permutation(train_label)
test_data = train_data[0:64]   #随机抽取一部分数据作为训练数据
test_label = train_label[0:64]
np.random.seed(100)    #重新生产随机数种子
test_data = np.random.permutation(test_data)  #进行无序排列
np.random.seed(100)  
test_label = np.random.permutation(test_label)

#读取的数据是64*64,rgb:0-255  需要对其归一化处理,变成灰度图,1表示灰色通道
# train [0-9] [10*N] [15*N]  [0 0 1 0 0 0 0 0 0 0] -> 2
train_data = train_data.reshape(train_data.shape[0],64,64,1).astype(np.float32)/255
train_labels_new = np.zeros((165,15))# 165 image 15  (yale数据库共165张图,分别描述15人)
for i in range(0,165):  #与用[0 0 1 0 0 0 0 0 0 0] 来代表数字2类似
    j = int(train_label[i,0])-1 # 之前是1-15进行标志,现转为 0-14,所以-1 
    train_labels_new[i,j] = 1   #识别到谁,就将谁设置谁的下标为1

#下面完成测试数据的处理
test_data_input = test_data.reshape(test_data.shape[0],64,64,1).astype(np.float32)/255
test_labels_input = np.zeros((64,15))# 64 image 15  前面抽取了64张图片作为测试图片,所以写64
for i in range(0,64):  #遍历图片,识别后把相应的下标置1
    j = int(test_label[i,0])-1 # 1-15 0-14 
    test_labels_input[i,j] = 1
    

# cnn acc  tf.nn tf.layer
data_input = tf.placeholder(tf.float32,[None,64,64,1])
label_input = tf.placeholder(tf.float32,[None,15])
#卷积(输入数据,滤波核设置为32,卷积核大小,滑动步长1,'SAME'边缘停留,激活函数relu)
layer1 = tf.layers.conv2d(inputs=data_input,filters=32,kernel_size=2,strides=1,padding='SAME',activation=tf.nn.relu)
#池化,数据减维,留下最大值; 最大值池化(输入数据,数据行列均减少1半,滑动步长)
layer1_pool = tf.layers.max_pooling2d(layer1,pool_size=2,strides=2)

#输出层,激励层(激励函数relu)
layer2 = tf.reshape(layer1_pool,[-1,32*32*32])  
layer2_relu = tf.layers.dense(layer2,1024,tf.nn.relu)
output = tf.layers.dense(layer2_relu,15)  #输出,15维,因为一共15人

#定义损失函数,采用交叉熵和梯度下降法(下降步长0.01)并最小化损失函数
loss = tf.losses.softmax_cross_entropy(onehot_labels=label_input,logits=output)
train = tf.train.GradientDescentOptimizer(0.01).minimize(loss)
#检测概率 axis=1:表示维度为1
accuracy = tf.metrics.accuracy(labels=tf.argmax(label_input,axis=1),predictions=tf.argmax(output,axis=1))[1]

# run acc 初始化所有变量
init = tf.group(tf.global_variables_initializer(),tf.local_variables_initializer())
with tf.Session() as sess:
    sess.run(init)
    for i in range(0,200):
        train_data_input = np.array(train_data)
        train_label_input = np.array(train_labels_new)
        sess.run([train,loss],feed_dict={data_input:train_data_input,label_input:train_label_input})
        acc = sess.run(accuracy,feed_dict={data_input:test_data_input,label_input:test_labels_input})
        print('acc:%.2f',acc)

你可能感兴趣的:(04)