其他
飞桨框架v2.4 API新升级!全面支持稀疏计算、图学习、语音处理等任务
一元计算: paddle.sparse.sin/sinh/tan/tanh/expm1/log1p/pow/square/sqrt/abs/cast/neg... 二元计算: paddle.sparse.add/substract/multiply/divide... 矩阵和向量计算: paddle.sparse.matmul/masked_matmul/addmm/mv... 数据变形:
paddle.sparse.transpose/reshape...
网络层 paddle.sparse.nn.Conv3D/SubmConv3D/MaxPool3D/BatchNorm... 激活层: paddle.sparse.nn.ReLU/ReLU6/LeakyReLU/Softmax...
3D点云目标检测
CenterPoint模型介绍
Sparse Transformer
coo = paddle.sparse.sparse_coo_tensor(
indices = [[0, 1, 2],
[1, 2, 0]],
values = [1., 2., 3.],
shape = [3, 3])
out = paddle.sparse.nn.functional.relu(coo)
# 稀疏CSR Tensor
csr = paddle.sparse.sparse_csr_tensor(
crows = [0, 1, 2, 3],
cols = [1, 2, 0],
values = [1., 2., 3.],
shape = [3, 3])
out = paddle.sparse.nn.functional.relu(csr)
coo = paddle.sparse.sparse_coo_tensor(
indices = [[0, 1, 2],
[1, 2, 0]],
values = [1., 2., 3.],
shape = [3, 3])
dense = paddle.rand([3, 2])
out = paddle.sparse.matmul(coo, dense)
# CSR与Dense矩阵乘,返回稠密Tensor
csr = paddle.sparse.sparse_csr_tensor(
crows = [0, 1, 2, 3],
cols = [1, 2, 0],
values = [1., 2., 3.],
shape = [3, 3])
dense = paddle.rand([3, 2])
out = paddle.sparse.matmul(csr, dense)
# Dense与Dense矩阵乘,返回稀疏Tensor
x = paddle.rand([3, 5])
y = paddle.rand([5, 4])
mask = paddle.sparse.sparse_csr_tensor(
crows = [0, 2, 3, 5],
cols = [1, 3, 2, 0, 1],
values= [1., 2., 3., 4., 5.],
shape = [3, 4])
out = paddle.sparse.masked_matmul(x, y, mask)
from paddle import sparse
from paddle.sparse import nn
class SparseBasicBlock(paddle.nn.Layer):
def __init__(
self,
in_channels,
out_channels,
stride=1,
downsample=None,
):
super(SparseBasicBlock, self).__init__()
self.conv1 = nn.SubmConv3D(
in_channels,
out_channels,
kernel_size=3,
stride=stride,
padding=1)
self.bn1 = nn.BatchNorm(out_channels, epsilon=1e-3, momentum=0.01)
self.relu = nn.ReLU()
self.conv2 = nn.SubmConv3D(
out_channels,
out_channels,
kernel_size=3,
stride=stride,
padding=1)
self.bn2 = nn.BatchNorm(out_channels, epsilon=1e-3, momentum=0.01)
self.downsample = downsample
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
identity = self.downsample(x)
out = sparse.add(out, identity)
out = self.relu(out)
return out
x = paddle.to_tensor([[0, 2, 3], [1, 4, 5], [2, 6, 7]], dtype="float32")
y = paddle.to_tensor([1, 1, 1, 1], dtype="float32")
indexes = paddle.to_tensor([[0, 1], [1, 2], [2, 1], [0, 0]], dtype="int32")
src_index, dst_index = indexes[:, 0], indexes[:, 1]
out = paddle.geometric.send_ue_recv(x, y, src_index, dst_index, message_op="add", reduce_op="sum")
from paddle.fluid import core
row = np.array([3, 7, 0, 9, 1, 4, 2, 9, 3, 9, 1, 9, 7])
colptr = np.array([0, 2, 4, 5, 6, 7, 9, 11, 11, 13, 13])
row = core.eager.to_uva_tensor(row)
colptr = core.eager.to_uva_tensor(colptr)
nodes = paddle.to_tensor([0, 8, 1, 2])
sample_size = 2
# 邻居采样API的输入要求图结构信息为CSC格式
neighbors, neighbor_count = paddle.geometric.sample_neighbors(row, colptr, nodes, sample_size=sample_size)
# 生成重编号后的边
reindex_src, reindex_dst, out_nodes = paddle.geometric.reindex_graph(nodes, neighbors, neighbor_count)
from paddle.audio.features import LogMelSpectrogram
#设置音频相关参数
sample_rate = 16000
wav_duration = 0.5
num_channels = 1
num_frames = sample_rate * wav_duration
wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1
waveform = wav_data.tile([num_channels, 1])
#设置特征提起器相关参数
feature_extractor = LogMelSpectrogram(sr=sample_rate, n_fft=512, window = 'hann', power = 1.0)
feats = feature_extractor(waveform)
#cosine窗函数示例
n_fft = 512
cosine_window = paddle.audio.functional.get_window('cosine', n_fft)
#高斯窗函数
std = 7
gaussian_window = paddle.audio.functional.get_window(('gaussian',std), n_fft)
#离散余弦变换示例
n_mfcc = 23
n_mels = 257
dct = paddle.audio.functional.create_dct(n_mfcc, n_mels)
import paddle
#设置相关参数,生成示例音频
sample_rate = 16000
wav_duration = 0.5
num_channels = 1
num_frames = sample_rate * wav_duration
wav_data = paddle.linspace(-1.0, 1.0, num_frames) * 0.1
waveform = wav_data.tile([num_channels, 1])
base_dir = os.getcwd()
filepath = os.path.join(base_dir, "test.wav")
#保存和提取音频信息
paddle.audio.save(filepath, waveform, sample_rate)
wav_info = paddle.audio.info(filepath)
#wav_info中会有sample_rate, num_frames, num_channels等信息
mode = 'dev'
esc50_dataset = paddle.audio.datasets.ESC50(mode=mode,
feat_type='raw')
for idx in range(5):
audio, label = esc50_dataset[idx]
# do something with audio, label
print(audio.shape, label)
# [audio_data_length] , label_id
esc50_dataset = paddle.audio.datasets.ESC50(mode=mode,
feat_type='mfcc',
n_mfcc=40)
for idx in range(5):
audio, label = esc50_dataset[idx]
# do something with mfcc feature, label
print(audio.shape, label)
# [feature_dim, length] , label_id
paddle.nn.functional.cosine_embedding_loss根据label类型,计算2个输入之间的CosineEmbedding损失。 paddle.nn.functional.soft_margin_loss计算输入和label间的二分类softmargin损失。 paddle.nn.functional.multi_label_soft_margin_loss计算输入和label间的多分类最大熵损失。 paddle.nn.functional.triplet_margin_loss和paddle.nn.functional.triplet_margin_with_distance_loss计算输入与正样本和负样本之间的相对相似性,后者可自定义距离计算函数。
新增paddle.sgn取复数的单位值和实数的符号。 新增paddle.count_nonzero沿给定的轴统计输入张量中非零元素的个数。 新增paddle.take将输入张量视为一维,返回指定索引上的元素集合。 新增paddle.bucketize根据给定的一维桶划分,得到输入张量对应的桶索引。 新增paddle.triu_indices和paddle.tril_indices分别取二维张量(矩阵)中上/下三角矩阵元素的行列坐标。 新增paddle.heaviside计算赫维赛德阶跃函数。 新增paddle.nanmedian和paddle.nanquantile忽略张量中的nan值,分别计算出中位数和分位数值。
paddle.vision.transforms.affine和paddle.vision.transforms.RandomAffine对图像进行仿射变换,后者使用随机产生的仿射变换矩阵参数。 paddle.vision.transforms.erase和paddle.vision.transforms.RandomErasing使用给定的值擦除输入图像中的像素,前者是选定区域,后者是随机区域。 paddle.vision.transforms.perspective和paddle.vision.transforms.RandomPerspective对图像进行透视变换,前者是选定区域,后者是随机区域,两者都可以选择插值方法。
Release Note地址
简化API开发步骤:飞桨框架v2.4完成了基础框架算子体系重构,构造高可复用的PHI算子库(Paddle HIgh reusability operator library),支持基于已有的算子内核以及Kernel Primitives API组合实现新的算子,支持插件式接入新硬件或者新加速库。PHI算子库的成熟,提升了飞桨API的开发效率,并形成了通用的API开发流程,使得开发者可以更加简洁流畅地参与飞桨API的开发与贡献。 发布标准API贡献指南:飞桨框架v2.4形成了标准的API贡献指南,包括贡献流程与操作指南、API设计文档模板、API代码模板、API文档写作规范,为飞桨社区开发者提供清晰的文档指引与辅助,使得开发者可以快速上手。 提供标准开发环境:飞桨AIStudio平台推出标准开发环境,为开发者提供飞桨镜像环境、在线IDE与专属GPU算力,登录即可开发调试,免去环境配置与算力限制,随时随地参与飞桨框架的开发与贡献。