当前位置: 首页 > wzjs >正文

网站开发需要准备什么软件企业策划方案怎么做

网站开发需要准备什么软件,企业策划方案怎么做,沈阳网站排名公司,建设部注册中心网站本期关键词:Conditional DDPM、Class Embedding、Label Control、CIFAR-10 条件生成 什么是条件生成(Conditional Generation)? 在标准的 DDPM 中,我们只是“随机生成”图像。 如果我想让模型生成「小狗」怎么办&…

本期关键词:Conditional DDPM、Class Embedding、Label Control、CIFAR-10 条件生成

什么是条件生成(Conditional Generation)?

在标准的 DDPM 中,我们只是“随机生成”图像。

如果我想让模型生成「小狗」怎么办?

这就要给模型添加“引导”——标签或文字,这种方式就叫 条件生成(Conditional Generation)

条件扩散的原理是什么?

我们要将类别信息 y 加入到模型中,使预测的噪声满足条件:

也就是说,模型要知道当前是“第几类”的图像,从而引导去噪方向。

实现思路:

  1. 将标签 y 进行嵌入(embedding);

  2. 将其与时间步编码、图像特征一起送入网络中。

修改 UNet 支持条件标签

我们对 UNet 加一点“料”——标签 embedding。

class ConditionalUNet(nn.Module):def __init__(self, num_classes, time_dim=256):super().__init__()self.time_embed = nn.Sequential(SinusoidalPositionEmbeddings(time_dim),nn.Linear(time_dim, time_dim),nn.ReLU())self.label_embed = nn.Embedding(num_classes, time_dim)self.conv0 = nn.Conv2d(3, 64, 3, padding=1)self.down1 = Block(64, 128, time_dim)self.down2 = Block(128, 256, time_dim)self.bot = Block(256, 256, time_dim)self.up1 = Block(512, 128, time_dim)self.up2 = Block(256, 64, time_dim)self.final = nn.Conv2d(64, 3, 1)def forward(self, x, t, y):t_embed = self.time_embed(t)y_embed = self.label_embed(y)cond = t_embed + y_embed  # 条件融合x0 = self.conv0(x)x1 = self.down1(x0, cond)x2 = self.down2(x1, cond)x3 = self.bot(x2, cond)x = self.up1(torch.cat([x3, x2], 1), cond)x = self.up2(torch.cat([x, x1], 1), cond)return self.final(x)

 我们为标签添加了一个 nn.Embedding,并与时间编码相加作为“条件向量”注入。

修改训练函数支持 label

def get_conditional_loss(model, x_0, t, y):noise = torch.randn_like(x_0)x_t = q_sample(x_0, t, noise)pred = model(x_t, t, y)return F.mse_loss(pred, noise)

训练主循环如下:

for epoch in range(epochs):for x, y in dataloader:x = x.to(device)y = y.to(device)t = torch.randint(0, T, (x.size(0),), device=device).long()loss = get_conditional_loss(model, x, t, y)optimizer.zero_grad()loss.backward()optimizer.step()

条件生成代码:指定类别生成图像!

@torch.no_grad()
def sample_with_labels(model, label, num_samples=16, img_size=32, device='cuda'):model.eval()x = torch.randn(num_samples, 3, img_size, img_size).to(device)y = torch.tensor([label] * num_samples).to(device)for i in reversed(range(T)):t = torch.full((num_samples,), i, device=device, dtype=torch.long)noise_pred = model(x, t, y)alpha = alphas_cumprod[t][:, None, None, None]sqrt_alpha = torch.sqrt(alpha)sqrt_one_minus_alpha = torch.sqrt(1 - alpha)x_0_pred = (x - sqrt_one_minus_alpha * noise_pred) / sqrt_alphax_0_pred = x_0_pred.clamp(-1, 1)if i > 0:noise = torch.randn_like(x)beta_t = betas[t][:, None, None, None]x = sqrt_alpha * x_0_pred + torch.sqrt(beta_t) * noiseelse:x = x_0_predreturn x

可视化指定类别的生成图像

samples = sample_with_labels(model, label=3, num_samples=16)  # e.g., cat
samples = (samples.clamp(-1, 1) + 1) / 2
grid = torchvision.utils.make_grid(samples, nrow=4)plt.figure(figsize=(6, 6))
plt.imshow(grid.permute(1, 2, 0).cpu().numpy())
plt.axis('off')
plt.title("Generated Class 3 (Cat)")
plt.show()

CIFAR-10 类别索引(参考)

类别编号类别名称
0airplane
1automobile
2bird
3cat
4deer
5dog
6frog
7horse
8ship
9truck

总结

在本期中,我们学习了如何:

  • ✅ 在 UNet 中添加类嵌入;

  • ✅ 修改损失函数以支持标签;

  • ✅ 实现条件采样生成指定类别图像;

  • ✅ 可视化生成效果。

第 9 期预告:「CLIP + Diffusion」文本条件扩散!

下一期我们将解锁 文字引导生成图像 的能力,用一句话生成图像!

“一只戴着墨镜冲浪的柴犬”将成为现实!

 

http://www.dtcms.com/wzjs/422938.html

相关文章:

  • 网站运营软件全渠道营销案例
  • 网站建设维护成本海淀区seo搜索优化
  • wordpress隐藏链接查看鹤岗网站seo
  • 政府网站建设招标珠海百度关键字优化
  • 雅马哈官网摩托车价格表图片昆明seo网站建设
  • 网站建设攵金手指科杰壹陆网络营销的特征和功能
  • wordpress全站背景厦门seo结算
  • 网站建设提案小红书seo关键词优化多少钱
  • 如何自己做优惠卷网站产品推销方案
  • 许昌网站建设哪家最好百度爱采购推广一个月多少钱
  • 阿里巴巴做网站多少钱游戏推广员是诈骗吗
  • 网络公司有哪些职位seo技术培训教程
  • 企业网站推广属于付费广州百度推广优化
  • 开发网站和app做网站公司哪家比较好
  • 怎么看别人网站是怎么做的360手机助手
  • 快速建设网站服务百度经验官网登录
  • angular做门户网站推特是谁的公司
  • 买个人家的网站绑定自己的域名semester是什么意思
  • 网站免费申请建站黄冈便宜的网站推广怎么做
  • 做直播网站赚钱吗微博营销策略
  • 自己做视频网站能赚钱吗新闻最近的大事10件
  • 国内外基于vue框架的网站建设现状杭州网络推广有限公司
  • 丹灶网站设计域名备案查询
  • flash软件做的网站最后需要用dreamweaver调整搜索排名优化
  • 动态网站开发难吗中国教师教育培训网
  • 环保公司网站架构怎么做谷歌优化的最佳方案
  • 在线看视频网站怎么做的品牌公关
  • 免费wordpress响应式企业主题广东seo推广外包
  • 上海专业网站建站竞价排名推广
  • 网站制作语言seo推广灰色词