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')

吴裕雄 python神经网络 水果图片识别(3)的更多相关文章

  1. 吴裕雄 python神经网络 水果图片识别(5)

    #-*- coding:utf-8 -*-### required libaraiedimport osimport matplotlib.image as imgimport matplotlib. ...

  2. 吴裕雄 python神经网络 水果图片识别(4)

    # coding: utf-8 # In[1]:import osimport numpy as npfrom skimage import color, data, transform, io # ...

  3. 吴裕雄 python神经网络 水果图片识别(2)

    import osimport numpy as npimport matplotlib.pyplot as pltfrom skimage import color,data,transform,i ...

  4. 吴裕雄 python神经网络 水果图片识别(1)

    import osimport numpy as npimport matplotlib.pyplot as pltfrom skimage import color,data,transform,i ...

  5. 吴裕雄 python神经网络 花朵图片识别(10)

    import osimport numpy as npimport matplotlib.pyplot as pltfrom PIL import Image, ImageChopsfrom skim ...

  6. 吴裕雄 python神经网络 花朵图片识别(9)

    import osimport numpy as npimport matplotlib.pyplot as pltfrom PIL import Image, ImageChopsfrom skim ...

  7. 吴裕雄 python 神经网络——TensorFlow图片预处理调整图片

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt def distort_color(image, ...

  8. 吴裕雄 python 神经网络——TensorFlow 花瓣识别2

    import glob import os.path import numpy as np import tensorflow as tf from tensorflow.python.platfor ...

  9. 吴裕雄 python 神经网络——TensorFlow图片预处理

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt # 使用'r'会出错,无法解码,只能以2进制形式读 ...

随机推荐

  1. html跳页面传值

    从a.html跳转到b.html并且把a.html的值name传入b.html  在a.html页面,url路径后面带参数,参数与url之间用?隔开 window.location.href = &q ...

  2. 使用sqoop从mysql导入数据到hive

      目录 前言 一.使用的导入命令 二.遇到的问题及解决 1. 用文本字段进行分区的问题 2. Hadoop历史服务器Hadoop JobHistory没开启的问题 3. 连接元数据存储数据库报错 4 ...

  3. python学习之----Lambda表达式

    Lambda 表达式本质上就是一个函数,可以作为其他函数的变量使用:也就是说,一个函 数不是定义成f(x, y),而是定义成f(g(x), y),或f(g(x), h(x)) 的形式. Beautif ...

  4. tornado-5.1版本

    server.py python server.py执行 import tornado.ioloop import tornado.options import tornado.web from to ...

  5. 8.2.1-优化SELECT语句

    8.2.1.优化 SELECT 语句 由SELECT 语句组成的查询,在数据中执行所有的查询.对这类语句的调优排在首位,无论是调优动态web网页的二级响应时间,还是减少生成巨大隔夜报告的时间. 而且, ...

  6. spring 之 depends-check

    我们知道 spring xml 配置中 bean 的子元素 property 是按照 setXxx(SomeType someParam) 方式来设值的,换句话说, 是根据属性的setter 方法, ...

  7. OpenGL ES平移矩阵和旋转矩阵的左乘与右乘效果

    OpenGL ES平移矩阵和旋转矩阵的左乘与右乘 在OpenGL .OpenGL ES中矩阵起着举足轻重的作用,而矩阵之间的左乘与右乘在效果上是不同的. 一.先平移后旋转 场景效果:人绕树旋转. 原理 ...

  8. WebForm(内置函数)

    Response - 响应对象1.定义:Response对象用于动态响应客户端请示,控制发送给用户的信息,并将动态生成响应.若指定的cookie不存在,则创建它.若存在,则将自动进行更新.结果返回给客 ...

  9. Andriod开发第一步-部署环境(搬运&&总结)

         第一步:安装JDK      第二步:配置Windows上JDK的变量环境      第三步:下载安装Eclipse      第四步:下载安装Android SDK 配置了JDK变量环境, ...

  10. Jmeter之Bean shell使用-常用内置变量

    Bean Shell常用内置变量   JMeter在它的BeanShell中内置了变量,用户可以通过这些变量与JMeter进行交互,其中主要的变量及其使用方法如下: log:写入信息到jmeber.l ...