解析:global_step = ckpt.model_checkpoint_path.split('/')[-1].split('-')[-1]

打印模型的路径及名称
print(ckpt.model_checkpoint_path)

输出如这样:MNIST_model/mnist_model-29001

split函数:拆分字符串。通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(list)。
split函数返回值为:分割后的字符串列表。
list[n]:即表示选取第n个分片,n为-1即为末尾倒数第一个分片(分片即为在返回值列表中元素)

通过文件名得到模型保存时的迭代轮数
global_step = ckpt.model_checkpoint_path.split(’/’)[-1].split(’-’)[-1]
即对字符串"MNIST_model/mnist_model-29001"进行分割
分隔结果为:29001

知识点部分
python中的split函数

Python中有split()和os.path.split()两个函数,具体作用如下:

split():拆分字符串。通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(list)

os.path.split():按照路径将文件名和路径分割开,返回的是元组

1. split()函数

语法:str.split(str="",num=string.count(str))[n]

参数说明:
str:表示为分隔符,默认为空格,但是不能为空(’’)。若字符串中没有分隔符,则把整个字符串作为列表的一个元素
num:表示分割次数。如果存在参数num,则仅分隔成 num+1 个子字符串,并且每一个子字符串可以赋给新的变量
[n]:表示选取第n个分片

注意:当没有参数的情况下,函数默认会以空格,回车符,空格符等作为分割条件。
str.split(str="",num=string.count(str)),返回值是列表,str.split(str="",num=string.count(str))[n],返回值是字符串

a = "My name is YoungFan"
b = "My\nname\tis\nYoungFan"  # \t 、\n是转义字符
c = "my\tname\tis YoungFan"

a = a.split()
b = b.split()
c = c.split(' ')  # 分隔符为一个空格

print(a)
print(b)
print(c)

输出:
['My', 'name', 'is', 'YoungFan']
['My', 'name', 'is', 'YoungFan']
['my\tname\tis', 'YoungFan']

string = "hello.world.python"
print(string.split('.'))  # 输出为:['hello', 'world', 'python']
print(string.split('.', 1))  # 输出为:['hello', 'world.python']
print(string.split('.', 1)[0])  # 输出为:hello
print(string.split('.', 1)[1])  # 输出为:world.python
string2 = "hello<python.world>and<c++>end"
print(string2.split("<", 2)[2].split(">")[0])  # 输出为:c++
2. os.path.split()

os.path.split() 函数将文件路径和文件名分开

x = os.path.split("E:/GitHub收藏项目/keras-yolo3-master/yolo.py")
print(x)

输出:('E:/GitHub收藏项目/keras-yolo3-master', 'yolo.py')

下面扩展一个知识点

3. os.path.splitext()

os.path.splitext() 函数将文件名和扩展名分开

y = os.path.splitext("E:/GitHub收藏项目/keras-yolo3-master/yolo.py")
print(y)  # 返回的是一个元组类型的  

输出:('E:/GitHub收藏项目/keras-yolo3-master/yolo', '.py')
分割完可以多重赋值
import cv2
major, minor = cv2.__version__.split('.')[:2]  # (字符串,列表,元组)切片:后边界不包括,例如[0,1,2][0:1]得[0]
print(major)  # 输出3
print(minor)  # 输出3
# 说明这是OpenCV3.3以上的版本

参考:
Python中的split()函数的用法
https://blog.csdn.net/meccaendless/article/details/78027012
python split(), os.path.split()和os.path.splitext()函数:
https://blog.csdn.net/zzc15806/article/details/81352742
python split(),os.path.split()和os.path.splitext()函数用法:
https://blog.csdn.net/T1243_3/article/details/80170006
python中split()函数讲解
https://blog.csdn.net/csdn15698845876/article/details/74012511

  • 2
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
以下代码有什么错误,怎么修改: import tensorflow.compat.v1 as tf tf.disable_v2_behavior() from PIL import Image import matplotlib.pyplot as plt import input_data import model import numpy as np import xlsxwriter num_threads = 4 def evaluate_one_image(): workbook = xlsxwriter.Workbook('formatting.xlsx') worksheet = workbook.add_worksheet('My Worksheet') with tf.Graph().as_default(): BATCH_SIZE = 1 N_CLASSES = 4 image = tf.cast(image_array, tf.float32) image = tf.image.per_image_standardization(image) image = tf.reshape(image, [1, 208, 208, 3]) logit = model.cnn_inference(image, BATCH_SIZE, N_CLASSES) logit = tf.nn.softmax(logit) x = tf.placeholder(tf.float32, shape=[208, 208, 3]) logs_train_dir = 'log/' saver = tf.train.Saver() with tf.Session() as sess: print("从指定路径中加载模型...") ckpt = tf.train.get_checkpoint_state(logs_train_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('模型加载成功, 训练的步数为: %s' % global_step) else: print('模型加载失败,checkpoint文件没找到!') prediction = sess.run(logit, feed_dict={x: image_array}) max_index = np.argmax(prediction) workbook.close() def evaluate_images(test_img): coord = tf.train.Coordinator() threads = tf.train.start_queue_runners(coord=coord) for index,img in enumerate(test_img): image = Image.open(img) image = image.resize([208, 208]) image_array = np.array(image) tf.compat.v1.threading.Thread(target=evaluate_one_image, args=(image_array, index)).start() coord.request_stop() coord.join(threads) if __name__ == '__main__': test_dir = 'data/test/' import glob import xlwt test_img = glob.glob(test_dir + '*.jpg') evaluate_images(test_img)
07-08
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值