您的位置:首页 > 编程语言 > Python开发

python基础较差的我学习关于语义分割模型训练代码

2019-05-15 17:31 369 查看

本文主要解读我遇到一个比较好的语义分割训练模型,所以我决定认真学习。
如有错误请大家多多指点,共同进步。

开始学习,主要列出我理解的程序逻辑流程,以及读不懂的语句。(以下英文解释都是我help出来的 )

  1. random.randint(0,1)

    randint(a, b) method of random.Random instance
    Return random integer in range [a, b], including both end points.
    随机在(0,1)之间返回一个正数,那么只能是0或者1。

  2. input_image = cv2.flip(input_image, 1)

    The function cv::flip flips the array in one of three different ways (row and column indices are 0-based)
    param flipCode a flag to specify how to flip the array;
    0 means flipping around the x-axis
    positive value (for example, 1) means flipping around y-axis
    Negative value (for example, -1) means flipping around both axes.
    用于输入图片的旋转,0代表沿x轴,1代表沿y轴,-1代表沿xy旋转即原点。

  3. input_image = cv2.LUT(input_image, table)

    The function LUT fills the output array with values from the look-up table. Indices of the entries are taken from the input array
    函数LUT用查找表中的值填充输出数组。项的索引是从输入数组中取出.
    创建自己的颜色表,下面是步骤:
    1、定义一个映射:色度图是从0-255值256种颜色映射。在OpenCV,我们需要创建一个大小为256×1的8位彩色图像来存储256个颜色值。
    2、对照颜色使用查找表:在OpenCV,你可以申请一个信息存储在一个256×1的彩色图像使用查找表LUT图像。

  4. M = cv2.getRotationMatrix2D((input_image.shape[1]//2, input_image.shape[0]//2), angle, 1.0)

    brief Calculates an affine matrix of 2D rotation.

getRotationMatrix2D(center, angle, scale) -> retval

@param center Center of the rotation in the source image.
. @param angle Rotation angle in degrees. Positive values mean counter-clockwise rotation (
the coordinate origin is assumed to be the top-left corner).
. @param scale Isotropic scale factor.
. @sa getAffineTransform, warpAffine, transform
对图像进行旋转任意角度。

  1. with open(csv_path, 'r') as csvfile: file_reader = csv.reader(csvfile, delimiter=',') header = next(file_reader)

    遇到了with…as…用法
    经参考:
    https://www.geek-share.com/detail/2728233041.html
    https://blog.csdn.net/mingyuli/article/details/80972135
    了解到
    with-as等价于
try:
执行 __enter__的内容
执行 with_block.
finally:

大概意思是打开文件执行操作,最后不需要再自己手动关闭,由finally执行关闭。

  1. `header = next(file_reader)``
    next(…)
    next(iterator[, default])

    Return the next item from the iterator. If default is given and the iterator
    is exhausted, it is returned instead of raising StopIteration.

next() 返回迭代器的下一个项目
例如

it = iter([1, 2, 3, 4, 5])
# 循环:
while True:
try:
# 获得下一个值:
x = next(it)
print(x)
except StopIteration:
# 遇到StopIteration就退出循环
break

输出结果为:

1
2
3
4
5

程序中使用next()目的是为了找到每一类标签RGB的值。

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
sess=tf.Session(config=config)

参考:https://blog.csdn.net/spring_willow/article/details/80143207
tf.ConfigProto()函数用在创建session的时候对session进行参数配置。
首先了解一下session
session是tensorflow框架中的一个重要机制,负责分配和管理资源。Tensorflow依赖于一个高效的C++后端来进行计算,与后端的这个连接叫做session。
在tensorflow中使用图(graph)表示计算任务,为了进行计算,图必须在会话(session)里启动,会话能够将图的节点(op)分发到CPU或GPU设备上,同时执行op方法,这些方法执行后将产生的数据(tensor)返回。会话提供了一个run方法,用于执行计算图整体或部分节点。
简单的说就是使用会话Session启动图,调用Session.run()执行操作,另外需通过上下文管理机制with管理该会话,会话结束后自动释放资源,如同前面介绍的with…as如下

with tf.Session() as sess:
sess.run(...)

回到正题 ,配置的内容最常用的两个:allow_soft_placement和log_device_placement.
1.

config = tf.ConfigProto(allow_soft_placement=True,
log_device_placement=True)
with tf.Session(config = config,...)

说明:

allow_soft_placement=True
:如果是true,则允许tensorflow自动分配设备

log_device_placement=True
:如果是true,记录每个节点分配到哪个设备上日志,用于方便调试
2.

config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.per_process_gpu_memory_fraction = 0.4
session = tf.Session(config=config)

说明:

gpu_options.allow_growth
:用于动态申请显存,从少到多慢慢增加gpu容量

gpu_options.per_process_gpu_memory_fraction
:用于限制gpu使用率,拿出40%给进程使用

参考:
https://www.geek-share.com/detail/2709203252.html

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: