1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| """ SHINE-PPG 非朗伯分解模型 """
import torch import torch.nn as nn import torch.nn.functional as F
class NonLambertianDecomposition(nn.Module): """ 非朗伯内在分解 将面部图像分解为: 1. 漫反射分量 (包含rPPG信号) 2. 环境光照分量 3. 稀疏镜面高光分量 """ def __init__(self, config: dict): super().__init__() in_channels = config.get('in_channels', 3) feat_dim = config.get('feat_dim', 64) self.encoder = nn.Sequential( nn.Conv2d(in_channels, feat_dim, 3, padding=1), nn.BatchNorm2d(feat_dim), nn.ReLU(inplace=True), nn.Conv2d(feat_dim, feat_dim*2, 3, stride=2, padding=1), nn.BatchNorm2d(feat_dim*2), nn.ReLU(inplace=True), nn.Conv2d(feat_dim*2, feat_dim*4, 3, stride=2, padding=1), nn.BatchNorm2d(feat_dim*4), nn.ReLU(inplace=True), ) self.diffuse_head = nn.Sequential( nn.Conv2d(feat_dim*4, feat_dim*2, 3, padding=1), nn.Upsample(scale_factor=2), nn.Conv2d(feat_dim*2, feat_dim, 3, padding=1), nn.Upsample(scale_factor=2), nn.Conv2d(feat_dim, in_channels, 3, padding=1), nn.Sigmoid() ) self.illumination_head = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(feat_dim*4, 32), nn.ReLU(inplace=True), nn.Linear(32, 3), nn.Sigmoid() ) self.specular_head = nn.Sequential( nn.Conv2d(feat_dim*4, feat_dim*2, 3, padding=1), nn.Upsample(scale_factor=2), nn.Conv2d(feat_dim*2, feat_dim, 3, padding=1), nn.Upsample(scale_factor=2), nn.Conv2d(feat_dim, in_channels, 3, padding=1), nn.Sigmoid() ) self.adain_params = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(feat_dim*4, 64), nn.ReLU(inplace=True), nn.Linear(64, 2 * feat_dim * 4) ) def forward(self, x: torch.Tensor) -> dict: """ 前向传播 Args: x: 面部图像 (B, C, H, W) Returns: { 'diffuse': (B, C, H, W), # 漫反射 'illumination': (B, 3), # 环境光 'specular': (B, C, H, W), # 镜面高光 'reconstruction': (B, C, H, W) # 重建 = diffuse + specular } """ feat = self.encoder(x) adain = self.adain_params(feat) gamma, beta = adain.chunk(2, dim=1) gamma = gamma.view(-1, feat.shape[1], 1, 1) beta = beta.view(-1, feat.shape[1], 1, 1) feat_normalized = gamma * feat + beta diffuse = self.diffuse_head(feat_normalized) illumination = self.illumination_head(feat) specular = self.specular_head(feat_normalized) reconstruction = diffuse + specular return { 'diffuse': diffuse, 'illumination': illumination, 'specular': specular, 'reconstruction': reconstruction } def extract_rppg(self, diffuse_frames: torch.Tensor, skin_mask: torch.Tensor) -> torch.Tensor: """ 从漫反射分量序列提取 rPPG 信号 Args: diffuse_frames: (T, C, H, W) 漫反射帧序列 skin_mask: (H, W) 皮肤mask Returns: rppg_signal: (T,) 心率信号 """ T, C, H, W = diffuse_frames.shape mask = skin_mask.unsqueeze(0).unsqueeze(0) masked = diffuse_frames * mask spatial_mean = masked.sum(dim=(2, 3)) / (skin_mask.sum() + 1e-10) green = spatial_mean[:, 1] green = green - green.mean() return green
if __name__ == "__main__": model = NonLambertianDecomposition({'in_channels': 3, 'feat_dim': 32}) x = torch.randn(2, 3, 64, 64) result = model(x) print(f"漫反射: {result['diffuse'].shape}") print(f"环境光: {result['illumination'].shape}") print(f"镜面: {result['specular'].shape}") print(f"重建: {result['reconstruction'].shape}") print(f"参数量: {sum(p.numel() for p in model.parameters()):,}")
|