Download code/model.py from lantudou/bihua: direct link, hf CLI and curl.
- Browser
- Download file 1.96 kB
-
https://huggingface.co/lantudou/bihua/resolve/main/code/model.py
- Command line
-
hf download hf://lantudou/bihua/code/model.py
-
curl -L -o model.py https://huggingface.co/lantudou/bihua/resolve/main/code/model.py
1.96 kB
| import math | |
| import torch | |
| import torch.nn as nn | |
| import torch.nn.functional as F | |
| # model.py | |
| # full6 用到的公共部件:坐标的傅里叶编码和卷积字形编码器(训练仓库里的同名文件还有一个早期回归基线,这里没有带上)。 | |
| def fourier(p, n=8): | |
| """坐标 → 多频正余弦编码。""" | |
| f = (2 ** torch.arange(n, device=p.device, dtype=p.dtype)) * math.pi | |
| x = p[..., None] * f | |
| return torch.cat([x.sin(), x.cos()], -1).flatten(-2) | |
| class Res(nn.Module): | |
| def __init__(self, c): | |
| super().__init__() | |
| self.a = nn.Conv2d(c, c, 3, padding=1); self.b = nn.Conv2d(c, c, 3, padding=1) | |
| self.n1 = nn.GroupNorm(8, c); self.n2 = nn.GroupNorm(8, c) | |
| def forward(self, x): | |
| return x + self.b(F.silu(self.n2(self.a(F.silu(self.n1(x)))))) | |
| class Encoder(nn.Module): | |
| """256 输入 → 64×64 的融合特征图(给点取样)和 16×16 的全局 token。""" | |
| def __init__(self, d=128, dg=192): | |
| super().__init__() | |
| def stage(i, o, n=2): | |
| return nn.Sequential(nn.Conv2d(i, o, 3, stride=2, padding=1), *[Res(o) for _ in range(n)]) | |
| self.s0 = nn.Sequential(nn.Conv2d(3, 48, 3, stride=2, padding=1), nn.GroupNorm(8, 48), nn.SiLU(), nn.Conv2d(48, 48, 3, padding=1)); self.s1 = stage(48, 96); self.s2 = stage(96, 160); self.s3 = stage(160, 224) | |
| self.p1 = nn.Conv2d(96, d, 1); self.p2 = nn.Conv2d(160, d, 1); self.p3 = nn.Conv2d(224, d, 1) | |
| self.out = nn.Sequential(nn.Conv2d(d, d, 3, padding=1), nn.GroupNorm(8, d)) | |
| self.g = nn.Linear(224, dg) | |
| self.gpos = nn.Parameter(torch.randn(256, dg) * 0.02) | |
| def forward(self, x): | |
| a = self.s1(self.s0(x)); b = self.s2(a); c = self.s3(b) | |
| f = self.p1(a) + F.interpolate(self.p2(b), scale_factor=2, mode='bilinear') + F.interpolate(self.p3(c), scale_factor=4, mode='bilinear') | |
| return self.out(f), self.g(c.flatten(2).transpose(1, 2)) + self.gpos | |