区分stable diffusion中的通道数与张量维度

区分stable diffusion中的通道数与张量形状

  • 1.通道数:
    • 1.1 channel = 3
    • 1.2 channel = 4
  • 2.张量形状
    • 2.1 3D 张量
    • 2.2 4D 张量
      • 2.2.1 通常
      • 2.2.2 stable diffusion
  • 3.应用
    • 3.1 问题
    • 3.2 举例
    • 3.3 张量可以理解为多维可变数组

前言:通道数与张量形状都在数值3和4之间变换,容易混淆。

1.通道数:

1.1 channel = 3

RGB 图像具有 3 个通道(红色、绿色和蓝色)。

1.2 channel = 4

Stable Diffusion has 4 latent channels。
如何理解卷积神经网络中的通道(channel)

2.张量形状

2.1 3D 张量

形状为 (C, H, W),其中 C 是通道数,H 是高度,W 是宽度。这适用于单个图像。

2.2 4D 张量

2.2.1 通常

形状为 (B, C, H, W),其中 B 是批次大小,C 是通道数,H 是高度,W 是宽度。这适用于多个图像(例如,批量处理)。

2.2.2 stable diffusion

在img2img中,将image用vae编码并按照timestep加噪:

		# This code copyed from diffusers.pipline_controlnet_img2img.py# 6. Prepare latent variableslatents = self.prepare_latents(image,latent_timestep,batch_size,num_images_per_prompt,prompt_embeds.dtype,device,generator,)

image的dim(维度)是3,而latents的dim为4。
让我们先看text2img的prepare_latents函数:

    # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline.prepare_latentsdef prepare_latents(self, batch_size, num_channels_latents, height, width, dtype, device, generator, latents=None):shape = (batch_size, num_channels_latents, height // self.vae_scale_factor, width // self.vae_scale_factor)if isinstance(generator, list) and len(generator) != batch_size:raise ValueError(f"You have passed a list of generators of length {len(generator)}, but requested an effective batch"f" size of {batch_size}. Make sure the batch size matches the length of the generators.")if latents is None:latents = randn_tensor(shape, generator=generator, device=device, dtype=dtype)else:latents = latents.to(device)# scale the initial noise by the standard deviation required by the schedulerlatents = latents * self.scheduler.init_noise_sigmareturn latents

显然,shape已经规定了latents的dim(4)和排列顺序。
在img2img中:

    # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.StableDiffusionImg2ImgPipeline.prepare_latentsdef prepare_latents(self, image, timestep, batch_size, num_images_per_prompt, dtype, device, generator=None):if not isinstance(image, (torch.Tensor, PIL.Image.Image, list)):raise ValueError(f"`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is {type(image)}")image = image.to(device=device, dtype=dtype)batch_size = batch_size * num_images_per_promptif image.shape[1] == 4:init_latents = imageelse:if isinstance(generator, list) and len(generator) != batch_size:raise ValueError(f"You have passed a list of generators of length {len(generator)}, but requested an effective batch"f" size of {batch_size}. Make sure the batch size matches the length of the generators.")elif isinstance(generator, list):init_latents = [self.vae.encode(image[i : i + 1]).latent_dist.sample(generator[i]) for i in range(batch_size)]init_latents = torch.cat(init_latents, dim=0)else:init_latents = self.vae.encode(image).latent_dist.sample(generator)init_latents = self.vae.config.scaling_factor * init_latentsif batch_size > init_latents.shape[0] and batch_size % init_latents.shape[0] == 0:# expand init_latents for batch_sizedeprecation_message = (f"You have passed {batch_size} text prompts (`prompt`), but only {init_latents.shape[0]} initial"" images (`image`). Initial images are now duplicating to match the number of text prompts. Note"" that this behavior is deprecated and will be removed in a version 1.0.0. Please make sure to update"" your script to pass as many initial images as text prompts to suppress this warning.")deprecate("len(prompt) != len(image)", "1.0.0", deprecation_message, standard_warn=False)additional_image_per_prompt = batch_size // init_latents.shape[0]init_latents = torch.cat([init_latents] * additional_image_per_prompt, dim=0)elif batch_size > init_latents.shape[0] and batch_size % init_latents.shape[0] != 0:raise ValueError(f"Cannot duplicate `image` of batch size {init_latents.shape[0]} to {batch_size} text prompts.")else:init_latents = torch.cat([init_latents], dim=0)shape = init_latents.shapenoise = randn_tensor(shape, generator=generator, device=device, dtype=dtype)# get latentsinit_latents = self.scheduler.add_noise(init_latents, noise, timestep)latents = init_latentsreturn latents

3.应用

3.1 问题

new_map = texture.permute(1, 2, 0)
RuntimeError: permute(sparse_coo): number of dimensions in the tensor input does not match the length of the desired ordering of dimensions i.e. input.dim() = 4 is not equal to len(dims) = 3

该问题是张量形状的问题,跟通道数毫无关系。

3.2 举例

问:4D 张量:形状为 (B, C, H, W),其中C可以为3吗?
答:4D 张量的形状为 (B,C,H,W),其中 C 表示通道数。通常情况下,C 可以为 3,这对应于 RGB 图像的三个颜色通道(红色、绿色和蓝色)。

3.3 张量可以理解为多维可变数组

print("sample:", sample.shape)
print("sample:", sample[0].shape)
print("sample:", sample[0][0].shape)
>>
sample: torch.Size([10, 4, 96, 96])
sample: torch.Size([4, 96, 96])
sample: torch.Size([96, 96])

由此可见,可以将张量形状为torch.size([10, 4, 96, 96])理解为一个4维可变数组。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/2973.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux的FTP服务

目录 1.什么是FTP服务? 2.FTP的工作原理和流程 1 主动模式 2 被动模式 3.搭建和配置FTP服务 1 下载服务包、备份配置文件 2 修改配置文件​编辑 3 匿名访问测试 4 设置黑白命令 1.什么是FTP服务? FTP(file Transfer Protocol&#…

Vue3 实现 Three.js粒子特效

效果 <template><div id"waves" /> </template><script setup> import { ref, onMounted, onUnmounted } from "vue"; import * as THREE from "three";const amountX ref(50); const amountY ref(50); const color …

QT学习之窗口基本设置

this->setWindowTitle("二代证测试工具"); // 设置窗口名this->setWindowIcon(QIcon("logo.jpg")); // 设置角标this->setFixedSize(900, 730); // 设置窗口大小设置exe图标 .rc文件代码中设置如下

数据结构入门——排序(代码实现)(下)

int GetMidi(int* a, int left, int right) {int mid (left right) / 2;// left mid rightif (a[left] < a[mid]){if (a[mid] < a[right]){return mid;}else if (a[left] > a[right]) // mid是最大值{return left;}else{return right;}}else // a[left] > a[mid…

一寸照片裁剪怎么弄?修改照片尺寸,3种方法调整

一寸照片裁剪怎么弄&#xff1f;将照片裁剪为一寸尺寸&#xff0c;可以方便我们在各种场合中使用。无论是办理证件、申请签证&#xff0c;还是制作简历、参与活动&#xff0c;一寸照片都是不可或缺的资料。通过裁剪&#xff0c;我们能够确保照片的尺寸、比例符合标准&#xff0…

视频怎么批量压缩?5个好用的电脑软件和在线网站

视频怎么批量压缩&#xff1f;有时候我们需要批量压缩视频来节省存储空间&#xff0c;便于管理文件和空间&#xff0c;快速的传输发送给他人。有些快捷的视频压缩工具却只支持单个视频导入&#xff0c;非常影响压缩效率&#xff0c;那么今天就向大家从软件和在线网站2个角度介绍…

GoLand远程开发IDE:使用SSH远程连接服务器进行云端编程

目录 ⛳️推荐 1. 安装配置GoLand 2. 服务器开启SSH服务 3. GoLand本地服务器远程连接测试 4. 安装cpolar内网穿透远程访问服务器端 4.1 服务器端安装cpolar 4.2 创建远程连接公网地址 5. 使用固定TCP地址远程开发 ⛳️推荐 前些天发现了一个巨牛的人工智能学习网站&am…

OpenG中的读写簇函数

1.首先需要在Vi Package Manager中安装Open G 2.找到openG中的读写ini函数&#xff0c;第一组是将簇标签作为段名&#xff0c;第二组是指定段名&#xff0c;本质上都是一样 3.读写簇到ini文件 4.禁用写入&#xff0c;更改簇的元素&#xff0c;增加或者删除&#xff0c;原来…

使用WebSocket在前端发送消息

在现代Web开发中&#xff0c;WebSocket提供了一种在用户的浏览器和服务器之间进行全双工通信的方法。这意味着服务器可以直接向客户端发送消息&#xff0c;而不需要客户端先请求数据。这种通信方式对于需要实时数据传输的应用&#xff08;如在线游戏、实时通知系统等&#xff0…

【Vue3】ref对象类型的响应式数据

文章目录 ref对象类型简介 ref对象类型简介 用法: let xx reactive({xxx:“xxx”})返回值: 一个Proxy的对象&#xff0c;就是响应式对象特别注意: 既能定义对象类型,也能定义基本类型的响应式数据。但是ref底层还是需要reactive的proxy做响应式数据 代码展示: <div>…

c++取经之路(其八)——基础模板

我认为的模板其实就是个懒人工具&#xff0c;你来弄个模板&#xff0c;编译器自动给你生成对应的函数。 函数模板&#xff1a; 定义&#xff1a;函数模板是一个蓝图&#xff0c;它本身并不是函数&#xff0c;是编译器用使用方式产生特定具体类型函数的模具。所以其实模板就是…

Edge浏览器下载文件提示 “无法安全下载” 的解决方法

提示如下&#xff1a; 虽然我们可以通过 "保留" 进行下载&#xff0c;但是每次需要选择&#xff0c;比较麻烦 解决方法&#xff1a; 1、打开注册表 HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft 2、创建2个 "项" Edge\InsecureContentAllowedForUrls…

C++奇迹之旅:从0开始实现日期时间计算器

文章目录 &#x1f4dd;前言&#x1f320; 头文件Date.h&#x1f309;日期计算函数&#x1f320;前后置&#x1f309;前后置-- &#x1f320;两对象日期相减&#x1f309;自定义流输入和输出 &#x1f309; 代码&#x1f309; 头文件Date.h&#x1f320;Date.cpp&#x1f309; …

Ubuntu 20.04 安装搜狗输入法,无法输入中文问题

搜狗输入法linux-安装指导 无法输入中文&#xff0c;主要是下面的命令没有执行&#xff1a; sudo apt install libqt5qml5 libqt5quick5 libqt5quickwidgets5qml-module-qtquick2 sudo apt install libgsettings-qt1 我的新台式机第一次执行上述命令失败&#xff0c;应该是默…

n-gram模型

N-gram是一种基于统计的语言模型&#xff0c;它基于一个假设&#xff0c;即一个词的出现仅与它前面的N-1个词有关&#xff0c;而与更远的词无关。 N-gram模型通常用于自然语言处理(NLP)任务&#xff0c;如文本生成、文本分类、机器翻译、拼写检查和语音识别等。在N-gram模型中…

微软专家分享 | 拯救者杯 OPENAIGC开发者大赛 能量加油上海站启动啦!

由联想拯救者、AIGC开放社区、英特尔联合主办的“AI生成未来第二届拯救者杯OPENAIGC开发者大赛”自上线以来&#xff0c;吸引了广大开发者的热情参与。 为了向技术开发者、业务人员、高校学生、以及个体创业人员等参赛者们提供更充分的帮助与支持&#xff0c;AIGC开放社区特别…

SpringBoot3.0新特性尝鲜,秒启动的快感!熟悉SpringAOT与RuntimeHints

文章目录 一、前置知识1、官网2、安装GraalVM3、GraalVM的限制4、安装maven5、背景 二、打包SpringBoot3.01、项目准备2、打包3、打包成docker 三、认识AOT1、RuntimeHints2、RuntimeHintsRegistrar3、RegisterReflectionForBinding4、ImportRuntimeHints5、使用JDK动态代理也需…

【禅道客户案例】专访鸿泉物联研发副总监徐小倩,感受上市公司研发项目管理“知与行”

杭州鸿泉物联网技术股份有限公司&#xff08;以下简称“鸿泉物联”、“公司”&#xff09;成立于2009年6月11日&#xff0c;2019年11月6日登陆上海证券交易所科创板&#xff08;股票代码&#xff1a;688288&#xff09;&#xff0c;注册资本10034.392万元&#xff0c;目前员工6…

电磁仿真--基本操作-CST-(3)

目录 1. 目的 2. 建模过程 2.1 创建工程 2.2 修改单位 2.3 创建线和圆柱 2.4 创建螺旋结构 2.5 创建另一个圆柱 2.6 设置频率、背景和边界 2.7 选择RLC求解器 2.8 设置端口 2.9 配置求解器 3. 仿真结果 4. 总结 1. 目的 本文将介绍一种较为复杂的建模方法&#x…

Java23种设计模式-结构型模式之适配器模式

适配器模式&#xff08;Adapter Pattern&#xff09;&#xff1a;核心思想是将现有的接口转换为客户端所期望的接口。它允许通过将一个接口转换为另一个接口&#xff0c;将不兼容的类或对象组合在一起。12 主要角色包括&#xff1a; 目标(Target)接口&#xff1a;当前系统业务…