Tensorflow之用自己的训练好的cpkt模型,进行测试识别
上街讲了如何使用tensorflow生成自己的图片训练模型cpkt。这节讲述如何讲如何使用训练好的cpkt模型进行测试识别。 直接线上代码: #############################################################################################!/usr/bin/python2.7#
·
上街讲了如何使用tensorflow生成自己的图片训练模型cpkt。这节讲述如何讲如何使用训练好的cpkt模型进行测试识别。
直接线上代码:
############################################################################################
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
#Author : zhaoqinghui
#Date : 2016.5.10
#Function: 利用cpkt模型进行测试识别
##########################################################################################
import tensorflow as tf
import numpy as np
import sys
import os
#import math
import cv2
from scipy import ndimage
from skimage.filter.thresholding import threshold_adaptive
import SaveandRestoreChar
#########################################################
result_label_index='result_label.txt'
test_img_path="./testImage/test"
classnum=36
#########################################################
#------中心化操作--------
def getBestShift(img):
cy,cx = ndimage.measurements.center_of_mass(img)
rows,cols = img.shape
shiftx = np.round(cols/2.0-cx).astype(int)
shifty = np.round(rows/2.0-cy).astype(int)
return shiftx, shifty
def shift(img,shiftx,shifty):
rows, cols = img.shape
M = np.float32([[1,0,shiftx],[0,1,shifty]])
shifted = cv2.warpAffine(img,M,(cols,rows))
return shifted
#------读取序号于标签的对应------
def read_result_label_list():
result_label_dir = []
result_label = []
reader = open(result_label_index)
while 1:
line = reader.readline()
tmp = line.split(" ")
if not line:
break
result_label_dir.append(tmp[1][0:-1])
for i in range(int(classnum)):
result_label.append(str(result_label_dir[i]))
return result_label
#------图片的识别部分------
def processImageProposal(imageid):
print test_img_path+str(imageid)+'.png'
gray = cv2.imread(test_img_path+str(imageid)+'.png',cv2.IMREAD_GRAYSCALE)
result_label=read_result_label_list()
shiftx, shifty = getBestShift(gray)
gray = shift(gray,shiftx,shifty)
flatten = gray.flatten()/255.0
pred = SaveandRestoreChar.sess.run(SaveandRestoreChar.predict,feed_dict={SaveandRestoreChar.x:[flatten],SaveandRestoreChar.keep_prob:1.0})
print "Prediction:",int(pred[1])
cv2.imshow(str(imageid)+".png",gray)
cv2.waitKey(0)
def main(argvs):
imageid = argvs[1]#此处为参数设置
#imageid =12
print "image_id:",imageid
processImageProposal(imageid)
if __name__ == '__main__':
main(sys.argv)
其中result_label.txt为正确的结果标签;test_img_path为测试的图片路径,使用result_label.txt来得到正确率。其中SaveandRestoreChar文件为:
############################################################################################
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
#Author : zhaoqinghui
#Date : 2016.5.10
#Function: restore the model from checkpoint
##########################################################################################
import tensorflow as tf
import numpy as np
import math
classnum=36
x = tf.placeholder(tf.float32,[None,28*28])
y_ = tf.placeholder(tf.float32,[None,classnum])
def weight_variable(shape):
init = tf.truncated_normal(shape,stddev = 0.1)
return tf.Variable(init)
def bias_variable(shape):
init = tf.constant(0.1, shape = shape)
return tf.Variable(init)
## 声明卷积操作和pool操作
## 在这里声明的卷积操作是步长为1,padding为0的vanilla版本
## pool操作是2X2的max pool
def conv2d(x,W):
# strides:[batch, in_height, in_width, in_channels]
return tf.nn.conv2d(x,W,strides = [1,1,1,1],padding = 'SAME')
def maxpool2d(x):
return tf.nn.max_pool(x,ksize = [1,2,2,1], strides = [1,2,2,1],padding = 'SAME')
## model构建过程
# 第一层是[一个卷积接一个max pooling], 卷积层的patch_size 是 5X5的,输入的通道数目是1(因为是灰度图),输出是32个feature maps
# [5,5,1,32]: patch_size是5x5,输入通道数目是1,输出通道的数目是32(此处的32是根据网络定义得来,非计算得到)
x_image = tf.reshape(x,[-1,28,28,1]) #把变成需要的格式
W_conv1 = weight_variable([5,5,1,32])
b_conv1 = bias_variable([32])
#做相应的操作,conv, relu, maxpool
h_conv1 = tf.nn.relu(conv2d(x_image,W_conv1)+b_conv1)
h_pool1 = maxpool2d(h_conv1)
# 第二层[一个卷积加一个maxpool]
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 = maxpool2d(h_conv2)
# 全连接层,共有1024个神经元,此时图片进行了两次2x2的maxpool,每次的步长是2,此时图片已经变为了7X7
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 表示的是保留参数的可能性,当等于1.0时表示不进行dropout
keep_prob = tf.placeholder("float") #要输入的值
#keep_prob = 1
h_fc1_drop = tf.nn.dropout(h_fc1,keep_prob)
#添加 softmax层
W_fc2 = weight_variable([1024,classnum])
b_fc2 = bias_variable([classnum])
y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop,W_fc2)+b_fc2)
# loss
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"))
predict = [tf.reduce_max(y_conv),tf.argmax(y_conv,1)[0]]
saver = tf.train.Saver()
checkpoint_dir = "./tmp/train_model.cpkt"
sess = tf.InteractiveSession()
saver.restore(sess,checkpoint_dir)
简单的tensorflow应于与自己的图片集就这样成功了。开心中ing,下次继续。
更多推荐


所有评论(0)