系列文章目录(共五章33节已完结)
第一章deeplabv3+源码之慢慢解析 根目录(1)main.py–get_argparser函数
第一章deeplabv3+源码之慢慢解析 根目录(2)main.py–get_dataset函数
第一章deeplabv3+源码之慢慢解析 根目录(3)main.py–validate函数
第一章deeplabv3+源码之慢慢解析 根目录(4)main.py–main函数
第一章deeplabv3+源码之慢慢解析 根目录(5)predict.py–get_argparser函数和main函数
第二章deeplabv3+源码之慢慢解析 datasets文件夹(1)voc.py–voc_cmap函数和download_extract函数
第二章deeplabv3+源码之慢慢解析 datasets文件夹(2)voc.py–VOCSegmentation类
第二章deeplabv3+源码之慢慢解析 datasets文件夹(3)cityscapes.py–Cityscapes类
第二章deeplabv3+源码之慢慢解析 datasets文件夹(4)utils.py–6个小函数
第三章deeplabv3+源码之慢慢解析 metrics文件夹stream_metrics.py–StreamSegMetrics类和AverageMeter类
第四章deeplabv3+源码之慢慢解析 network文件夹(1)backbone文件夹(a1)hrnetv2.py–4个函数和可执行代码
第四章deeplabv3+源码之慢慢解析 network文件夹(1)backbone文件夹(a2)hrnetv2.py–Bottleneck类和BasicBlock类
第四章deeplabv3+源码之慢慢解析 network文件夹(1)backbone文件夹(a3)hrnetv2.py–StageModule类
第四章deeplabv3+源码之慢慢解析 network文件夹(1)backbone文件夹(a4)hrnetv2.py–HRNet类
第四章deeplabv3+源码之慢慢解析 network文件夹(1)backbone文件夹(b1)mobilenetv2.py–2个类和2个函数
第四章deeplabv3+源码之慢慢解析 network文件夹(1)backbone文件夹(b2)mobilenetv2.py–MobileNetV2类和mobilenet_v2函数
第四章deeplabv3+源码之慢慢解析 network文件夹(1)backbone文件夹(c1)resnet.py–2个基础函数,BasicBlock类和Bottleneck类
第四章deeplabv3+源码之慢慢解析 network文件夹(1)backbone文件夹(c2)resnet.py–ResNet类和10个不同结构的调用函数
第四章deeplabv3+源码之慢慢解析 network文件夹(1)backbone文件夹(d1)xception.py–SeparableConv2d类和Block类
第四章deeplabv3+源码之慢慢解析 network文件夹(1)backbone文件夹(d2)xception.py–Xception类和xception函数
第四章deeplabv3+源码之慢慢解析 network文件夹(2)_deeplab.py–ASPP相关的4个类和1个函数
第四章deeplabv3+源码之慢慢解析 network文件夹(3)_deeplab.py–DeepLabV3类,DeepLabHeadV3Plus类和DeepLabHead类
第四章deeplabv3+源码之慢慢解析 network文件夹(4)modeling.py–5个私有函数(4个骨干网,1个模型载入)
第四章deeplabv3+源码之慢慢解析 network文件夹(5)modeling.py–12个调用函数
第四章deeplabv3+源码之慢慢解析 network文件夹(6)utils.py–_SimpleSegmentationModel类和IntermediateLayerGetter类
第五章deeplabv3+源码之慢慢解析 utils文件夹(1)ext_transforms.py.py–2个翻转类和ExtCompose类
第五章deeplabv3+源码之慢慢解析 utils文件夹(2)ext_transforms.py.py–2个裁剪类和2个缩放类
第五章deeplabv3+源码之慢慢解析 utils文件夹(3)ext_transforms.py.py–旋转类,填充类,张量转化类和标准化类
第五章deeplabv3+源码之慢慢解析 utils文件夹(4)ext_transforms.py.py–ExtResize类,ExtColorJitter类,Lambda类和Compose类
第五章deeplabv3+源码之慢慢解析 utils文件夹(5)loss.py–FocalLoss类
第五章deeplabv3+源码之慢慢解析 utils文件夹(6)scheduler.py–PolyLR类
第五章deeplabv3+源码之慢慢解析 utils文件夹(7)utils.py–去标准化,momentum设定,标准化层锁定和路径创建
第五章deeplabv3+源码之慢慢解析 utils文件夹(8)visualizer.py–Visualizer类(完结)
文章目录
说明
- utils.py是datasets文件夹中的最后一个文件,涉及的是6个具体功能的小函数。功能相对单一明确,因此,一次说完。
- 在network和utils两个文件夹下面都有各自的utils.py文件,都是放一些具体的小函数,但这些之间没有什么联系,大家不要看名字以为是一回事。
- 最有意思的一个事情,愚以为,本utils.py貌似没有被源代码使用过!!!所以新手朋友们只是作为学习连看看,时间不够的,跳过即可。例如VOC数据集使用的是from torchvision.datasets.utils import download_url, check_integrity而不是本段代码的。这也解释了本代码段中一些不合理的地方,之前在这里纠结过好久。如有大神路过,也请指点一二。
- 本段代码中很多想不明白的地方,可以参考torchvision.datasets.utils。个人推测(没追溯过,很多代码都有这几段),这个utils.py就是参照torchvision.datasets.utils写的,最后又没用。
utils.py导入
import os
import os.path
import hashlib #hashlib 是一个提供了一些流行的hash(摘要)算法的Python标准库.其中所包括的算法有 md5, sha1, sha224, sha256, sha384, sha512等。后补链接。
import errno #即标准的errno系统符号,其数字代码所对应的文字描述可通过os.strerror来获取。后补链接。
from tqdm import tqdm #进度条库,很常见了,新手同学看后补链接吧。
- 导入部分,按注释理解即可。
- 后续6个小函数,有些地方需要在理解了导入部分才能明白,建议先搞清楚自己不熟悉的导入库,再开始接下来的内容。
1. 进度条更新gen_bar_updater函数
提示:在下文download_url函数中使用此功能。这个函数是一个经典函数,很多地方搜得到。但必须注意,真正应用中,需要指定这三个参数count, block_size, total_size。
def gen_bar_updater(pbar): #在下文download_url函数中使用此功能。
def bar_update(count, block_size, total_size): #没有指定这三个参数的话,这段函数没有办法实用。
if pbar.total is None and total_size: #进度条的长度为None,且total_size不为零,则赋值。
pbar.total = total_size #pbar.total是指进度条预设的最大迭代次数。pbar.n是迭代数据的个数。如进度条是20,但迭代数据是10个,则10/20就结束了。如进度条是10,但迭代数据是20个,则10/10之后还会有11-20,只是这一轮不显示了。
progress_bytes = count * block_size #进度=count次数*block_size(每次)块的长度。
pbar.update(progress_bytes - pbar.n) #每(progress_bytes - pbar.n)次更新一下进度条。
return bar_update
2.检查完整性check_integrity函数
提示:即数据校验。在下文download_url函数中使用此功能。
def check_integrity(fpath, md5=None): #md5校验数据
if md5 is None:
return True
if not os.path.isfile(fpath): #路径
return False
md5o = hashlib.md5() #按hashlib的补充链接理解。
with open(fpath, 'rb') as f: #以下完成数据校验过程。
# read in 1MB chunks
for chunk in iter(lambda: f.read(1024 * 1024), b''):
md5o.update(chunk)
md5c = md5o.hexdigest()
if md5c != md5:
return False
return True
3. 创建文件夹 makedir_exist_ok函数
提示:创建文件夹。
def makedir_exist_ok(dirpath):
"""
Python2 support for os.makedirs(.., exist_ok=True)
"""
try:
os.makedirs(dirpath) #创建文件夹
except OSError as e:
if e.errno == errno.EEXIST: #已经存在
pass
else:
raise
4. 下载download_url函数
提示:下载函数是这文件的重点。
def download_url(url, root, filename=None, md5=None):
"""Download a file from a url and place it in root.
Args:
url (str): URL to download file from
root (str): Directory to place downloaded file in
filename (str): Name to save the file under. If None, use the basename of the URL
md5 (str): MD5 checksum of the download. If None, do not check
"""
from six.moves import urllib #网友指出:six.moves 是用来处理那些在python2 和 3里面函数的位置有变化的,直接用six.moves就可以屏蔽掉这些变化。
root = os.path.expanduser(root)
if not filename:
filename = os.path.basename(url)
fpath = os.path.join(root, filename)
makedir_exist_ok(root) #本文的makedir_exist_ok函数。
# downloads file
if os.path.isfile(fpath) and check_integrity(fpath, md5):
print('Using downloaded and verified file: ' + fpath)
else:
try:
print('Downloading ' + url + ' to ' + fpath)
urllib.request.urlretrieve(
url, fpath,
reporthook=gen_bar_updater(tqdm(unit='B', unit_scale=True)) #进度条,参考本文第一个函数gen_bar_updater。
)
except OSError:
if url[:5] == 'https':
url = url.replace('https:', 'http:')
print('Failed download. Trying https -> http instead.'
' Downloading ' + url + ' to ' + fpath)
urllib.request.urlretrieve(
url, fpath,
reporthook=gen_bar_updater(tqdm(unit='B', unit_scale=True))
)
5. 列举路径list_dir函数
提示:列举路径函数。
def list_dir(root, prefix=False):
"""List all directories at a given root
Args:
root (str): Path to directory whose folders need to be listed
prefix (bool, optional): If true, prepends the path to each result, otherwise
only returns the name of the directories found
"""
root = os.path.expanduser(root) #返回目录,可参考前文对os.path的链接,和本文的后补链接。
directories = list(
filter(
lambda p: os.path.isdir(os.path.join(root, p)),
os.listdir(root)
)
)
if prefix is True:
directories = [os.path.join(root, d) for d in directories]
return directories
6. 列举文件list_files函数
提示:列举路径内文件的函数。
def list_files(root, suffix, prefix=False):
"""List all files ending with a suffix at a given root
Args:
root (str): Path to directory whose folders need to be listed
suffix (str or tuple): Suffix of the files to match, e.g. '.png' or ('.jpg', '.png').
It uses the Python "str.endswith" method and is passed directly
prefix (bool, optional): If true, prepends the path to each result, otherwise
only returns the name of the files found
"""
root = os.path.expanduser(root)
files = list(
filter(
lambda p: os.path.isfile(os.path.join(root, p)) and p.endswith(suffix),
os.listdir(root)
)
)
if prefix is True:
files = [os.path.join(root, d) for d in files]
return files
Tips
-
补充tqdm示例链接。
-
datasets文件夹至此结束(如按实际代码运行,上一节即可结束)。下一章介绍metrics文件夹。