STEP_DIR="$ROOT_DIR/4-start-training"

# Reset folder
rm -rf "$STEP_DIR"
mkdir -p "$STEP_DIR"

# activate environment
source "$ROOT_DIR/.venv/bin/activate"

# Start training
tacotron-cli train \
  "$ROOT_DIR/3-create-train-val-set/export/train" \
  "$ROOT_DIR/3-create-train-val-set/export/val" \
  "transcription" \
  "$STEP_DIR/checkpoints" \
  --custom-hparams="seed=1111,batch_size=64,iters_per_checkpoint=0,epochs_per_checkpoint=10,learning_rate=0.001,iterations=,epochs=500,use_speaker_embedding=1,speakers_embedding_dim=16,use_stress_embedding=1,use_tone_embedding=,use_duration_embedding=1,use_exponential_lr_decay=1,lr_decay_start_after_epoch=300,lr_decay_gamma=0.97,lr_decay_min=0.00001" \
  --tl-dir "$STEP_DIR/logs" \
  --log-path "$STEP_DIR/logs/log.txt" \
  --ckp-log-path "$STEP_DIR/logs/log-checkpoints.txt"

# Get logs
tail -n 20 "$STEP_DIR/logs/log.txt"
head -n 100 "$STEP_DIR/logs/log.txt"

# Optional analysis
rm -rf "$STEP_DIR/analysis"
checkpoint="101000"
tacotron-cli analyze \
  "$STEP_DIR/checkpoints/$checkpoint.pt" \
  "$STEP_DIR/analysis"

# Validation

# Download pre-trained Waveglow model checkpoint
wget https://tuc.cloud/index.php/s/yBRaWz5oHrFwigf/download/LJS-v3-580000.pt \
  -O "$STEP_DIR/waveglow.pt"

# Synthesize two validation utterances
rm -rf "$STEP_DIR/validation"
tacotron-cli validate \
  "$STEP_DIR/checkpoints" \
  "$STEP_DIR/validation" \
  "$ROOT_DIR/3-create-train-val-set/export/val" \
  "transcription" --include-stats \
  --files "LJ001-0001" "LJ001-0002"

# Synthesize all validation utterances
rm -rf "$STEP_DIR/validation"
tacotron-cli validate \
  "$STEP_DIR/checkpoints" \
  "$STEP_DIR/validation" \
  "$ROOT_DIR/3-create-train-val-set/export/val" \
  "transcription" --include-stats --full-run 

# Convert synthesized validation utterances to an audio signal
waveglow-cli synthesize \
  "$STEP_DIR/waveglow.pt" \
  "$STEP_DIR/validation" -o \
  --custom-seed 1111 \
  --denoiser-strength 0.0005 \
  --sigma 1.0
