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

NeRF基础代码解析

embedders

对position和view direction做embedding。

class FreqEmbedder(nn.Module):def __init__(self, in_dim=3, multi_res=10, use_log_bands=True, include_input=True):super().__init__()self.in_dim = in_dimself.num_freqs = multi_resself.max_freq_log2 = multi_resself.use_log_bands = use_log_bandsself.periodic_fns = [torch.sin, torch.cos]self.include_input = include_inputself.embed_fns = Noneself.out_dim = Noneself.num_embed_fns = Noneself.create_embedding_fn()def create_embedding_fn(self):self.embed_fns = []# 10 * 2 * 3 = 60self.out_dim = self.num_freqs * len(self.periodic_fns) * self.in_dim)if self.include_input:self.embed_fns.append(lambda x: x)self.out_dim += self.in_dim	# 63if self.use_log_lands:freq_bands = 2. ** torch.linspace(0., self.max_freq_log2, steps=self.num_freqs)else:freq_bands = torch.linspace(2.**0, 2.**self.max_freq_log2, steps=self.num_freqs)for freq in freq_bands:for p_fn in self.periodic_fns:self.embed_fns.append(lambda x, p_fn=p_fn, freq=freq: p_fn(x*freq))self.num_embed_fns = len(self.embed_fns)def forward(self, x):"""x: [..., in_dim], xyz or view direction.embedding: [..., out_dim], corresponding frequency encoding."""embed_lst = [embed_fn(x) for embed_fn in self.embed_fns]# [[x, sin(x), cos(x), sin(2x), cos(2x),...,sin(512x), cos(512x)]]embedding = torch.cat(embed_lst, dim=-1)return embedding

NeRFBackbone

position和view经过embedding后,得到特征向量。再输入到NeRFBackbone网络中,得到sigma和color输出。

class NeRFBackbone(nn.Module):def __init__(self, pos_dim=3, cond_dim=64, view_dim=3, hid_dim=128, num_density_linears=8, num_color_linears=3, skip_layer_indices=[4]):self.pos_dim = pos_dimself.cond_dim = cond_dimself.view_dim = view_dimself.hid_dim = hid_dimself.out_dim = 4	# rgb + sigmaself.num_density_linears = num_density_linearsself.num_color_linears = num_color_linearsself.skip_layer_indices = skip_layer_indicesdensity_input_dim = pos_dim + cond_dimself.density_linears = nn.ModuleList([nn.Linear(density_input_dim, hid_dim)] +[nn.Linear(hid_dim, hid_dim) if i not in self.skip_layer_indices else nn.Linear(hid_dim + density_input_dim, hid_dim) for i in range(num_density_linears - 1)])self.density_out_linear = nn.Linear(hid_dim, 1)color_input_dim = view_dim + hid_dimself.color_linears = nn.ModuleList([nn.Linear(color_input_dim, hid_dim//2)] +[nn.Linear(hid_dim//2, hid_dim//2) for _ in range(num_color_linears - 1)])self.color_out_linear = nn.Linear(hid_dim//2, 3)def forward(self, pos, view, view):"""pos: [bs, n_sample, pos_dim], encoding of position.cond: [cond_dim,], condition features.view: [bs, view_dim], encoding of view direction."""bs, n_sample, _ = pos.shapeif cond.dim == 1:	# [cond_dim]cond = cond.squeeze()[None, None, :].expand([bs, n_sample, self.cond_dim])elif cond_dim == 2:	# [batch, cond_dim]cond = cond[:, None, :].expand([bs, n_sample, self.cond_dim])view = view[:, None, :].expand([bs, n_sample, self.view_dim])density_linear_input = torch.cat([pos, cond], dim=-1)h = density_linear_inputfor i in range(len(self.density_linears)):h = self.density_linears[i](h)h = F.relu(h)if i in self.skip_layer_indices:h = torch.cat([density_linear_input, h], -1)sigma = self.density_out_linear(h)h = torch.cat([h, view], -1)for i in range(len(self.color_linears)):h = self.color_linears[i](h)h = F.relu(h)rgb = self.color_out_linear(h)outputs = torch.cat([rgb, sigma], -1)return outputs

Ray Sampler

一张图的height = 1280, width = 720, 对这张图采样4096条从相机原点发出的光线ray。

def get_rays(H, W, focal, c2w, cx=None, cy=None):"""Get the rays emitted from camera to all pixels.The ray is represented in world coordinate.input:H: height of the image in pixel.W: width of the image in pixel.focal: focal length of the camera in pixel.c2w: 3x4 camera-to-world matrix, it should be something like this:[[r11, r12, r13, t1],[r21, r22, r23, t2],[r31, r32, r33, t3]]cx: center of camera in width axis.cy: center of camera in height axis.return:rays_o: start point of the ray.rays_d: direction of the ray. so you can sample the point in the ray with: xyz = rays_o + rays_d * z_val, where z_val is the distance."""j_pixels, i_pixels = torch.meshgrid(torch.linspace(0, H-1, H), torch.linspace(0, W-1, W))if cx is None:cx = W * 0.5if cy is None:cy = H * 0.5directions = torch.stack([(i_pixels - cx)/focal, -(j_pixels - cy)/focal, -torch.ones_like(i_pixels)], dim=-1)	# [W, H, 3]# Rotate ray directions from camera to the world frame.rays_d = torch.sum(directions[..., None, :] * c2w[:3, :3], dim=-1)# origin point of all ray, camera center in world coodinate.rays_o = c2w[:3, -1].expand(rays_d.shape)return rays_o, rays_dclass BaseRaySampler:def __init__(self, N_rays):super(BaseRaySampler, self).__init__()self.N_rays = N_raysdef __call__(self, H, W, focal, c2w):rays_o, rays_d = get_rays(H, W, focal, c2w)selected_coords = self.sample_rays(H, W)rays_o = rays_o[select_coords[:, 0], select_coords[:, 1]]	# [N_rand, 3]rays_d = rays_d[select_coords[:, 0], select_coords[:, 1]]	# [N_rand, 3]return rays_o, rays_d, select_coordsdef sample_rays(self, H, W, **kwargs):raise NotImplementedErrorclass UniformRaySampler(BaseRaySampler):def __init__(self, N_rays=None):super().__init__(N_rays=N_rays)def sample_ray(self, H, W, n_rays=None, rect=None, in_rect_percent=0.9, **kwargs):if n_rays is None:n_rays = self.N_rayscoords = torch.stack(torch.meshgrid(torch.linspace(0, H-1, H), torch.linspace(0, W-1, W)), -1)	# [H, W, 2]coords = torch.reshape(coords, [-1, 2])	# [H * W, 2]if rect is None:# uniformly sample the whole imageselected_inds = np.random.choice(coords.shape[0], size=[n_rays], replace=False)selected_coords = coords[selected_inds].long()else:# uniformly sample from rect region and out-rect, respectively.......return seleced_coordsdef __call__(self, H, W, focal, c2w, n_rays=None, selected_coords=None, rect=None, in_rect_percent=0.9, **kwargs):rays_o, rays_d = get_rays(H, W, focal, c2w)if select_coords s None:select_coords = self.sample_rays(H, W, n_rays, rect, in_rect_percent)rays_o = rays_o[selected_coords[:, 0], selected_coords[:, 1]]rays_d = rays_d[selected_coords[:, 0], selected_coords[:, 1]]return rays_o, rays_d, selected_coordsdef sample_pixels_from_img_with_select_coords(self, img, select_coords):return img[selected_coords[:, 0], select_coords[:, 1]]

相关文章:

NeRF基础代码解析

embedders 对position和view direction做embedding。 class FreqEmbedder(nn.Module):def __init__(self, in_dim3, multi_res10, use_log_bandsTrue, include_inputTrue):super().__init__()self.in_dim in_dimself.num_freqs multi_resself.max_freq_log2 multi_resself…...

职场新星:Java面试干货让你笑傲求职路(三)

职场新星:Java面试干货让你笑傲求职路 1、token 为什么存放在 redis 中?2、索引的底层原理是什么?3、Spring IOC和AOP的原理4、接口和抽象类有什么共同点和区别?5、为什么要使用线程池?直接new个线程不好吗&#xff1f…...

获取指定收获地址的信息

目录 1 /// 获取指定收获地址的信息 2 /// 删除指定的收获地址信息 3 /// 取消订单 4 /// 确认订单收货 /// <summary> /// 获取指定收获地址的信息</...

突破笔试:力扣全排列(medium)

1. 题目链接&#xff1a;46. 全排列 2. 题目描述&#xff1a;给定一个不含重复数字的数组 nums &#xff0c;返回其 所有可能的全排列 。你可以 按任意顺序 返回答案。 示例 1&#xff1a; 输入&#xff1a;nums [1,2,3] 输出&#xff1a;[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[…...

gitlab 503 错误的解决方案

首先使用 sudo gitlab-ctl status 命令查看哪些服务没用启动 sudo gitlab-ctl status 再用 gitlab-rake gitlab:check 命令检查 gitlab。根据发生的错误一步一步纠正。 gitlab-rake gitlab:check 查看日志 tail /var/log/gitlab/gitaly/current删除gitaly.pid rm /var/opt…...

智能离子风棒联网监控静电消除器的主要功能和特点

智能离子风棒联网监控静电消除器是一种集成了智能化和网络化监控功能的设备&#xff0c;用于监测和消除静电现象。它的工作原理是通过产生大量的正负离子&#xff0c;将空气中的静电中和和消除&#xff0c;从而达到防止静电积累和放电的目的。 智能离子风棒联网监控静电消除器的…...

matplotlib 设置legend的位置在轴最上方,长度与图的长度相同

import matplotlib.pyplot as plt import numpy as npx1 np.linspace(0, 10, 50) x2 [6,4,3]ax plt.subplot() ax.plot(x1, label"test1") ax.plot(x2, label"test2") # 设置图例的位置 # 将左下角放置在【0, 1.02】位置处&#xff0c;横为1&#xff0c…...

Docker-Compose 安装rabbitmq

【编写&#xff1a;docker-compose-rabbitmq.yml】创建数据目录&#xff1a; mkdir -p /opt/rabbitmq/data cd /opt/rabbitmq# 创建 docker-compose-rabbitmq.yml vim docker-compose-rabbitmq.yml 输入&#xff1a; version: "3.1" services:rabbitmq:image: rabbit…...

leetcode357- 2812. 找出最安全路径

这个题比较经典&#xff0c;可以用多个算法来求解&#xff0c;分别给出各个算法的求解方法&#xff0c;主要是分为第一部分的多源BFS求每个位置的距离和第二部分求(0,0)到(n-1,n-1)的最短路径&#xff08;可以用多种方法求&#xff09; 目录 多源BFS求最短路径枚举安全系数判断…...

Oracle连接数据库提示 ORA-12638:身份证明检索失败

ORA-12638 是一个 Oracle 数据库的错误代码&#xff0c;它表示身份验证&#xff08;认证&#xff09;检索失败。这通常与数据库连接相关&#xff0c;可能由于以下几个原因之一引起&#xff1a; 错误的用户名或密码&#xff1a; 提供的数据库用户名或密码不正确&#xff0c;导致…...

在 Linux 中使用 systemd 注册服务

Systemd 是一种现代的 Linux 系统初始化系统和服务管理器。它旨在管理系统服务的初始化、配置和控制。Systemd 的一个关键特性是它可以管理服务&#xff0c;这些服务是为系统提供特定功能的后台进程。在本指南中&#xff0c;我们将探讨如何使用 systemd 在 Linux 中注册服务。 …...

(03)Unity HTC VRTK 基于 URP 开发记录

1.简介 本篇主要内容为&#xff1a;URP如何与VRTK结合、URP需要注意的地方、VRTK的功能进行阐述。 因项目本身要求要渲染出比较好的画质&#xff0c;所以抛弃了Unity默认渲染管线Built-in&#xff0c;使用URP进行渲染&#xff0c;当然也可以选HDRP&#xff0c;但考虑到后期项目…...

.bit域名调研

.bit域名研究 问题&#xff1a; .bit域名和ENS域名的相同点&#xff1f;不同点&#xff1f;有什么关系&#xff1f; .bit的定义 .bit 是基于区块链的&#xff0c;开源的&#xff0c;跨链去中心化账户系统.bit 提供了以 .bit 为后缀的全局唯一的命名体系&#xff0c;可用于加密…...

Vue数组变更方法和替换方法

一、可以引起UI界面变化 Vue 将被侦听的数组的变更方法进行了包裹&#xff0c;所以它们也将会触发视图更新。这些被包裹过的方法包括&#xff1a; push()pop()shift()unshift()splice()sort()reverse() 以上七个数组都会改变原数组&#xff0c;下面来分别讲解它们的区别&…...

Centos-6.3安装使用MongoDB

安装说明 系统环境&#xff1a;Centos-6.3 安装软件&#xff1a;mongodb-linux-x86_64-2.2.2.tgz 下载地址&#xff1a;http://www.mongodb.org/downloads 安装机器&#xff1a;192.168.15.237 上传位置&#xff1a;/usr/local/ 软件安装位置&#xff1a;/usr/local/mongodb 数…...

Mysql 复杂查询丨联表查询

&#x1f497;wei_shuo的个人主页 &#x1f4ab;wei_shuo的学习社区 &#x1f310;Hello World &#xff01; JOIN&#xff08;联表查询&#xff09; 联表查询&#xff08;Join&#xff09;是一种在数据库中使用多个表进行关联查询的操作。它通过使用 JOIN 关键字将多个表连接在…...

C语言进阶第二课-----------指针的进阶----------升级版

作者前言 &#x1f382; ✨✨✨✨✨✨&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f367;&#x1f382; ​&#x1f382; 作者介绍&#xff1a; &#x1f382;&#x1f382; &#x1f382; &#x1f389;&#x1f389;&#x1f389…...

若依vue -【 111 ~ 更 ~ 127 完 】

【更】111 3.5.0版本更新介绍 112 使用docker实现一键部署 1、安装docker yum install https://download.docker.com/linux/fedora/30/x86_64/stable/Packages/containerd.io-1.2.6-3.3.fc30.x86_64.rpm yum install -y yum-utils device-mapper-persistent-data lvm2 yum-c…...

vue-pc端实现按钮防抖处理-自定义指令

前言 我们经常在移动端会处理按钮和输入框的防抖和节流处理&#xff0c;在pc端很少进行这样的操作 但是在pc端也是可以进行按钮的防抖操作&#xff0c;这样也是比较合理&#xff0c;可以不用但不可以不会 我们只要配合vue项目自定义指令加上全局注册&#xff0c;就可以实现按…...

python解决8皇后问题

def is_valid(queens, row, col):for i in range(row):if queens[i] == col or abs(queens[i] - col) == abs(i - row):return Falsereturn Truedef solve_n_queens(n, row, queens, result):if row == n:result.append(queens[:]) # 将当前解添加到结果中returnfor col in ra…...

多云管理“拦路虎”:深入解析网络互联、身份同步与成本可视化的技术复杂度​

一、引言&#xff1a;多云环境的技术复杂性本质​​ 企业采用多云策略已从技术选型升维至生存刚需。当业务系统分散部署在多个云平台时&#xff0c;​​基础设施的技术债呈现指数级积累​​。网络连接、身份认证、成本管理这三大核心挑战相互嵌套&#xff1a;跨云网络构建数据…...

(十)学生端搭建

本次旨在将之前的已完成的部分功能进行拼装到学生端&#xff0c;同时完善学生端的构建。本次工作主要包括&#xff1a; 1.学生端整体界面布局 2.模拟考场与部分个人画像流程的串联 3.整体学生端逻辑 一、学生端 在主界面可以选择自己的用户角色 选择学生则进入学生登录界面…...

《Qt C++ 与 OpenCV:解锁视频播放程序设计的奥秘》

引言:探索视频播放程序设计之旅 在当今数字化时代,多媒体应用已渗透到我们生活的方方面面,从日常的视频娱乐到专业的视频监控、视频会议系统,视频播放程序作为多媒体应用的核心组成部分,扮演着至关重要的角色。无论是在个人电脑、移动设备还是智能电视等平台上,用户都期望…...

安宝特方案丨XRSOP人员作业标准化管理平台:AR智慧点检验收套件

在选煤厂、化工厂、钢铁厂等过程生产型企业&#xff0c;其生产设备的运行效率和非计划停机对工业制造效益有较大影响。 随着企业自动化和智能化建设的推进&#xff0c;需提前预防假检、错检、漏检&#xff0c;推动智慧生产运维系统数据的流动和现场赋能应用。同时&#xff0c;…...

uni-app学习笔记二十二---使用vite.config.js全局导入常用依赖

在前面的练习中&#xff0c;每个页面需要使用ref&#xff0c;onShow等生命周期钩子函数时都需要像下面这样导入 import {onMounted, ref} from "vue" 如果不想每个页面都导入&#xff0c;需要使用node.js命令npm安装unplugin-auto-import npm install unplugin-au…...

Python爬虫实战:研究feedparser库相关技术

1. 引言 1.1 研究背景与意义 在当今信息爆炸的时代,互联网上存在着海量的信息资源。RSS(Really Simple Syndication)作为一种标准化的信息聚合技术,被广泛用于网站内容的发布和订阅。通过 RSS,用户可以方便地获取网站更新的内容,而无需频繁访问各个网站。 然而,互联网…...

系统设计 --- MongoDB亿级数据查询优化策略

系统设计 --- MongoDB亿级数据查询分表策略 背景Solution --- 分表 背景 使用audit log实现Audi Trail功能 Audit Trail范围: 六个月数据量: 每秒5-7条audi log&#xff0c;共计7千万 – 1亿条数据需要实现全文检索按照时间倒序因为license问题&#xff0c;不能使用ELK只能使用…...

视觉slam十四讲实践部分记录——ch2、ch3

ch2 一、使用g++编译.cpp为可执行文件并运行(P30) g++ helloSLAM.cpp ./a.out运行 二、使用cmake编译 mkdir build cd build cmake .. makeCMakeCache.txt 文件仍然指向旧的目录。这表明在源代码目录中可能还存在旧的 CMakeCache.txt 文件,或者在构建过程中仍然引用了旧的路…...

SQL慢可能是触发了ring buffer

简介 最近在进行 postgresql 性能排查的时候,发现 PG 在某一个时间并行执行的 SQL 变得特别慢。最后通过监控监观察到并行发起得时间 buffers_alloc 就急速上升,且低水位伴随在整个慢 SQL,一直是 buferIO 的等待事件,此时也没有其他会话的争抢。SQL 虽然不是高效 SQL ,但…...

永磁同步电机无速度算法--基于卡尔曼滤波器的滑模观测器

一、原理介绍 传统滑模观测器采用如下结构&#xff1a; 传统SMO中LPF会带来相位延迟和幅值衰减&#xff0c;并且需要额外的相位补偿。 采用扩展卡尔曼滤波器代替常用低通滤波器(LPF)&#xff0c;可以去除高次谐波&#xff0c;并且不用相位补偿就可以获得一个误差较小的转子位…...