当前位置:网站首页>[paddleseg source code reading] normalize operation of paddleseg transform
[paddleseg source code reading] normalize operation of paddleseg transform
2022-07-04 03:37:00 【Master Fuwen】
Connected to a ,PaddleSeg Custom data class https://blog.csdn.net/HaoZiHuang/article/details/125566058 Let's talk about it here Normalize operation
Let's write it ourselves Transform when , Always worry about some details , such as :
- np.uint8 Did you turn , Do you want to change to np.float32
- Have you done it yet (
/255
Did you do it ) - With or without subtracting 0.5, Then divide by 0.5, Move it to -1 To 1
- Before sending data , Is there any transpose And so on.
- When will numpy.ndarray Initialize to paddle.Tensor
Last one PaddleSeg Speaking of , stay Compose ending , Can convert the number of input channels (Transpose), And look at the source code :
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, )
This is instantiation ,Dataset and DataLoader Part of , Even though eval_dataset
Every data returned is np.ndarray, however paddle.io.DataLoader
This class , Will go straight back to Paddle.Tensor Class , That is, there is no need to do paddle.to_tensor
operation
OK, The next three items :
- np.uint8 Did you turn , Do you want to change to np.float32
- Have you done it yet
- With or without subtracting 0.5, Then divide by 0.5, Move it to -1 To 1
Namely Normalize Part of the operation , Take a look at the source code of its implementation
paddleseg\transforms\transforms.py
in Normalize
class
@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
in normalize
function
def normalize(im, mean, std):
im = im.astype(np.float32, copy=False) / 255.0
im -= mean
im /= std
return im
You can see stay normalize
Function , Made a turn np.float32, normalization
then -0.5
after /0.5
If you are interested in these two fields :
mean = np.array(self.mean)[np.newaxis, np.newaxis, :]
std = np.array(self.std )[np.newaxis, np.newaxis, :]
Print it yourself , It's the original array The dimension is preceded by two 1
shape from (3,) Turned into (1, 1, 3)
and np.newaxis
In fact, that is None
(np.newaxis == None) == True
If the Normalize __init__ Function reduce Interested in :
Look at this :
functools Under the reduce function
边栏推荐
- Objective-C description method and type method
- 智慧地铁| 云计算为城市地铁交通注入智慧
- Calculate the odd sum of 1~n (1~100 as an example)
- MySQL one master multiple slaves + linear replication
- Have you entered the workplace since the first 00???
- Rhcsa day 3
- SQL statement strengthening exercise (MySQL 8.0 as an example)
- Future源碼一觀-JUC系列
- Is online futures account opening safe and reliable? Which domestic futures company is better?
- Redis notes (I) Linux installation process of redis
猜你喜欢
Audio and video technology development weekly | 232
选择排序与冒泡排序模板
PHP database connection succeeded, but data cannot be inserted
Ningde times and BYD have refuted rumors one after another. Why does someone always want to harm domestic brands?
AAAI2022 | Word Embeddings via Causal Inference: Gender Bias Reducing and Semantic Information Preserving
Wechat official account web page authorization
Defensive programming skills
MySQL data query optimization -- data structure of index
Package details_ Four access control characters_ Two details of protected
What are the virtual machine software? What are their respective functions?
随机推荐
If you have just joined a new company, don't be fired because of your mistakes
Slurm view node configuration information
潘多拉 IOT 开发板学习(HAL 库)—— 实验6 独立看门狗实验(学习笔记)
渗透实战-guest账户-mimikatz-向日葵-sql提权-离线解密
Jenkins configures IP address access
Contest3145 - the 37th game of 2021 freshman individual training match_ F: Smallest ball
Explain AI accelerator in detail: why is this the golden age of AI accelerator?
PID of sunflower classic
Want to do something in production? Then try these redis commands
Which product is better for 2022 annual gold insurance?
PMP 考試常見工具與技術點總結
JVM family -- monitoring tools
Love and self-discipline and strive to live a core life
warning: LF will be replaced by CRLF in XXXXXX
Package details_ Four access control characters_ Two details of protected
[UE4] parse JSON string
System integration meets the three business needs of enterprises
Easy to win insert sort
Learning video website
2006 translation