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 161 162 163 164 165 166 167 168 169
| """ GAN 合成驾驶员面部数据
方法: - StyleGAN3 生成高保真面部图像 - 疲劳/分心状态可控生成 - 多种族、多年龄覆盖
优势: - 无隐私问题(合成数据) - 无限生成能力 - 可控属性(疲劳等级、分心类型)
参考:Deloitte Synthetic Data in Autonomous Vehicles """
import torch import torch.nn as nn
class DMSDataGenerator: """ DMS 合成数据生成器 基于 StyleGAN3 生成驾驶员面部图像 """ def __init__(self): self.generator = StyleGAN3Generator() self.state_controller = StateController() def generate_fatigue_images(self, num_samples: int, fatigue_level: str) -> torch.Tensor: """ 生成疲劳状态图像 Args: num_samples: 生成样本数 fatigue_level: 疲劳等级('normal', 'light', 'severe') Returns: images: 合成面部图像, shape=(N, C, H, W) """ state_code = self.state_controller.encode_fatigue(fatigue_level) noise = torch.randn(num_samples, 512) images = self.generator(noise, state_code) return images def generate_distraction_images(self, num_samples: int, distraction_type: str) -> torch.Tensor: """ 生成分心状态图像 Args: num_samples: 生成样本数 distraction_type: 分心类型('phone', 'dashboard', 'mirror') Returns: images: 合成面部图像 """ state_code = self.state_controller.encode_distraction(distraction_type) noise = torch.randn(num_samples, 512) images = self.generator(noise, state_code) return images def generate_occluded_images(self, num_samples: int, occlusion_type: str) -> torch.Tensor: """ 生成遮挡场景图像 Args: num_samples: 生成样本数 occlusion_type: 遮挡类型('glasses', 'mask', 'hand') Returns: images: 合成遮挡面部图像 """ state_code = self.state_controller.encode_occlusion(occlusion_type) noise = torch.randn(num_samples, 512) images = self.generator(noise, state_code) return images
class StyleGAN3Generator(nn.Module): """StyleGAN3 生成器(简化)""" def __init__(self): super().__init__() self.fc = nn.Linear(512, 4 * 4 * 512) self.conv_blocks = nn.ModuleList([ nn.ConvTranspose2d(512, 256, 4, 2, 1), nn.ConvTranspose2d(256, 128, 4, 2, 1), nn.ConvTranspose2d(128, 64, 4, 2, 1), nn.ConvTranspose2d(64, 3, 4, 2, 1) ]) def forward(self, noise: torch.Tensor, condition: torch.Tensor) -> torch.Tensor: x = noise + condition x = self.fc(x).view(-1, 512, 4, 4) for conv in self.conv_blocks: x = conv(x) return x
class StateController(nn.Module): """状态控制器""" def __init__(self): super().__init__() self.fatigue_encoder = nn.Linear(3, 512) self.distraction_encoder = nn.Linear(5, 512) self.occlusion_encoder = nn.Linear(4, 512) def encode_fatigue(self, fatigue_level: str) -> torch.Tensor: """编码疲劳等级""" levels = {'normal': [1, 0, 0], 'light': [0, 1, 0], 'severe': [0, 0, 1]} code = torch.tensor(levels[fatigue_level]).float() return self.fatigue_encoder(code) def encode_distraction(self, distraction_type: str) -> torch.Tensor: """编码分心类型""" types = {'normal': [1, 0, 0, 0, 0], 'phone': [0, 1, 0, 0, 0], 'dashboard': [0, 0, 1, 0, 0], 'mirror': [0, 0, 0, 1, 0], 'passenger': [0, 0, 0, 0, 1]} code = torch.tensor(types[distraction_type]).float() return self.distraction_encoder(code) def encode_occlusion(self, occlusion_type: str) -> torch.Tensor: """编码遮挡类型""" types = {'none': [1, 0, 0, 0], 'glasses': [0, 1, 0, 0], 'mask': [0, 0, 1, 0], 'hand': [0, 0, 0, 1]} code = torch.tensor(types[occlusion_type]).float() return self.occlusion_encoder(code)
if __name__ == "__main__": generator = DMSDataGenerator() fatigue_images = generator.generate_fatigue_images(100, 'severe') print(f"生成疲劳图像: {fatigue_images.shape}") print("隐私保护: 合成数据无真实个人信息")
|