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. 初级安全入门——XSS注入的原理与利用

    XSS的简单介绍 跨站脚本攻击(Cross Site Scripting),为不和层叠样式表(Cascading Style Sheets,CSS)的缩写混淆,故将跨站脚本攻击缩写为XSS.恶意攻击者 ...

  2. switch嵌套--猜拳游戏

    <!DOCTYPE html> <html>     <head>         <meta charset="UTF-8">   ...

  3. windows 下安装 docker

    1. 使用阿里云的镜像进行安装: http://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/ 2. 安装完成后点击图标 “Dock ...

  4. 微信小程序 setData 的坑(转)

    最近在使用微信小程序的setData时,遇到了以下问题.如下: 官网文档在使用setData()设置数组对象的某个元素的属性时,是这么使用的: Page({ data: { array: [{text ...

  5. shell中的时间值提取(date)

    shell中的时间值提取(date) 方法1 # date +%F # date +%T # cat time.sh #!/bin/bash DATE=`date +%F | sed 's/-//g' ...

  6. android 开发 实现一个带图片Image的ListView

    注意:这种实现方法不是实现ListView的最优方法,只是希望通过练习了解ListView的实现原理 思维路线: 1.创建drawable文件夹将要使用的图片导入进去 2.写一个类,用于存放图片ID数 ...

  7. py库: scrapy (深坑未填)

    scrapy 一个快速高级的屏幕爬取及网页采集框架 http://scrapy.org/ 官网 https://docs.scrapy.org/en/latest/ Scrapy1.4文档 http: ...

  8. srbac配置

    Yii框架中安装srbac扩展方法 以前自己安装过一次srbac,遇到很多问题,虽然都解决了,可是一时偷懒,没做记录. 再次安装时,还是遇到了点麻烦,所以这一还是记下来,以备不时之需. 首先,下载sr ...

  9. JS中点击事件冒泡阻止

    JS中点击事件冒泡阻止 解析: 一个div层'out',内含有一个div层'in'.如下: 两个层都绑定了点击事件,但是点击in层的时候,点击事件会出现冒泡现象,同时也会触发out层的点击事件. 但是 ...

  10. groovy 从jsonList中读取某个字段

    今天又被groovy的高效吓到了. 想提取所有的itemCodes,两种玩法 一.常规方法:遍历组装 RestResult items = getListPager() def temp = [] i ...