|
|
@@ -8,7 +8,6 @@ import os
|
|
|
import importlib.util
|
|
|
import json
|
|
|
from utils.training_interface import TrainerInterface
|
|
|
-from multiprocessing import Manager
|
|
|
|
|
|
# Configure logging to file
|
|
|
logger = logging.getLogger(__name__)
|
|
|
@@ -19,11 +18,15 @@ MODEL_DIR = './model/hotword/'
|
|
|
BACKGROUND_DIR = './training/background/'
|
|
|
SAMPLER_DIR = './training/sampler/'
|
|
|
|
|
|
-# Create manager for shared variables
|
|
|
-manager = Manager()
|
|
|
-current_progress = manager.list()
|
|
|
-current_loss = manager.list()
|
|
|
-current_accuracy = manager.list()
|
|
|
+training_status = "Not started"
|
|
|
+training_start_time = None
|
|
|
+training_thread = None
|
|
|
+training_info = {}
|
|
|
+
|
|
|
+# Global variables for progress tracking
|
|
|
+current_progress = 0
|
|
|
+current_loss = 0.0
|
|
|
+current_accuracy = 0.0
|
|
|
total_epochs = 0
|
|
|
total_batches = 0
|
|
|
|
|
|
@@ -63,26 +66,26 @@ def register_training_callbacks(app):
|
|
|
DashState("num-epochs", "value"),
|
|
|
DashState("batch-size", "value")]
|
|
|
)
|
|
|
- def manage_training(n_clicks_start, n_clicks_optimize, n_clicks_stop, n_intervals, pathname, training_method, dropout_rate, epochs, batch_size):
|
|
|
- global total_epochs, total_batches, logger
|
|
|
+ def manage_training(n_clicks_start, n_clicks_optimize, n_clicks_stop, n_intervals, pathname, training_method, dropout_rate, num_epochs, batch_size):
|
|
|
+ global training_status, training_start_time, training_thread, training_info, logger
|
|
|
+ global current_progress, current_loss, current_accuracy, total_epochs, total_batches
|
|
|
|
|
|
model_name = pathname.split('/')[-1]
|
|
|
ctx = callback_context
|
|
|
|
|
|
- if not os.path.exists(MODEL_DIR + model_name + "/"):
|
|
|
- os.mkdir(MODEL_DIR + model_name + "/")
|
|
|
+ if not os.path.exists(MODEL_DIR + model_name):
|
|
|
+ os.mkdir(MODEL_DIR + model_name)
|
|
|
|
|
|
if not ctx.triggered:
|
|
|
logger.error("Callback context triggered is empty.")
|
|
|
- return False, False, f"Status: Not started", "", {}, 0
|
|
|
+ return False, False, f"Status: {training_status}", "", {}, 0
|
|
|
|
|
|
triggered_id = ctx.triggered[0]['prop_id'].split('.')[0]
|
|
|
|
|
|
- if triggered_id in ["start-training-hotword", "optimize-training-hotword"] and (n_clicks_start or n_clicks_optimize):
|
|
|
- training_status = "Training" if triggered_id == "start-training-hotword" else "Optimizing"
|
|
|
+ if triggered_id == "start-training-hotword" and n_clicks_start:
|
|
|
+ training_status = "Preprocessing"
|
|
|
training_start_time = time.time()
|
|
|
- training_info = {"accuracy": [], "loss": [], "epochs": 0}
|
|
|
-
|
|
|
+ training_info[model_name] = {"accuracy": [], "loss": [], "epochs": 0}
|
|
|
if training_method in trainer_modules:
|
|
|
trainer_class = trainer_modules[training_method]
|
|
|
trainer = trainer_class()
|
|
|
@@ -95,14 +98,34 @@ def register_training_callbacks(app):
|
|
|
trainer.set_sample_directory(SAMPLER_DIR)
|
|
|
trainer.set_background_directory(BACKGROUND_DIR)
|
|
|
trainer.set_dropout_rate(dropout_rate)
|
|
|
- trainer.set_epochs(epochs)
|
|
|
+ trainer.set_epochs(num_epochs)
|
|
|
trainer.set_batch_size(batch_size)
|
|
|
- trainer.set_status_references(current_progress, current_loss, current_accuracy)
|
|
|
-
|
|
|
- training_thread = Thread(target=trainer.start_new if triggered_id == "start-training-hotword" else trainer.start)
|
|
|
+ training_thread = Thread(target=trainer.start_new)
|
|
|
training_thread.start()
|
|
|
logger.warning(f"Started training thread for model: {model_name}")
|
|
|
+ return True, True, f"Status: {training_status}", f"Started at: {time.ctime(training_start_time)}", {}, 0
|
|
|
+
|
|
|
+ elif triggered_id == "optimize-training-hotword" and n_clicks_optimize:
|
|
|
+ training_status = "Optimizing"
|
|
|
+ training_start_time = time.time()
|
|
|
+ training_info[model_name] = {"accuracy": [], "loss": [], "epochs": 0}
|
|
|
+ if training_method in trainer_modules:
|
|
|
+ trainer_class = trainer_modules[training_method]
|
|
|
+ trainer = trainer_class()
|
|
|
+ else:
|
|
|
+ logger.error(f"Unsupported training method: {training_method}")
|
|
|
+ return False, False, f"Status: {training_status}", "", {}, 0
|
|
|
|
|
|
+ trainer.set_model_path(MODEL_DIR + model_name + "/")
|
|
|
+ trainer.set_training_directories([TRAINING_HOTWORD_DIR + model_name + "/", TRAINING_HOTWORD_DIR + "systemcommand/"])
|
|
|
+ trainer.set_sample_directory(SAMPLER_DIR)
|
|
|
+ trainer.set_background_directory(BACKGROUND_DIR)
|
|
|
+ trainer.set_dropout_rate(dropout_rate)
|
|
|
+ trainer.set_epochs(num_epochs)
|
|
|
+ trainer.set_batch_size(batch_size)
|
|
|
+ training_thread = Thread(target=trainer.start)
|
|
|
+ training_thread.start()
|
|
|
+ logger.warning(f"Started optimization thread for model: {model_name}")
|
|
|
return True, True, f"Status: {training_status}", f"Started at: {time.ctime(training_start_time)}", {}, 0
|
|
|
|
|
|
elif triggered_id == "stop-training-hotword" and n_clicks_stop:
|
|
|
@@ -110,32 +133,34 @@ def register_training_callbacks(app):
|
|
|
logger.debug(f"Stopped training for model: {model_name}")
|
|
|
return False, False, f"Status: {training_status}", "", {}, 0
|
|
|
|
|
|
- elif triggered_id == "interval-component":
|
|
|
- if current_progress:
|
|
|
- elapsed_time = time.time() - training_start_time
|
|
|
- figure = {
|
|
|
- "data": [
|
|
|
- {"x": list(range(len(current_accuracy))), "y": list(current_accuracy), "type": "line", "name": "Accuracy", "line": {"color": "blue"}},
|
|
|
- {"x": list(range(len(current_loss))), "y": list(current_loss), "type": "line", "name": "Loss", "line": {"color": "green"}},
|
|
|
- ],
|
|
|
- "layout": {
|
|
|
- "title": "Training Progress",
|
|
|
- "xaxis": {"title": "Epoch"},
|
|
|
- "yaxis": {"title": "Value", "range": [0, 2]},
|
|
|
- "height": 200,
|
|
|
- "width": 500,
|
|
|
- "plot_bgcolor": "darkgray",
|
|
|
- "gridcolor": "lightgray"
|
|
|
- }
|
|
|
+ elif triggered_id == "interval-component" and training_start_time and training_status in ["Training", "Optimizing"]:
|
|
|
+ elapsed_time = time.time() - training_start_time
|
|
|
+ figure = {
|
|
|
+ "data": [
|
|
|
+ {"x": list(range(len(training_info[model_name]["accuracy"]))), "y": training_info[model_name]["accuracy"], "type": "line", "name": "Accuracy", "line": {"color": "blue"}},
|
|
|
+ {"x": list(range(len(training_info[model_name]["loss"]))), "y": training_info[model_name]["loss"], "type": "line", "name": "Loss", "line": {"color": "green"}},
|
|
|
+ ],
|
|
|
+ "layout": {
|
|
|
+ "title": "Training Progress",
|
|
|
+ "xaxis": {"title": "Epoch"},
|
|
|
+ "yaxis": {"title": "Value", "range": [0, 2]},
|
|
|
+ "height": 200,
|
|
|
+ "width": 500,
|
|
|
+ "plot_bgcolor": "darkgray",
|
|
|
+ "gridcolor": "lightgray"
|
|
|
}
|
|
|
- progress = current_progress[-1] if current_progress else 0
|
|
|
- return True, True, f"Status: {training_status}", f"Training time: {int(elapsed_time // 60)} minutes {int(elapsed_time % 60)} seconds", figure, progress
|
|
|
+ }
|
|
|
+ progress = current_progress
|
|
|
+ training_info[model_name]["accuracy"].append(current_accuracy)
|
|
|
+ training_info[model_name]["loss"].append(current_loss)
|
|
|
+ training_info[model_name]["epochs"] = total_epochs
|
|
|
+ return True, True, f"Status: {training_status}", f"Training time: {int(elapsed_time // 60)} minutes {int(elapsed_time % 60)} seconds", figure, progress
|
|
|
|
|
|
training_data = list_files_in_directory(f"{TRAINING_HOTWORD_DIR}/{model_name}", extension='.wav')
|
|
|
if training_status in ["Training", "Optimizing"] or not training_data:
|
|
|
- return True, True, f"Status: Not started", "", {}, 0
|
|
|
+ return True, True, f"Status: {training_status}", "", {}, 0
|
|
|
|
|
|
- return False, False, f"Status: Not started", "", {}, 0
|
|
|
+ return False, False, f"Status: {training_status}", "", {}, 0
|
|
|
|
|
|
app.layout.children.append(dcc.Interval(
|
|
|
id='interval-component',
|