einops.rearrange、repeat、reduce 对数据维度进行操作

举报
风吹稻花香 发表于 2022/04/13 00:48:26 2022/04/13
【摘要】 支持numpy和torch 目录 1.einops.rearrange 重新指定维度 2.einops.repeat 重排和重复(增加)维度 3.einops.reduce 1.einops.rearrange 重新指定维度 def rearrange(tensor, pattern, **axes_lengths)...

支持numpy和torch

目录

1.einops.rearrange 重新指定维度

2.einops.repeat 重排和重复(增加)维度

3.einops.reduce


1.einops.rearrange 重新指定维度


def rearrange(tensor, pattern, **axes_lengths):
einops.rearrange is a reader-friendly smart element reordering for multidimensional tensors. This operation includes functionality of transpose (axes permutation), reshape (view), squeeze, unsqueeze, stack, concatenate and other operations.

其中:括号代表合并


  
  1. import torch
  2. if __name__ == '__main__':
  3. # data = torch.arange(0,27,1)
  4. # print(data)
  5. import numpy as np
  6. from einops import rearrange, repeat
  7. # suppose we have a set of 32 images in "h w c" format (height-width-channel)
  8. images = [np.random.randn(30, 40, 3) for _ in range(32)]
  9. print("data shape",len(images),images[0].shape)
  10. # stack along first (batch) axis, output is a single array :(32, 30, 40, 3)
  11. print(rearrange(images, 'b h w c -> b w h c').shape)
  12. # concatenate images along height (vertical axis), 960 = 32 * 30 :(960, 40, 3)
  13. print(rearrange(images, 'b h w c -> (b h) w c').shape)
  14. # concatenated images along horizontal axis, 1280 = 32 * 40 :(30, 1280, 3)
  15. print(rearrange(images, 'b h w c -> h (b w) c').shape)
  16. # reordered axes to "b c h w" format for deep learning :(32, 3, 30, 40)
  17. print(rearrange(images, 'b h w c -> b c h w').shape)
  18. # flattened each image into a vector, 3600 = 30 * 40 * 3 :(32, 3600)
  19. print(rearrange(images, 'b h w c -> b (c h w)').shape)
  20. print(rearrange(images, 'b h w (c ph) -> b (c h) (w ph)',ph=1).shape)


# ======================================================================================================================
# 这里(h h1) (w w1)就相当于h与w变为原来的1/h1,1/w1倍
 
# split each image into 4 smaller (top-left, top-right, bottom-left, bottom-right), 128 = 32 * 2 * 2  :(128, 15, 20, 3)
print(rearrange(images, 'b (h h1) (w w1) c -> (b h1 w1) h w c', h1=2, w1=2).shape)
 
# space-to-depth operation  :(32, 15, 20, 12)
print(rearrange(images, 'b (h h1) (w w1) c -> b h w (c h1 w1)', h1=2, w1=2).shape)


2.einops.repeat 重排和重复(增加)维度


einops.repeat allows reordering elements and repeating them in arbitrary combinations. This operation includes functionality of repeat, tile, broadcast functions.


  
  1. import numpy as np
  2. from einops import rearrange, repeat,reduce
  3.  
  4. # a grayscale image (of shape height x width)
  5. image = np.random.randn(30, 40)
  6.  
  7. # change it to RGB format by repeating in each channel:(30, 40, 3)
  8. print(repeat(image, 'h w -> h w c', c=3).shape)
  9.  
  10. # repeat image 2 times along height (vertical axis):(60, 40)
  11. print(repeat(image, 'h w -> (repeat h) w', repeat=2).shape)
  12.  
  13. # repeat image 2 time along height and 3 times along width:(30, 120)
  14. print(repeat(image, 'h w -> h (repeat w)', repeat=3).shape)
  15.  
  16. # convert each pixel to a small square 2x2. Upsample image by 2x:(60, 80)
  17. print(repeat(image, 'h w -> (h h2) (w w2)', h2=2, w2=2).shape)
  18.  
  19. # pixelate image first by downsampling by 2x, then upsampling:(30, 40)
  20. downsampled = reduce(image, '(h h2) (w w2) -> h w', 'mean', h2=2, w2=2)
  21. print(repeat(downsampled, 'h w -> (h h2) (w w2)', h2=2, w2=2).shape)


3.einops.reduce


einops.reduce provides combination of reordering and reduction using reader-friendly notation.


  
  1. import numpy as np
  2. from einops import rearrange,reduce
  3.  
  4. x = np.random.randn(100, 32, 64)
  5. # perform max-reduction on the first axis:(32, 64)
  6. print(reduce(x, 't b c -> b c', 'max').shape) 
  7.  
  8. # same as previous, but with clearer axes meaning:(32, 64)
  9. print(reduce(x, 'time batch channel -> batch channel', 'max').shape)
  10.  
  11. x = np.random.randn(10, 20, 30, 40)
  12. # 2d max-pooling with kernel size = 2 * 2 for image processing:(10, 20, 15, 20)
  13. y1 = reduce(x, 'b c (h1 h2) (w1 w2) -> b c h1 w1', 'max', h2=2, w2=2)
  14. print(y1.shape)
  15.  
  16. # if one wants to go back to the original height and width, depth-to-space trick can be applied:(10, 5, 30, 40)
  17. y2 = rearrange(y1, 'b (c h2 w2) h1 w1 -> b c (h1 h2) (w1 w2)', h2=2, w2=2)
  18. print(y2.shape)
  19.  
  20. # Adaptive 2d max-pooling to 3 * 4 grid:(10, 20, 3, 4)
  21. print(reduce(x, 'b c (h1 h2) (w1 w2) -> b c h1 w1', 'max', h1=3, w1=4).shape)
  22.  
  23. # Global average pooling:(10, 20)
  24. print(reduce(x, 'b c h w -> b c', 'mean').shape)


————————————————
版权声明:本文为CSDN博主「马鹏森」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_43135178/article/details/118877384

文章来源: blog.csdn.net,作者:AI视觉网奇,版权归原作者所有,如需转载,请联系作者。

原文链接:blog.csdn.net/jacke121/article/details/124099056

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。