|
|
|
@ -131,8 +131,8 @@ async def train_model(request: Request, features_list: List[Features]):
|
|
|
|
feature_label_weighted.to_excel(data_path, index=False)
|
|
|
|
feature_label_weighted.to_excel(data_path, index=False)
|
|
|
|
|
|
|
|
|
|
|
|
# 添加模型保存路径
|
|
|
|
# 添加模型保存路径
|
|
|
|
model_path = os.path.abspath(os.path.join(static_dir, f"train_model_{now}.pth"))
|
|
|
|
train_model_path = os.path.abspath(os.path.join(static_dir, f"train_model_{now}.pth"))
|
|
|
|
config['train_model_path'] = model_path
|
|
|
|
config['train_model_path'] = train_model_path
|
|
|
|
|
|
|
|
|
|
|
|
# 配置日志
|
|
|
|
# 配置日志
|
|
|
|
log_path = os.path.abspath(os.path.join(static_dir, f"train_log_{now}.log"))
|
|
|
|
log_path = os.path.abspath(os.path.join(static_dir, f"train_log_{now}.log"))
|
|
|
|
@ -171,8 +171,15 @@ async def train_model(request: Request, features_list: List[Features]):
|
|
|
|
end_time = time.time() # 记录结束时间
|
|
|
|
end_time = time.time() # 记录结束时间
|
|
|
|
log_print("预测耗时: " + str(end_time - start_time) + " 秒") # 打印执行时间
|
|
|
|
log_print("预测耗时: " + str(end_time - start_time) + " 秒") # 打印执行时间
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static_dir = os.path.abspath(os.path.join(os.path.dirname(__file__), "train_api")) # 设置模型文件和配置文件的存放目录,和本py同级
|
|
|
|
|
|
|
|
config_path = os.path.abspath(os.path.join(os.path.dirname(__file__), "config/config.yaml"))
|
|
|
|
|
|
|
|
|
|
|
|
# 替换现有检测模型
|
|
|
|
# 替换现有检测模型
|
|
|
|
if(config["replace_model"] == True):
|
|
|
|
if(config["replace_model"] == True):
|
|
|
|
|
|
|
|
# 如果模型路径不是绝对路径,则转换为绝对路径
|
|
|
|
|
|
|
|
if not os.path.isabs(config["model_path"]):
|
|
|
|
|
|
|
|
abs_model_path = os.path.abspath(os.path.join(os.path.dirname(__file__), config["model_path"]))
|
|
|
|
|
|
|
|
config["model_path"] = abs_model_path
|
|
|
|
shutil.copyfile(config["train_model_path"], config["model_path"])
|
|
|
|
shutil.copyfile(config["train_model_path"], config["model_path"])
|
|
|
|
log_print(f"Model file has been copied from {config['train_model_path']} to {config['model_path']}")
|
|
|
|
log_print(f"Model file has been copied from {config['train_model_path']} to {config['model_path']}")
|
|
|
|
|
|
|
|
|
|
|
|
@ -234,6 +241,11 @@ async def evaluate_model(request: Request, features_list: List[Features]):
|
|
|
|
evaluate_result_path = os.path.abspath(os.path.join(static_dir, f"evaluate_result_img_{now}.png"))
|
|
|
|
evaluate_result_path = os.path.abspath(os.path.join(static_dir, f"evaluate_result_img_{now}.png"))
|
|
|
|
config['evaluate_result_path'] = evaluate_result_path
|
|
|
|
config['evaluate_result_path'] = evaluate_result_path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 如果模型路径不是绝对路径,则转换为绝对路径
|
|
|
|
|
|
|
|
if not os.path.isabs(config["model_path"]):
|
|
|
|
|
|
|
|
abs_model_path = os.path.abspath(os.path.join(os.path.dirname(__file__), config["model_path"]))
|
|
|
|
|
|
|
|
config["model_path"] = abs_model_path
|
|
|
|
|
|
|
|
|
|
|
|
# 配置日志
|
|
|
|
# 配置日志
|
|
|
|
log_path = os.path.abspath(os.path.join(static_dir, f"evaluate_log_{now}.log"))
|
|
|
|
log_path = os.path.abspath(os.path.join(static_dir, f"evaluate_log_{now}.log"))
|
|
|
|
update_log_handler(log_path)
|
|
|
|
update_log_handler(log_path)
|
|
|
|
@ -307,6 +319,11 @@ async def inference_model(request: Request, features_list: List[Features]):
|
|
|
|
log_path = os.path.abspath(os.path.join(static_dir, f"inference_log_{now}.log"))
|
|
|
|
log_path = os.path.abspath(os.path.join(static_dir, f"inference_log_{now}.log"))
|
|
|
|
update_log_handler(log_path)
|
|
|
|
update_log_handler(log_path)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# 如果模型路径不是绝对路径,则转换为绝对路径
|
|
|
|
|
|
|
|
if not os.path.isabs(config["model_path"]):
|
|
|
|
|
|
|
|
abs_model_path = os.path.abspath(os.path.join(os.path.dirname(__file__), config["model_path"]))
|
|
|
|
|
|
|
|
config["model_path"] = abs_model_path
|
|
|
|
|
|
|
|
|
|
|
|
# 特征和标签
|
|
|
|
# 特征和标签
|
|
|
|
X = feature_label_weighted[config['feature_names']].values
|
|
|
|
X = feature_label_weighted[config['feature_names']].values
|
|
|
|
|
|
|
|
|
|
|
|
|