From 04e2bf0edc69493743632cce0dbe50589b85b69f Mon Sep 17 00:00:00 2001 From: ClovertaTheTrilobita Date: Mon, 22 Sep 2025 17:17:33 +0000 Subject: [PATCH] =?UTF-8?q?refactor(path):=20=E6=96=B0=E5=A2=9Econfig.yaml?= =?UTF-8?q?=E4=BD=9C=E4=B8=BA=E9=85=8D=E7=BD=AE=E6=96=87=E4=BB=B6=EF=BC=8C?= =?UTF-8?q?=E9=87=8D=E6=9E=84=E9=A1=B9=E7=9B=AE=E8=B7=AF=E5=BE=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/config.yaml | 16 ++++++++++++++++ backend/cp_run.py | 13 +++++++++++-- backend/cp_train.py | 33 +++++++++++++++++++++++++++++++++ backend/flaskApp.py | 41 ++++++++++++++++++++++++++++------------- 4 files changed, 88 insertions(+), 15 deletions(-) create mode 100644 backend/config.yaml diff --git a/backend/config.yaml b/backend/config.yaml new file mode 100644 index 0000000..19b5208 --- /dev/null +++ b/backend/config.yaml @@ -0,0 +1,16 @@ +model: + +data: + root_dir: . + + run: + test_output_dir: ${data.root_dir}/run/test_output + output_dir: ${data.root_dir}/run/output + + train: + test_test_dir: ${data.root_dir}/train/test_test + test_train_dir: ${data.root_dir}/train/test_train + test_dir: ${data.root_dir}/train/test + train_dir: ${data.root_dir}/train/train + + upload_dir: ${data.root_dir}/uploads diff --git a/backend/cp_run.py b/backend/cp_run.py index 857914f..37b40f6 100644 --- a/backend/cp_run.py +++ b/backend/cp_run.py @@ -4,7 +4,16 @@ from PIL import Image import numpy as np import os, datetime import time +from omegaconf import OmegaConf +from pathlib import Path +CONFIG_PATH = Path(__file__).parent / "config.yaml" +cfg = OmegaConf.load(CONFIG_PATH) +cfg.data.root_dir = str((CONFIG_PATH.parent / cfg.data.root_dir).resolve()) +BASE_DIR = cfg.data.root_dir +UPLOAD_DIR = cfg.data.upload_dir +OUTPUT_DIR = cfg.data.run.output_dir +OUTPUT_TEST_DIR = cfg.data.run.test_output_dir class Cprun: @@ -22,7 +31,7 @@ class Cprun: ) ts = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + f"-{int(time.time()*1000)%1000:03d}" - outdir = os.path.join(os.path.dirname(__file__), "test_output", ts) + outdir = os.path.join(OUTPUT_TEST_DIR, ts) os.makedirs(outdir, exist_ok=True) # 自动创建目录 for img, mask, flow, name in zip(imgs, masks, flows, files): base = os.path.join(outdir, os.path.splitext(os.path.basename(name))[0]) @@ -63,7 +72,7 @@ class Cprun: ) ts = time - outdir = os.path.join(os.path.dirname(__file__), "output", ts) + outdir = os.path.join(OUTPUT_DIR, ts) os.makedirs(outdir, exist_ok=True) # 自动创建目录 for img, mask, flow, name in zip(imgs, masks, flows, files): base = os.path.join(outdir, os.path.splitext(os.path.basename(name))[0]) diff --git a/backend/cp_train.py b/backend/cp_train.py index e69de29..9a94e38 100644 --- a/backend/cp_train.py +++ b/backend/cp_train.py @@ -0,0 +1,33 @@ +import os.path +from cellpose import io, models, train +from pathlib import Path +from omegaconf import OmegaConf + +CONFIG_PATH = Path(__file__).parent / "config.yaml" +cfg = OmegaConf.load(CONFIG_PATH) +cfg.data.root_dir = str((CONFIG_PATH.parent / cfg.data.root_dir).resolve()) +BASE_DIR = cfg.data.root_dir +TEST_TRAIN_DIR = cfg.data.train.test_train_dir +TEST_TEST_DIR = cfg.data.train.test_test_dir + +class Cptrain: + + @classmethod + def train_test(cls): + + train_dir = TEST_TRAIN_DIR + test_dir = TEST_TEST_DIR + os.makedirs(train_dir, exist_ok=True) + os.makedirs(test_dir, exist_ok=True) + io.logger_setup() + output = io.load_train_test_data(train_dir, test_dir, image_filter="_img", + mask_filter="_masks", look_one_level_down=False) + images, labels, image_names, test_images, test_labels, image_names_test = output + + model = models.CellposeModel(gpu=True) + + model_path, train_losses, test_losses = train.train_seg(model.net, + train_data=images, train_labels=labels, + test_data=test_images, test_labels=test_labels, + weight_decay=0.1, learning_rate=1e-5, + n_epochs=100, model_name="my_new_model") \ No newline at end of file diff --git a/backend/flaskApp.py b/backend/flaskApp.py index 83f558f..bc7a00f 100644 --- a/backend/flaskApp.py +++ b/backend/flaskApp.py @@ -1,17 +1,31 @@ import asyncio import base64 +import datetime +import json +import os +import redis +import shutil +import time +from omegaconf import OmegaConf from concurrent.futures import ThreadPoolExecutor -from flask import Flask, send_from_directory, request, jsonify -import os, shutil, time, threading, datetime, json, redis -from werkzeug.utils import secure_filename -from flask_cors import CORS from pathlib import Path + +from flask import Flask, send_from_directory, request, jsonify +from flask_cors import CORS +from werkzeug.utils import secure_filename + from cp_run import Cprun app = Flask(__name__) CORS(app) -BASE_DIR = Path(__file__).resolve().parent -UPLOAD_DIR = BASE_DIR / "uploads" + +CONFIG_PATH = Path(__file__).parent / "config.yaml" +cfg = OmegaConf.load(CONFIG_PATH) +cfg.data.root_dir = str((CONFIG_PATH.parent / cfg.data.root_dir).resolve()) +BASE_DIR = cfg.data.root_dir +UPLOAD_DIR = cfg.data.upload_dir +OUTPUT_DIR = cfg.data.run.output_dir + os.makedirs(UPLOAD_DIR, exist_ok=True) executor = ThreadPoolExecutor(max_workers=4) TASKS = {} @@ -40,12 +54,13 @@ def test_download(): @app.get("/dl") def download(): timestamp = request.args.get("id") - input_dir = os.path.join(BASE_DIR, "output", timestamp) - output_dir = os.path.join(BASE_DIR, "output/tmp", timestamp) # 不要加 .zip,make_archive 会自动加 - os.makedirs(BASE_DIR / "output/tmp", exist_ok=True) # 确保 tmp 存在 + input_dir = os.path.join(OUTPUT_DIR, timestamp) + output_dir = os.path.join(OUTPUT_DIR, "tmp", timestamp) # 不要加 .zip,make_archive 会自动加 + os.makedirs(Path(OUTPUT_DIR) / "tmp", exist_ok=True) # 确保 tmp 存在 shutil.make_archive(output_dir, 'zip', input_dir) print(f"压缩完成: {output_dir}.zip") - return send_from_directory("output/tmp", f"{timestamp}.zip", as_attachment=True) + print(OUTPUT_DIR) + return send_from_directory(f"{OUTPUT_DIR}/tmp/", f"{timestamp}.zip", as_attachment=True) @app.post("/upload") def upload(): @@ -75,14 +90,14 @@ def upload(): # 将文件保存在本地目录中 ts = datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S") + f"-{int(time.time()*1000)%1000:03d}" - os.makedirs(UPLOAD_DIR / ts, exist_ok=True) + os.makedirs(Path(UPLOAD_DIR) / ts, exist_ok=True) files = request.files.getlist("files") saved = [] for f in files: if not f or f.filename == "": continue name = secure_filename(f.filename) - f.save(os.path.join(UPLOAD_DIR / ts, name)) + f.save(os.path.join(UPLOAD_DIR, ts, name)) saved.append(os.path.join(UPLOAD_DIR, ts, name)) # 新建一个线程,防止返回被阻塞 @@ -125,7 +140,7 @@ def status(): @app.get("/preview") def preview(): task_id = request.args.get('id') - task_dir = BASE_DIR / "output" / task_id + task_dir = Path(OUTPUT_DIR) / task_id if not task_dir.exists(): return jsonify({"ok": False, "error": "task not found"}), 200