热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Tensorflow训练自己的cnn模型行人识别

代码如下:#coding:utf-8importstring,os,sysimportnumpyasnpimportmatplotlib.py
代码如下:

#coding:utf-8
import string,os,sys
import numpy as np
import matplotlib.pyplot as plt
import scipy.io
import random
import csv
import tensorflow as tf
import pandas as pd


filename = "/home/yc/prid_2011_test/ptest.csv"

data = pd.read_csv(filename,dtype='a')
label = np.array(data['pid'])
img_data = np.array(data['pixels'])
N_sample = label.size
Face_data = np.zeros((N_sample, 128*64))
Face_label=np.zeros((N_sample,40),dtype=int)
for i in range(N_sample):
x = img_data[i]

x = np.fromstring(x, dtype=float, sep=' ')

x_max = x.max()
x = x/(x_max+0.0001)
Face_data[i] = x
Face_label[i, label[i]] = 1
#img_x = np.reshape(x, (128, 64))
#plt.subplot(10,10,i+1)
#plt.axis('off')
#plt.imshow(img_x, plt.cm.gray)
#plt.show()
train_num = 4000
test_num = 2000

train_x = Face_data [0:train_num, :]
train_y = Face_label [0:train_num, :]

test_x = Face_data [train_num : train_num+test_num, :]
test_y = Face_label [train_num : train_num+test_num, :]

print ("All is well")

batch_size = 100
train_batch_num = train_num/batch_size
test_batch_num = test_num/batch_size
train_epoch = 30

learning_rate = 0.001
# Network Parameters
n_input = 8192 # data input (img shape: 48*48)
n_classes = 40 # total classes
dropout = 0.5 # Dropout, probability to keep units
7
# tf Graph input

x = tf.placeholder(tf.float32, [None, n_input])
y = tf.placeholder(tf.float32, [None, n_classes])
keep_prob = tf.placeholder(tf.float32) #dropout (keep probability)
x1=[]
x2=[]
# Create some wrappers for simplicity

def conv2d(x, W, b, strides=1):
# Conv2D wrapper, with bias and relu activation
x = tf.nn.conv2d(x, W, strides=[1, strides, strides, 1], padding='SAME')
x = tf.nn.bias_add(x, b)
return tf.nn.relu(x)

def maxpool2d(x, k=2):
# MaxPool2D wrapper
return tf.nn.max_pool(x, ksize=[1, k, k, 1], strides=[1, k, k, 1],
padding='SAME')

# Create model
def conv_net(x, weights, biases, dropout):
# Reshape input picture
print 'begin net'
print 'x1',x
x = tf.reshape(x, shape=[-1, 64, 128, 1])
print x
# Convolution Layer
conv1 = conv2d(x, weights['wc1'], biases['bc1'])
# Max Pooling (down-sampling)
conv1 = maxpool2d(conv1, k=2)

# Convolution Layer
conv2 = conv2d(conv1, weights['wc2'], biases['bc2'])
# Max Pooling (down-sampling)
conv2 = maxpool2d(conv2, k=2)

# Convolution Layer
conv3 = conv2d(conv2, weights['wc3'], biases['bc3'])
# Max Pooling (down-sampling)
conv3 = maxpool2d(conv3, k=2)

# Fully connected layer
# Reshape conv2 output to fit fully connected layer input
fc1 = tf.reshape(conv3, [-1, weights['wd1'].get_shape().as_list()[0]])
fc1 = tf.add(tf.matmul(fc1, weights['wd1']), biases['bd1'])
fc1 = tf.nn.relu(fc1)

# Apply Dropout
fc1 = tf.nn.dropout(fc1, dropout)

# Output, class prediction
out = tf.add(tf.matmul(fc1, weights['out']), biases['out'])

return out

# Store layers weight & bias
weights = {
# 3x3 conv, 1 input, 128 outputs
'wc1': tf.Variable(tf.random_normal([3, 3, 1, 256])),
# 3x3 conv, 128 inputs, 64 outputs
'wc2': tf.Variable(tf.random_normal([3, 3, 256, 128])),
# 3x3 conv, 64 inputs, 32 outputs
'wc3': tf.Variable(tf.random_normal([3, 3, 128, 64])),
# fully connected,
'wd1': tf.Variable(tf.random_normal([8*16*64, 500])),
# 1024 inputs, 10 outputs (class prediction)
'out': tf.Variable(tf.random_normal([500, n_classes]))
}


biases = {
'bc1': tf.Variable(tf.random_normal([256])),

'bc2': tf.Variable(tf.random_normal([128])),

'bc3': tf.Variable(tf.random_normal([64])),

'bd1': tf.Variable(tf.random_normal([500])),

'out': tf.Variable(tf.random_normal([n_classes]))
}

# Construct model
pred = conv_net(x, weights, biases, keep_prob)

# Define loss and optimizer
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(pred, y))
optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(cost)
# Evaluate model
correct_pred = tf.equal(tf.argmax(pred, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

# Initializing the variables
init = tf.initialize_all_variables()

Train_ind = np.arange(train_num)
Test_ind = np.arange(test_num)

with tf.Session() as sess:
sess.run(init)
for epoch in range(0, train_epoch):

Total_test_loss = 0
Total_test_acc = 0

for train_batch in range (0, train_batch_num):
sample_ind = Train_ind[train_batch * batch_size:(train_batch + 1) * batch_size]

batch_x = train_x[sample_ind, :]

batch_y = train_y[sample_ind, :]
# Run optimization op (backprop)
sess.run(optimizer, feed_dict={x: batch_x, y: batch_y,
keep_prob: dropout})
if train_batch % batch_size == 0:
# Calculate loss and accuracy

loss, acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
y: batch_y,
keep_prob: 1.})

print("Epoch: " + str(epoch+1) + ", Batch: "+ str(train_batch) + ", Loss= " + \
"{:.3f}".format(loss) + ", Training Accuracy= " + \
"{:.3f}".format(acc))

# Calculate test loss and test accuracy
for test_batch in range (0, test_batch_num):
sample_ind = Test_ind[test_batch * batch_size:(test_batch + 1) * batch_size]
batch_x = test_x[sample_ind, :]
batch_y = test_y[sample_ind, :]
test_loss, test_acc = sess.run([cost, accuracy], feed_dict={x: batch_x,
y: batch_y,
keep_prob: 1.})
Total_test_lost = Total_test_loss + test_loss
Total_test_acc =Total_test_acc + test_acc



Total_test_acc = Total_test_acc/test_batch_num
Total_test_loss =Total_test_lost/test_batch_num

print("Epoch: " + str(epoch + 1) + ", Test Loss= " + \
"{:.3f}".format(Total_test_loss) + ", Test Accuracy= " + \
"{:.3f}".format(Total_test_acc))

x1.append(Total_test_loss)
x2.append(Total_test_acc)
plt.subplot(2,1,1)
plt.ylabel('Test loss')
plt.plot(x1)
plt.subplot(2,1,2)
plt.ylabel('Test Accuracy')
plt.plot(x2)


plt.show()
print "All is well"

挑选了40个人,cam_a中100张训练,cam_b中50张测试,


推荐阅读
  • 利用决策树预测NBA比赛胜负的Python数据挖掘实践
    本文通过使用2013-14赛季NBA赛程与结果数据集以及2013年NBA排名数据,结合《Python数据挖掘入门与实践》一书中的方法,展示如何应用决策树算法进行比赛胜负预测。我们将详细讲解数据预处理、特征工程及模型评估等关键步骤。 ... [详细]
  • 反向投影技术主要用于在大型输入图像中定位特定的小型模板图像。通过直方图对比,它能够识别出最匹配的区域或点,从而确定模板图像在输入图像中的位置。 ... [详细]
  • 对象自省自省在计算机编程领域里,是指在运行时判断一个对象的类型和能力。dir能够返回一个列表,列举了一个对象所拥有的属性和方法。my_list[ ... [详细]
  • 本文介绍如何使用 Python 的 Pandas 库中 Series 对象的 round() 方法,对数值进行四舍五入处理。该方法在数据预处理和分析中非常有用。 ... [详细]
  • Python处理Word文档的高效技巧
    本文详细介绍了如何使用Python处理Word文档,涵盖从基础操作到高级功能的各种技巧。我们将探讨如何生成文档、定义样式、提取表格数据以及处理超链接和图片等内容。 ... [详细]
  • Redux入门指南
    本文介绍Redux的基本概念和工作原理,帮助初学者理解如何使用Redux管理应用程序的状态。Redux是一个用于JavaScript应用的状态管理库,特别适用于React项目。 ... [详细]
  • 本文介绍如何从字符串中移除大写、小写、特殊、数字和非数字字符,并提供了多种编程语言的实现示例。 ... [详细]
  • 本文详细介绍了使用NumPy和TensorFlow实现的逻辑回归算法。通过具体代码示例,解释了数据加载、模型训练及分类预测的过程。 ... [详细]
  • 开发笔记:9.八大排序
    开发笔记:9.八大排序 ... [详细]
  • #点球小游戏fromrandomimportchoiceimporttimescore[0,0]direction[left,center,right]defkick() ... [详细]
  • 在现代Web应用中,当用户滚动到页面底部时,自动加载更多内容的功能变得越来越普遍。这种无刷新加载技术不仅提升了用户体验,还优化了页面性能。本文将探讨如何实现这一功能,并介绍一些实际应用案例。 ... [详细]
  • 本文详细介绍如何在Linux系统中配置SSH密钥对,以实现从一台主机到另一台主机的无密码登录。内容涵盖密钥对生成、公钥分发及权限设置等关键步骤。 ... [详细]
  • 本文详细介绍了 org.apache.commons.io.IOCase 类中的 checkCompareTo() 方法,通过多个代码示例展示其在不同场景下的使用方法。 ... [详细]
  • 本文介绍如何使用 Android 的 Canvas 和 View 组件创建一个简单的绘图板应用程序,支持触摸绘画和保存图片功能。 ... [详细]
  • 本教程详细介绍了如何使用 TensorFlow 2.0 构建和训练多层感知机(MLP)网络,涵盖回归和分类任务。通过具体示例和代码实现,帮助初学者快速掌握 TensorFlow 的核心概念和操作。 ... [详细]
author-avatar
广东庚舞飞扬
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有