→ 성능을 최대한 끌어내야 하는 단계에서 이 전략들을 다시 적용
모델 저장과 결과 저장하는 함수에 model_type
, data_type
을 인자로 받아야하는 경우가 있음
이를 enum으로 코드를 짜서 실수하지 않도록 방지
config.case_name 디렉토리 하위에 모델 결과들이 저장됨
from dataclasses import dataclass
@dataclass
class Config:
case_name: str = "LSTM" # 바꿔야하는 부분
sequence_length: int = 12
from enum import Enum
class ModelType(Enum):
BEST = "best"
LAST = "last"
class DataType(Enum):
TRAIN = "train"
VAL = "val"
TEST = "test"
val_labels, val_preds, val_probs = evaluate_model(model, cfg.device, val_loader, data_type="validation")
ModelType.BEST
ModelType.LAST
모델은 best_model, last_model 둘만 저장하기 때문에, 어떤 모델을 load 할지에 따라 model_type을 넘겨주면 됩니다.
DataType.TRAIN
DataType.VAL
DataType.TEST
어떤 데이터셋으로 모델 성능을 평가할지에 따라 data_type을 넘겨주면 됩니다.
best_train_classification_report.png | best_model 로 train 성능 평가한 classification_report |
trainval_curve.png | loss, accuracy curve 그래프 |
training_results.csv | 매 epoch 마다의 train, validation 의 loss, accuracy |
weights | best_model.pth last_model.pth 저장 |
실험 유형 | 설명 |
Stacked LSTM | 기본 LSTM(3 layers) |
Bi-LSTM | 양방향 처리 |
Bi-LSTM + Attention | Attention Layer 추가 |
No phone_detected | 해당 feature 제거 |
mar/ear only | 핵심 feature subset 사용 |
Full feature set | 모든 feature 사용 |
Yawn oversampling | 하품 클래스 샘플 수 증강 |
Noise injection | 입력 feature에 jittering |
Long window | 시퀀스 길이 24 |
Short window | 시퀀스 길이 12 |
CNN + LSTM | 앞단에 Conv1D 추가 - 이미 작성한 코드가 있어서 코드 템플릿에 고려 안해도됨 |
TimeDistributed Dense | 시점별 Dense 적용 |
./runs/
└── BiLSTM_Attention/
├── classification_report.png
├── confusion_matrix.png
└── model.pt
import os
CASE_NAME = 'BiLSTM_Attention'
SAVE_DIR = os.path.join("results", CASE_NAME)
os.makedirs(SAVE_DIR, exist_ok=True)
from dataclasses import dataclass
@dataclass
class ExperimentConfig:
model_type: str = "bilstm_attention"
use_attention: bool = True
use_bidirectional: bool = True
dropout: float = 0.3
recurrent_dropout: float = 0.0
sequence_length: int = 24
features: list = ("mar", "ear", "yaw", "pitch")
oversample: bool = False
noise_injection: bool = False
case_name: str = "BiLSTM_Attention"
# 사용 예
cfg = ExperimentConfig()
print(cfg.model_type) # 자동완성 기능
from sklearn.metrics import classification_report, confusion_matrix
# classification_report
report = classification_report(y_true, y_pred, output_dict=True)
df = pd.DataFrame(report).transpose()
fig, ax = plt.subplots(figsize=(10, len(df) * 0.5 + 1))
ax.axis('tight')
ax.axis('off')
table = ax.table(cellText=df.round(3).values,
colLabels=df.columns,
rowLabels=df.index,
loc='center')
plt.savefig("results/BiLSTM_Attention/classification_report.png")
plt.close()
# confusion matrix
import matplotlib.pyplot as plt
import seaborn as sns
sns.heatmap(cm, annot=True)
plt.savefig(os.path.join(SAVE_DIR, "confusion_matrix.png"))