日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

使用einops簡化數據維度操作

作者:萬里鵬程轉瞬至 更新時間: 2022-09-22 編程語言

einops主要實現了三個函數:rearrange、reduce、repeat。rearrange用于重排數據,reduce用于對數據進行sum、mean、max等統計,repeat用于重復數據的維度。
通過使用einops的函數可以便捷的對數據的維度僅修改操作,此外einops的數據操作還可以在神經網絡模型的forward中生效。
用法參考:http://einops.rocks/1-einops-basics/
官網地址:http://einops.rocks/

einops里面的表達式對應著ims.shape 的具體值,bhwcd等各種字母并無實際含義,只是說第0個字母對應著ims.shape[0],第i個字母對應著ims.shape[i]。例如img的shape為100,300,4,語句rearrange(ims, ‘a b c -> b a c’) ;則a表示100,b表示300,c表示4。同時表達式中用括號()包裹在一起的,表示一個數。就如rearrange(ims, ‘b h w c -> (b h) w c’)里面的(b h)其實表示b*h的值。

1、安裝einops

pip install einops

2、Rearrange

Rearrange操作本質是保證數據總量不變,調整順序重新排列

2.1 維度順序交換

交換不同維度的順序,相當于torch.transpose()。可以安裝個人需求任意交換數據的維度順序。

rearrange(ims, 'h w c -> w h c')
rearrange(ims, 'h w c -> c w h')

2.2 維度間自由組合

實現維度的壓縮與拉伸,可以實現torch.flatten(), torch.reshape等功能

rearrange(ims, 'b h w c -> (b h) w c')
rearrange(ims, 'b h w c -> h (b w) c')

2.3 維度填充或刪除

實現1維度的添加與刪除,可以實現類似torch.squeeze(),torch.unsqueeze()的功能

rearrange(ims, 'h w c -> 1 h w c')
rearrange(ims, '1 h w c -> h w c')

2.4 空間的自由組合

可以自由劃定維度進行重排序,基于此可以實現圖像的切片與切片還原。

基于變量實現數據的切片

pads = rearrange(pad, '(a1 h) (a2 w) c -> (a1 a2) h w c',a1=2,a2=2) #可以實現圖像的切片
pads_megre = rearrange(pads, '(a1 a2) h w c -> (a1 h) (a2 w) c',a1=2,a2=2) #可以實現圖像切片的還原

基于常量實現數據的切片

pads = rearrange(pad, '(2 h) (2 w) c -> (2 2) h w c') 
pads_megre = rearrange(pads, '(2 2) h w c -> (2 h) (2 w) c') 

2.5 維度內部的重排序

實現shufflenet的維度重排序。這里可以只寫一個變量,因為c2的值可以根據c1推斷出來

rearrange(ims, 'b (c1 c2) h w -> b (c2 c1) h w', c1=groups) 

3、reduce

3.1 在維度上進行reduce

mean操作

reduce(ims, 'b h w c -> h w c', 'mean') #在h維度求均值
reduce(ims, 'b h w c -> b c', 'mean') #在h、w維度求均值(相當于全局平均池化)
reduce(ims, 'b h w c -> c', 'mean') #在h、w、c維度求均值

sum操作

reduce(ims, 'b h w c -> h w c', 'sum') #在h維度求sum
reduce(ims, 'b h w c -> b c', 'sum') #在h、w維度求sum
reduce(ims, 'b h w c -> c', 'sum') #在h、w、c維度求sum

max操作

reduce(ims, 'b h w c -> h w c', 'max') #在h維度求max
reduce(ims, 'b h w c -> b c', 'max') #在h、w維度求max(相當于全局最大池化)
reduce(ims, 'b h w c -> c', 'max') #在h、w、c維度求max

3.2 在空間上進行reduce

常量寫法

reduce(ims, 'b (2 h) (2 w) c -> h w c', 'mean') #在hw維度上劃分2x2的空間求均值

變量寫法

reduce(ims, 'b (a1 h) (a2 w) c -> h w c', 'mean',a1=2,a2=2) #在hw維度上劃分2x2的空間求均值

此外,sum、max都是這種寫法

3.3 保留維度進行reduce

保留數據維度的長度進行reduce操作

reduce(ims, 'b h w c -> b () w c', 'max') #在h維度求均值,并保持括號的維度為1
reduce(ims, 'b h w c -> b () () c', 'max') #在hw維度求均值,并保持括號的維度為1
reduce(ims, 'b h w c -> () () () c', 'max') #在bhw維度求均值,并保持括號的維度為1

4、repeat

可以將特定維度的數據進行重復,可以基于此實現圖像的上采樣

repeat(ims, 'h w c -> h (a w) c', a=3) #將w維度重復三次
repeat(ims, 'h w c -> (a1 h) (a2 w) c', a1=2,a2=3) #將h維度重復兩次,將w維度重復三次。類似于實現圖像的放大

通常與reduce組合使用,形成一種給圖像添加馬賽克的效果(本質就是先縮小圖片,再重排序后放大圖片)

averaged = reduce(ims, 'b (h h2) (w w2) c -> b h w c', 'mean', h2=10, w2=8)
repeat(averaged, 'b h w c -> (h h2) (b w w2) c', h2=10, w2=8)

5、實現圖片的切片與還原

基于rearrange的操作可以實現對圖片的切片與切片的還原,這里需要注意的是,圖像的尺寸不能整除patch size時,需要進行填充。

import math
import numpy as np
img=np.random.random((2340,3402))
b_h=500
b_w=500
h=math.ceil(img.shape[0]/b_h)*b_h
w=math.ceil(img.shape[1]/b_w)*b_w
print(img.shape,h,w)

#對原圖像進行填充
pad=np.zeros((h,w,3),dtype=img.dtype)
pad[:img.shape[0],:img.shape[1]]=img
print(pad.shape)#(4000, 3000, 3)

pads = rearrange(pad, '(a1 h) (a2 w) c -> (a1 a2) h w c',a1=int(pad.shape[0]/b_h),a2=int(pad.shape[1]/b_w)) 
print(pads.shape)#(48, 500, 500, 3)
pads_megre = rearrange(pads, '(a1 a2) h w c -> (a1 h) (a2 w) c',a1=int(pad.shape[0]/b_h),a2=int(pad.shape[1]/b_w)) 
print(pads_megre.shape)#(4000, 3000, 3)

diff=np.abs(pads_megre-pad)
print(diff.sum())#0

6、在深度學習中的用法

可以使用Rearrange, Reduce等來構建layer,支持chainer、gluon、keras、tensorflow、torch

from einops.layers.chainer import Rearrange, Reduce,WeightedEinsum
from einops.layers.gluon import Rearrange, Reduce,WeightedEinsum
from einops.layers.keras import Rearrange, Reduce,WeightedEinsum
from einops.layers.tensorflow import Rearrange, Reduce,WeightedEinsum
from einops.layers.torch import Rearrange, Reduce,WeightedEinsum

通過以上方式導入的Rearrange, Reduce,WeightedEinsum相當于深度學習框架中的layer對象,相關用法可見下圖
在這里插入圖片描述

在pytorch中的一些用法http://einops.rocks/pytorch-examples.html

恒等變化,通常用于深度學習中

Rearrange('...->...') # identity

此外,einops還支持einsum操作,具體可以參考http://einops.rocks/3-einmix-layer/

在這里插入圖片描述

原文鏈接:https://blog.csdn.net/a486259/article/details/126966772

欄目分類
最近更新