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

相关推荐
QC777LX8 分钟前
运营岗位怎么借AI提升内容、活动和复盘效率?
大数据·人工智能
小李@Free2FA9 分钟前
AI Agent 在操作你的账号时,2FA 怎么配合
人工智能·ai编程·2fa·账号安全·二次验证
CV-Climber44 分钟前
检索技术类型(retrieval techniques)
人工智能
liuliuqiqirr1 小时前
2026最新5款AI编程工具平替实测深度对比
人工智能
m0_466525291 小时前
WAIC 2026 现场速览 | 聚焦AI+医疗!拆解东软添翼AI 2.0解决三甲医院四大院内难题
人工智能
新知图书1 小时前
工作流编排
人工智能·agent·ai agent·智能体·扣子
mit6.8241 小时前
gpt5.6sol 的删库跑路
人工智能
艾醒2 小时前
2026年第29周(7.13-7.19)AI全复盘:技术突破、行业趣闻翻车、算力服务器商业动态
人工智能·算法
tntxia2 小时前
SwinTransformer
人工智能