当前位置:网站首页>[PaddleSeg 源码阅读] PaddleSeg Transform 的 Normalize操作
[PaddleSeg 源码阅读] PaddleSeg Transform 的 Normalize操作
2022-07-04 03:32:00 【氵文大师】
接上一篇,PaddleSeg 自定义数据类 https://blog.csdn.net/HaoZiHuang/article/details/125566058 这里再说一下 Normalize 操作
咱们自己写 Transform 时,总会担心一些细节,比如:
- np.uint8 转了没,要不要转为 np.float32
- 归一化做了没(
/255
做了没) - 用不用减去0.5,然后除以0.5,将其移至 -1 到 1
- 送入数据之前,有没有 transpose 之类的
- 何时将 numpy.ndarray 初始化为 paddle.Tensor
上一篇PaddleSeg说到,在Compose结尾,会做输入通道数的转换(Transpose),而看源代码:
batch_sampler = paddle.io.DistributedBatchSampler(
eval_dataset, batch_size=1, shuffle=False, drop_last=False)
loader = paddle.io.DataLoader(
eval_dataset,
batch_sampler=batch_sampler,
num_workers=num_workers,
return_list=True, )
这个是实例化,Dataset 和 DataLoader 的部分,尽管 eval_dataset
返回的每个数据都是 np.ndarray,但是 paddle.io.DataLoader
这个类,会直接返回 Paddle.Tensor 类的数据,也就是无需再做 paddle.to_tensor
操作
OK, 接下里的这三项:
- np.uint8 转了没,要不要转为 np.float32
- 归一化做了没
- 用不用减去0.5,然后除以0.5,将其移至 -1 到 1
就是 Normalize 部分的操作了,来看看它实现的源码
paddleseg\transforms\transforms.py
中 Normalize
类
@manager.TRANSFORMS.add_component
class Normalize:
""" Normalize an image. Args: mean (list, optional): The mean value of a data set. Default: [0.5, 0.5, 0.5]. std (list, optional): The standard deviation of a data set. Default: [0.5, 0.5, 0.5]. Raises: ValueError: When mean/std is not list or any value in std is 0. """
def __init__(self, mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)):
self.mean = mean
self.std = std
if not (isinstance(self.mean,
(list, tuple)) and isinstance(self.std,
(list, tuple))):
raise ValueError(
"{}: input type is invalid. It should be list or tuple".format(
self))
from functools import reduce
if reduce(lambda x, y: x * y, self.std) == 0:
raise ValueError('{}: std is invalid!'.format(self))
def __call__(self, im, label=None):
""" Args: im (np.ndarray): The Image data. label (np.ndarray, optional): The label data. Default: None. Returns: (tuple). When label is None, it returns (im, ), otherwise it returns (im, label). """
mean = np.array(self.mean)[np.newaxis, np.newaxis, :]
std = np.array(self.std)[np.newaxis, np.newaxis, :]
im = functional.normalize(im, mean, std)
if label is None:
return (im, )
else:
return (im, label)
paddleseg\transforms\functional.py
中 normalize
函数
def normalize(im, mean, std):
im = im.astype(np.float32, copy=False) / 255.0
im -= mean
im /= std
return im
可以看到 在 normalize
函数中,做了转 np.float32, 归一化
然后 -0.5
之后 /0.5
如果你对这两行感兴趣:
mean = np.array(self.mean)[np.newaxis, np.newaxis, :]
std = np.array(self.std )[np.newaxis, np.newaxis, :]
自己打印看看吧,就是个原来的array维度前面加了两个1
shape 从 (3,) 变成了 (1, 1, 3)
而 np.newaxis
其实就是 None
(np.newaxis == None) == True
如果对 Normalize __init__函数中的 reduce 感兴趣:
可以看看这个:
functools下的reduce函数
边栏推荐
- Stm32bug [stlink forced update prompt appears in keilmdk, but it cannot be updated]
- Enhanced for loop
- Handler source code analysis
- Development of digital collection trading platform development of digital collection platform
- [UE4] parse JSON string
- What is the difference between enterprise wechat applet and wechat applet
- 機器學習基礎:用 Lasso 做特征選擇
- MySQL query
- Examination question bank of constructor decoration direction post skills (constructor) and examination data of constructor decoration direction post skills (constructor) in 2022
- What are the conditions for the opening of Tiktok live broadcast preview?
猜你喜欢
Add token validation in swagger
@Scheduled scheduled tasks
Audio and video technology development weekly | 232
Third party login initial version
Easy to win insert sort
This function has none of DETERMINISTIC, NO SQL..... (you *might* want to use the less safe log_bin_t
Setting methods, usage methods and common usage scenarios of environment variables in postman
What is the difference between enterprise wechat applet and wechat applet
3D game modeling is in full swing. Are you still confused about the future?
Package details_ Four access control characters_ Two details of protected
随机推荐
Contest3145 - the 37th game of 2021 freshman individual training match_ G: Score
Lichuang EDA learning notes 14: PCB board canvas settings
What is cloud primordial?
Contest3145 - the 37th game of 2021 freshman individual training match_ D: Ranking
Leetcode 110 balanced binary tree
2006 translation
@Scheduled scheduled tasks
Management and thesis of job management system based on SSM
7 * 24-hour business without interruption! Practice of applying multiple live landing in rookie villages
CSP drawing
Explain AI accelerator in detail: why is this the golden age of AI accelerator?
XSS prevention
A brief talk on professional modeler: the prospect and professional development of 3D game modeling industry in China
Sword finger offer:55 - I. depth of binary tree
In my spare time, I like to write some technical blogs and read some useless books. If you want to read more of my original articles, you can follow my personal wechat official account up technology c
Have you entered the workplace since the first 00???
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
Package and download 10 sets of Apple CMS templates / download the source code of Apple CMS video and film website
JSON string conversion in unity
2022 Guangxi provincial safety officer a certificate examination materials and Guangxi provincial safety officer a certificate simulation test questions