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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| import torch import torch.nn as nn import numpy as np
class DMSSyntheticDataGAN: """ DMS合成数据GAN生成器 生成驾驶员面部图像+眼动特征 保护隐私:无真实个人信息 """ def __init__(self, config): self.generator = Generator(config['latent_dim'], config['output_shape']) self.discriminator = Discriminator(config['input_shape']) def train(self, real_data_samples): """ GAN训练 学习真实数据分布,生成合成数据 """ for epoch in range(config['epochs']): real_batch = real_data_samples.sample(batch_size) fake_batch = self.generator.generate(batch_size) d_loss = self.train_discriminator(real_batch, fake_batch) g_loss = self.train_generator() return self.generator def generate_synthetic_data(self, num_samples): """ 生成合成数据 无真实个人信息,可用于DMS训练 """ latent_vectors = torch.randn(num_samples, self.latent_dim) synthetic_data = self.generator(latent_vectors) return { 'images': synthetic_data['images'], 'eye_features': synthetic_data['eye_features'], 'pose_features': synthetic_data['pose_features'], 'privacy_preserved': True }
class Generator(nn.Module): """ GAN生成器 生成驾驶员面部图像 """ def __init__(self, latent_dim, output_shape): super().__init__() self.latent_dim = latent_dim self.output_shape = output_shape self.fc = nn.Sequential( nn.Linear(latent_dim, 512), nn.ReLU(), nn.Linear(512, 1024), nn.ReLU(), nn.Linear(1024, output_shape[0] * output_shape[1] * output_shape[2]), nn.Tanh() ) self.eye_feature_gen = nn.Sequential( nn.Linear(latent_dim, 128), nn.ReLU(), nn.Linear(128, 64), nn.ReLU(), nn.Linear(64, 10) ) def forward(self, z): """ 生成合成数据 Args: z: 潜在向量 (B, latent_dim) Returns: synthetic: 合成数据字典 """ img = self.fc(z) img = img.view(-1, *self.output_shape) eye_feat = self.eye_feature_gen(z) return { 'images': img, 'eye_features': eye_feat }
class Discriminator(nn.Module): """ GAN判别器 判断图像真实性 """ def __init__(self, input_shape): super().__init__() self.conv = nn.Sequential( nn.Conv2d(input_shape[0], 64, 4, 2, 1), nn.LeakyReLU(0.2), nn.Conv2d(64, 128, 4, 2, 1), nn.LeakyReLU(0.2), nn.Conv2d(128, 256, 4, 2, 1), nn.LeakyReLU(0.2), nn.AdaptiveAvgPool2d(1), nn.Flatten(), nn.Linear(256, 1) ) def forward(self, img): return self.conv(img)
class DPGAN: """ 差分隐私GAN 在GAN训练中注入噪声,满足差分隐私要求 """ def __init__(self, epsilon=1.0): self.epsilon = epsilon self.generator = None self.discriminator = None def train_with_dp(self, real_data): """ 差分隐私训练 判别器训练时梯度裁剪+噪声注入 """ clipping_bound = 1.0 noise_scale = clipping_bound / self.epsilon for batch in real_data: gradients = self.compute_gradients(batch) clipped_gradients = torch.clamp(gradients, -clipping_bound, clipping_bound) noisy_gradients = clipped_gradients + torch.randn_like(clipped_gradients) * noise_scale self.update_with_gradients(noisy_gradients) return { 'privacy_guarantee': f'ε={self.epsilon}', 'synthetic_data_quality': 'preserved' }
if __name__ == "__main__": config = { 'latent_dim': 100, 'input_shape': (3, 64, 64), 'output_shape': (3, 64, 64), 'epochs': 100 } gan = DMSSyntheticDataGAN(config) synthetic_data = gan.generate_synthetic_data(1000) print(f"生成合成数据: {synthetic_data['images'].shape}") print(f"眼动特征: {synthetic_data['eye_features'].shape}") print(f"隐私保护: {synthetic_data['privacy_preserved']}")
|