tensorflow-图像识别-卷积深度神经网络

# -*- coding: utf-8 -*-

import numpy as np
from PIL import Image
import tensorflow as tf
import os
import matplotlib.pyplot as plt
try:
    from tqdm import tqdm
except ImportError:
    def tqdm(x,*args,**kwargs):
        return x
    
def to_onehot(labels,nclasses=10):#一维数组转为二维数组
    outlabels=np.zeros((len(labels),nclasses))
    for i,l in enumerate(labels):#转换二维数组,加标签
        outlabels[i,l]=1
    return outlabels

def loadlabels(filedir):#读取标签
    label = np.loadtxt(filedir,skiprows=1,dtype='str',unpack=True)
    label.sort(axis=0)#按列排序
#    print(type(label))
    label2=[]
    for i in range(len(label)):
        label2.append(label[i].split(','))#按,分割字符串
        label[i]=label2[i][1]#只获取第二列数据
    label=label.astype(int)#转为int格式
    label3=to_onehot(label)
    return label3

def loadimages(filedir):#读取图像数据
    imgs=os.listdir(filedir)
    imgs.sort()#升序排列
    arr2=np.empty((len(imgs),784))
    for i in range(len(imgs)):
        img=Image.open(filedir+'/'+imgs[i])#读取图像
        arr=np.asarray(img,dtype='float')#转为数组
        arr=arr.reshape(784)#28*28二维数组合并为一维数组
        arr2[i]=arr/255
    return arr2,imgs


filedir='D:/flyai/001/dev.csv'#读取标签
labels=loadlabels(filedir)
filedir='D:/flyai/001/images'#读取图像数据
data,imgs_name=loadimages(filedir)
'''拆分训练集与测试集'''
indices=np.random.permutation(data.shape[0])#随机打乱重新排序
valid_cnt=int(data.shape[0]*0.1)#取10%数据作测试数据
test_idx,training_idx=indices[:valid_cnt],indices[valid_cnt:]
test,train=data[test_idx,:],data[training_idx,:]
labels_test,labels_train=labels[test_idx,:],labels[training_idx,:]

#运行图时可插入图
sess=tf.InteractiveSession()

x=tf.placeholder('float',shape=[None,784])#占位符,28*28像素矩阵
y_=tf.placeholder('float',shape=[None,10])#占位符,10矩阵

w=tf.Variable(tf.zeros([784,10]))#变量,权值
b=tf.Variable(tf.zeros([10]))#变量,偏移值

#权重初始化
def weight_variable(shape):
    initial=tf.truncated_normal(shape,stddev=0.1)
    return tf.Variable(initial)

def bias_variable(shape):
    initial=tf.constant(0.1,shape=shape)
    return tf.Variable(initial)

#卷积和池化
def conv2d(x,w):
    return tf.nn.conv2d(x,w,strides=[1,1,1,1],padding='SAME')

def max_pool_2x2(x):
    return tf.nn.max_pool(x,ksize=[1,2,2,1],strides=[1,2,2,1],padding='SAME')

#第一层卷积
W_conv1=weight_variable([5,5,1,32])#第一层权值
b_conv1=bias_variable([32])#第一层偏移值
x_image=tf.reshape(x,[-1,28,28,1])
h_conv1=tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)
h_pool1=max_pool_2x2(h_conv1)

#第二层卷积
W_conv2=weight_variable([5,5,32,64])
b_conv2=bias_variable([64])
h_conv2=tf.nn.relu(conv2d(h_pool1,W_conv2)+b_conv2)
h_pool2=max_pool_2x2(h_conv2)

#密集连接层
W_fc1=weight_variable([7*7*64,1024])
b_fc1=bias_variable([1024])
h_pool2_flat=tf.reshape(h_pool2,[-1,7*7*64])
h_fc1=tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1)+b_fc1)

#Dropout
keep_prob=tf.placeholder('float')
rate=1-keep_prob
h_fc1_drop=tf.nn.dropout(h_fc1,1-rate)

#输出层
W_fc2=weight_variable([1024,10])
b_fc2=bias_variable([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2)

#训练和评估
cross_entropy=-tf.reduce_sum(y_*tf.log(y_conv))

train_step=tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)#自适应优化
correct_prediction=tf.equal(tf.argmax(y_conv,1),tf.argmax(y_,1))

accuracy=tf.reduce_mean(tf.cast(correct_prediction,'float'))

sess.run(tf.global_variables_initializer())#全局参数初始化

epochs=1000#学习次数
train_acc=np.zeros(epochs//50)
test_acc=np.zeros(epochs//50)

for i in tqdm(range(epochs)):#进度条
    A=accuracy.eval(feed_dict={x:train,y_:labels_train,rate:0.0})
    train_acc[i//50]=A
    A=accuracy.eval(feed_dict={x:test,y_:labels_test,rate:0.0})
    test_acc[i//50]=A
    train_step.run(feed_dict={x:train,y_:labels_train,rate:0.0})

#计算所学习到的模型在测试数据集上面的正确率
print(sess.run(accuracy,feed_dict={x:test,y_:labels_test,rate:0.0}))


plt.figure(figsize=(6,6))#观察学习曲线
plt.plot(train_acc,'bo')
plt.plot(test_acc,'rx')
sess.close()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值