YOLO中的Python语句

本文涵盖Python中路径操作、数据类型转换、图像处理、深度学习框架TensorFlow的使用技巧,包括路径拼接、色彩空间转换、数据类型管理、模型训练、日志记录等功能详解。

路径拼接os.path.join()函数
os.path.join()函数用于路径拼接文件路径。 os.path.join(path1[, path2[, …]]) 把目录和文件名合成一个路径

os.path.join()函数中可以传入多个路径:

os.path.isfile(path) 判断路径是否为文件

os.path.isdir(path) 判断路径是否为目录

os.path.exists(path) 如果路径 path 存在,返回 True;如果路径 path 不存在,返回 False。

os.makedirs() 方法用于递归创建目录。像 mkdir(), 但创建的所有intermediate-level文件夹需要包含子目录。

np.random.shuffle(x) 现场修改序列,改变自身内容。(类似洗牌,打乱顺序)

图像处理中有多种色彩空间,例如 RGB、HLS、HSV、HSB、YCrCb、CIE XYZ、CIE Lab等,经常要遇到色彩空间的转化,以便生成mask图等操作.
OpenCV 颜色转换 - cv2.cvtColor() 函数

Python中与数据类型相关函数及属性有如下三个:type/dtype/astype
type() 返回参数的数据类型
dtype 返回数组中元素的数据类型
astype() 对数据类型进行转换

s.strip(rm) 删除s字符串中开头、结尾处,位于 rm删除序列的字符

pickle.dump(obj, file[, protocol])
  序列化对象,并将结果数据流写入到文件对象中。参数protocol是序列化模式,默认值为0,表示以文本的形式序列化。protocol的值还可以是1或2,表示以二进制的形式序列化。

text.lower(): 返回一个被转换为全小写字母的字符串副本。

tf.losses.add_loss() : 为loss集合添加额外定义的loss

tf.summary()的各类方法,能够保存训练过程以及参数分布图并在tensorboard显示。
tf.summary有诸多函数:
1、tf.summary.scalar
用来显示标量信息,其格式为:

tf.summary.scalar(tags, values, collections=None, name=None)

例如:tf.summary.scalar(‘mean’, mean)
一般在画loss,accuary时会用到这个函数。
2、tf.summary.histogram
用来显示直方图信息,其格式为:

tf.summary.histogram(tags, values, collections=None, name=None)

例如: tf.summary.histogram(‘histogram’, var)
一般用来显示训练过程中变量的分布情况

argparse介绍
是python的一个命令行解析包,非常编写可读性非常好的程序

tf.global_variables():使用global_variables获取全局变量

TensorFlow使用 tf.train.Saver()保存模型

tf.summary.merge_all
merge_all 可以将所有summary全部保存到磁盘,以便tensorboard显示。如果没有特殊要求,一般用这一句就可一显示训练时的各种信息了。
格式:tf.summaries.merge_all(key=‘summaries’)

tf.summary.FileWriter
指定一个文件用来保存图。
格式:tf.summary.FileWritter(path,sess.graph)

flush_secs:: Number. How often, in seconds, to flush the pending events and summaries to disk.

tf.train.create_global_step(graph=None)
Defined in tensorflow/python/training/training_util.py.
Create global step tensor in graph.

通过tf.train.exponential_decay函数实现指数衰减学习率

slim在learning.py中提供了一个简单而有用的训练模型的工具。我们只需调用slim.learning.create_train_op 和slim.learning.train就可以完成优化过程。
slim.learning.train函数被用来训练神经网络,函数定义如下:

def slim.learning.train(train_op,
logdir,
train_step_fn=train_step,
train_step_kwargs=_USE_DEFAULT,
log_every_n_steps=1,
graph=None,
master=’’,
is_chief=True,
global_step=None,
number_of_steps=None,
init_op=_USE_DEFAULT,
init_feed_dict=None,
local_init_op=_USE_DEFAULT,
init_fn=None,
ready_op=_USE_DEFAULT,
summary_op=_USE_DEFAULT,
save_summaries_secs=600,
summary_writer=_USE_DEFAULT,
startup_delay_steps=0,
saver=None,
save_interval_secs=600,
sync_optimizer=None,
session_config=None,
trace_every_n_steps=None):
其中部分参数的说明如下:
train_op: A Tensor that, when executed, will apply the gradients and return the loss value.
logdir: The directory where training logs are written to. If None, model checkpoints and summaries will not be written.检查点文件和日志文件的保存路径。
number_of_steps: The max number of gradient steps to take during training,as measured by ‘global_step’: training will stop if global_step is greater than ‘number_of_steps’. If the value is left as None, training proceeds indefinitely.默认是一致循环训练。
save_summaries_secs: How often, in seconds, to save summaries.
summary_writer: SummaryWriter to use. Can be None to indicate that no summaries should be written. If unset, we create a SummaryWriter.
startup_delay_steps: The number of steps to wait for before beginning. Note that this must be 0 if a sync_optimizer is supplied.
saver: Saver to save checkpoints. If None, a default one will be created and used.
save_interval_secs: How often, in seconds, to save the model to logdir.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值