import os
import numpy as np
import matplotlib.pyplot as plt
from skimage import color,data,transform,io

labelList = os.listdir("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\Training")
allFruitsImageName = []
for i in range(len(labelList)):
allFruitsImageName.append(os.listdir("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\Training\\"+labelList[i]))
allsortImageName = []
for i in range(len(allFruitsImageName)):
oneClass = allFruitsImageName[i]
nr = []
r = []
r2 = []
for i in range(len(oneClass)):
if(oneClass[i].split("_")[0].isdigit()):
nr.append(int(oneClass[i].split("_")[0]))
else:
if(len(oneClass[i].split("_")[0])==1):
r.append(int(oneClass[i].split("_")[1]))
else:
r2.append(int(oneClass[i].split("_")[1]))
sortnr = sorted(nr)
sortnrImageName = []
for i in range(len(sortnr)):
sortnrImageName.append(str(sortnr[i])+"_100.jpg")
sortr = sorted(r)
sortrImageName = []
for i in range(len(sortr)):
sortrImageName.append("r_"+str(sortr[i])+"_100.jpg")
sortr2 = sorted(r2)
sortr2ImageName = []
for i in range(len(sortr2)):
sortr2ImageName.append("r2_"+str(sortr2[i])+"_100.jpg")
sortnrImageName.extend(sortrImageName)
sortnrImageName.extend(sortr2ImageName)
allsortImageName.append(sortnrImageName)

trainData = []
for i in range(len(allsortImageName)):
one = []
for j in range(len(allsortImageName[i])):
rgb=io.imread("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\Training\\"+labelList[i]+"\\" + allsortImageName[i][j]) #读取图片
gray=color.rgb2gray(rgb) #将彩色图片转换为灰度图片
dst=transform.resize(gray,(64,64)) #调整大小,图像分辨率为64*64
one.append(dst)
trainData.append(one)
print(np.shape(trainData))

trainLabelNum = []
for i in range(len(trainData)):
for j in range(len(trainData[i])):
trainLabelNum.append(i)
imageGray = trainData[i][j]
io.imsave("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\trainGrayImage\\"+str(i)+"_"+str(j)+".jpg",imageGray)
np.save("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\trainLabelNum",trainLabelNum)
print("图片处理完了")

testLabelList = os.listdir("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\Test")
testallFruitsImageName = []
for i in range(len(testLabelList)):
testallFruitsImageName.append(os.listdir("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\Test\\"+testLabelList[i]))
testallsortImageName = []
for i in range(len(testallFruitsImageName)):
oneClass = testallFruitsImageName[i]
nr = []
r = []
r2 = []
for i in range(len(oneClass)):
if(oneClass[i].split("_")[0].isdigit()):
nr.append(int(oneClass[i].split("_")[0]))
else:
if(len(oneClass[i].split("_")[0])==1):
r.append(int(oneClass[i].split("_")[1]))
else:
r2.append(int(oneClass[i].split("_")[1]))
sortnr = sorted(nr)
sortnrImageName = []
for i in range(len(sortnr)):
sortnrImageName.append(str(sortnr[i])+"_100.jpg")
sortr = sorted(r)
sortrImageName = []
for i in range(len(sortr)):
sortrImageName.append("r_"+str(sortr[i])+"_100.jpg")
sortr2 = sorted(r2)
sortr2ImageName = []
for i in range(len(sortr2)):
sortr2ImageName.append("r2_"+str(sortr2[i])+"_100.jpg")
sortnrImageName.extend(sortrImageName)
sortnrImageName.extend(sortr2ImageName)
testallsortImageName.append(sortnrImageName)

testData = []
for i in range(len(testallsortImageName)):
one = []
for j in range(len(testallsortImageName[i])):
rgb=io.imread("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\Test\\"+testLabelList[i]+"\\" + testallsortImageName[i][j])
gray=color.rgb2gray(rgb)
dst=transform.resize(gray,(64,64))
one.append(dst)
testData.append(one)
print(np.shape(testData))

testLabelNum = []
for i in range(len(testData)):
for j in range(len(testData[i])):
testLabelNum.append(i)
imageGray = testData[i][j]
io.imsave("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\testGrayImage\\"+str(i)+"_"+str(j)+".jpg",imageGray)
np.save("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\testLabelNum",testLabelNum)
print("图片处理完了")

import os
import numpy as np
import matplotlib.pyplot as plt
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")

import tensorflow as tf
from random import shuffle

INPUT_NODE = 64*64
OUT_NODE = 77
IMAGE_SIZE = 64
NUM_CHANNELS = 1
NUM_LABELS = 77
#第一层卷积层的尺寸和深度
CONV1_DEEP = 64
CONV1_SIZE = 5
#第二层卷积层的尺寸和深度
CONV2_DEEP = 128
CONV2_SIZE = 5
#全连接层的节点数
FC_SIZE = 1024

def inference(input_tensor, train, regularizer):
#卷积
with tf.variable_scope('layer1-conv1'):
conv1_weights = tf.Variable(tf.random_normal([CONV1_SIZE,CONV1_SIZE,NUM_CHANNELS,CONV1_DEEP],stddev=0.1),name='weight')
conv1_biases = tf.Variable(tf.Variable(tf.random_normal([CONV1_DEEP])),name="bias")
conv1 = tf.nn.conv2d(input_tensor,conv1_weights,strides=[1,1,1,1],padding='SAME')
relu1 = tf.nn.relu(tf.nn.bias_add(conv1,conv1_biases))
#池化
with tf.variable_scope('layer2-pool1'):
pool1 = tf.nn.max_pool(relu1,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
#卷积
with tf.variable_scope('layer3-conv2'):
conv2_weights = tf.Variable(tf.random_normal([CONV2_SIZE,CONV2_SIZE,CONV1_DEEP,CONV2_DEEP],stddev=0.1),name='weight')
conv2_biases = tf.Variable(tf.random_normal([CONV2_DEEP]),name="bias")
#卷积向前学习
conv2 = tf.nn.conv2d(pool1,conv2_weights,strides=[1,1,1,1],padding='SAME')
relu2 = tf.nn.relu(tf.nn.bias_add(conv2,conv2_biases))
#池化
with tf.variable_scope('layer4-pool2'):
pool2 = tf.nn.max_pool(relu2,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

#卷积
with tf.variable_scope('layer5-conv3'):
conv3_weights = tf.Variable(tf.random_normal([5,5,CONV2_DEEP,512],stddev=0.1),name='weight')
conv3_biases = tf.Variable(tf.random_normal([512]),name="bias")
#卷积向前学习
conv3 = tf.nn.conv2d(pool2,conv3_weights,strides=[1,1,1,1],padding='SAME')
relu3 = tf.nn.relu(tf.nn.bias_add(conv3,conv3_biases))
#池化
with tf.variable_scope('layer6-pool3'):
pool3 = tf.nn.max_pool(relu3,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

#卷积
with tf.variable_scope('layer7-conv4'):
conv4_weights = tf.Variable(tf.random_normal([5,5,512,64],stddev=0.1),name='weight')
conv4_biases = tf.Variable(tf.random_normal([64]),name="bias")
#卷积向前学习
conv4 = tf.nn.conv2d(pool3,conv4_weights,strides=[1,1,1,1],padding='SAME')
relu4 = tf.nn.relu(tf.nn.bias_add(conv4,conv4_biases))
#池化
with tf.variable_scope('layer7-pool4'):
pool4 = tf.nn.max_pool(relu3,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')
#变型
pool_shape = pool4.get_shape().as_list()
#计算最后一次池化后对象的体积(数据个数\节点数\像素个数)
nodes = pool_shape[1]*pool_shape[2]*pool_shape[3]
#根据上面的nodes再次把最后池化的结果pool2变为batch行nodes列的数据
reshaped = tf.reshape(pool4,[-1,nodes])

#全连接层
with tf.variable_scope('layer8-fc1'):
fc1_weights = tf.Variable(tf.random_normal([nodes,FC_SIZE],stddev=0.1),name='weight')
if(regularizer != None):
tf.add_to_collection('losses',tf.contrib.layers.l2_regularizer(0.03)(fc1_weights))
fc1_biases = tf.Variable(tf.random_normal([FC_SIZE]),name="bias")
#预测
fc1 = tf.nn.relu(tf.matmul(reshaped,fc1_weights)+fc1_biases)
if(train):
fc1 = tf.nn.dropout(fc1,0.5)
#全连接层
with tf.variable_scope('layer9-fc2'):
fc2_weights = tf.Variable(tf.random_normal([FC_SIZE,64],stddev=0.1),name="weight")
if(regularizer != None):
tf.add_to_collection('losses',tf.contrib.layers.l2_regularizer(0.03)(fc2_weights))
fc2_biases = tf.Variable(tf.random_normal([64]),name="bias")
#预测
fc2 = tf.nn.relu(tf.matmul(fc1,fc2_weights)+fc2_biases)
if(train):
fc2 = tf.nn.dropout(fc2,0.5)
#全连接层
with tf.variable_scope('layer10-fc3'):
fc3_weights = tf.Variable(tf.random_normal([64,NUM_LABELS],stddev=0.1),name="weight")
if(regularizer != None):
tf.add_to_collection('losses',tf.contrib.layers.l2_regularizer(0.03)(fc3_weights))
fc3_biases = tf.Variable(tf.random_normal([NUM_LABELS]),name="bias")
#预测
logit = tf.matmul(fc2,fc3_weights)+fc3_biases
return logit

import keras
import time
from keras.utils import np_utils

X = np.vstack(trainDataList).reshape(-1, 64,64,1)
Y = np.vstack(trainLabelNum).reshape(-1, 1)
Xrandom = []
Yrandom = []
index = [i for i in range(len(X))]
shuffle(index)
for i in range(len(index)):
Xrandom.append(X[index[i]])
Yrandom.append(Y[index[i]])
np.save("E:\\Xrandom",Xrandom)
np.save("E:\\Xrandom",Yrandom)

X = Xrandom
Y = Yrandom
Y=keras.utils.to_categorical(Y,OUT_NODE)

batch_size = 200
n_classes=77
epochs=20#循环次数
learning_rate=1e-4
batch_num=int(np.shape(X)[0]/batch_size)
dropout=0.75

x=tf.placeholder(tf.float32,[None,64,64,1])
y=tf.placeholder(tf.float32,[None,n_classes])
# keep_prob = tf.placeholder(tf.float32)

pred=inference(x,1,"regularizer")

cost=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred,labels=y))

# 三种优化方法选择一个就可以
optimizer=tf.train.AdamOptimizer(1e-4).minimize(cost)
# train_step = tf.train.GradientDescentOptimizer(0.001).minimize(cost)
# train_step = tf.train.MomentumOptimizer(0.001,0.9).minimize(cost)
keep_prob = tf.placeholder(dtype=tf.float32, name="keep_prob")
correct_pred=tf.equal(tf.argmax(pred,1),tf.argmax(y,1))
accuracy=tf.reduce_mean(tf.cast(correct_pred,tf.float32))
# merged = tf.summary.merge_all()
init=tf.global_variables_initializer()
start_time = time.time()

with tf.Session() as sess:
sess.run(init)
# writer = tf.summary.FileWriter('./fruit', sess.graph)
for i in range(epochs):
for j in range(batch_num):
start = (j*batch_size)
end = start+batch_size
sess.run(optimizer, feed_dict={x:X[start:end],y:Y[start:end],keep_prob: 0.5})
loss,acc = sess.run([cost,accuracy],feed_dict={x:X[start:end],y:Y[start:end],keep_prob: 1})
# result = sess.run(merged, feed_dict={x:X[start:end],y:Y[start:end]})
# writer.add_summary(result, i)
if epochs % 1 == 0:
print("Epoch:", '%04d' % (i+1),"cost=", "{:.9f}".format(loss),"Training accuracy","{:.5f}".format(acc*100))

end_time = time.time()
print('运行时间:',(end_time-start_time))
print('Optimization Completed')

def gen_small_data(inputs,batch_size):
i=0
j = True
while j:
small_data=inputs[i:(batch_size+i)]
i+=batch_size
if len(small_data)!=0:
yield small_data
if len(small_data)==0:
j=False

with tf.Session() as sess:
sess.run(init)
# writer = tf.summary.FileWriter('./fruit', sess.graph)
for i in range(epochs):
x_=gen_small_data(X,batch_size)
y_=gen_small_data(Y,batch_size)
X = next(x_)
Y = next(y_)
sess.run(optimizer, feed_dict={x:X,y:Y})
loss,acc = sess.run([cost,accuracy],feed_dict={x:X,y:Y})
# result = sess.run(merged, feed_dict={x:X[start:end],y:Y[start:end]})
# writer.add_summary(result, i)
if epochs % 1 == 0:
print("Epoch:", '%04d' % (i+1),"cost=", "{:.9f}".format(loss),"Training accuracy","{:.5f}".format(acc))

labelNameList = []
for i in range(len(labelList)):
labelNameList.append("label:"+labelList[i])
theFireImage = []
for i in range(len(allsortImageName)):
theFireImage.append(plt.imread("F:\\MachineLearn\\ML-xiaoxueqi\\fruits\\Training\\"+labelList[i]+"\\" + allsortImageName[i][4]))
gs = plt.GridSpec(11,7)
fig = plt.figure(figsize=(10,10))
imageIndex = 0
ax = plt.gca()
for i in range(11):
for j in range(7):
fi = fig.add_subplot(gs[i,j])
fi.imshow(theFireImage[imageIndex])
plt.xticks(())
plt.yticks(())
plt.axis('off')
plt.title(labelNameList[imageIndex],fontsize=7)
ax.set_xticks([])
ax.set_yticks([])
ax.spines['top'].set_color('none')
ax.spines['left'].set_color('none')
ax.spines['right'].set_color('none')
ax.spines['bottom'].set_color('none')
imageIndex += 1
plt.show()

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

  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神经网络 水果图片识别(3)

    import osimport kerasimport timeimport numpy as npimport tensorflow as tffrom random import shufflef ...

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

    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. myeclipse修改编译器版本的方法 .

    今天在导入一个工程时,发现出现java.lang.UnsupportedClassVersionError: Bad version number in .class file异常,检查了一下我的my ...

  2. Linux内核编译:很少有人提及的一些内容

    1. 你可以使用O=参数将编译结果放到其他位置(非源代码目录),例如:make O=~/build ... 这样做的好处是你的源代码目录不会受到任何改变:你甚至可以在不同的体系结构间共享源代码. 注意 ...

  3. Redis内存数据库操作命令详解

    一.连接操作相关的命令 Ÿ   quit:关闭连接(connection) Ÿ   auth:简单密码认证 二.对value操作的命令 Ÿ   exists(key):确认一个key是否存在 Ÿ   ...

  4. Asio 注意事项

    本文列举 Asio 各种值得注意的细节. 另见:基于 Asio 的 C++ 网络编程 No Deprecated 在包含 Asio 头文件之前,定义宏 BOOST_ASIO_NO_DEPRECATED ...

  5. Web(click and script) 与 Web(HTTP/HTML)协议区别

    先从最简单的说明上来看, Web(HTTP/HTML):       Emulation of     communication between a    browser and Web Serve ...

  6. 服务注册发现Eureka之二:高可用服务注册中心

    前言 在Spring Cloud系列文章的开始,我们就介绍了服务注册与发现,其中,主要演示了如何构建和启动服务注册中心Eureka Server,以及如何将服务注册到Eureka Server中,但是 ...

  7. [UE4]C++ 动态内存分配(6种情况,好几个例子)

    1.堆内存分配 : C/C++定义了4个内存区间: 代码区,全局变量与静态变量区,局部变量区即栈区,动态存储区,即堆(heap)区或自由存储区(free store). 堆的概念: 通常定义变量(或对 ...

  8. 在ie6下的png图片的兼容问题

    png图片在ie6下是这样的: 正确样式: 这样解决: html代码: <body> <div class="gys"></div> </ ...

  9. Spark分析之Master、Worker以及Application三者之间如何建立连接

    Master.preStart(){ webUi.bind() context.system.scheduler.schedule( millis, WORKER_TIMEOUT millis, se ...

  10. 小朋友学Python(3)

    运算符 一.基本运算符 Python运算符多数与C/C++/Java类似,但有少数不一样. “/”表示求商,“//”求商的整数部分.11 / 2 = 5.500000, 11 // 2 = 5 “** ...