網站首頁 編程語言 正文
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
- 上一篇:C語言自定義類型——枚舉,聯合體
- 下一篇:YOLO v5模型的yaml文件參數理解
相關推薦
- 2022-04-08 swift自定義表格控件(UITableView)_Swift
- 2023-01-26 Python+Sklearn實現異常檢測_python
- 2024-03-28 SpringBoot項目中的500錯誤
- 2022-07-21 nginx的配置優化及經常使用的超時配置說明
- 2022-05-01 C#使用log4net打日志_C#教程
- 2022-11-25 詳解Python中的數據精度問題_python
- 2022-04-18 騰訊im中調用 setMessageRead 會話列表中的未讀消息還在存在
- 2022-06-07 C語言超詳細講解排序算法下篇_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支