prompt工程起步

embedded/2025/3/17 4:42:40/

1.手工提示词

有关CLIP和ActionClip的手工特征,也是一个进步。通过给标签填入不同的修饰语当中,组成一段话来,来增强语义理解

python">def text_prompt(data):text_aug = [f"a photo of action {{}}", f"a picture of action {{}}", f"Human action of {{}}", f"{{}}, an action",f"{{}} this is an action", f"{{}}, a video of action", f"Playing action of {{}}", f"{{}}",f"Playing a kind of action, {{}}", f"Doing a kind of action, {{}}", f"Look, the human is {{}}",f"Can you recognize the action of {{}}?", f"Video classification of {{}}", f"A video of {{}}",f"The man is {{}}", f"The woman is {{}}"]text_dict = {}num_text_aug = len(text_aug)for ii, txt in enumerate(text_aug):text_dict[ii] = torch.cat([clip.tokenize(txt.format(c)) for i, c in data.classes])classes = torch.cat([v for k, v in text_dict.items()])return classes, num_text_aug,text_dict

这个就是一个手工提示词,针对于特殊的任务设计出来的。text_dict就是蕴含着一个模板是键,对应不同的一句话!

应用为:

python">text_id = numpy.random.randint(num_text_aug,size=len(list_id))#类别长度 上限#text_dict[j][i, :]:根据 text_id 中的索引 j 从 text_dict 中选择对应的编码张量,再根据 list_id 中的索引 i 从该张量中选择一行。texts = torch.stack([text_dict[j][i,:] for i,j in zip(list_id,text_id)])

将输入标签随机选一个模板与视频进行对比学习!

2.自动生成提示

(1) 视觉特征的提取
首先,从视频中提取视觉特征。这些特征可以是视频帧的特征,也可以是视频的整体特征。视觉特征的提取通常使用预训练的视觉模型(如 CLIP 的视觉编码器)来完成。

(2) 文本特征的初始化
文本特征可以是类别标签的嵌入,也可以是其他与任务相关的文本信息。这些文本特征将作为提示的初始输入。

(3)跨模态注意力机制

python">class MulitHeadAttention(nn.Module):def __init__(self, dim, num_heads=8, qkv_bias=False, qk_scale=None, attn_drop=0., proj_drop=0.):super().__init__()self.num_heads = num_headshead_dim = dim // num_headsself.scale = qk_scale or head_dim ** -0.5self.q_proj = nn.Linear(dim, dim, bias=qkv_bias)self.k_proj = nn.Linear(dim, dim, bias=qkv_bias)self.v_proj = nn.Linear(dim, dim, bias=qkv_bias)self.attn_drop = nn.Dropout(attn_drop)self.proj = nn.Linear(dim, dim)self.proj_drop = nn.Dropout(proj_drop)def forward(self, q, k, v):B, N, C = q.shapeB, M, C = k.shapeq = self.q_proj(q).reshape(B, N, self.num_heads, C // self.num_heads).permute(0,2,1,3)k = self.k_proj(k).reshape(B, M, self.num_heads, C // self.num_heads).permute(0,2,1,3)v = self.v_proj(v).reshape(B, M, self.num_heads, C // self.num_heads).permute(0,2,1,3)attn = (q @ k.transpose(-2, -1)) * self.scaleattn = attn.softmax(dim=-1)attn = self.attn_drop(attn)x = (attn @ v).transpose(1, 2).reshape(B, N, C)x = self.proj(x)x = self.proj_drop(x)return x

(4) PromptGeneratorLayer(本质上是一个transfomer编码器)

python">class PromptGeneratorLayer(nn.Module):def __init__(self,d_model,nhead,dropout=0.,):super().__init__()self.cross_attn = MulitHeadAttention(d_model, nhead, proj_drop=dropout)self.norm1 = nn.LayerNorm(d_model)self.norm3 = nn.LayerNorm(d_model)self.dropout = nn.Dropout(dropout)self.mlp = nn.Sequential(nn.Linear(d_model, d_model * 4),QuickGELU(),nn.Dropout(dropout),nn.Linear(d_model * 4, d_model))def forward(self, x, visual):q = k = v = self.norm1(x)x = x + self.cross_attn(q, visual, visual)x = x + self.dropout(self.mlp(self.norm3(x)))return x

(5)融合

python">class VideoSpecificPrompt(nn.Module):def __init__(self, layers=2, embed_dim=512, alpha=0.1,):super().__init__()self.norm = nn.LayerNorm(embed_dim)self.decoder = nn.ModuleList([PromptGeneratorLayer(embed_dim, embed_dim//64) for _ in range(layers)])self.alpha = nn.Parameter(torch.ones(embed_dim) * alpha)self.apply(self._init_weights)def _init_weights(self, m):if isinstance(m, nn.Linear):trunc_normal_(m.weight, std=.02)if isinstance(m, nn.Linear) and m.bias is not None:nn.init.constant_(m.bias, 0)elif isinstance(m, nn.LayerNorm):nn.init.constant_(m.bias, 0)nn.init.constant_(m.weight, 1.0)def forward(self, text, visual):B, N, C = visual.shapevisual = self.norm(visual)for layer in self.decoder:text = layer(text, visual)

应用:

python">def generate_text(data):text_aug = f"{{}}"classes = torch.cat([clip.tokenize(text_aug.format(c), context_length=77) for i, c in data.classes])return classes
python">  def classes(self):classes_all = pd.read_csv(self.labels_file)return classes_all.values.tolist()

class是:classes 是一个 torch.Tensor 类型的对象,其形状为 (n, 77),其中 n 是类别名称的数量,每一行代表一个类别名称经过 CLIP 分词后的结果。


http://www.ppmy.cn/embedded/173245.html

相关文章

QT:非模态使用WA_DeleteOnClose避免内存泄漏

connect(ui->actionnewFile,&QAction::triggered,this,[](){QDialog*dlg new QDialog(this);//dlg.exec();dlg->show();dlg->setAttribute(Qt::WA_DeleteOnClose);qDebug()<<"打开对话框";}); 1. QDialog* dlg new QDialog(this); - 创建了…

java手机号、邮箱、日期正则表达式

Java正则核心API Java中用 java.util.regex 包的两个类&#xff1a; Pattern&#xff1a;编译正则表达式Matcher&#xff1a;执行匹配操作 1. 验证手机号 String regex "1[3-9]\\d{9}"; boolean isValid "18812345678".matches(regex); // true2. 提取…

【BP神经网络】实战

1.参考Python实战&#xff1a;BP神经网络_bp神经网络实战python-CSDN博客 2.实践 &#xff08;1&#xff09;运行环境 anocanda Powershell Prompt&#xff08;anocanda3&#xff09; &#xff08;2&#xff09;创建虚拟环境&#xff0c;解决安装包的版本问题 *打开终端&a…

【小沐学Web3D】three.js 加载三维模型(React)

文章目录 1、简介1.1 three.js1.2 react.js 2、three.js React结语 1、简介 1.1 three.js Three.js 是一款 webGL&#xff08;3D绘图标准&#xff09;引擎&#xff0c;可以运行于所有支持 webGL 的浏览器。Three.js 封装了 webGL 底层的 API &#xff0c;为我们提供了高级的…

Javascript基础语法详解

面向对象的语言.脚本语言,不需要编译,浏览器解释即可运行 .用于控制网页的行为.浏览器的source可以打断点调试, console输入代码可以执行 use strict指令: 在“严格模式”下运行js代码, 防止意外创建全局变量等, 提高代码安全性和执行效率. 使用: 全局严格模式&#xff1a;…

Qt 信号与槽机制

1. 信号 和 槽 Qt 信号与槽机制 是一种用于对象间通信的低耦合设计模式&#xff0c;核心思想是&#xff1a;当某个信号触发&#xff0c;自动调用预先关联的处理函数&#xff08;槽函数&#xff09;。 在 Qt 中&#xff0c;如果一个类需要使用信号与槽机制&#xff0c;则该类必…

Redis 数据结构及使用场景介绍

Redis 是一种高性能的键值存储数据库&#xff0c;支持多种数据结构&#xff0c;每种数据结构都有其独特的特点和使用场景。以下是 Redis 的主要数据结构及其深入解析和实际使用场景介绍&#xff1a; 1.String&#xff08;字符串&#xff09; 特点 • 最基本的数据结构&#…

3.1 Spring Boot性能优化:从线程池调优到JVM参数配置

markdown # Spring Boot性能优化&#xff1a;从线程池调优到JVM参数配置![性能优化](https://img-blog.csdnimg.cn/direct/0a3e3d2e4d4b4f7f9c3d4a5b0e8d4e4c.png)## 引言 在微服务架构中&#xff0c;Spring Boot作为主流开发框架&#xff0c;其性能直接影响系统的吞吐量和响应…