main.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. # time: 2023/3/2 10:28
  4. # file: config.py
  5. # author: David
  6. # company: shenyang JY
  7. """
  8. 模型调参及系统功能配置
  9. """
  10. from tqdm import tqdm
  11. import argparse, yaml
  12. import pandas as pd
  13. from pathlib import Path
  14. from functools import partial
  15. from concurrent.futures import ProcessPoolExecutor
  16. from app.common.logs import params, logger
  17. from app.model.config import myargparse
  18. from app.model.resource_manager import ResourceController
  19. # from app.model.task_worker import station_task
  20. """"
  21. 调用思路
  22. xxxx 1. 从入口参数中获取IN OUT文件位置 xxxx
  23. 2. 按照训练和预测加载和解析数据
  24. 3. 对数据进行预处理
  25. 4. 执行训练,保存模型,输出状态
  26. 5. 执行预测,输出结果,输出状态
  27. """
  28. """
  29. 训练任务
  30. 1.将一个省份下的所有场站加入队列
  31. 2.队列中的每个场站是一个子任务,还有最终的区域级子任务
  32. """
  33. def main():
  34. # ---------------------------- 解析参数 ----------------------------
  35. # 创建解析器对象
  36. parser = myargparse(description='算法', add_help=False)
  37. # 解析参数
  38. opt = parser.parse_args_and_yaml()
  39. config = opt.__dict__
  40. # 使用参数
  41. print(f"文件: {opt.input_file}")
  42. # ---------------------------- 配置计算资源和任务 ----------------------------
  43. # 初始化资源管理器
  44. rc = ResourceController(
  45. max_workers=opt.system['max_workers'],
  46. gpu_list=opt.system['gpu_devices']
  47. )
  48. # 生成任务列表
  49. all_stations = [str(child) for child in Path(opt.input_file).iterdir() if child.is_dir()]
  50. task_func = partial(station_task, config=config)
  51. # ---------------------------- 监控任务 ----------------------------
  52. # 进度跟踪
  53. completed = 0
  54. with tqdm(total=len(all_stations)) as pbar:
  55. with ProcessPoolExecutor(max_workers=rc.cpu_cores) as executor:
  56. futures = []
  57. for sid in all_stations:
  58. # 动态分配GPU
  59. gpu_id = rc.get_gpu()
  60. task_config = config.copy()
  61. task_config['gpu_assignment'] = gpu_id
  62. # 提交任务
  63. future = executor.submit(task_func, sid, task_config)
  64. future.add_done_callback(
  65. lambda _: rc.release_gpu(task_config['gpu_assignment']))
  66. futures.append(future)
  67. # 处理完成情况
  68. for future in futures:
  69. result = future.result()
  70. if result['status'] == 'success':
  71. completed += 1
  72. pbar.update(1)
  73. pbar.set_postfix_str(f"Completed: {completed}/{len(all_stations)}")
  74. print(f"Final result: {completed} stations trained successfully")
  75. if __name__ == "__main__":
  76. main()