hejianhao
2026-04-02 0940d56748a73525790c350c393a5be960aab62c
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
// 模型参数名称映射配置
// 用于将英文参数名映射为中文显示名称
 
export const parameterNameMapping = {
  // 基础字段
  id: 'ID',
  name: '名称',
  status: '状态',
  createdAt: '创建时间',
  updatedAt: '更新时间',
  split: '拆分配置',
  
  // 数据集和路径相关
  dtd_path: '纹理数据集路径',
  data_path: '数据路径',
  data_dir: '数据目录',
  split_id: '拆分ID',
  
  // 半监督学习相关
  use_semi_supervised: '启用半监督',
  
  // 骨干网络和预训练权重
  backbone: '骨干网络',
  backbone_weight: '骨干网络权重',
  pretrained_weights: '预训练权重',
  pretrain_weight: '预训练权重',
  vgg_weights: 'VGG权重',
  inception_weights: 'Inception权重',
  
  // 前景分割相关
  use_fg: '启用前景剪裁',
  fg_threshold: '前景分割阈值',
  
  // 评估和保存相关
  cal_pro: '评估AUPRO',
  save_pics: '保存热力图',
  
  // 模型结构参数
  num_classes: '类别数量',
  
  // 训练策略
  finetune: '启用微调',
  
  // 生成相关
  generate_imgs_numbers: '生成数量',
  
  // 网络结构参数
  depth: '网络深度',
  
  // 模块名称相关
  module_name: '模块名称',
  train_dataset_name: '训练数据集名称',
  test_dataset_name: '测试数据集名称',
  optimizer_name: '优化器',
  scheduler_name: '调度器名称',
  loss_name: '损失函数',
  
  // 训练参数
  batch_size: '批次大小',
  lr: '学习率',
  lr_weights: '学习率权重',
  epochs: '训练轮数',
  start_epoch: '起始轮数',
  
  // 优化器相关
  momentum: '动量',
  weight_decay: '权重衰减',
  scheduler_percent: '调度器百分比',
  scheduler_gamma: '调度器Gamma',
  
  // 随机增强参数
  random_rotate: '随机旋转',
  random_rotate_ratio: '随机旋转比例',
  random_shift: '随机偏移',
  random_shift_ratio: '随机偏移比例',
  random_shift_range: '随机偏移范围',
  random_rescale: '随机缩放',
  random_rescale_ratio: '随机缩放比例',
  random_rescale_range: '随机缩放范围',
  random_flip: '随机翻转',
  random_flip_ratio: '随机翻转比例',
  random_flip_type: '随机翻转类型',
  random_brightness: '随机亮度',
  random_brightness_ratio: '随机亮度比例',
  random_brightness_range: '随机亮度范围',
  random_contrast: '随机对比度',
  random_contrast_ratio: '随机对比度比例',
  random_contrast_range: '随机对比度范围',
  random_saturation: '随机饱和度',
  random_saturation_ratio: '随机饱和度比例',
  random_saturation_range: '随机饱和度范围',
  
  // 设备和性能
  use_gpu: '使用GPU',
  gpu_ids: 'GPU设备ID列表',
  num_workers: '工作线程数',
  
  // 检查点和恢复
  resume_checkpoint: '恢复检查点',
  checkpoint_path: '检查点路径',
  checkpoint_interval: '检查点间隔',
  
  // 输出和日志
  output_dir: '输出目录',
  
  // 验证和测试
  val_interval: '验证间隔',
  
  // 评估参数
  eval_phases: '评估阶段',
  max_eval_samples: '最大评估样本数',
  max_det: '最大检测数',
  max_overlap: '最大重叠',
  max_overlap_agnostic: '最大重叠(类别无关)',
  min_confidence: '最小置信度',
  
  // 其他常见参数
  random_seed: '随机种子',
  
  // 图像相关
  input_width: '输入宽度',
  input_height: '输入高度',
  crop_width: '裁剪宽度',
  crop_height: '裁剪高度',
  
  // 阈值相关
  iou_threshold: 'IoU阈值',
  
  // 模型特定参数 - SupportLabeler
  topk_regions: 'TopK区域',
  
  // 模型特定参数 - DINO
  use_mean_pooling: '使用平均池化',
  use_bnneck: '使用BNNeck',
  neck_feat: 'Neck特征',
  
  // 模型特定参数 - Mask2Former
  num_feature_levels: '特征层级数',
  hidden_dim: '隐藏层维度',
  num_queries: '查询数量',
  aux_loss: '辅助损失',
  with_box_refine: '边框细化',
  two_stage: '两阶段'
}
 
// 参数类型映射(用于识别参数类型)
export const parameterTypeMapping = {
  // 路径类型参数
  pathParams: [
    'dtd_path',
    'data_path',
    'dataset_path',
    'backbone_weight',
    'pretrained_weights',
    'vgg_weights',
    'inception_weights',
    'checkpoint_path',
    'output_dir',
    'log_dir',
    'save_dir',
    'results_dir'
  ],
  
  // 布尔类型参数
  booleanParams: [
    'use_semi_supervised',
    'use_fg',
    'cal_pro',
    'save_pics',
    'finetune',
    'use_gpu',
    'use_augmentation',
    'horizontal_flip',
    'vertical_flip',
    'save_checkpoint',
    'deterministic',
    'verbose',
    'debug',
    'early_stopping'
  ],
  
  // 整数类型参数
  integerParams: [
    'fg_threshold',
    'scale',
    'num_class',
    'num_classes',
    'generate_imgs_numbers',
    'depth',
    'batch_size',
    'epochs',
    'num_epochs',
    'max_epochs',
    'gpu_id',
    'num_workers',
    'checkpoint_interval',
    'val_interval',
    'seed',
    'random_seed',
    'image_size',
    'img_size',
    'input_size',
    'crop_size',
    'patience',
    'hidden_dim',
    'embedding_dim',
    'num_layers',
    'kernel_size',
    'stride',
    'padding'
  ],
  
  // 浮点数类型参数
  floatParams: [
    'learning_rate',
    'lr',
    'momentum',
    'weight_decay',
    'validation_split',
    'test_split',
    'loss_weight',
    'dropout',
    'dropout_rate',
    'l1_regularization',
    'l2_regularization',
    'min_delta',
    'lr_decay',
    'lr_gamma',
    'confidence_threshold',
    'iou_threshold',
    'nms_threshold',
    'score_threshold'
  ],
  
  // 枚举类型参数(暂时还用不上)
  enumParams: {
    lr_scheduler: ['step', 'cosine', 'exponential', 'plateau'],
    device: ['cpu', 'cuda', 'mps'],
    depth: [18, 34, 50, 101, 152]  // ResNet深度选项
  },
 
  // 动态选项参数(从配置文件获取)
  dynamicParams: {
    optimizer_name: [], // 将动态加载
    loss_name: []       // 将动态加载
  }
}
 
// 获取参数的中文名称
export function getParameterLabel(paramName) {
  // 首先尝试直接匹配
  if (parameterNameMapping[paramName]) {
    return parameterNameMapping[paramName]
  }
  
  // 尝试转换下划线为驼峰式后匹配
  const camelCase = paramName.replace(/_([a-z])/g, (g) => g[1].toUpperCase())
  if (parameterNameMapping[camelCase]) {
    return parameterNameMapping[camelCase]
  }
  
  // 尝试添加常见前缀后匹配
  const commonPrefixes = ['use_', 'is_', 'enable_', 'num_', 'max_', 'min_']
  for (const prefix of commonPrefixes) {
    if (paramName.startsWith(prefix)) {
      const withoutPrefix = paramName.substring(prefix.length)
      if (parameterNameMapping[withoutPrefix]) {
        return parameterNameMapping[withoutPrefix]
      }
    }
  }
  
  // 如果都没有匹配,返回原始名称并转换为更友好的格式
  return paramName
    .replace(/_/g, ' ')  // 替换下划线为空格
    .replace(/\b\w/g, l => l.toUpperCase())  // 首字母大写
}
 
// 获取参数类型
export function getParameterType(paramName) {
  const { pathParams, booleanParams, integerParams, floatParams, enumParams } = parameterTypeMapping
  
  if (pathParams.includes(paramName)) return 'path'
  if (booleanParams.includes(paramName)) return 'boolean'
  if (integerParams.includes(paramName)) return 'integer'
  if (floatParams.includes(paramName)) return 'float'
  if (enumParams[paramName]) return 'enum'
  
  // 根据参数名称模式推断类型
  if (paramName.includes('path') || paramName.includes('dir') || paramName.includes('file')) {
    return 'path'
  }
  if (paramName.startsWith('use_') || paramName.startsWith('is_') || paramName.startsWith('enable_')) {
    return 'boolean'
  }
  if (paramName.includes('num_') || paramName.includes('count') || paramName.includes('size')) {
    return 'integer'
  }
  if (paramName.includes('rate') || paramName.includes('ratio') || paramName.includes('threshold')) {
    return 'float'
  }
  
  return 'string'  // 默认类型
}
 
// 动态加载指定模型的配置选项
export async function loadModelConfigOptions(modelName) {
  console.log('loadModelConfigOptions 被调用,modelName:', modelName)
  
  if (typeof window === 'undefined' || !window.electronAPI || !modelName) {
    console.warn('loadModelConfigOptions: 缺少必要条件', {
      hasWindow: typeof window !== 'undefined',
      hasElectronAPI: typeof window !== 'undefined' && !!window.electronAPI,
      hasModelName: !!modelName
    })
    return { optimizer_name: null, loss_name: null }
  }
  
  try {
    const configPath = `config/models/${modelName}.json`
    console.log('尝试读取配置文件:', configPath)
    const result = await window.electronAPI.readFile(configPath)
    console.log('文件读取结果:', result)
    
    if (result.success) {
      const config = JSON.parse(result.content)
      console.log('解析的配置:', config)
      const returnValue = {
        optimizer_name: config.optimizer_name || null,
        loss_name: config.loss_name || null
      }
      console.log('返回值:', returnValue)
      return returnValue
    } else {
      console.error('文件读取失败:', result.error)
    }
  } catch (error) {
    console.error(`无法读取模型配置文件 ${modelName}.json:`, error)
  }
  
  return { optimizer_name: null, loss_name: null }
}
 
// 获取参数的枚举选项
export function getParameterOptions(paramName) {
  // 先检查固定枚举选项
  if (parameterTypeMapping.enumParams[paramName]) {
    return parameterTypeMapping.enumParams[paramName]
  }
  
  // 再检查动态选项
  if (parameterTypeMapping.dynamicParams[paramName]) {
    return parameterTypeMapping.dynamicParams[paramName]
  }
  
  return null
}
 
// 参数验证规则
export function getParameterValidation(paramName) {
  const type = getParameterType(paramName)
  const validation = {
    type,
    required: false
  }
  
  // 根据参数名称添加特定的验证规则
  switch (paramName) {
    case 'batch_size':
      validation.min = 1
      validation.max = 256
      break
    case 'learning_rate':
    case 'lr':
      validation.min = 0.00001
      validation.max = 1.0
      break
    case 'epochs':
    case 'num_epochs':
    case 'max_epochs':
      validation.min = 1
      validation.max = 10000
      break
    case 'depth':
      validation.options = [18, 34, 50, 101, 152]
      break
    case 'num_classes':
    case 'num_class':
      validation.min = 1
      validation.max = 10000
      break
    case 'fg_threshold':
      validation.min = 0
      validation.max = 255
      break
    case 'scale':
      validation.min = 1
      validation.max = 32
      break
    case 'generate_imgs_numbers':
      validation.min = 1
      validation.max = 100
      break
    case 'confidence_threshold':
    case 'iou_threshold':
    case 'nms_threshold':
    case 'score_threshold':
      validation.min = 0.0
      validation.max = 1.0
      break
    case 'dropout':
    case 'dropout_rate':
      validation.min = 0.0
      validation.max = 0.99
      break
  }
  
  return validation
}
 
export default {
  parameterNameMapping,
  parameterTypeMapping,
  getParameterLabel,
  getParameterType,
  getParameterOptions,
  getParameterValidation,
  loadModelConfigOptions
}