import os
import keras
import time
import numpy as np
import tensorflow as tf
from random import shuffle
from keras.utils import np_utils
from skimage import color, data, transform, io

trainDataDirList = os.listdir("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\trainGrayImage")
trainDataList = []
for i in range(len(trainDataDirList)):
image = io.imread("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\trainGrayImage\\"+trainDataDirList[i])
trainDataList.append(image)
trainLabelNum = np.load("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\trainLabelNum.npy")

testDataDirList = os.listdir("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\testGrayImage")
testDataList = []
for i in range(len(testDataDirList)):
image = io.imread("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\testGrayImage\\"+testDataDirList[i])
testDataList.append(image)
testLabelNum = np.load("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\testLabelNum.npy")

#乱序
train_images = []
train_labels = []
index = [i for i in range(len(trainDataList))]
shuffle(index)
for i in range(len(index)):
train_images.append(trainDataList[index[i]])
train_labels.append(trainLabelNum[index[i]])
#将标签转码
train_labels=keras.utils.to_categorical(train_labels,77)
#保存处理后的数据
np.save("E:\\tmp\\train_images",train_images)
np.save("E:\\tmp\\train_labels",train_labels)

#加载上面保存的数据
train77_images = np.load("E:\\train_images.npy")
train77_labeles = np.load("E:\\train_labels.npy")

#变成四维训练数据,两维标签
dataset = train77_images.reshape((-1, 64, 64, 1)).astype(np.float32)
labels = train77_labeles

## 配置神经网络的参数
n_classes = 77
batch_size = 64
kernel_h = kernel_w = 5
#dropout = 0.8
depth_in = 1
depth_out1 = 64
depth_out2 = 128
image_size = 64 ##图片尺寸
n_sample = len(dataset) ##样本个数

#每张图片的像素大小为64*64,训练样本
x = tf.placeholder(tf.float32, [None, 64, 64, 1])
#训练样本对应的真实label
y=tf.placeholder(tf.float32,[None,n_classes])

# y_ = tf.placeholder(tf.float32, [None, n_classes])

#设置dropout的placeholder
dropout = tf.placeholder(tf.float32)

# 扁平化
fla = int((image_size * image_size / 16) * depth_out2)

#卷积函数
def inference(x, dropout):
#第一层卷积
with tf.name_scope('convLayer1'):
Weights = tf.Variable(tf.random_normal([kernel_h, kernel_w, depth_in, depth_out1]))
bias = tf.Variable(tf.random_normal([depth_out1]))
x = tf.nn.conv2d(x, Weights, strides=[1, 1, 1, 1], padding="SAME")
x = tf.nn.bias_add(x, bias)
conv1 = tf.nn.relu(x)
#可视化权值
tf.summary.histogram('convLayer1/weights1', Weights)
#可视化偏置
tf.summary.histogram('convLayer1/bias1', bias)
#可视化卷积结果
tf.summary.histogram('convLayer1/conv1', conv1)
#对卷积的结果进行池化
pool1 = tf.nn.max_pool(conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
#可视化池化结果
tf.summary.histogram('ConvLayer1/pool1', pool1)

#第二层卷积
with tf.name_scope('convLayer2'):
Weights = tf.Variable(tf.random_normal([kernel_h, kernel_w, depth_out1, depth_out2]))
bias = tf.Variable(tf.random_normal([depth_out2]))
x = tf.nn.conv2d(pool1, Weights, strides=[1, 1, 1, 1], padding="SAME")
x = tf.nn.bias_add(x, bias)
conv2 = tf.nn.relu(x)
#可视化权值
tf.summary.histogram('convLayer2/weights2', Weights)
#可视化偏置
tf.summary.histogram('convLayer2/bias2', bias)
#可视化卷积结果
tf.summary.histogram('convLayer2/conv2', conv2)
#对卷积的结果进行池化
pool2 = tf.nn.max_pool(conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="SAME")
#可视化池化结果
tf.summary.histogram('ConvLayer2/pool2', pool2)

#扁平化处理
flatten = tf.reshape(pool2, [-1, fla])

#第一层全连接
Weights = tf.Variable(tf.random_normal([int((image_size * image_size / 16) * depth_out2), 512]))
bias = tf.Variable(tf.random_normal([512]))
fc1 = tf.add(tf.matmul(flatten, Weights), bias)
#使用relu激活函数处理全连接层结果
fc1r = tf.nn.relu(fc1)

#第二层全连接
Weights = tf.Variable(tf.random_normal([512, 128]))
bias = tf.Variable(tf.random_normal([128]))
fc2 = tf.add(tf.matmul(fc1r, Weights), bias)
#使用relu激活函数处理全连接层结果
fc2 = tf.nn.relu(fc2)
#使用Dropout(Dropout层防止预测数据过拟合)
fc2 = tf.nn.dropout(fc2, dropout)

#输出预测的结果
Weights = tf.Variable(tf.random_normal([128, n_classes]))
bias = tf.Variable(tf.random_normal([n_classes]))
prediction = tf.add(tf.matmul(fc2, Weights), bias)
return prediction

#使用上面定义好的神经网络进行训练,得到预测的label
prediction = inference(x, dropout)
#定义损失函数,使用上面的预测label与真实的label作运算
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction, labels=y))
#选定一个优化器和学习率(步长)
optimizer = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
merged = tf.summary.merge_all()

#评估模型(准确率)
correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, tf.float32))

#初始会话并开始训练过程
with tf.Session() as sess:
tf.global_variables_initializer().run()
for i in range(20):
for j in range(int(n_sample / batch_size) + 1):
start = (j * batch_size)
end = start + batch_size
x_ = dataset[start:end]
y_ = labels[start:end]
#准备验证数据
sess.run(optimizer, feed_dict={x: x_, y: y_, dropout: 0.5})
#计算当前块训练数据的损失和准确率
loss, acc = sess.run([cross_entropy, accuracy], feed_dict={x: x_, y: y_, dropout: 0.5})
print("Epoch:", '%04d' % (i + 1), "cost=", "{:.9f}".format(loss), "Training accuracy", "{:.5f}".format(acc*100))
print('Optimization Completed')

最新文章

  1. 死性不改ISO9000系列系统 2011q4~2016q1
  2. 支付宝WAP支付接口开发(Node/Coffee语言)
  3. HDU 3695 Computer Virus on Planet Pandora(AC自动机模版题)
  4. HibernateTemplate和HibernateDaoSupport(spring注入问题)
  5. java隐士类型转换和强制类型转换
  6. Squid代理服务器&&搭建透明代理网关服务器
  7. PIC16SCM设置不同IO功耗端口状态的影响
  8. 服务器如何防ssh服务暴力破解??
  9. sublime使用方法
  10. 论文笔记(1):Deep Learning.
  11. jar、war、ear
  12. HTML5 本地文件操作之FileSystemAPI简介
  13. C/C++之面向对象
  14. 解决Linux命令行为什么变成-bash-3.2$
  15. [转]一步一步部署SSIS包图解教程
  16. python3工作环境部署+spyder3+jupyter notebook
  17. ZJOI2017游记
  18. SQL Server ->> Memory Allocation Mechanism and Performance Analysis(内存分配机制与性能分析)之 -- Minimum server memory与Maximum server memory
  19. 虚拟机 VMware Workstation12 安装OS X 系统
  20. PL/SQL查询设计器

热门文章

  1. properties文件读取
  2. 事件驱动架构 (Event-Driven Architecture,EDA) 简介
  3. Linux终端小技巧
  4. mysql数据库创建编码及排序
  5. ROS 进阶学习笔记(12) - Communication with ROS through USART Serial Port
  6. IntelliJ IDEA tomcat 热部署
  7. 2.HTML+CSS制作一闪一闪亮晶晶的星星(stars)
  8. mybatis关系映射之一对多和多对一
  9. 查SQLserver性能瓶颈
  10. 转载 :实例详解Django的 select_related 和 prefetch_related 函数对 QuerySet 查询的优化(一)