1.训练数据:

import tensorflow as tf
import cv2
import os
import numpy as np
import time
import matplotlib.pyplot as plt
from sklearn.metrics import confusion_matrix, classification_report drop_prob = 0.4
input_imgs = tf.placeholder(dtype=tf.float32,shape=[None,128,64,3],name='input_imgs')
input_label = tf.placeholder(dtype=tf.float32,shape=[None,2],name='input_label') # 初始化权重(卷积核)
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) # 全连接层
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) # 卷积核3*3*3 16个 第一层卷积
w1 = weight_init([3, 3, 3, 16])
b1 = bias_init([16])
# 结果 NHWC N H W C
conv_1 = conv2d(input_imgs, 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') 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') print(max_pool_3.shape, '-------扁平-------',max_pool_3.shape[1]*max_pool_3.shape[2]*max_pool_3.shape[3])
f_input = tf.reshape(max_pool_3, [-1, max_pool_3.shape[1]*max_pool_3.shape[2]*max_pool_3.shape[3]])
print('-=--=', f_input.shape) # 全连接第一层 31*31*32,512
f_w1 = fch_init(8192, 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)
print('f_dropout_r1.shape-----------', f_dropout_r1.shape) #第二层
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
# print(f_r3.shape, '-=-===============')
f_softmax = tf.nn.softmax(f_r3, name='f_softmax') # 定义交叉熵
cross_entry = tf.reduce_mean(tf.reduce_sum(-input_label * tf.log(f_softmax)))
optimizer = tf.train.AdamOptimizer(0.0001).minimize(cross_entry) # 计算准确率
arg1 = tf.argmax(input_label, 1)
arg2 = tf.argmax(f_softmax, 1)
cos = tf.equal(arg1, arg2)
acc = tf.reduce_mean(tf.cast(cos, dtype=tf.float32)) sess = tf.Session()
sess.run(tf.global_variables_initializer()) train_img = []
train_labels = []
test_img = []
test_labels = [] images = []
labels = []
# for root, dirs, files in os.walk('../img/pos/'):
# images.append(os.path.join('../img.pos',))
for fileName in os.listdir('../img/pos'):
images.append([os.path.join('../img/pos', fileName)])
labels.append([1,0]) for fileName in os.listdir('../img/neg'):
images.append([os.path.join('../img/neg', fileName)])
labels.append([0,1]) images = np.array(images)
labels = np.array(labels)
permutation = np.random.permutation(labels.shape[0])
images = images[permutation,:]
labels = labels[permutation,:]
#获取训练数据或者测试数据
def get_train_data(batch,isTrain=True):
global test_labels,test_img
if isTrain:
train_num = int(labels.shape[0]*0.8)
train_img = images[:train_num,:]
train_labels = labels[:train_num,:]
test_img = images[train_num:,:]
test_labels = labels[train_num:,:]
# print(train_img[batch:batch+20], train_labels[batch:batch+20])
return train_img[batch*20:(batch+1)*20], train_labels[batch*20:(batch+1)*20]
else:
return test_img[batch*20:(batch+1)*20], test_labels[batch*20:(batch+1)*20] # get_train_data(20) def read_img(train_img):
# print(train_img)
imgs = []
for i in range(20):
img = cv2.imread(train_img[i][0])
imgs.append(img)
# cv2.imshow('12',img)
# cv2.waitKey(0)
imgs = np.array(imgs)
return imgs
# print(imgs) Cost = []
Accuracy=[] start_time = time.time()
for i in range(100):
train_img, train_labels = get_train_data(i)
imgs = read_img(train_img) result,acc1,cross_entry_r,cos1,f_softmax1,relu_1_r= sess.run([optimizer,acc,cross_entry,cos,f_softmax,relu_1],feed_dict={input_imgs:imgs,input_label:train_labels})
print("rpoch: {}, accurate: {} , cross_loss:{}".format(i,acc1,cross_entry_r))
Cost.append(cross_entry_r)
Accuracy.append(acc1) print('total time:%d'%(time.time()-start_time))
# 代价函数曲线
fig1,ax1 = plt.subplots(figsize=(10,7))
plt.plot(Cost)
print('---------cost-----------',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() #测试
test_img,test_labels = get_train_data(1,False)
test_img = read_img(test_img)
arg2_r = sess.run(arg2,feed_dict={input_imgs:test_img,input_label:test_labels})
arg1_r = sess.run(arg1,feed_dict={input_imgs:test_img,input_label:test_labels}) print (classification_report(arg1_r, arg2_r)) #保存模型 global_step:训练模型的命名
saver = tf.train.Saver()
saver.save(sess, './model/my-gender-v1.0',global_step=123)

2. 从保存的模型中读取数据

import tensorflow as tf
import numpy as np
import cv2
import matplotlib.pyplot as plt
import os #取一张图片 # img/pos/758.jpg
img = cv2.imread('../img/pos/760.jpg')
# labels = train_data.labels[0:1]
fig2,ax2 = plt.subplots(figsize=(2,2))
ax2.imshow(img)
plt.show()
img = np.reshape(img,[1,128,64,3]) sess = tf.Session()
graph_path=os.path.abspath('./model/my-gender-v1.0-123.meta')
model=os.path.abspath('./model/') server = tf.train.import_meta_graph(graph_path)
server.restore(sess,tf.train.latest_checkpoint(model)) graph = tf.get_default_graph() #填充feed_dict
x = graph.get_tensor_by_name('input_imgs:0')
y = graph.get_tensor_by_name('input_label:0')
feed_dict={x:img,y:[[1,0]]} #第一层卷积+池化
relu_1 = graph.get_tensor_by_name('relu_1:0')
max_pool_1 = graph.get_tensor_by_name('max_pool_1:0') #第二层卷积+池化
relu_2 = graph.get_tensor_by_name('relu_2:0')
max_pool_2 = graph.get_tensor_by_name('max_pool_2:0') #第三层卷积+池化
relu_3 = graph.get_tensor_by_name('relu_3:0')
max_pool_3 = graph.get_tensor_by_name('max_pool_3:0') #全连接最后一层输出
f_softmax = graph.get_tensor_by_name('f_softmax:0') #relu_1_r,max_pool_1_,relu_2,max_pool_2,relu_3,max_pool_3,f_softmax=sess.run([relu_1,max_pool_1,relu_2,max_pool_2,relu_3,max_pool_3,f_softmax],feed_dict) #----------------------------------各个层特征可视化------------------------------- #conv1 特征
r1_relu = sess.run(relu_1,feed_dict)
print('r1_relu',r1_relu.shape)
# 将矩阵转置
r1_tranpose = sess.run(tf.transpose(r1_relu,[3,0,1,2]))
print('r1_tranpose',r1_tranpose.shape)
fig,ax = plt.subplots(nrows=1,ncols=16,figsize=(16,1))
for i in range(16):
ax[i].imshow(r1_tranpose[i][0])
plt.title('Conv1 16*112*92')
plt.show() #pool1特征
max_pool_1 = sess.run(max_pool_1,feed_dict)
r1_tranpose = sess.run(tf.transpose(max_pool_1,[3,0,1,2]))
fig,ax = plt.subplots(nrows=1,ncols=16,figsize=(16,1))
for i in range(16):
ax[i].imshow(r1_tranpose[i][0])
plt.title('Pool1 16*56*46')
plt.show() #conv2 特征
r2_relu = sess.run(relu_2,feed_dict)
r2_tranpose = sess.run(tf.transpose(r2_relu,[3,0,1,2]))
fig,ax = plt.subplots(nrows=1,ncols=32,figsize=(32,1))
for i in range(32):
ax[i].imshow(r2_tranpose[i][0])
plt.title('Conv2 32*56*46')
plt.show() #pool2 特征
max_pool_2 = sess.run(max_pool_2,feed_dict)
tranpose = sess.run(tf.transpose(max_pool_2,[3,0,1,2]))
fig,ax = plt.subplots(nrows=1,ncols=32,figsize=(32,1))
for i in range(32):
ax[i].imshow(tranpose[i][0])
plt.title('Pool2 32*28*23')
plt.show() #conv3 特征
r3_relu = sess.run(relu_3,feed_dict)
tranpose = sess.run(tf.transpose(r3_relu,[3,0,1,2]))
fig,ax = plt.subplots(nrows=1,ncols=64,figsize=(32,1))
for i in range(64):
ax[i].imshow(tranpose[i][0])
plt.title('Conv3 64*28*23')
plt.show() #pool3 特征
max_pool_3 = sess.run(max_pool_3,feed_dict)
tranpose = sess.run(tf.transpose(max_pool_3,[3,0,1,2]))
fig,ax = plt.subplots(nrows=1,ncols=64,figsize=(32,1))
for i in range(64):
ax[i].imshow(tranpose[i][0])
plt.title('Pool3 64*14*12')
plt.show() print(sess.run(f_softmax,feed_dict))

  

注意:

  卷积神经网络:conv2d ->pool->relu(softmax二分类)  多层卷积神经网络的使用,注意使用卷积核的个数,步长及大小。

简单CNN 测试例的更多相关文章

  1. Tensorflow简单CNN实现

    觉得有用的话,欢迎一起讨论相互学习~Follow Me 少说废话多写代码~ """转换图像数据格式时需要将它们的颜色空间变为灰度空间,将图像尺寸修改为同一尺寸,并将标签依 ...

  2. IDDD 实现领域驱动设计-一个简单业务用例的回顾和理解

    上一篇:<IDDD 实现领域驱动设计-由贫血导致的失忆症> 这篇博文是对<实现领域驱动设计>第一章后半部分内容的理解. Domain Experts-领域专家 这节点内容是昨天 ...

  3. scrapy爬虫学习系列二:scrapy简单爬虫样例学习

    系列文章列表: scrapy爬虫学习系列一:scrapy爬虫环境的准备:      http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_python_00 ...

  4. TersorflowTutorial_MNIST数据集上简单CNN实现

    MNIST数据集上简单CNN实现 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 Tensorflow机器学习实战指南 源代码请点击下方链接欢迎加星 Tesorflow实现基于MNI ...

  5. [转]PROC简单使用用例--VC连接ORACLE

    [转]PROC简单使用用例--VC连接ORACLE 操作系统:windows 7 数据库版本:oracle 10g VS版本:VS2010 前言:连接ORACLE的方式有很多,此处仅以PROC为例,说 ...

  6. Jmeter 快速入门教程(二)--创建简单web测试

    [版权所有: whoistester.com & jmeter.cf] http://wenku.baidu.com/linkurl=9zc4VHe6vUUeMdDZPpNsRehkazZFw ...

  7. Introspector(内省)简单演示样例 与 简单应用

    简单演示样例: package com.asdfLeftHand.test; import java.beans.BeanDescriptor; import java.beans.BeanInfo; ...

  8. JBoss 系列九十六:JBoss MSC - 简介及一个简单演示样例

    什么是 JBoss MSC JBoss MSC 即 JBoss Modular Service Container,是第三代 JBoss 产品 JBoss 7和WildFfly的内核,JBoss MS ...

  9. Thrift的安装和简单演示样例

    本文仅仅是简单的解说Thrift开源框架的安装和简单使用演示样例.对于具体的解说,后面在进行阐述. Thrift简述                                           ...

随机推荐

  1. Java模式—适配器模式

    适配器模式(Adapter): 1.概念:将一个类中的接口转换为客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类可以一起工作. 2.模式中的角色 1 目标接口:客户所期待的接口.目 ...

  2. 站在巨人肩上的.NET Core 2.1

    .NET Core 1.0自发布两年以来,得到了开发者群体相当高地认可. 下图来自Stack overflow survey 2018的统计: .NET Core已经成为前五的主流框架工具,现今借鉴了 ...

  3. vue子组件传参给父组件

    关于父组件传参给子组件,可以看我另一篇文章 教程开始: 我们要实现的效果是:在子组件的Input框输入,父组件中实时更新显示.(也就是把子组件中的数据传给父组件) 一.子组件代码 template部分 ...

  4. mycat ER 分片表

    <table name="order" dataNode="dn$1-32" rule="mod-long"> <chil ...

  5. 解决docker镜像无法下载的问题

    从daocloud.io中找到了获取镜像的方式,在镜像仓库中可以找到镜像的地址,其他镜像地址可以以此类推: # docker pull daocloud.io/library/centos:lates ...

  6. 关于VS2017的安装和WDK的选择问题

    首先就是关于VS2017的安装,我们可以通过微软的官网去寻找我们想要安装的VS版本 我们可以随便选择一个我们需要的版本进行下载,任意版本的VS都可以. 然后下载安装的时候,要进行一下这个安装选项的选择 ...

  7. 20-hadoop-pagerank的计算

    转: http://www.cnblogs.com/rubinorth/p/5799848.html 参考尚学堂视频 1, 概念( 来自百度百科) PageRank是Google专有的算法,用于衡量特 ...

  8. little eye 精简版

    https://github.com/salomon1184/PerfMan/ 代码没整理,特乱 来源于一次聊天,一天半做完,考虑继续深做

  9. 线性回归浅谈(Linear Regression)

    在现实生活中普遍存在着变量之间的关系,有确定的和非确定的.确定关系指的是变量之间可以使用函数关系式表示,还有一种是属于非确定的(相关),比如人的身高和体重,一样的身高体重是不一样的.       线性 ...

  10. [日常] Go语言圣经--包和文件-导入包习题

    1.每个包都有一个全局唯一的导入路径 2.按照惯例,一个包的名字和包的导入路径的最后一个字段相同 练习 2.2: 写一个通用的单位转换程序,用类似cf程序的方式从命令行读取参数,如果缺省的话则是从标准 ...