本文是在参考文献2的基础上进行扩展和改进的,并将代码进行注释,方便学习。并将每层的size进行简要注释
参数设定
N_CLASSES = 5
IMG_W = 208
IMG_H = 208
BATCH_SIZE = 8
CAPACITY = 64
MAX_STEP = 200
learning_rate = 0.0001
(1)获取数据
def get_files(file_dir):
A5 = []
label_A5 = []
A6 = []
label_A6 = []
SEG = []
label_SEG = []
SUM = []
label_SUM = []
LTAX1 = []
label_LTAX1 = []
for file in os.listdir(file_dir):
name = file.split(sep='.')
if name[0]=='A5':
A5.append(file_dir+file)
label_A5.append(0)
elif name[0] == 'A6':
A6.append(file_dir+file)
label_A6.append(1)
elif name[0]=='LTAX1':
LTAX1.append(file_dir+file)
label_LTAX1.append(2)
elif name[0] == 'SEG':
SEG.append(file_dir+file)
label_SEG.append(3)
else:
SUM.append(file_dir+file)
label_SUM.append(4)
image_list = np.array(A5+A6+LTAX1+SEG+SUM)
label_list = np.array(label_A5+label_A6+label_LTAX1+label_SEG+label_SUM)
#随机打乱顺序
temp = np.array([image_list,label_list]) #转换为二维数组
temp = temp.T
np.random.shuffle(temp) #进行打乱顺序
image_list = list(temp[:,0]) #提取第一列
label_list = list(temp[:,1]) #提取二维数组中的第二列
label_list = [int(i) for i in label_list] #将字符串转换为整形
return image_list,label_list
返回一个image和标签的列表。
def get_batch(image,label,image_W,image_H,batch_size,capacity):
image = tf.cast(image,tf.string) #转换image
label = tf.cast(label,tf.int32)
input_queue = tf.train.slice_input_producer([image,label])
label = input_queue[1]
image_contents = tf.read_file(input_queue[0])
image = tf.image.decode_jpeg(image_contents,channels=3)
image = tf.image.resize_image_with_crop_or_pad(image,image_W,image_H)
image = tf.image.per_image_standardization(image)
image_batch,label_batch = tf.train.batch([image,label],batch_size = batch_size,num_threads=16,capacity = capacity)
label_batch = tf.reshape(label_batch,[batch_size])
return image_batch,label_batch
将image,label转换为两个batch,方便tensorflow进行计算。
(2)神经网络模型
大致模型如下所示,有2个卷积层和池化层,通过上述的计算我们将得到我们想要的特征,然后,把池化后的所有节点进行平铺进行全连接层的计算。
def inference(images, batch_size, n_classes):
# conv1, shape = [kernel_size, kernel_size, channels, kernel_numbers]
with tf.variable_scope("conv1") as scope:
weights = tf.get_variable("weights",
shape=[3, 3, 3, 16],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32)) #patch 3x3,in size 3,out size 16
biases = tf.get_variable("biases",
shape=[16],
dtype=tf.float32,
initializer=tf.constant_initializer(0.1)) #biases 16
tf.initialize_all_variables() #初始化所有变量
conv = tf.nn.conv2d(images, weights, strides=[1, 1, 1, 1], padding="SAME") #卷积开始计算,‘SAME’自动加zero padding
pre_activation = tf.nn.bias_add(conv, biases) #求和
conv1 = tf.nn.relu(pre_activation, name="conv1") #output size 208x208x16
print('images.shape\weights.shape',images.shape,weights.shape)
print('conv1.shape',conv1.shape)
# pool1 && norm1
with tf.variable_scope("pooling1_lrn") as scope:
pool1 = tf.nn.max_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
padding="SAME", name="pooling1") #pool 3x3, stride 2x2
norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001/9.0, beta=0.75, name='norm1') #池化后,归一化
print('norm1.shape',norm1.shape)
# conv2
with tf.variable_scope("conv2") as scope:
weights = tf.get_variable("weights",
shape=[3, 3, 16, 32],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32)) #patch 3x3,in size 16, out size 32
biases = tf.get_variable("biases",
shape=[32],
dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
conv = tf.nn.conv2d(norm1, weights, strides=[1, 1, 1, 1], padding="SAME") #卷积计算
pre_activation = tf.nn.bias_add(conv, biases)
conv2 = tf.nn.relu(pre_activation, name="conv2") #output 104x104x32
print('norm1.shape\weights.shape',norm1.shape,weights.shape)
print('conv2.shape',conv2.shape)
# pool2 && norm2
with tf.variable_scope("pooling2_lrn") as scope:
pool2 = tf.nn.max_pool(conv2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
padding="SAME", name="pooling2")
norm2 = tf.nn.lrn(pool2, depth_radius=4, bias=1.0, alpha=0.001/9.0,
beta=0.75, name='norm2')
# full-connect1
with tf.variable_scope("fc1") as scope:
reshape = tf.reshape(norm2, shape=[batch_size, -1])
dim = reshape.get_shape()[1].value #shape 1,52x52x32,将所有节点平铺得到的矩阵
print(reshape.shape)
weights = tf.get_variable("weights",
shape=[dim, 120],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
biases = tf.get_variable("biases",
shape=[120],
dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
fc1 = tf.nn.relu(tf.matmul(reshape, weights) + biases, name="fc1") #激活函数,relu(0,x)
#dropout1 = tf.nn.dropout(fc1, keep_prob = 0.5, name='dropout1') #防止过拟合的函数,长用来连接层
# full_connect2
with tf.variable_scope("fc2") as scope:
weights = tf.get_variable("weights",
shape=[120, 120],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
biases = tf.get_variable("biases",
shape=[120],
dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
fc2 = tf.nn.relu(tf.matmul(fc1, weights) + biases, name="fc2") #
#dropout2 = tf.nn.dropout(fc2, keep_prob = 0.5, name='dropout2')
# softmax
with tf.variable_scope("softmax_linear") as scope:
weights = tf.get_variable("weights",
shape=[120, n_classes],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
biases = tf.get_variable("biases",
shape=[n_classes],
dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
softmax_linear = tf.add(tf.matmul(fc2, weights), biases, name="softmax_linear") #利用常用的softmax线性函数进行模型预测
return softmax_linear
(3)训练模型
计算loss函数
def losses(logits, labels):
with tf.variable_scope("loss") as scope:
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
labels=labels, name="xentropy_per_example")
loss = tf.reduce_mean(cross_entropy, name="loss")
tf.summary.scalar(scope.name + "loss", loss) #记录数据
return loss
进行模型的迭代训练
def trainning(loss, learning_rate):
with tf.name_scope("optimizer"):
#optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) #adam优化算法
#optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) #梯度下降方法
#global_step = tf.Variable(0, name="global_step", trainable=False)
#train_op = optimizer.minimize(loss, global_step=global_step)
train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) #利用adam进行模型迭代,最小loss
return train_op
模型的评估
def evaluation(logits, labels):
with tf.variable_scope("accuracy") as scope:
correct = tf.nn.in_top_k(logits, labels, 1) #用于计算模型预测结果和实际结果是否相等
correct = tf.cast(correct, tf.float16) #将上述的结果转换
accuracy = tf.reduce_mean(correct)
tf.summary.scalar(scope.name + "accuracy", accuracy) #记录一下accuracy数据,方便查看
return accuracy
进行整体的迭代训练
def run_training():
train_path = 'D:/picture/train/'
logs_train_path = 'D:/picture/log/'
train,train_label = get_files(train_path)
train_batch,train_label_batch = get_batch(train,train_label,
IMG_W,
IMG_H,
BATCH_SIZE,
CAPACITY)
train_logits =inference(train_batch,BATCH_SIZE,N_CLASSES)
train_loss = losses(train_logits,train_label_batch)
train_op = trainning(train_loss,learning_rate)
train_acc = evaluation(train_logits,train_label_batch)
summary_op = tf.summary.merge_all() #保存所有数据
sess = tf.Session()
train_writer = tf.summary.FileWriter(logs_train_path,sess.graph)
saver = tf.train.Saver() #引入保存模型函数
sess.run(tf.global_variables_initializer())
coord = tf.train.Coordinator() #创建线程管理器
threads = tf.train.start_queue_runners(sess = sess,coord = coord) #创建一个线程
try:
for step in np.arange(MAX_STEP): #迭代次数
if coord.should_stop():
break
_,tra_loss,tra_acc = sess.run([train_op,train_loss,train_acc]) #模型迭代
if step % 50 == 0: #打印模型
print('Step %d,train loss = %.2f,train occuracy = %.2f%%'%(step,tra_loss,tra_acc))
summary_str = sess.run(summary_op)
print(summary_op)
train_writer.add_summary(summary_str,step)
if step % 200 ==0 or (step +1) == MAX_STEP: #保存模型
checkpoint_path = os.path.join(logs_train_path,'model.ckpt')
saver.save(sess,checkpoint_path,global_step = step)
except tf.errors.OutOfRangeError:
print('Done training epoch limit reached')
finally:
coord.request_stop()
coord.join(threads)
sess.close()
运行run_training() 即可完成模型的训练
(4)测试模型
得到一个img的numpy
def get_one_image(img_dir):
image = Image.open(img_dir)
image = image.resize([208, 208]) #将一个img重采样转换为特定大小的numpy
image_arr = np.array(image)
return image_arr
进行测试
def test(test_file):
log_dir = 'D:/picture/log/' #加载模型
image_arr = get_one_image(test_file)
with tf.Graph().as_default():
image = tf.cast(image_arr, tf.float32)
image = tf.image.per_image_standardization(image)
image = tf.reshape(image, [1,208, 208, 3])
print(image.shape)
p = inference(image,1,5)
logits = tf.nn.softmax(p)
x = tf.placeholder(tf.float32,shape = [208,208,3])
saver = tf.train.Saver()
with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state(log_dir)
if ckpt and ckpt.model_checkpoint_path:
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
saver.restore(sess, ckpt.model_checkpoint_path)
print('Loading success')
else:
print('No checkpoint')
prediction = sess.run(logits, feed_dict={x: image_arr})
max_index = np.argmax(prediction)
print('预测的标签为:')
print(max_index)
print('预测的结果为:')
print(prediction)
if max_index==0:
print('This is a LTAX with possibility %.6f' %prediction[:, 0])
elif max_index == 1:
print('This is a SUM with possibility %.6f' %prediction[:, 1])
elif max_index == 2:
print('This is a A5 with possibility %.6f' %prediction[:, 2])
elif max_index == 3:
print('This is a A6 with possibility %.6f' %prediction[:, 3])
else :
print('This is a SEG with possibility %.6f' %prediction[:, 4])
(5)打印图片
def print_img_train(train_path,IMG_W,IMG_H,BATCH_SIZE,CAPACITY):
image_list,label_list = get_files(train_path)
image_batch,label_batch = get_batch(image_list,label_list,IMG_W,IMG_H,BATCH_SIZE,CAPACITY)
with tf.Session() as sess:
i=0
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord = coord)
try:
while not coord.should_stop() and i<1:
img,label = sess.run([image_batch,label_batch])
for j in np.arange(BATCH_SIZE):
print('label: %d'%label[j])
plt.imshow(img[j,:,:,:])
plt.show()
i+=1
except tf.errors.OutOfRangeError:
print('done!')
finally:
coord.request_stop()
coord.join(threads)
test_path = 'D:/picture/test/' #测试路径,以及打印整个图片的预测值
def read_test_img(test_path):
test_list = [test_path+x for x in os.listdir(test_path)]
for file_path in test_list:
print(file_path)
test(file_path)
利用上述神经网络实现花卉的图片分类。
数据集:http://download.tensorflow.org/example_images/flower_photos.tgz
具体代码如下:
import os
import numpy as np
from PIL import Image
import tensorflow as tf
import matplotlib.pyplot as plt
train_path = 'D:/flower_photos/data/'
def get_files(train_path):
daisy = []
label_daisy = []
dandelion = []
label_dandelion = []
sunflowers = []
label_sunflowers = []
tulips = []
label_tulips = []
roses = []
label_roses = []
for file in os.listdir(train_path):
print(file)
for file_dir in os.listdir(train_path+file):
if file=='daisy':
daisy.append(train_path+file+'/'+file_dir)
label_daisy.append(0)
elif file == 'dandelion':
dandelion.append(train_path+file+'/'+file_dir)
label_dandelion.append(1)
elif file=='roses':
roses.append(train_path+file+'/'+file_dir)
label_roses.append(2)
elif file == 'sunflowers':
sunflowers.append(train_path+file+'/'+file_dir)
label_sunflowers.append(3)
else:
tulips.append(train_path+file+'/'+file_dir)
label_tulips.append(4)
image_list = np.array(daisy+dandelion+roses+sunflowers+tulips)
label_list = np.array(label_daisy+label_dandelion+label_roses+label_sunflowers+label_tulips)
#随机打乱顺序
temp = np.array([image_list,label_list]) #转换为二维数组
temp = temp.T
np.random.shuffle(temp) #进行打乱顺序
image_list = list(temp[:,0]) #提取第一列
label_list = list(temp[:,1]) #提取二维数组中的第二列
label_list = [int(i) for i in label_list] #将字符串转换为整形
return image_list,label_list
def get_batch(image,label,image_W,image_H,batch_size,capacity):
image = tf.cast(image,tf.string)
label = tf.cast(label,tf.int32)
input_queue = tf.train.slice_input_producer([image,label])
label = input_queue[1]
image_contents = tf.read_file(input_queue[0])
image = tf.image.decode_jpeg(image_contents,channels=3)
image = tf.image.resize_image_with_crop_or_pad(image,image_W,image_H)
image = tf.image.per_image_standardization(image)
image_batch,label_batch = tf.train.batch([image,label],batch_size = batch_size,num_threads=16,capacity = capacity)
label_batch = tf.reshape(label_batch,[batch_size])
return image_batch,label_batch
def print_img_train(train_path,IMG_W,IMG_H,BATCH_SIZE,CAPACITY):
image_list,label_list = get_files(train_path)
image_batch,label_batch = get_batch(image_list,label_list,IMG_W,IMG_H,BATCH_SIZE,CAPACITY)
with tf.Session() as sess:
i=0
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(coord = coord)
try:
while not coord.should_stop() and i<1:
img,label = sess.run([image_batch,label_batch])
for j in np.arange(BATCH_SIZE):
print('label: %d'%label[j])
plt.imshow(img[j,:,:,:])
plt.show()
i+=1
except tf.errors.OutOfRangeError:
print('done!')
finally:
coord.request_stop()
coord.join(threads)
def inference(images, batch_size, n_classes):
# conv1, shape = [kernel_size, kernel_size, channels, kernel_numbers]
with tf.variable_scope("conv1") as scope:
weights = tf.get_variable("weights",
shape=[3, 3, 3, 16],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32)) #patch 3x3,in size 3,out size 16
biases = tf.get_variable("biases",
shape=[16],
dtype=tf.float32,
initializer=tf.constant_initializer(0.1)) #biases 16
tf.initialize_all_variables()
conv = tf.nn.conv2d(images, weights, strides=[1, 1, 1, 1], padding="SAME")
pre_activation = tf.nn.bias_add(conv, biases)
conv1 = tf.nn.relu(pre_activation, name="conv1") #output size 208x208x16
print('images.shape\weights.shape',images.shape,weights.shape)
print('conv1.shape',conv1.shape)
# pool1 && norm1
with tf.variable_scope("pooling1_lrn") as scope:
pool1 = tf.nn.max_pool(conv1, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
padding="SAME", name="pooling1") #pool 3x3, stride 2x2
norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001/9.0, beta=0.75, name='norm1') #归一化
print('norm1.shape',norm1.shape)
# conv2
with tf.variable_scope("conv2") as scope:
weights = tf.get_variable("weights",
shape=[3, 3, 16, 32],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.1, dtype=tf.float32)) #patch 3x3,in size 16, out size 32
biases = tf.get_variable("biases",
shape=[32],
dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
conv = tf.nn.conv2d(norm1, weights, strides=[1, 1, 1, 1], padding="SAME")
pre_activation = tf.nn.bias_add(conv, biases)
conv2 = tf.nn.relu(pre_activation, name="conv2") #output 104x104x32
print('norm1.shape\weights.shape',norm1.shape,weights.shape)
print('conv2.shape',conv2.shape)
# pool2 && norm2
with tf.variable_scope("pooling2_lrn") as scope:
pool2 = tf.nn.max_pool(conv2, ksize=[1, 3, 3, 1], strides=[1, 2, 2, 1],
padding="SAME", name="pooling2")
norm2 = tf.nn.lrn(pool2, depth_radius=4, bias=1.0, alpha=0.001/9.0,
beta=0.75, name='norm2')
# full-connect1
with tf.variable_scope("fc1") as scope:
reshape = tf.reshape(norm2, shape=[batch_size, -1])
dim = reshape.get_shape()[1].value #shape 1,52x52x32,平铺得到的矩阵
print(reshape.shape)
weights = tf.get_variable("weights",
shape=[dim, 120],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
biases = tf.get_variable("biases",
shape=[120],
dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
fc1 = tf.nn.relu(tf.matmul(reshape, weights) + biases, name="fc1")
dropout1 = tf.nn.dropout(fc1, keep_prob = 0.3, name='dropout1')
# full_connect2
with tf.variable_scope("fc2") as scope:
weights = tf.get_variable("weights",
shape=[120, 120],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
biases = tf.get_variable("biases",
shape=[120],
dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
fc2 = tf.nn.relu(tf.matmul(dropout1, weights) + biases, name="fc2")
dropout2 = tf.nn.dropout(fc2, keep_prob = 0.3, name='dropout2')
# softmax
with tf.variable_scope("softmax_linear") as scope:
weights = tf.get_variable("weights",
shape=[120, n_classes],
dtype=tf.float32,
initializer=tf.truncated_normal_initializer(stddev=0.005, dtype=tf.float32))
biases = tf.get_variable("biases",
shape=[n_classes],
dtype=tf.float32,
initializer=tf.constant_initializer(0.1))
softmax_linear = tf.add(tf.matmul(dropout2, weights), biases, name="softmax_linear")
return softmax_linear
def losses(logits, labels):
with tf.variable_scope("loss") as scope:
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits,
labels=labels, name="xentropy_per_example")
loss = tf.reduce_mean(cross_entropy, name="loss")
#tf.summary.scalar(scope.name + "loss", loss)
return loss
def trainning(loss, learning_rate):
with tf.name_scope("optimizer"):
#optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss) #adam优化算法
#optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate) #梯度下降方法
#global_step = tf.Variable(0, name="global_step", trainable=False)
#train_op = optimizer.minimize(loss, global_step=global_step)
train_op = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)
return train_op
def evaluation(logits, labels):
with tf.variable_scope("accuracy") as scope:
correct = tf.nn.in_top_k(logits, labels, 1)
correct = tf.cast(correct, tf.float16)
accuracy = tf.reduce_mean(correct)
tf.summary.scalar(scope.name + "accuracy", accuracy)
return accuracy
N_CLASSES = 5
IMG_W = 208
IMG_H = 208
BATCH_SIZE = 100
CAPACITY = 64
MAX_STEP = 2000
learning_rate = 0.0001
def split_train_test(temp_train,temp_train_label):
ratio = 0.8
s = np.int(len(temp_train)*ratio)
train = temp_train[:s]
train_label = temp_train_label[:s]
test = temp_train[s:]
test_label = temp_train_label[s:]
return train,train_label,test,test_label
def run_training():
train_path = 'D:/flower_photos/data/'
logs_train_path = 'D:/flower_photos/log/'
temp_train,temp_train_label = get_files(train_path)
ratio = 0.8
s = np.int(len(temp_train)*ratio)
train = temp_train[:s]
train_label = temp_train_label[:s]
test = temp_train[s:]
test_label = temp_train_label[s:]
train_batch,train_label_batch = get_batch(train,train_label,
IMG_W,
IMG_H,
BATCH_SIZE,
CAPACITY)
train_logits =inference(train_batch,BATCH_SIZE,N_CLASSES)
train_loss = losses(train_logits,train_label_batch)
train_op = trainning(train_loss,learning_rate)
train_acc = evaluation(train_logits,train_label_batch)
summary_op = tf.summary.merge_all()
sess = tf.Session()
train_writer = tf.summary.FileWriter(logs_train_path,sess.graph)
saver = tf.train.Saver()
sess.run(tf.global_variables_initializer())
coord = tf.train.Coordinator()
threads = tf.train.start_queue_runners(sess = sess,coord = coord)
try:
for step in np.arange(MAX_STEP):
if coord.should_stop():
break
_,tra_loss,tra_acc = sess.run([train_op,train_loss,train_acc])
if step % 50 == 0:
print('Step %d,train loss = %.2f,train occuracy = %.2f%%'%(step,tra_loss,tra_acc))
summary_str = sess.run(summary_op)
print(summary_op)
train_writer.add_summary(summary_str,step)
if step % 200 ==0 or (step +1) == MAX_STEP:
checkpoint_path = os.path.join(logs_train_path,'model.ckpt')
saver.save(sess,checkpoint_path,global_step = step)
except tf.errors.OutOfRangeError:
print('Done training epoch limit reached')
finally:
coord.request_stop()
coord.join(threads)
sess.close()
return test, test_label
def get_one_image(img_dir):
image = Image.open(img_dir)
image = image.resize([208, 208])
image_arr = np.array(image)
return image_arr
def test(test_file):
log_dir = 'D:/flower_photos/log/'
image_arr = get_one_image(test_file)
with tf.Graph().as_default():
image = tf.cast(image_arr, tf.float32)
image = tf.image.per_image_standardization(image)
image = tf.reshape(image, [1,208, 208, 3])
print(image.shape)
p = inference(image,1,5)
logits = tf.nn.softmax(p)
x = tf.placeholder(tf.float32,shape = [208,208,3])
saver = tf.train.Saver()
with tf.Session() as sess:
ckpt = tf.train.get_checkpoint_state(log_dir)
if ckpt and ckpt.model_checkpoint_path:
global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]
saver.restore(sess, ckpt.model_checkpoint_path)
print('Loading success')
else:
print('No checkpoint')
prediction = sess.run(logits, feed_dict={x: image_arr})
max_index = np.argmax(prediction)
print('预测的标签为:')
print(max_index)
print('预测的结果为:')
print(prediction)
if max_index==0:
print('This is a roses with possibility %.6f' %prediction[:, 0])
elif max_index == 1:
print('This is a tulips with possibility %.6f' %prediction[:, 1])
elif max_index == 2:
print('This is a daisy with possibility %.6f' %prediction[:, 2])
elif max_index == 3:
print('This is a dandelion with possibility %.6f' %prediction[:, 3])
else :
print('This is a sunflowers with possibility %.6f' %prediction[:, 4])
return max_index
test_data, test_label = run_training()
#test_path = 'D:/picture/test/'
#print(test_data,test_label)
def read_test_img(test_data, test_label):
num = 0
for i in range(len(test_data)):
if test(test_data[i]) == int(test_label[i]):
num += 1
print(len(test_data),num)
read_test_img(test_data, test_label)
参考文献:
https://blog.csdn.net/Missayaaa/article/details/79119839
https://blog.csdn.net/qq_30159351/article/details/52641644
https://blog.csdn.net/lenbow/article/details/52218551 常用函数
https://blog.csdn.net/marsjhao/article/details/72630147 交叉熵