VIT中的einops包详解

'''einops有三个常用方法:rearrange,repeat,reduce'''

rearrange的操作相当于转置

rearrange(image,'h w c -> w h c') 高和宽转置

python 复制代码
path = '../data/cat_and_mouse.jpg'
image = cv2.imread(path)
h,w,c = image.shape # shape第一个值是h,第二个是w
image = cv2.resize(image,(w*2,h*2)) #resize处(w,h)
print(image.shape)
cv2.imshow('image',image)
cv2.waitKey()
cv2.destroyAllWindows()

原图(h,w,c) = (281,500,3) 为了后面将图片一分为2的操作,把图片尺寸放大两倍:

(h2,w 2,c) = (562,1000,3)

python 复制代码
# 1.变换 h,w,c的位置: 如把h,w,c 变成 w,h,c
image_wh = rearrange(image,'h w c -> w h c')
print(image_wh.shape)
cv2.imshow('image_wh',image_wh)
cv2.imwrite('../data/cat_and_mouse_new.jpg',image_wh)
cv2.waitKey()
cv2.destroyAllWindows()

(h,w,c) = (1000,562,3)

image = rearrange(image,'h (a w) c -> w (a h) c',a=2) 是将宽一分为2,变成两个图片,然后两个图片分别转置

h (2 w) c = (562,2x500,3)是把图片的按宽的方向一分为2,变成两个图片。w原来是1000,(2 w) 将w变成500。

将一分为2的两个图片转置。由(h,2w,c) = (562,2x500,3)变成 w, (2 h) c = (500,562,3) 拼接 (500,562,3) = (500,562x2,3)

repeat操作

repeat是指定张量在哪个维度上复制。

csharp 复制代码
image = repeat(image, 'h w c -> h (repeat w) c', repeat=2)

如在宽上复制:

reduce操作

详见:

https://www.cnblogs.com/c-chenbin/p/15375637.html

相关推荐
AI多Agent协作实战派6 小时前
【AI探索历程17】从“给自己用“到想“让别人用“
人工智能
吴佳浩7 小时前
Memory Provider 实战:Hermes、Mem0、Honcho、Hindsight 为什么都这样设计?
人工智能·agent·ai编程
吴佳浩7 小时前
企业级 Agent Memory 选型指南:如何构建可扩展的 Memory Service?
人工智能·agent·ai编程
北方的银狐-Zero8 小时前
ERP 管执行,数据管标准,OntoL本体 管思考:企业智能化升级的规划图
大数据·人工智能·本体论
浅安的邂逅9 小时前
260918-白帽把 OpenAI 论坛“打穿“了:攻防赛漏洞、账户被 Claude 入侵、模型偷偷掩盖不当行为
人工智能·大模型·ai编程·ai模型·行业动态
jinyishu_9 小时前
认识 AI Agent:概念、架构、设计模式与主流框架
人工智能
Hrain-AI9 小时前
AI 爬虫三档授权工程落地:搜索放行、训练拦截、Agent 分层(附脚本)
人工智能·elasticsearch·milvus
sunhy_csdn9 小时前
“词码”作为 Token 候选译名
人工智能
Hrain-AI9 小时前
多 Agent 并行不打架:worktree 隔离与反馈回流落地(附脚本)
网络·数据库·人工智能·架构